codefloe/forgejo-codefloe
Watch
4
12
Fork
You've already forked forgejo-codefloe
0

feat(quota): limit CI build minutes per runner class #105

Merged
pat-s merged 1 commit from t3code/resolve-issue-96 into v16.0/forgejo-codefloe 2026-08-30 06:34:09 +00:00
Owner

Implements #96, phase 4 of the CI build minutes quota work, on top of #100 (the ledger), #102 (the evaluator) and #104 (the external CI interface).

The motivating case is cost.
A macOS native runner costs several times what a shared Linux one does, and an owner should be able to hold a generous Linux budget and a small macOS one at the same time.

Everything needed for that was already in place: class has been on quota_ci_usage and quota_ci_record since phase 1 and both producers have been filling it in, so there is history to enforce against, no backfill and no schema change.
What was missing is that no limit subject addressed a class.

What changed

Three new subjects, on a second axis

time:ci:all
├── time:ci:actions
├── time:ci:external
├── time:ci:class:linux
├── time:ci:class:windows
└── time:ci:class:macos

The class subtree is a second axis under time:ci:all rather than a nesting of the source subtree: a job has both a source and a class, so both are evaluated and either can refuse it.

The class list is a small closed set, matching the ledger's own.
windows is included alongside the two the issue names, because accrual has been recording it since phase 1 and a class the ledger can write but no rule can name would be a trap.
A class outside the set, which an external reporter may still book, accrues against time:ci:all like any other minute; it just has no dedicated subject to be capped by.

The class axis caps, it does not grant

This is the one design decision worth reviewing.

Upstream's rule is that belonging to a quota group grants exactly what its rules mention, so a subject no rule mentions is refused.
Applied to the class axis that would break every setup written before this change: an owner in a group carrying a time:ci:actions rule would have their Linux jobs refused for want of a time:ci:class:linux rule nobody had any reason to write.

So GroupList.AssessCap in models/quota/group_codefloe.go binds only where an administrator wrote a rule naming the class outright.
Rules that reach a class through time:ci:all are left to the source axis, which evaluates them already.
Within what is left the ordinary precedence applies: the strictest rule in a group binds, the most generous group wins.

time:ci:all is still registered as the parent of each class subject, so the hierarchy reads the way the issue draws it and a total rule still covers a class the way it covers a source.

Enforcement

quota_ci.Evaluate now takes a class as well as a source and returns a Decision that says whether a refusal was class-scoped.

For Forgejo Actions the class is the class of the runner asking for work, so a class limit binds in PickTask rather than at run creation, where no runner is known and which class each job lands on has not been decided.
A job refused by a class rule is cancelled on its own, carrying a new WarningCodeCIQuotaClassExceeded on its run that names the class, and the rest of the run goes ahead on the budget it has.
Cancelling the whole run would charge a Linux job for a macOS limit; leaving the job queued instead would have the pick loop offer it again on every poll.

A refusal of the owner's CI time as a whole still takes the whole run, exactly as before.

The in-flight estimate is split by class

InFlightSecondsByClass resolves the class of each running task from its runner's labels, through the same function accrual uses when the task finishes, so the estimate and the bill cannot disagree.

Without the split a macOS budget of a couple of hours would be hours past spent before the first job finished and reached the ledger, which is exactly the window a small class budget has to be right about.

Surfaces

GET /api/v1/quota/ci/check?repo=owner/name&class=macos now answers against the class instead of validating and ignoring it, and remaining_seconds is whichever of the two budgets is smaller.
A class-scoped refusal names the class in reason, since a scheduler that can place the work elsewhere needs to know it was refused the machine rather than the minutes.

The CI minutes card and GET /api/v1/user/quota/ci gain a breakdown by class, built from the same numbers the rules are evaluated against so that it includes work still running and cannot disagree with the breakdown by source next to it.

Notes for the reviewer

Per-class cost multipliers are deliberately out of scope, as the issue asks.

The only edit outside the fork's own files, besides the two enforcement call sites, is one case arm in models/actions/pre_execution_warnings.go routing the new warning code to the fork's translator.

Checklist

