Skip to content

pubsub: the embedded bus states its store, and streams reserve a share of it - #87

Merged
zeekay merged 6 commits into
hanzoai:mainfrom
dissoupov:local-runtime-budget
Nov 28, 2026
Merged

zeekay merged 6 commits into
hanzoai:mainfrom
dissoupov:local-runtime-budget

Conversation

@dissoupov

Copy link
Copy Markdown
Contributor

What this fixes

On a node with a small store, every consumer of the event stream is refused by JetStream with err_code=10047 (insufficient storage resources available) and retries every two seconds. Measured on a local runtime with 3.92 GB free: 38,086 refused requests in two hours, from webhook, marketing, destination and triage.

Cause

The embedded server's file store is the free space under its store directory when it boots, and its account states no limit. AccountInfo therefore answers max_storage: -1 whatever the server can hold. apps/event already clamped the EVENT stream to half the account's limit, but with -1 the clamp never engaged, so it asked for its 8 GiB production ceiling everywhere. USAGE (1 GiB) read nothing at all.

Change

  • sdk/bus/budget.go: one table for the streams that reserve storage. A Reservation is a production ceiling and a share of a store too small to hold it: EVENT 8 GiB / 50%, USAGE 1 GiB / 10%. The package still links no bus client.
  • apps/event, internal/driver/ledger: both streams hold their ceiling or their share of the limit the bus reports.
  • apps/pubsub/budget.go: the embedded server states its store on the account in two cases: the deployment declares a budget (CLOUD_PUBSUB_MAX_STORE, bytes), or it declares none and the store cannot hold the ceilings together.

What does not change

A node whose store holds the ceilings and declares no budget is left exactly as it was: the account stays unlimited and the streams keep 8 GiB and 1 GiB. That is production.

One thing worth a reviewer's eye

The server counts an account's limit beside the reservations already made under it (sufficientResources), and refuses a limit when the two exceed its store. So the limit is bounded to the part of the store whose own reservations still fit beside it (store × 100 / (100 + reserved share)). That bound is also what keeps the limit the same across a restart that restores last boot's streams; without it the streams would shrink on every boot. TestTheLimitHoldsAcrossARestart pins it.

Verified

  • go vet and tests pass for sdk/bus, apps/pubsub, apps/event, internal/driver/ledger.
  • TestADeclaredBudgetIsWhatTheAccountReports: on a 256 MiB budget the account reports 256 MiB, every reserving stream fits at its share, a stream with no ceiling is taken, and the 8 GiB ceiling is refused with 10047.
  • End to end on a local k3s runtime with a 2 GiB declared store: EVENT at 1 GiB, USAGE at 204 MiB, 0 refused declarations after boot, a five-minute soak and a smoke pass.

Not verified: a production-sized node. The no-change path there is covered by unit tests of limit() only.

The branch is cut from c7268d3f; the files it touches are unchanged between that commit and main.

🤖 Generated with Claude Code

Copilot AI 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.

🟡 Changes recommended

Critical active issues remain: declared budgets can be silently skipped when capacity is zero, and replica-unaware share calculations can overcommit finite account limits and reject declarations.

2 open issues
What changed in this PR

This pull request makes embedded PubSub storage limits visible to JetStream and scales the EVENT and USAGE streams according to shared storage reservations.

Changes:

  • Defines shared stream ceilings, storage shares, and sizing helpers.
  • Applies account-aware limits to EVENT and USAGE stream declarations.
  • Adds embedded-server budget parsing, account limit configuration, documentation, and regression tests.
File Description
sdk/​bus/​budget.go Defines shared stream storage ceilings, shares, and reservation calculations.
sdk/​bus/​budget_test.go Tests reservation arithmetic and aggregate storage fit.
internal/​driver/​ledger/​bus.go Sizes the USAGE stream from the reported account storage limit.
apps/​pubsub/​pubsub.go Applies the embedded server's storage budget before exposing the bus.
apps/​pubsub/​doc.go Documents the CLOUD_PUBSUB_MAX_STORE setting and constrained-store behavior.
apps/​pubsub/​budget.go Parses storage budgets and configures embedded JetStream account limits.
apps/​pubsub/​budget_test.go Tests budget calculation, restart stability, invalid values, and stream behavior.
apps/​event/​plane_test.go Updates event stream limit assertions to use shared reservation constants.
apps/​event/​bus.go Sizes the EVENT stream using the shared reservation policy and account limit.
apps/​event/​bus_test.go Tests EVENT stream behavior on a constrained account store.

Review effort: Lite

Comment thread apps/pubsub/budget.go
Comment on lines +85 to +87
if n == 0 {
return 0, nil
}
Comment thread sdk/bus/budget.go Outdated
}
// In two parts, so a store of any size divides exactly and never overflows.
p := int64(r.Share)
return min(r.Ceiling, store/100*p+store%100*p/100)
@dissoupov

Copy link
Copy Markdown
Contributor Author

Added a fourth commit (cb7b3e11) after a run on a store carried over from the old sizes exposed a gap.

The volume still held USAGE at its old 1 GiB under an account limit of 1.36 GiB. EVENT was refused at its share, and where it existed it could not be resized either, because a resize is weighed against what the other streams hold. Its consumers went back to retrying every two seconds.

