Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pm-stack/aggregator/scripts/append-usage-snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ const quotaViewPath =
"workspace/goals/stewardship-commission-recurring-review-and-dispatch/hidden_files/visibility/quota-view.md",
);
const registryPath = process.env.QUOTA_REGISTRY_PATH ?? join(HIDDEN, "provider-quota-registry.md");
const providerUsagePath =
process.env.PROVIDER_USAGE_SNAPSHOT_PATH ?? join(HIDDEN, "provider-usage-snapshot.json");
const providerUsagePath = process.env.PROVIDER_USAGE_SNAPSHOT_PATH ?? join(HIDDEN, "provider-usage-snapshot.json");
const outboxPath = process.env.USAGE_SNAPSHOT_OUTBOX_PATH ?? join(HIDDEN, "usage-snapshot-outbox.jsonl");

async function main(): Promise<void> {
Expand Down
8 changes: 5 additions & 3 deletions pm-stack/aggregator/scripts/collect-fleet-feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ const launchRecordsPath = process.env.CLAUDE_LAUNCH_RECORDS_PATH ?? join(HIDDEN,
const quotaRegistryPath = process.env.QUOTA_REGISTRY_PATH ?? join(HIDDEN, "provider-quota-registry.md");
const snapshotOutboxPath = process.env.USAGE_SNAPSHOT_OUTBOX_PATH ?? join(HIDDEN, "usage-snapshot-outbox.jsonl");
const chargesExportPath = process.env.CHARGES_EXPORT_PATH ?? join(HOME, "atlas/charges-export/latest.json");
const chargesSurfacedStatePath =
process.env.CHARGES_SURFACED_STATE_PATH ?? join(HIDDEN, "charges-surfaced-state.json");
const chargesSurfacedStatePath = process.env.CHARGES_SURFACED_STATE_PATH ?? join(HIDDEN, "charges-surfaced-state.json");
const opencodeConfigPath = process.env.AA_OPENCODE_CONFIG_PATH ?? join(PKG, "..", "..", ".opencode", "opencode.jsonc");
const serverUrl = process.env.SANDBOX_OPENCODE_URL ?? "http://127.0.0.1:4096";
const passwordFile =
Expand Down Expand Up @@ -608,7 +607,10 @@ async function main(): Promise<void> {
await writeFile(outPath, JSON.stringify(feed, null, 2));
if (pendingSurfacedKeys) {
await writeFile(chargesSurfacedStatePath, JSON.stringify({ surfacedKeys: pendingSurfacedKeys }, null, 2)).catch(
(err) => console.error("collector: could not persist charges surfaced state (" + err + "); unmatched charges may re-surface"),
(err) =>
console.error(
"collector: could not persist charges surfaced state (" + err + "); unmatched charges may re-surface",
),
);
console.log(
"collector: charges auto-match - " +
Expand Down
4 changes: 1 addition & 3 deletions pm-stack/aggregator/src/charges-collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ export function parseChargeCandidatesExport(text: string): ChargeCandidate[] | n
} catch {
return null;
}
const list = Array.isArray(parsed)
? parsed
: (parsed as { candidates?: unknown })?.candidates;
const list = Array.isArray(parsed) ? parsed : (parsed as { candidates?: unknown })?.candidates;
if (!Array.isArray(list)) return null;
const out: ChargeCandidate[] = [];
for (const entry of list) {
Expand Down
5 changes: 1 addition & 4 deletions pm-stack/aggregator/src/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ export function projectPageInfo(json: unknown): { hasNextPage: boolean; endCurso

/** Fetch the whole pool (paginated). Throws on HTTP/GraphQL errors so
* the collector can fail-soft and omit the section. Never logs the token. */
export async function fetchProjectPool(
token: string,
fetchFn: typeof fetch = fetch,
): Promise<ProjectPoolItem[]> {
export async function fetchProjectPool(token: string, fetchFn: typeof fetch = fetch): Promise<ProjectPoolItem[]> {
const out: ProjectPoolItem[] = [];
let cursor: string | null = null;
for (let page = 0; page < 10; page++) {
Expand Down
9 changes: 1 addition & 8 deletions pm-stack/aggregator/src/snapshot-records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@
// (service, metric, ts, value), so an unchanged read re-derives the
// identical record and appends nothing.
import { POOL_SPECS, sectionOf } from "./quota-extract.ts";
import type {
DnsSection,
PaygLine,
QuotaPool,
SaasService,
SnapshotRecord,
UsageUnit,
} from "./model.ts";
import type { DnsSection, PaygLine, QuotaPool, SaasService, SnapshotRecord, UsageUnit } from "./model.ts";

const DATE_RE = /\d{4}-\d{2}-\d{2}/g;

Expand Down
32 changes: 27 additions & 5 deletions pm-stack/aggregator/test/snapshot-writers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ describe("recordsFromSaasServices", () => {
describe("recordsFromPaygLines", () => {
test("backfill ids are reused so the series continues", () => {
const lines: PaygLine[] = [
{ id: "meta-payg", label: "Meta", accruedUsd: 23.92, unbilledUsd: 0.01, note: "", measuredAt: "2026-10-25", sourceClass: "console" },
{
id: "meta-payg",
label: "Meta",
accruedUsd: 23.92,
unbilledUsd: 0.01,
note: "",
measuredAt: "2026-10-25",
sourceClass: "console",
},
{
id: "cloudflare-gateway",
label: "GW",
Expand Down Expand Up @@ -126,7 +134,9 @@ describe("recordsFromPools", () => {
provider: "Anthropic",
plan: "Claude Pro",
state: "healthy",
windows: [{ name: "weekly", unit: "other", limit: 100, used: 42, percent: 42, resetsAt: "2026-10-28T14:00:00-05:00" }],
windows: [
{ name: "weekly", unit: "other", limit: 100, used: 42, percent: 42, resetsAt: "2026-10-28T14:00:00-05:00" },
],
};
// The 2027 renewal date elsewhere in the section is an operational
// date, not a read — the Last checked bullet wins.
Expand Down Expand Up @@ -162,7 +172,11 @@ describe("recordsFromDns", () => {

describe("recordsFromProviderUsage", () => {
test("session count always; codex windows only when the endpoint answered", () => {
const walled = { generated_at: "2026-10-26T10:59:33Z", sessions: [1, 2, 3], codex_usage: { status: "http 403", usage: null } };
const walled = {
generated_at: "2026-10-26T10:59:33Z",
sessions: [1, 2, 3],
codex_usage: { status: "http 403", usage: null },
};
expect(recordsFromProviderUsage(walled, "check-usage")).toEqual([
{
ts: "2026-10-26T10:59:33Z",
Expand Down Expand Up @@ -205,13 +219,21 @@ describe("writer dedupe invariant", () => {
const store = new SnapshotStore(dir);
const batch = [
...recordsFromSaasServices(
[svc({ metrics: [{ id: "databases", label: "Databases", unit: "count", used: 1, limit: 100, highlight: true }] })],
[
svc({
metrics: [{ id: "databases", label: "Databases", unit: "count", used: 1, limit: 100, highlight: true }],
}),
],
"fleet-feed-collector",
),
// The same fact derived by another writer (different
// observedBy) is the same record to the store.
...recordsFromSaasServices(
[svc({ metrics: [{ id: "databases", label: "Databases", unit: "count", used: 1, limit: 100, highlight: true }] })],
[
svc({
metrics: [{ id: "databases", label: "Databases", unit: "count", used: 1, limit: 100, highlight: true }],
}),
],
"stewardship-cycle",
),
];
Expand Down
28 changes: 22 additions & 6 deletions pm-stack/aggregator/test/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ describe("payg lines + charges (§1.2, §2.4)", () => {
});

test("subscriptions strip is renewal-only rows from the records", () => {
const subs = extractSubscriptions(`${VIEW}\n### Other / flat (b)\n- **Amazon RxPass:** Subscription $5/mo; flat pharmacy.\n### Wispr Flow\n- **Plan:** Flow Pro annual $151.91/yr charged; renews 2027-10-09.`);
const subs = extractSubscriptions(
`${VIEW}\n### Other / flat (b)\n- **Amazon RxPass:** Subscription $5/mo; flat pharmacy.\n### Wispr Flow\n- **Plan:** Flow Pro annual $151.91/yr charged; renews 2027-10-09.`,
);
expect(subs).toEqual([
{ name: "Amazon RxPass", plan: "$5/mo flat pharmacy", renewsAt: null },
{ name: "Wispr Flow", plan: "Pro $151.91/yr", renewsAt: "2027-10-09" },
Expand Down Expand Up @@ -183,7 +185,15 @@ describe("bubble rules (§1.3)", () => {
services: [],
payg: [
{ id: "a", label: "A", accruedUsd: null, unbilledUsd: 5, note: "", measuredAt: null, sourceClass: "estimate" },
{ id: "b", label: "B", accruedUsd: null, unbilledUsd: 5.01, note: "", measuredAt: null, sourceClass: "estimate" },
{
id: "b",
label: "B",
accruedUsd: null,
unbilledUsd: 5.01,
note: "",
measuredAt: null,
sourceClass: "estimate",
},
],
});
expect(flags.filter((f) => f.kind === "payg-unbilled").map((f) => f.label)).toEqual(["B"]);
Expand Down Expand Up @@ -222,7 +232,15 @@ describe("snapshot store (§4)", () => {
try {
const store = new SnapshotStore(dir);
await store.append([
{ ts: "2026-10-25T12:00:00Z", serviceId: "cloudflare", metricId: "est", value: { low: 29.6, high: 45.1 }, unit: "usd-est", source: "estimate", observedBy: "test" },
{
ts: "2026-10-25T12:00:00Z",
serviceId: "cloudflare",
metricId: "est",
value: { low: 29.6, high: 45.1 },
unit: "usd-est",
source: "estimate",
observedBy: "test",
},
]);
const all = await store.readAll();
expect(all[0].value).toEqual({ low: 29.6, high: 45.1 });
Expand Down Expand Up @@ -259,9 +277,7 @@ describe("pace + freshness (§4, §1.1)", () => {
});
test("no projection from a single point or a flat series", () => {
expect(projectPace([rec("2026-10-24T00:00:00Z", 30)], "weekly")).toBeNull();
expect(
projectPace([rec("2026-10-22T00:00:00Z", 30), rec("2026-10-24T00:00:00Z", 30)], "weekly"),
).toBeNull();
expect(projectPace([rec("2026-10-22T00:00:00Z", 30), rec("2026-10-24T00:00:00Z", 30)], "weekly")).toBeNull();
});
test("freshness: older than cadence is stale; same-day is not", () => {
const now = new Date("2026-10-26T12:00:00Z");
Expand Down
Loading