Tests for Go changes

  • I added test coverage for Go changes...
    • in their respective *_test.go for unit tests.
    • in the tests/integration directory if it involves interactions with a live Forgejo server.
  • I ran...
    • make lint-go, make lint-locale, make generate-swagger and the quota unit and integration suites before pushing.

Unit tests cover the new subjects and their units, AssessCap (silent where no rule names the subject, binding where one does, unlimited rules, most-permissive-group-wins), the two-axis decision (a class cap refusing while the other class runs, a total refusing every class, the smaller remaining budget, an unknown class), per-class in-flight estimation and the new warning's rendering.
Integration tests cover the motivating case end to end: a macOS runner is refused and only its job is cancelled while the Linux job of the same run is picked up and billed to its own class, plus the regression guard that a group granting CI time by source alone still runs work of every class.

Documentation

  • I created a pull request to the documentation to explain to Forgejo users how to use this change.
  • I did not document these changes and I do not expect someone else to do it. The class subjects and their cap semantics are documented in custom/conf/app.example.ini next to [quota.ci.default].

Release notes

  • This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
  • This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.
Implements #96, phase 4 of the CI build minutes quota work, on top of #100 (the ledger), #102 (the evaluator) and #104 (the external CI interface). The motivating case is cost. A macOS native runner costs several times what a shared Linux one does, and an owner should be able to hold a generous Linux budget and a small macOS one at the same time. Everything needed for that was already in place: `class` has been on `quota_ci_usage` and `quota_ci_record` since phase 1 and both producers have been filling it in, so there is history to enforce against, no backfill and no schema change. What was missing is that no limit subject addressed a class. ## What changed ### Three new subjects, on a second axis ``` time:ci:all ├── time:ci:actions ├── time:ci:external ├── time:ci:class:linux ├── time:ci:class:windows └── time:ci:class:macos ``` The class subtree is a second axis under `time:ci:all` rather than a nesting of the source subtree: a job has both a source and a class, so both are evaluated and either can refuse it. The class list is a small closed set, matching the ledger's own. `windows` is included alongside the two the issue names, because accrual has been recording it since phase 1 and a class the ledger can write but no rule can name would be a trap. A class outside the set, which an external reporter may still book, accrues against `time:ci:all` like any other minute; it just has no dedicated subject to be capped by. ### The class axis caps, it does not grant This is the one design decision worth reviewing. Upstream's rule is that belonging to a quota group grants exactly what its rules mention, so a subject no rule mentions is refused. Applied to the class axis that would break every setup written before this change: an owner in a group carrying a `time:ci:actions` rule would have their Linux jobs refused for want of a `time:ci:class:linux` rule nobody had any reason to write. So `GroupList.AssessCap` in `models/quota/group_codefloe.go` binds only where an administrator wrote a rule naming the class outright. Rules that reach a class through `time:ci:all` are left to the source axis, which evaluates them already. Within what is left the ordinary precedence applies: the strictest rule in a group binds, the most generous group wins. `time:ci:all` is still registered as the parent of each class subject, so the hierarchy reads the way the issue draws it and a total rule still covers a class the way it covers a source. ### Enforcement `quota_ci.Evaluate` now takes a class as well as a source and returns a `Decision` that says whether a refusal was class-scoped. For Forgejo Actions the class is the class of the runner asking for work, so a class limit binds in `PickTask` rather than at run creation, where no runner is known and which class each job lands on has not been decided. A job refused by a class rule is cancelled on its own, carrying a new `WarningCodeCIQuotaClassExceeded` on its run that names the class, and the rest of the run goes ahead on the budget it has. Cancelling the whole run would charge a Linux job for a macOS limit; leaving the job queued instead would have the pick loop offer it again on every poll. A refusal of the owner's CI time as a whole still takes the whole run, exactly as before. ### The in-flight estimate is split by class `InFlightSecondsByClass` resolves the class of each running task from its runner's labels, through the same function accrual uses when the task finishes, so the estimate and the bill cannot disagree. Without the split a macOS budget of a couple of hours would be hours past spent before the first job finished and reached the ledger, which is exactly the window a small class budget has to be right about. ### Surfaces `GET /api/v1/quota/ci/check?repo=owner/name&class=macos` now answers against the class instead of validating and ignoring it, and `remaining_seconds` is whichever of the two budgets is smaller. A class-scoped refusal names the class in `reason`, since a scheduler that can place the work elsewhere needs to know it was refused the machine rather than the minutes. The CI minutes card and `GET /api/v1/user/quota/ci` gain a breakdown by class, built from the same numbers the rules are evaluated against so that it includes work still running and cannot disagree with the breakdown by source next to it. ## Notes for the reviewer Per-class cost multipliers are deliberately out of scope, as the issue asks. The only edit outside the fork's own files, besides the two enforcement call sites, is one `case` arm in `models/actions/pre_execution_warnings.go` routing the new warning code to the fork's translator. ## Checklist ### Tests for Go changes - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [x] `make lint-go`, `make lint-locale`, `make generate-swagger` and the quota unit and integration suites before pushing. Unit tests cover the new subjects and their units, `AssessCap` (silent where no rule names the subject, binding where one does, unlimited rules, most-permissive-group-wins), the two-axis decision (a class cap refusing while the other class runs, a total refusing every class, the smaller remaining budget, an unknown class), per-class in-flight estimation and the new warning's rendering. Integration tests cover the motivating case end to end: a macOS runner is refused and only its job is cancelled while the Linux job of the same run is picked up and billed to its own class, plus the regression guard that a group granting CI time by source alone still runs work of every class. ### Documentation - [ ] I created a pull request to the documentation to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. The class subjects and their cap semantics are documented in `custom/conf/app.example.ini` next to `[quota.ci.default]`. ### Release notes - [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.
Author
Owner

