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

feat(quota): add the CI minutes ledger, subjects and Actions accrual #100

Merged
pat-s merged 1 commit from t3code/implement-issue-93 into v16.0/forgejo-codefloe 2026-08-28 18:21:50 +00:00
Owner

Implements #93, phase 1 of the CI build minutes quota work.

Builds the accounting layer and makes CI minutes visible.
Nothing is enforced in this phase: rules over the new subjects can be created and evaluate correctly, but no code path calls the evaluator to deny anything.
The point is to get numbers that can be trusted before anything blocks on them.

What changed

Schema

New models/codefloe/quota_ci package holding two tables, created by Codefloe migration v10.

quota_ci_usage is the per-owner monthly aggregate, unique on (owner_id, period, source, class), and is what quota rules are evaluated against.
quota_ci_record is the individual billed intervals it was built from, unique on (source, external_ref).
That second index is what makes accrual idempotent, so a producer that retries a report cannot double charge.

Limit subjects

models/quota/limit_subject_codefloe.go declares time:ci:all, time:ci:actions and time:ci:external at offset 1000, well clear of upstream's enum, and registers their reprs and parent relations from init().

It also gives every subject a unit.
A rule sums its subjects, so a rule mixing size: and time: would add bytes to seconds and compare the result against one limit; such a rule is now rejected at rule creation, at rule edit, and when a group is created with inline rules.

Accrual

services/codefloe/quota_ci.AccrueTask writes the record and the aggregate increment in one transaction, keyed task:<id>, with the class taken from the runner's labels.

It is called from both places an Actions task reaches a terminal state, UpdateTaskByState and StopTask, so a zombie task stopped by StopZombieTasks is still billed for the time it ran.
CancelAbandonedJobs needs no hook: a job that was never picked up has no task and consumed no minutes.

Reading usage

GetUsedForUser now also fills a consumed-time half of Used, from the ledger plus an in-flight estimate of SUM(now - started) over the owner's running tasks.

The estimate matters.
Without it an owner running fifty eight-hour jobs in parallel reads as near-zero usage until they all finish, which is exactly the window phase 2 would need to be right about.
With it, no heartbeat writer is needed.

API

GET /api/v1/user/quota/ci and /api/v1/orgs/{org}/quota/ci return used seconds per source and class, the applicable limits, the period start and the reset time.
GET /api/v1/user/quota/ci/records and the org equivalent are paginated and ordered by seconds descending.

These are separate endpoints rather than fields on QuotaInfo, which keeps modules/structs/quota.go and services/convert/quota.go untouched.

UI

A CI minutes card on the storage overview, showing used against limit, the reset date, the per-source split, and the top repositories for the period.
The existing storage card is now filtered to byte-counted rules, so a time: rule is never rendered as a size.

Config

[quota.ci]
ENABLED = false
RECORD_RETENTION_PERIODS = 12

Retention is applied by a nightly cron task that drops records past the window and keeps the aggregates, which are small and are what history is read from once the detail is gone.

Notes for the reviewer

Two deviations from the issue worth a look.

models/quota/used.go could not be left untouched.
Used is a struct, so making time rules evaluate correctly needed a field on it.
The file carries three small edits: the Time field, a tail call in CalculateFor, and one call in GetUsedForUser.
Everything else lives in new *_codefloe.go files.

