model-usage: the vendor-neutral usage store (ADR 0054)

The home ADR 0050 left open for a usage reading. mesh-control is a CLI and
cannot consume events, so the store that keeps the current usage picture is a
MODULE — the audit-logger's sibling: it consumes `module.*.usage.*` and upserts
each reading into its own provisioned postgres store, latest per
(licence, consumer, period, metric), in the clear. One vendor-neutral table
holds BOTH grains; they differ only in `consumer` (the holding module for the
licence grain, the session for the finer one). The consumer creates its table
on startup and, as a restart-until-ready service, self-heals rather than
gating the apply on a run-once that must reach a provider over the overlay.

The vendor->row normalisation moves into the adapter, as ADR 0054 requires:
anthropic-manager (licence grain, utilization%) and anthropic-consumer (session
grain, token/cost) now emit already-normalised { rows: UsageRow[], raw } on
their existing keys, so the store stays vendor-blind.

Proven end to end by the mesh-lab model-usage bed (green): a usage event
emitted into the mesh is upserted at both grains, latest-per-key, in the clear.

Claude-Session: https://claude.ai/code/session_01LrgweAeERJYBg88c5cKDzF
This commit is contained in:
2026-09-07 04:07:26 +02:00
parent bfbdf90496
commit e8c3ea8979
9 changed files with 351 additions and 4 deletions
+34 -2
View File
@@ -25,9 +25,36 @@
import { readFileSync, writeFileSync, renameSync, mkdirSync } from "node:fs";
import { dirname } from "node:path";
import { readEnv } from "@novox/mesh-sdk/primitives";
import { seal } from "../sealedbox.js";
import { managerPublicKey, writeSealedGrant } from "../grantfile.js";
import { refreshGrant, grantFromRefresh, readUsage, flattenUsage } from "../client.js";
import { refreshGrant, grantFromRefresh, readUsage, flattenUsage, type UsageReading } from "../client.js";
/** The vendor-neutral usage row ADR 0054 fixes — the shape the model-usage store upserts. Kept local
* to the producer (the normalisation lives in the adapter), so nothing here couples to the SDK. */
interface UsageRow {
licence: string;
consumer: string;
period: string;
metric: string;
value: number;
}
/** Normalise a licence-grain reading into ADR 0054 rows: one utilization row per window, a row
* omitted when its percentage is absent. `consumer` is the holding module — the licence grain. */
function licenceRows(licence: string, consumer: string, reading: UsageReading): UsageRow[] {
const rows: UsageRow[] = [];
const add = (period: string, pct: number | null): void => {
if (pct !== null && pct !== undefined && Number.isFinite(pct)) {
rows.push({ licence, consumer, period, metric: "utilization", value: pct });
}
};
add("5h", reading.sessionPct);
add("7d", reading.weeklyPct);
add("extra", reading.extraPct);
return rows;
}
function required(name: string): string {
const v = process.env[name];
@@ -95,7 +122,12 @@ async function main(): Promise<void> {
JSON.stringify({ licence, grain: "licence", ...reading }),
);
}
await emitUsage({ licence, grain: "licence", ...reading });
// Emit ADR-0054 rows, not a vendor-shaped body: the consumer of module.*.usage.* is the
// vendor-neutral model-usage store, so the normalisation is done HERE. The node names the
// holding module; with MESH_NODE unset the consumer is the module alone.
const node = readEnv("MESH_NODE", "");
const consumer = node ? `${node}/anthropic-manager` : "anthropic-manager";
await emitUsage({ rows: licenceRows(licence, consumer, reading), raw: usage });
}
} catch (err) {
console.error(`[anthropic-manager] usage poll for ${licence} failed: ${err}`);