A limit is now a whole number of zero or more wherever the console reads
one: a rule's value, a contribution and a pool's limit each carry a CHECK
that refuses a negative. The console has no unlimited value. The one place
that read -1 as "no ceiling", the over-usage test of a rule change, now
compares the limit as the number it is. The fold of a pool's limit
saturates at the largest value the column holds instead of wrapping, so a
very high limit times a quantity, or several very high limits added,
cannot turn negative.
core.commit_rule_change refuses a negative value and a hidden limit below
one by name, with the same SQLSTATE as its other refusals. On a modify it
checks that the rule is active before it checks the value, as before.
The page that applies a staged rule change now keeps the errors the form
already computes for each staged row. Before, it parsed a staged value
again without the form's checks, so a request built by hand could store a
negative limit.
A limit rule gains limit_hidden, set in a new Hide limit column of the
Rules table and in the add form. Only a limit rule with a value of one or
more can hide its limit. A pool's limit for a key is hidden when any
active rule funding it hides it; the console works that out when a page
is read, from the rules the fold counts, and stores no copy. While a
limit is hidden and usage is below it, a member's usage row shows the
count with no limit and no bar, a plan card names the resource with no
number, and the FedWiki sites card shows the count alone. Once usage
reaches the limit, the usage row and the sites card show the limit again,
so a member is never stopped by a limit they cannot see; the plan card,
which has no usage, reads the mark from the rule and prints the name
alone. Operator pages always show the number; the organization's pool
table adds "Limit hidden" beside it, and the set's tray and History
state the flag. The mark is not part of what an application receives
and fires no delivery. If reading the mark fails, the page logs it and
shows the limit.
The console never prints the word "unlimited": the lint rule
unlimited-copy refuses it in the console's templates, Go strings and
scripts. Text an operator writes on a product, such as an additional
feature, is printed as given. Before the rule was switched on, the word
appeared in no copy, and in five places outside tests and SQL: four in
internal/entitlements/rule_change.go, the constant unlimitedLimit, its
comment, and the comment and comparison of atOrOver, which went with
the -1 value; and one comment in internal/domains/querier.go on a query
that deliberately takes every row, which stays.
The demo data gains a second numeric key, demo.gadgets, whose limit is
hidden, so the screens show a hidden limit beside a shown one.
Deliberate breaks: 34, run with scripts/mutation-check; 33 caught. The
one not listed as caught, removing rule_type = 'limit' from the two
hidden-limit queries, is equivalent: only limit rules carry
limit_hidden = true, which the CHECK enforces. Two breaks were missed on
the first run and are now caught by tests that stage two changes at once:
removing the pass that keeps a staged row's form errors, and removing the
staged check of a hidden limit below one. The list:
- IsLimitHidden without r.is_active (internal/entitlements/entitlement_set_rules.sql.go): ^TestLimitHiddenIsDerivedFromTheFundingRules$
- ListHiddenLimitKeysByPoolIDs without r.is_active (internal/entitlements/entitlement_set_rules.sql.go): ^TestHiddenLimitKeysAgreesWithIsLimitHidden$
- IsLimitHidden without the stacking-policy clause (internal/entitlements/entitlement_set_rules.sql.go): ^TestLimitHiddenIsDerivedFromTheFundingRules$
- ListHiddenLimitKeysByPoolIDs without the stacking-policy clause (internal/entitlements/entitlement_set_rules.sql.go): ^TestHiddenLimitKeysAgreesWithIsLimitHidden$
- IsLimitHidden without pp.status (internal/entitlements/entitlement_set_rules.sql.go): ^TestLimitHiddenIsDerivedFromTheFundingRules$
- saturatingMul > becomes >= (internal/entitlements/saturate.go): ^TestSaturatingArithmetic$
- saturatingMul in the fold is plain multiplication (internal/entitlements/materialize.go): ^TestMaterializeSaturatesInsteadOfWrapping$
- saturatingAdd in the fold is plain addition (internal/entitlements/materialize.go): ^TestMaterializeSaturatesInsteadOfWrapping$
- CeilingShows ignores usage (internal/entitlements/ceiling.go): ^TestCeilingShows$
- atOrOver reads > not >= (internal/entitlements/rule_change.go): ^TestCeilingShows$|^TestDiffEffects$
- drop the resource_value sign CHECK (scratch) (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestLimitSignMigrationRefusesANegativeRow$
- drop the contribution sign CHECK (scratch) (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestLimitSignMigrationRefusesANegativeRow$
- drop the resource_limit sign CHECK (scratch) (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestLimitSignMigrationRefusesANegativeRow$
- function body misspells limit_hidden on the modify path (scratch) (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestLimitSignMigrationDownRestoresTheFunction$
- drop the resource_value sign CHECK (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestLimitSignAndHiddenConstraints$
- drop the contribution sign CHECK (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestLimitSignAndHiddenConstraints$
- drop the resource_limit sign CHECK (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestLimitSignAndHiddenConstraints$
- drop the limit_hidden CHECK (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestLimitSignAndHiddenConstraints$
- function: drop the hidden-zero refusal on add (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestCommitRuleChangeRefusesANegativeAndAHiddenZero$
- function: drop the hidden-zero refusal on modify (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestCommitRuleChangeRefusesANegativeAndAHiddenZero$
- function: drop the negative refusal on add (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestCommitRuleChangeRefusesANegativeAndAHiddenZero$
- function: drop the negative refusal on modify (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestCommitRuleChangeRefusesANegativeAndAHiddenZero$
- function: modify reads an absent flag as false (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestCommitRuleChangeRefusesANegativeAndAHiddenZero$
- function: modify reads a JSON null flag as a value (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestCommitRuleChangeReadsANullFlagAsAbsentAndChecksActivityFirst$
- function: modify's UPDATE reads a JSON null flag as a value (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestCommitRuleChangeReadsANullFlagAsAbsentAndChecksActivityFirst$
- function: drop the active-rule check before the refusals (internal/db/migrations/00042_limit_sign_and_hidden.sql): ^TestCommitRuleChangeReadsANullFlagAsAbsentAndChecksActivityFirst$
- staged-row error pass removed (internal/server/operator_entitlement_set_rule_change.go): ^TestApplyRefusesEveryCraftedDeltaOnItsOwnLine$
- staged hidden-zero cross-check removed (internal/server/operator_entitlement_set_rule_change.go): ^TestApplyRefusesEveryCraftedHiddenZeroOnItsOwnLine$
- member panel fails whole when the hidden read fails (internal/server/member_entitlements.go): ^TestAFailedHiddenLimitReadShowsTheCeiling$
- member rows always show the ceiling (internal/server/member_entitlements.go): ^TestHiddenLimitUnderUsageShowsTheCountAlone$|^TestAFlagOnlyChangeIsSeenByTheMemberWithoutAMaterialization$|^TestTwoPlansFundingOneKeyHideItIfEitherHidesItDB$
- plan card prints a hidden rule's number (internal/server/member_plans.go): ^TestPlanFeatures$
- sites card always shows its ceiling (internal/integrations/fedwiki/web/partials.go): ^TestSitesCardHidesAHiddenLimitUnderTheFigure$
- sites card hides a hidden limit at usage too (internal/integrations/fedwiki/web/partials.go): ^TestSitesCardShowsAHiddenLimitAtTheFigure$
- operator pool table never marks a hidden limit (internal/server/operator_enrollment.go): Enrollment|PoolUsage
Deploy notes. Every step runs against the console database.
1. Before stopping the old release, count read-only the rows the new
CHECKs would refuse (console_reader can run it):
SELECT 'rule value' AS what, count(*) FROM core.entitlement_set_rules
WHERE resource_value < 0
UNION ALL
SELECT 'contribution', count(*) FROM core.numeric_entitlement_contributions
WHERE contributed_value < 0
UNION ALL
SELECT 'pool limit', count(*) FROM core.numeric_entitlements
WHERE resource_limit < 0;
Deploy when all three are 0. Correct a negative rule value in the old
release, through its set's Rules table, to the number you mean: the
commit recomputes every pool the rule funds, so the contributions and
pool limits that came from it clear with it. Then count again.
2. Stop every process of the old release, as for every upgrade.
3. Migrate to 00042 and start the new release. The migration fails at
the CHECK that finds a negative row and names it; nothing is changed
until it succeeds.
4. Run scripts/db-reader-role.sql again. Until it runs, console_reader
cannot read core.entitlement_set_rules.limit_hidden.
Rollback is the previous image on the 00041 schema: the Down drops
limit_hidden and the four CHECKs and restores the previous body of
core.commit_rule_change. Hidden marks are lost; no limit changes.
Closes #164.
Server Package
This package implements the HTTP server for the member-console application, including HTMX partial handlers.
Overview
The server uses Go's standard net/http package with:
- Bootstrap 5 for styling
- HTMX 2.0 for dynamic content updates
- gorilla/csrf for CSRF protection
- scs (
alexedwards/scs) for server-side session management (Valkey/Redis-backed)
Architecture
The HTTP server handles several types of requests:
- Static Files (
/static/*) - CSS, JS, images served from embedded assets - Page Handlers (
/) - Full HTML page rendering - Partial Handlers (
/partials/*) - HTMX partial HTML responses - Auth Handlers (
/login,/logout,/callback) - OAuth2/OIDC authentication
Partial handlers interact with Temporal workflows for async operations like site provisioning.
Files
server.go- Main server setup, middleware chain, route registrationmember_plans.go,member_plan_moves.go,member_entitlements.go,member_addons.go,member_invoices.go,workspace_partials.go,operator*.go- HTMX partial handlers for this package's own featuresprice_text.go- price text shared by the member and operator pages
Integration-owned handlers (e.g. FedWiki's member-facing API and partials)
live in their own tree under internal/integrations/<key>/web instead of
here — see docs/building-an-integration.md and
internal/integrations/fedwiki/web for the worked example. The patterns
below apply equally to handlers in either location.
HTMX Integration
Partial Handler Pattern
Partial handlers return HTML fragments (not full pages) that HTMX swaps into the DOM:
// Handler returns HTML fragment
func (h *Handler) GetItems(w http.ResponseWriter, r *http.Request) {
// ... fetch data ...
h.Templates.Render(w, "items.html", data)
}
<!-- Template returns partial HTML -->
<div class="table-responsive">
<table class="table">
{{ range .Sites }}
<tr>...</tr>
{{ end }}
</table>
</div>
HTMX Triggers
Use HX-Trigger header to trigger events that refresh other parts of the page:
// After successful operation, trigger refresh
w.Header().Set("HX-Trigger", `{"refreshSites": true}`)
<!-- Element listens for the trigger -->
<div hx-get="/partials/items"
hx-trigger="load, refreshItems from:body">
</div>
Cross-origin protection with HTMX
There is no CSRF token. internal/middleware/csrf.go wraps net/http's
CrossOriginProtection, which refuses a non-safe request whose
Sec-Fetch-Site (or, failing that, Origin) says it came from another
site. An htmx request from one of the console's own pages is same-origin
and passes with nothing added; a refusal is a 403 whose body names the
CSRF check, which error-handler.js reads to keep the person's input.
Workflow Execution
Handlers that start a Temporal workflow for a member's request answer when the start is confirmed, not when the workflow finishes. An integration records the request first (FedWiki's site lifecycle records a row in core.lifecycle_requests), starts the workflow under the identifier stored on that row, and returns a fragment or an HX-Trigger refresh; the workflow records its own outcome, and the list that shows the request re-renders until the outcome lands. A handler never waits for a workflow to finish: one that reads a result (the Stripe environment check) waits on WorkflowRun.Get under a context of a few seconds and answers that the check started when it runs out, because a workflow can outlast the server's write timeout. FedWiki starts a recorded request's workflow detached from the member's request, so a closed connection cannot leave the request with no workflow.
Route Organization
Routes are organized by feature using handler structs:
// ItemPartialsHandler groups related routes
type ItemPartialsHandler struct { ... }
func (h *ItemPartialsHandler) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /partials/items", h.GetItems)
mux.HandleFunc("POST /partials/items", h.CreateItem)
mux.HandleFunc("DELETE /partials/items/{id}", h.DeleteItem)
// ...
}
Error Rendering
Errors are rendered back into the same partial/modal that initiated the request:
func (h *Handler) renderCreateFormError(w http.ResponseWriter, userID int64, errMsg string) {
// Re-fetch form data
data := h.getCreateFormData(userID)
data.Error = errMsg
// Re-render the form partial with error displayed
h.Templates.Render(w, "create_form.html", data)
}
This keeps the user in context - they see the error in the same modal/form they were using.
Middleware Stack
handler = middleware.Logging(logger)(handler) // Request logging
handler = middleware.Recover(logger)(handler) // Panic recovery
handler = middleware.Compress()(handler) // Gzip compression
handler = middleware.SecurityHeaders()(handler) // Security headers
handler = middleware.RequestID()(handler) // Request ID generation
handler = csrf.Protect(key, opts...)(handler) // CSRF protection