Request changes: addCIQuotaWarning deduplicates class-scoped warnings only by WarningCodeCIQuotaClassExceeded at services/actions/codefloe_quota_ci.go:98.

If a run has, for example, both macOS and Windows jobs and both class budgets are exhausted, the first refusal records the shared warning code.
The second job is still cancelled, but its warning is discarded because the code is already present.
The run can therefore tell the user that only macOS jobs were affected even though Windows jobs were cancelled too.

Please deduplicate class-scoped warnings by both code and class, or aggregate all refused classes into one warning.

The focused package tests otherwise pass, and the two-axis evaluator, in-flight accounting, and job-scoped cancellation match the issue requirements.

Request changes: `addCIQuotaWarning` deduplicates class-scoped warnings only by `WarningCodeCIQuotaClassExceeded` at `services/actions/codefloe_quota_ci.go:98`. If a run has, for example, both macOS and Windows jobs and both class budgets are exhausted, the first refusal records the shared warning code. The second job is still cancelled, but its warning is discarded because the code is already present. The run can therefore tell the user that only macOS jobs were affected even though Windows jobs were cancelled too. Please deduplicate class-scoped warnings by both code and class, or aggregate all refused classes into one warning. The focused package tests otherwise pass, and the two-axis evaluator, in-flight accounting, and job-scoped cancellation match the issue requirements.
pat-s force-pushed t3code/resolve-issue-96 from 3d0cf93e5f
All checks were successful
testing / frontend-checks (pull_request) Successful in 1m2s
testing / backend-checks (pull_request) Successful in 5m26s
testing / test-remote-cacher (redis) (pull_request) Successful in 1m36s
testing / test-unit (pull_request) Successful in 6m15s
testing / test-remote-cacher (valkey) (pull_request) Successful in 2m18s
testing / test-remote-cacher (garnet) (pull_request) Successful in 2m22s
testing / test-mysql (pull_request) Successful in 28m6s
testing / test-sqlite (pull_request) Successful in 41m9s
testing / test-e2e (pull_request) Successful in 21m39s
testing / test-pgsql (pull_request) Successful in 38m3s
to 9df63151b6
All checks were successful
testing / frontend-checks (pull_request) Successful in 1m6s
testing / backend-checks (pull_request) Successful in 5m20s
testing / test-remote-cacher (redis) (pull_request) Successful in 1m41s
testing / test-remote-cacher (valkey) (pull_request) Successful in 1m41s
testing / test-unit (pull_request) Successful in 10m13s
testing / test-remote-cacher (garnet) (pull_request) Successful in 1m40s
testing / test-e2e (pull_request) Successful in 26m34s
testing / test-pgsql (pull_request) Successful in 42m54s
testing / test-sqlite (pull_request) Successful in 40m50s
testing / test-mysql (pull_request) Successful in 22m55s
issue-labels / backporting (pull_request_target) Has been skipped
milestone / set (pull_request_target) Has been skipped
2026-08-29 21:45:48 +00:00
Compare
Author
Owner