The issue says limits are expressed as durations in config but names no key, so I added [quota.ci.default] TOTAL as a duration, mirroring [quota.default] TOTAL.
It is the fallback reported when no quota group gives the owner a time: rule.
If limits are meant to come only from quota rules, that key can be dropped.

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 fmt, make generate-swagger, make swagger-validate, make lint-locale, make lint-editorconfig, the deadcode check, and the unit and integration suites for every touched package.
    • make lint-go could not run in this environment: the pinned golangci-lint cannot read Go 1.27 export data.

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 [quota.ci] section is documented by its comments in modules/setting/codefloe_quota_ci.go; user-facing docs belong with phase 2, when the feature starts enforcing.

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 [#93](https://codefloe.com/codefloe/forgejo-codefloe/issues/93), phase 1 of the CI build minutes quota work. Builds the accounting layer and makes CI minutes visible. Nothing is enforced in this phase: rules over the new subjects can be created and evaluate correctly, but no code path calls the evaluator to deny anything. The point is to get numbers that can be trusted before anything blocks on them. ## What changed ### Schema New `models/codefloe/quota_ci` package holding two tables, created by Codefloe migration v10. `quota_ci_usage` is the per-owner monthly aggregate, unique on `(owner_id, period, source, class)`, and is what quota rules are evaluated against. `quota_ci_record` is the individual billed intervals it was built from, unique on `(source, external_ref)`. That second index is what makes accrual idempotent, so a producer that retries a report cannot double charge. ### Limit subjects `models/quota/limit_subject_codefloe.go` declares `time:ci:all`, `time:ci:actions` and `time:ci:external` at offset 1000, well clear of upstream's enum, and registers their reprs and parent relations from `init()`. It also gives every subject a unit. A rule sums its subjects, so a rule mixing `size:` and `time:` would add bytes to seconds and compare the result against one limit; such a rule is now rejected at rule creation, at rule edit, and when a group is created with inline rules. ### Accrual `services/codefloe/quota_ci.AccrueTask` writes the record and the aggregate increment in one transaction, keyed `task:<id>`, with the class taken from the runner's labels. It is called from both places an Actions task reaches a terminal state, `UpdateTaskByState` and `StopTask`, so a zombie task stopped by `StopZombieTasks` is still billed for the time it ran. `CancelAbandonedJobs` needs no hook: a job that was never picked up has no task and consumed no minutes. ### Reading usage `GetUsedForUser` now also fills a consumed-time half of `Used`, from the ledger plus an in-flight estimate of `SUM(now - started)` over the owner's running tasks. The estimate matters. Without it an owner running fifty eight-hour jobs in parallel reads as near-zero usage until they all finish, which is exactly the window phase 2 would need to be right about. With it, no heartbeat writer is needed. ### API `GET /api/v1/user/quota/ci` and `/api/v1/orgs/{org}/quota/ci` return used seconds per source and class, the applicable limits, the period start and the reset time. `GET /api/v1/user/quota/ci/records` and the org equivalent are paginated and ordered by seconds descending. These are separate endpoints rather than fields on `QuotaInfo`, which keeps `modules/structs/quota.go` and `services/convert/quota.go` untouched. ### UI A CI minutes card on the storage overview, showing used against limit, the reset date, the per-source split, and the top repositories for the period. The existing storage card is now filtered to byte-counted rules, so a `time:` rule is never rendered as a size. ### Config ```ini [quota.ci] ENABLED = false RECORD_RETENTION_PERIODS = 12 ``` Retention is applied by a nightly cron task that drops records past the window and keeps the aggregates, which are small and are what history is read from once the detail is gone. ## Notes for the reviewer Two deviations from the issue worth a look. `models/quota/used.go` could not be left untouched. `Used` is a struct, so making time rules evaluate correctly needed a field on it. The file carries three small edits: the `Time` field, a tail call in `CalculateFor`, and one call in `GetUsedForUser`. Everything else lives in new `*_codefloe.go` files. The issue says limits are expressed as durations in config but names no key, so I added `[quota.ci.default] TOTAL` as a duration, mirroring `[quota.default] TOTAL`. It is the fallback reported when no quota group gives the owner a `time:` rule. If limits are meant to come only from quota rules, that key can be dropped. ## 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 fmt`, `make generate-swagger`, `make swagger-validate`, `make lint-locale`, `make lint-editorconfig`, the deadcode check, and the unit and integration suites for every touched package. - [ ] `make lint-go` could not run in this environment: the pinned golangci-lint cannot read Go 1.27 export data. ### 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 `[quota.ci]` section is documented by its comments in `modules/setting/codefloe_quota_ci.go`; user-facing docs belong with phase 2, when the feature starts enforcing. ### 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.
pat-s force-pushed t3code/implement-issue-93 from d961f92d28
Some checks failed
testing / test-unit (pull_request) Has been cancelled
testing / test-e2e (pull_request) Has been cancelled
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
testing / backend-checks (pull_request) Has been cancelled
testing / frontend-checks (pull_request) Has been cancelled
to 9433477bef
Some checks failed
testing / frontend-checks (pull_request) Successful in 1m10s
testing / test-unit (pull_request) Successful in 10m10s
testing / backend-checks (pull_request) Successful in 6m19s
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
testing / test-e2e (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
2026-08-28 11:32:39 +00:00
Compare
pat-s force-pushed t3code/implement-issue-93 from 9433477bef
Some checks failed
testing / frontend-checks (pull_request) Successful in 1m10s
testing / test-unit (pull_request) Successful in 10m10s
testing / backend-checks (pull_request) Successful in 6m19s
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
testing / test-e2e (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
to 65e6e5a002
All checks were successful
testing / frontend-checks (pull_request) Successful in 1m43s
testing / backend-checks (pull_request) Successful in 5m48s
testing / test-unit (pull_request) Successful in 10m40s
testing / test-remote-cacher (garnet) (pull_request) Successful in 1m36s
testing / test-remote-cacher (redis) (pull_request) Successful in 1m39s
testing / test-remote-cacher (valkey) (pull_request) Successful in 1m37s
testing / test-e2e (pull_request) Successful in 28m14s
testing / test-sqlite (pull_request) Successful in 41m42s
testing / test-mysql (pull_request) Successful in 32m33s
testing / test-pgsql (pull_request) Successful in 43m45s
issue-labels / backporting (pull_request_target) Has been skipped
milestone / set (pull_request_target) Has been skipped
2026-08-28 11:52:27 +00:00
Compare
pat-s merged commit 5e085f1bc3 into v16.0/forgejo-codefloe 2026-08-28 18:21:50 +00:00
pat-s deleted branch t3code/implement-issue-93 2026-08-28 18:21:51 +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!100
No description provided.