@nodii/capacity-contract
v0.4.1
Published
Dimension Capacity contract — the band-aware subscription capacity-replica projection (over capacity is admitted and charged, S08-29), the capacity quote DTO, and the deprecated AT_CAPACITY body.
Readme
@nodii/capacity-contract (TS)
The shared Dimension Capacity contract: one dialect spoken by CRM / HR / ticketing / FE for reading a tenant's per-dimension band from the capacity replica, and for the capacity quote.
Zero runtime dependencies — pure types, pure functions, hand-rolled runtime guards.
The same contract ships as nodii-capacity-contract (Python, python/capacity-contract)
and nodii.co/nodii-libs/go/capacity-contract (Go, go/capacity-contract).
All three ports assert the shared vectors in
scripts/parity/capacity-contract-cases.json, and the parity fence in
synthetic-consumers/capacity-contract checks that they answer every vector
identically.
The band model: over capacity is admitted, never refused
A roster owner MUST NOT refuse an assignment because the dimension is over
capacity. Over-capacity intake is admitted and charged under the band model,
with no holding pen (S08-29). Going past the band is overage, charged through
tenant-service's CapacityService. The only refusal on the assignment path is
the one CapacityService answers, 409 OVERAGE_BLOCKED (an enforcing budget,
or a wallet that cannot pay with negative balance off; S08-26).
So nothing in this package is a gate. capacityFor tells you where the band
ceiling is; it never tells you to say no.
What it carries
1. The capacity-replica projection spec
The per-(subscription, dimension) row a roster owner projects from the subscription replication stream it already consumes:
import {
capacityFor,
bandReplicaViewFor,
remainingCapacity,
type CapacityReplicaRow, // { tenant_id, subscription_id, dimension_key,
// capacity, source_entity_version,
// module_key?, entitled? }
} from "@nodii/capacity-contract";
// The add-on that carries the dimension is the module key (D1154 §5, D1301).
const ceiling = capacityFor(replicaRows, "collectors", "crm.collector");
const insideBand = remainingCapacity(ceiling, rosterCount);
// insideBand === 0 ⇒ the next add is overage: admit it and charge it through
// tenant-service. Do NOT refuse it.How capacityFor(rows, dimensionKey, moduleKey) reads the band. Each module
bills by dimensions, and each dimension has a band the tenant picked; no
capacity record exists without a module (Q-231).
- Malformed rows, rows of other dimensions and rows of other modules are ignored.
- A row of this dimension without
module_keyis not capacity. It is never summed, never used as a ceiling, and never evicts a row of this module. It is reported as a defect: once per reading, every such row goes tooptions.onModuleLessRows. The default,warnModuleLessRows, logs oneconsole.warnwithMODULE_LESS_ROW_DEFECTfor each (tenant, subscription, dimension) not yet named in the process. Pass a handler to send the report to your own logger or metrics. - Last-Writer-Wins per subscription on
source_entity_version. At an equal version an entitled row, then the larger ceiling, wins, so the reading never depends on row order. - Across subscriptions an entitled row beats an unentitled one, then the larger ceiling wins (one module is one subscription, D1301, so more than one is not expected).
entitled: false(the module is not subscribed) gives 0.- No matching row gives 0.
bandReplicaViewFor returns the same reading with its subscription, module and
entitlement, or null when nothing matches. moduleKey must be a non-empty
string; anything else throws a TypeError.
2. The capacity quote DTO (D615)
What tenant-service returns from POST /commerce/capacity/quote, and what the
confirm-with-charge dialog renders:
import { isCapacityQuote, type CapacityQuote } from "@nodii/capacity-contract";
// { amount_minor, currency, wallet_balance_minor, wallet_covers,
// topup_required_minor, minimum_applied }Integer minor units; currency an ISO-4217 code. wallet_balance_minor MAY be
negative: a prepaid wallet can sit below zero under a mandate plus acknowledgement
(S08-34), and tenant sends billing's balance unclamped. wallet_covers true ⇒ "it
just works" (no charge). topup_required_minor > 0 ⇒ embedded top-up. minimum_applied
⇒ the platform-minimum floor was hit.
3. Deprecated: the legacy AT_CAPACITY body
AT_CAPACITY_CODE, AtCapacityError and isAtCapacityError are deprecated.
Never produce an AT_CAPACITY refusal (S08-29). The atCapacityError builder
is removed, so the package cannot produce one. The guard stays only so a
consumer can read a body from an older producer while it moves off it. These
exports are removed in a later major release.