Fixed in 9df63151b6 (amended, so the PR stays a single commit).

You are right, and the second half of it was worse than the warning: clearCIQuotaWarning deleted only the first entry per code, so a rerun of a run carrying two class warnings would have left one behind to describe the previous attempt.

Deduplicated by code and class rather than aggregated into one warning. Two refusals by two different budgets are two different sentences, each with its own rule name and its own used/limit pair, and the run view already renders one line per warning; folding them into one would have meant either dropping the rule names or inventing a message that lists them.

  • addCIQuotaWarning now asks carriesCIQuotaWarning, which for WarningCodeCIQuotaClassExceeded compares the class in the warning's details rather than only the code. The details survive the round trip through the database as JSON, so the comparison is against a string and falls through safely if it is anything else. Every other code still dedupes per code: those describe the run as a whole.
  • clearCIQuotaWarning now walks the codes backwards and removes every quota entry, keeping the details in step.

Tests, both of which fail against the previous commit and pass against this one:

  • TestQuotaCIWarningIsKeptPerClass in services/actions/codefloe_quota_ci_test.go — two classes get two warnings, the same class twice gets one, details read back as JSON still dedupe, and a rerun clears both while leaving an unrelated WarningCodePermissions and its details untouched.
  • TestActionsQuotaCIClassLimitBlocksOnlyThatClass now pushes a run with linux, mac and win jobs and exhausts both the macOS and the Windows budgets. Both runners are refused, both jobs are cancelled, the run names both classes in order, and the Linux job is still picked up and billed to its own class.

make lint-go is clean and the quota unit and integration suites pass.

Fixed in 9df63151b6 (amended, so the PR stays a single commit). You are right, and the second half of it was worse than the warning: `clearCIQuotaWarning` deleted only the *first* entry per code, so a rerun of a run carrying two class warnings would have left one behind to describe the previous attempt. Deduplicated by code **and** class rather than aggregated into one warning. Two refusals by two different budgets are two different sentences, each with its own rule name and its own used/limit pair, and the run view already renders one line per warning; folding them into one would have meant either dropping the rule names or inventing a message that lists them. - `addCIQuotaWarning` now asks `carriesCIQuotaWarning`, which for `WarningCodeCIQuotaClassExceeded` compares the class in the warning's details rather than only the code. The details survive the round trip through the database as JSON, so the comparison is against a `string` and falls through safely if it is anything else. Every other code still dedupes per code: those describe the run as a whole. - `clearCIQuotaWarning` now walks the codes backwards and removes *every* quota entry, keeping the details in step. Tests, both of which fail against the previous commit and pass against this one: - `TestQuotaCIWarningIsKeptPerClass` in `services/actions/codefloe_quota_ci_test.go` — two classes get two warnings, the same class twice gets one, details read back as JSON still dedupe, and a rerun clears both while leaving an unrelated `WarningCodePermissions` and its details untouched. - `TestActionsQuotaCIClassLimitBlocksOnlyThatClass` now pushes a run with `linux`, `mac` and `win` jobs and exhausts both the macOS and the Windows budgets. Both runners are refused, both jobs are cancelled, the run names both classes in order, and the Linux job is still picked up and billed to its own class. `make lint-go` is clean and the quota unit and integration suites pass.
pat-s merged commit 442e723c2a into v16.0/forgejo-codefloe 2026-08-30 06:34:09 +00:00
pat-s deleted branch t3code/resolve-issue-96 2026-08-30 06:34:10 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
codefloe/forgejo-codefloe!105
No description provided.