A stream now asks for its share or for what the others leave, whichever is smaller (Reservation.Within), and when no size is taken at all, a stream that already exists is kept as it is. TestTheStreamSurvivesACarriedOverStore reproduces it against a real embedded server.

Verified on that same runtime after the change: 0 refused declarations, EVENT at 1 GiB, USAGE resized to 204 MiB on a 2 GiB account.

🤖 Generated with Claude Code

dissoupov and others added 6 commits November 27, 2026 21:06
A stream that states MaxBytes reserves that many bytes when it is declared,
and JetStream refuses a declaration that does not fit what is left (10047).
The ceilings were written where each stream is declared, 8 GiB for EVENT and
1 GiB for USAGE, with nothing that made them fit one store together.

Reservation is a stream's production ceiling and its share of a store too
small to hold that; Events and Usage are the rows. Need and Reserved are what
the embedded server reads to know when a store is too small for the ceilings.
The package stays free of any bus client.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
EVENT read the account's file limit and halved it, which was right, and USAGE
read nothing. Both now hold their ceiling or their row's share of the limit
the bus reports (bus.Events, bus.Usage).

On an account that reports no limit the ceiling stands, as before. That is
the case the next commit closes for the embedded server, which never
reported one.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The embedded server's file store is the free space under its store directory
when it boots, and its account stated no limit, so AccountInfo answered -1
whatever the server could hold. A stream that sizes itself from that answer
asked for its production ceiling everywhere. Measured on a local runtime with
3.92 GB free: EVENT asked for 8 GiB, was refused with 10047, and its five
consumers retried every two seconds, 38,086 refused requests in two hours.

The server now states the limit in two cases: the deployment declares a
budget (CLOUD_PUBSUB_MAX_STORE, bytes), or it declares none and the store
cannot hold the ceilings together. A node with room for them and no budget is
left exactly as it was, production included.

The server counts an account's limit beside the reservations made under it,
so the limit is bounded to what still fits with its own reservations; that is
also what keeps it the same across a restart that restores last boot's
streams.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…t has

The shares fit together only when every stream holds its share. A store
carried over from a build with other sizes does not: measured on a local
runtime whose volume kept USAGE at its old 1 GiB under an account limit of
1.36 GiB, EVENT was refused at its share (10047), and where it existed it
could not be resized either, since a resize is weighed against what the
others hold. Its consumers went back to retrying every two seconds.

A stream now asks for its share or for what the others leave, whichever is
smaller (Reservation.Within), and takes its share again once they have
taken theirs. When no size is taken at all, a stream that already exists is
kept as it is: a refused resize is no reason to stop using it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…tream shares by replicas

Address PR hanzoai#87 review findings:
- Reject a declared CLOUD_PUBSUB_MAX_STORE budget when limit() resolves
  to zero (exhausted reservations or zero server capacity) instead of
  silently skipping UpdateJetStreamLimits and leaving MaxStore unlimited.
- Make Reservation.Of, Reservation.Within, Need, and embedded limit
  calculations replica-aware so un-tiered account MaxStore limits
  (charged by JetStream as MaxBytes * Replicas) fit cleanly at both
  replicas=1 and replicas=3 without triggering 10047.

Co-Authored-By: Denis Issoupov <denis@averlon.io>
…and quiet missing-token drain

- Check Forge repo Mirror / Actions metadata in gateOpen before writing
  the fork error status, so fork PRs against mirror or non-Actions repos
  are never stamped with a bogus hanzo/ci error while still never
  fetching or proposing fork branches on Forge.
- Log once at Info instead of spamming Warn on every runner queue tick
  when FORGE_TRACKER_TOKEN@prod is unconfigured in local/standalone KMS.
@zeekay
zeekay force-pushed the local-runtime-budget branch from cb7b3e1 to e95c699 Compare November 28, 2026 05:10
@zeekay
zeekay merged commit e95c699 into hanzoai:main Nov 28, 2026
1 of 3 checks passed
@zeekay

zeekay commented Nov 28, 2026

Copy link
Copy Markdown
Member

Rebased onto latest main and merged (e95c699) with follow-up fixes:

  1. apps/pubsub/budget.go: Fail closed with an explicit error when CLOUD_PUBSUB_MAX_STORE is declared (want > 0) and limit() resolves to 0 (exhausted store reservations or zero server capacity), instead of silently skipping UpdateJetStreamLimits and leaving MaxStore: -1 unlimited.
  2. sdk/bus/budget.go & apps/pubsub/budget.go: Make Reservation.Of, Reservation.Within, Need, and embedded limit math replica-aware (max(replicas, 1)) so un-tiered account MaxStore limits (charged as MaxBytes * Replicas) fit cleanly at both replicas=1 and replicas=3 without 10047 errors.
  3. apps/provider/gate.go: Check Forge repository Mirror / Actions metadata before writing the fork error status so fork PRs on mirror or non-Actions repos are not stamped with a spurious hanzo/ci error.
  4. apps/platform/queue.go: Log once at Info instead of warning every tick when FORGE_TRACKER_TOKEN@prod is unconfigured in standalone/local KMS.

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.

3 participants