How QuotaPulse is built, and why
QuotaPulse does one thing: it tells you before something you depend on runs out or comes due. Every design decision below follows from taking that sentence literally.
It is worked by a person and by agents, side by side, and is built around the agent as the one that uses it most: every capability is an operation an agent can call, every change is written down with who made it and why, problems have owners and can be handed from one to the other, and an agent can be let to answer first — while the person keeps everything they had, sees everything an agent does, and decides what an agent may only ask for. agents-and-the-person.md is that model from the user's side; One interface and Agents and the person below are how it is built.
The model
Two kinds of things are watched.
| What it is | Where it comes from | What is asked of it | |
|---|---|---|---|
| Meter | Something that runs out: a prepaid balance, a quota that resets, a running total of spend | Sampled from a platform through an account, or pushed in by a script | How long will it last? |
| Reminder (a date) | Something that comes due: a renewal, a birthday, an expiry | A recurrence rule, on the solar or the lunar calendar | How many days are left? |
Both feed one stream of incidents, and incidents are the only thing anyone is ever notified about.
account ──sample──▶ meter ──┐
script ──push────▶ meter ──┼─ evaluate ─▶ conditions ─ reconcile ─▶ incidents ─ announce ─▶ outbox ─▶ channels
rule ──calendar▶ date ──┘ ▲ │ owner, timeline (people's, agents')
│ ▼
person (dashboard) ──┐ the log ◀── every change, with who and why
agent (MCP, HTTP) ──┴── operations ── grants · one transaction · log ──▶ the engine
A meter has a kind, and the kind decides how its history is read
A meter is only ever sampled: there is no ledger of transactions, just a number every half hour. Everything else is reconstructed from the difference between neighbouring readings, and what a difference means depends on the kind.
- balance — a stock. A drop is spend; a rise is a top-up.
- quota — an allowance that resets. The value is what is left of
limituntilresets_at. A rise is a reset, never income. - counter — an odometer of spend. A rise is spend; a drop means it started over (a new month), and whatever it reads now was spent since.
Keeping the kinds apart is what keeps the arithmetic honest. An earlier version stored one number per account and treated every drop as money, so a coding plan's percentage bouncing between 100 and 0 showed up as "189 spent this month". A quota is not money and is never added to it.
One account, several meters
A platform's answer often holds more than one thing worth watching: a five-hour token window and a monthly tool quota; a balance per currency. A provider definition yields a list of meters, each with its own history, forecast and rules. Collapsing them into one number throws away the reset time, which is the most useful thing a quota knows about itself.
Incidents, not alerts
A check that fires every time it runs produces a message a day for a balance that has been low for a week, and people learn to stop reading. So QuotaPulse does not "send alerts". It evaluates conditions — plain facts about the present, such as runs out in five days — and reconciles them with the incidents already open:
- a new condition opens an incident;
- a worse one (higher severity, or a more pressing reason at the same severity) escalates it;
- a condition that no longer holds resolves it.
Those edges are announced. In between, nothing is said — with one exception: a critical incident nobody has acknowledged repeats once a day. A warning is said once; if it gets worse, that is an escalation and a message of its own.
Three details make this behave well in practice:
- Stickiness. An open incident closes a little past where it opened. A runway hovering at exactly the seven-day line would otherwise open and resolve on alternate samples.
- Quiet hours hold back what can wait. Anything that is not critical is queued until morning. If the problem goes away overnight, the queued message is dropped: nobody should wake up to news of a problem that no longer exists.
- Recoveries go to whoever was told. "It's fine again" is sent to the channels that actually received the "it broke" message, and to nobody else.
Dates take the same path. A reminder whose day is inside its lead window is a condition; the incident it opens carries the notifications — one per lead day, at the time of day the user chose — and remembers which have been sent, so a restart neither repeats one nor loses it.
One interface
Everything anyone can ask of QuotaPulse or do to it is an operation,
defined once in internal/ops: a name, what it is for — written for an agent
choosing a tool — its input and answer as JSON Schema, the scope it needs,
what it does to the world (reads, changes, removes for good), whether calling
it twice is the same as once, and whether it only touches the database. The
full list is operations.md, which a test regenerates from the
code.
Every way in is generated from that catalog and goes through one dispatcher:
- MCP (
internal/mcp) offers each operation the caller may use as a tool, annotated from the catalog; the skill is served beside them. - HTTP:
POST /api/v1/ops/{name}takes the same input as the tool, andGET /api/v1/opslists what the caller may call. - The command line:
quotapulse opsandquotapulse call. - The dashboard's routes (
internal/api) are named after the operations they carry out and run under them: they take and give the dashboard's own shapes, but are checked, carried out and logged as the operation is.
So the person and an agent are given the same capabilities by construction. What the dispatcher does for every change, whoever calls it:
- Grants. The person may do everything. An agent holds each scope directly, by proposal only, or not at all; a change it may only propose becomes a proposal instead of happening. A few operations belong to one side: deciding on a proposal is the person's.
- One transaction. A change that only touches the database runs in one
SQLite transaction carried by its context (
store.Atomic), under the engine's lock taken first — one order for everyone, so the background loop and an operation never wait on each other in a circle. It happens entirely or not at all, and what it announces to the dashboard waits for the commit. - Rehearsal. The same transaction, rolled back, is a dry run: the change runs for real, the dispatcher reads what it is about before (outside the transaction, which still sees the committed state) and after (inside), and reports the difference field by field, and the log entries it would have written — incidents it would open or resolve included.
- The log. The change is written down: who, through which door, the reason they gave, what it was about and what it changed.
- Retries. With an idempotency key, a repeated call gets the first answer.
Agents and the person
Actors. Every change is made by the person, an agent, or QuotaPulse
itself, and says which (core.Actor). An agent (core.Agent) has a key of
its own — only its hash is stored — a kind (an agent, or an integration that
only reads or reports), a description, and its grants: scopes held
directly, propose held by proposal only. A 1.x API token became an agent
on upgrade, keeping its key.
The log (events) is one ordered table: QuotaPulse's own entries —
incidents opening, escalating, resolving — and every change anyone made. Its
IDs never repeat, so they serve as a cursor: an agent reads the log as a
feed (changes), from a position the server keeps for it, and can wait on it
— a long poll that wakes when the next entry commits. The person reads it as
history. Entries are pruned with the rest of the history.
Ownership. An incident has an assignee — the person, an agent, or
nobody — and remembers who acknowledged it. Taking, giving, noting and
acknowledging are log entries tied to the incident, which with its
deliveries make its timeline. An agent hands over an incident to the
person with a note; the person is told at once by a message of its own, the
note first.
Answering first. With an agent set as the responder, a new operational
incident is assigned to it as it opens, and the person's deliveries are
queued with their not_before pushed past a hold and marked held_for the
agent. If the agent acknowledges in time, the held deliveries are dropped;
if it hands over, they are dropped for the hand-over's message; if nothing
happens, the outbox sends them when their time comes. Critical news and date
reminders are never held. This is the same mechanism as quiet hours, and
recoveries still go only to the channels that were told.
Proposals are stored with their input sealed — it may carry a credential — and the rehearsal made when they were asked. Each opens an approval incident, announced like any other, which resolves without a message when the proposal is decided or expires. Approving carries the operation out with the proposal's input, as the agent's change, approved by the person.
The forecast
internal/forecast is pure: readings in, numbers out, no clock. A forecast is
"as of the last reading", so a meter that stopped reporting does not quietly grow a
longer runway.
Burn rate. Spend per day over the last 30 days, with each day's weight halving every 7 days. A one-off batch job fades out instead of dragging the average for a month, and a new habit shows up within days. The plain 7- and 30-day averages are reported next to it so the estimate can be checked by eye.
Runway is the balance divided by that burn. It is only acted on once there is a full day of history; before that it is shown, marked as thin.
Bursts. The same history is read a second way: how fast did money leave in the last 3 hours, and in the last 24? A burst becomes an incident only if it is both
- a real jump — at least three times the pace before it — and
- soon enough to matter — at that rate the balance would be gone within days.
Either test alone is useless. A jump alone is noise: spending 2.50 after a week of 0.10 a day is "25 times the baseline" on a balance that still lasts two hundred days (that exact false alarm is a test case). Soon alone is the runway rule's business. Together they are what a leaked key looks like.
Quota pace. For an allowance with a known window: the share used so far divided by the share of the window that has passed. Above 1, it runs out before it resets, and the forecast says when.
Reading a platform
A platform is a definition: one HTTP request and one jq program.
.balance_infos[]? | {
key: ("balance-" + (.currency | ascii_downcase)),
kind: "balance", unit: .currency,
value: (.total_balance | num)
}
The program receives the response and outputs meters. Calling error("…") fails the
sample with that message, which is how an API that answers 200 with an error inside
is handled. jq was chosen over a home-grown path syntax because platforms differ in
ways a path cannot express — a balance that is the difference of two fields, a list
of windows, a number hidden in a sentence — and because the people who run this
already know it.
The built-in catalog (internal/provider/catalog.go) is nothing but definitions;
it can do nothing a definition saved from the dashboard cannot. Each entry says how
it was verified: against the live API, by replaying a recorded response, or from
documentation only.
Things the fetcher is strict about, because it handles credentials:
- Redirects are not followed. A redirect to another host would carry the credential with it.
- Errors are redacted. Platforms like to echo the key they rejected; every error is scrubbed of the account's field values before it is stored or shown.
- The jq program sees the response and nothing else — no environment, no files, a two-second budget, and only the account's non-secret fields.
Notifications
internal/notify turns a channel-independent Message into each platform's
dialect and reports honestly whether the platform took it. Several chat platforms
answer HTTP 200 to a message they have thrown away (wrong keyword, bad signature);
their verdict is in the response body and each driver reads it there.
A channel can belong to an agent (agent_id): a webhook into the agent's
own runtime is how it is woken. It hears at once — through quiet hours and
through the responder's hold — and is told when an incident is assigned to
its agent. The webhook payload's subject carries the incident's assignee
and, for an assignment or a hand-over, the note.
A browser is a channel too. When someone turns on notifications on a device, its
push subscription becomes a channel of type webpush, so a phone gets the same
controls as a chat group: which topics, how severe, a test button, a delivery
log. When the browser unsubscribes, the push service says so and the channel is
removed — an ended subscription is not a failing channel.
Delivery goes through an outbox in the database: a row is written before anything is sent, retried with backoff for about three hours, and kept afterwards as the notification log. A channel that fails three deliveries in a row gets an incident of its own, announced over the other channels.
Storage
One SQLite file (modernc.org/sqlite, pure Go, WAL). Every query goes through
its context's transaction when there is one (a test refuses a query that
reaches past it), which is what lets an operation run as one unit, or be
rehearsed.
This is a deliberate choice. QuotaPulse is a single-user tool that samples a handful of accounts every half hour; a database server would be the largest moving part of the deployment and add nothing. One file makes backup a copy and migration a move. Earlier versions supported three database engines with three sets of generated queries, and every feature paid for it three times.
Two things worth knowing about the schema:
- Flat runs are folded. Most samples change nothing — a balance sits still for hours. A flat run is stored as two rows, where it began and where it currently ends; a third identical sample just moves the end. Nothing is lost (a straight line between the two is exactly what was observed) and a year of history stays small.
- The database enforces the incident invariant. A partial unique index allows at most one open incident per subject, kind and occurrence.
Credentials and channel settings are sealed with AES-256-GCM under a key derived
from QUOTAPULSE_SECRET, or from a key file created next to the database on first
start. The key file means a stolen database backup is useless on its own; it does
not protect against someone who can read the whole data directory, and nothing
here pretends otherwise. A start that finds the key file missing makes a new one
only when the database holds no sealed value yet; otherwise it refuses and
removes what it made, because the old values would stay unreadable for good and
whatever was saved next would be sealed under the new key — no single key would
read the database again. export is the exception: it runs with a key that is
written nowhere, so that what can be read without the credentials can still be
saved. A credential or a channel setting that cannot be opened is flagged where
it is shown (credential_unreadable, config_unreadable) rather than failing
the list, and can be entered again.
Configuration
The database is the single source of truth, edited from the dashboard. The environment can say how to start — a data directory, a port, a password — and need not say anything: a new install asks its password, time zone and language the first time it is opened. There are no feature flags: an unused feature is an empty list.
For those who want configuration in a file there is the bundle
(internal/bundle): one JSON document holding accounts, dates, channels and
settings by name. It is the export format, the import format, what
QUOTAPULSE_BOOTSTRAP loads into an empty database, and what the tool that
migrates a pre-1.0 install produces.
Access
- The dashboard signs in with one password and gets an HttpOnly, SameSite=Strict session cookie. The cookie is a signed expiry, not a server-side session; the password is mixed into the signing key, so changing it signs everyone out. The password is only ever used stretched (PBKDF2, with a salt of the install's own): neither a cookie nor a sign-in is checked against anything cheaper. Requests that change something must also come from the dashboard's own origin.
- The password is
QUOTAPULSE_PASSWORD, or — without it — chosen in the dashboard and kept in the database as its stretch (internal/api/access.go). A new install can be set up by whoever opens it within an hour of the start; after that, or once anything is configured, choosing the password takes a one-time code from the server's log or fromquotapulse reset-password. - Agents and scripts use an agent's key, of which only a hash is stored.
Its grants are scopes —
read,act(problems, dates, muting, a fresh reading),ingest(push readings),admin— each held directly or by proposal only. A switched-off or expired agent's key is refused. - MCP (
/mcp) accepts keys only, and offers exactly the operations the agent's grants allow, directly or by proposal.
Until a password is chosen nobody can sign in, and an install left unclaimed does not stay open to anyone who finds it. Something that holds API keys should never be open by default.
Layout
cmd/quotapulse/ entry point: serve, demo, export, import, healthcheck
internal/
core/ the vocabulary: Meter, Reminder, Incident, Channel, Settings
recur/ recurrence rules, solar and lunar — pure
forecast/ burn rate, runway, bursts, quota pace — pure
provider/ definitions, the built-in catalog, the fetcher, request signing
notify/ messages and the channel drivers
secret/ encryption at rest, sessions, tokens
store/ SQLite: schema, queries, nothing else
engine/ sample -> evaluate -> reconcile -> announce; owners, hand-overs,
the responder's hold, proposals, the log; the views; every write
ops/ the operations: the catalog, and the dispatcher every door goes
through — grants, proposals, one transaction, rehearsal, the log
runtime/ the background loops
api/ HTTP: the operations endpoint, the dashboard's routes, the event
stream, metrics, the dashboard's files
mcp/ the operations as MCP tools, and the skill
bundle/ export and import
i18n/ the sentences that leave the server, in English and Chinese
demo/ generated data and a fake internet, for trying it and for screenshots
web/ the dashboard: Preact + TypeScript, built into the binary
tools/legacy-import/ migrates a pre-1.0 database; a module of its own
Dependencies point one way: core, recur and forecast know nothing of storage
or HTTP; engine is the only package that writes; ops decides who may do
what, and writes the log; api and mcp translate and decide nothing. Anything
the dashboard can do, an agent does through the same operation, under the same
rules.
Working on it
npm --prefix web ci && npm --prefix web run build # the dashboard; the binary embeds it
go build ./cmd/quotapulse
go test -race ./... # the engine tests run on a clock that only moves when told to
(cd tools/legacy-import && go test ./...)
./quotapulse demo --lang zh # realistic data, no real credentials; password: demo
QUOTAPULSE_WEB_DIR=web/dist ./quotapulse demo # serve the dashboard from disk while working on it
The demo runs the real engine against a fake internet (internal/demo): every
platform it samples and every chat app it notifies is answered locally. It is the
quickest way to see a change, and it is what the screenshots are taken from.