@saadnvd1/agent-os 0.3.498 → 0.3.501
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -0
- package/app/api/usage/rollups/route.ts +36 -0
- package/components/Devices/ConnectCard.tsx +10 -12
- package/components/Tasks/TaskRow.tsx +2 -0
- package/components/Usage/RatchetView.tsx +249 -0
- package/components/Usage/UsageDialog.tsx +7 -1
- package/components/Usage/WindowCard.tsx +2 -1
- package/data/usage/index.ts +28 -0
- package/lib/chat/drivers/claude-mapper.ts +1 -0
- package/lib/chat/drivers/claude.ts +46 -0
- package/lib/chat/events.ts +3 -0
- package/lib/chat/worker/host.ts +21 -0
- package/lib/chat/worker/protocol.ts +8 -1
- package/lib/connect/cli.ts +51 -72
- package/lib/connect/cloudflared.ts +172 -0
- package/lib/connect/config.ts +75 -26
- package/lib/connect/control-api.ts +107 -48
- package/lib/connect/enrol.ts +33 -107
- package/lib/connect/serve.ts +177 -94
- package/lib/connect/supervise.ts +281 -0
- package/lib/db/migrations.ts +86 -0
- package/lib/orchestrator/armed.ts +2 -1
- package/lib/orchestrator/deliver.ts +49 -7
- package/lib/orchestrator/events.ts +35 -2
- package/lib/security/auth.ts +20 -4
- package/lib/security/gate.ts +8 -5
- package/lib/security/reach.ts +1 -1
- package/lib/tasks/index.ts +8 -0
- package/lib/usage/calls.ts +284 -0
- package/lib/usage/check-runs.ts +40 -19
- package/lib/usage/config.ts +56 -0
- package/lib/usage/nightly.ts +157 -0
- package/lib/usage/quality.ts +239 -0
- package/lib/usage/ratchet.ts +275 -0
- package/lib/usage/rate-limits.ts +192 -0
- package/lib/usage/rollup.ts +90 -0
- package/lib/usage/span.ts +152 -0
- package/lib/usage/transcript-import.ts +226 -0
- package/lib/usage/weights.ts +64 -0
- package/lib/usage/windows.ts +27 -2
- package/package.json +2 -4
- package/server.ts +7 -4
- package/stores/usageUi.ts +3 -3
- package/lib/connect/cert-check.ts +0 -32
- package/lib/connect/cloudflare-dns.ts +0 -56
- package/lib/connect/frames.ts +0 -72
- package/lib/connect/identity.ts +0 -98
- package/lib/connect/mux.ts +0 -110
- package/lib/connect/stream.ts +0 -106
- package/lib/connect/tunnel.ts +0 -98
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ratchet. The baseline is the first week of rollups, and from then on
|
|
3
|
+
* only ever moves to a better value. An alert fires, once, when cost per
|
|
4
|
+
* merged PR or the clean-merge rate has drifted past the configured margin,
|
|
5
|
+
* naming the supporting metric that moved most.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { db } from "../db";
|
|
9
|
+
import { spanOf, type Rollup, type Span } from "./span";
|
|
10
|
+
import type { UsageConfig } from "./config";
|
|
11
|
+
|
|
12
|
+
export type Direction = "lower" | "higher";
|
|
13
|
+
|
|
14
|
+
// The better of the two. A missing or invalid candidate keeps what was.
|
|
15
|
+
export function ratchet(
|
|
16
|
+
prev: number | null,
|
|
17
|
+
candidate: number | null,
|
|
18
|
+
better: Direction
|
|
19
|
+
): number | null {
|
|
20
|
+
if (candidate === null || !Number.isFinite(candidate)) return prev;
|
|
21
|
+
if (prev === null || !Number.isFinite(prev)) return candidate;
|
|
22
|
+
return better === "lower"
|
|
23
|
+
? Math.min(prev, candidate)
|
|
24
|
+
: Math.max(prev, candidate);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// What the baseline is judged on.
|
|
28
|
+
export const HEADLINE = {
|
|
29
|
+
costPerMerged: "lower",
|
|
30
|
+
costPerClean: "lower",
|
|
31
|
+
cleanRate: "higher",
|
|
32
|
+
} as const satisfies Record<string, Direction>;
|
|
33
|
+
export type HeadlineKey = keyof typeof HEADLINE;
|
|
34
|
+
|
|
35
|
+
// Supporting metrics, each where lower is better, per PR where it is a count.
|
|
36
|
+
export interface Supporting {
|
|
37
|
+
reviewPerPr: number | null;
|
|
38
|
+
over200kShare: number | null;
|
|
39
|
+
orchestratorShare: number | null;
|
|
40
|
+
opusShare: number | null;
|
|
41
|
+
fixRoundsPerPr: number | null;
|
|
42
|
+
escalationsPerPr: number | null;
|
|
43
|
+
scopeChangesPerPr: number | null;
|
|
44
|
+
escapesPerPr: number | null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const SUPPORTING_LABELS: Record<keyof Supporting, string> = {
|
|
48
|
+
reviewPerPr: "review cost per PR",
|
|
49
|
+
over200kShare: "share of calls above 200k context",
|
|
50
|
+
orchestratorShare: "orchestrator share",
|
|
51
|
+
opusShare: "Opus share of the model mix",
|
|
52
|
+
fixRoundsPerPr: "fix rounds per PR",
|
|
53
|
+
escalationsPerPr: "escalations per PR",
|
|
54
|
+
scopeChangesPerPr: "scope changes per PR",
|
|
55
|
+
escapesPerPr: "escapes per PR",
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const per = (n: number | null, merged: number) =>
|
|
59
|
+
n !== null && merged > 0 ? n / merged : null;
|
|
60
|
+
|
|
61
|
+
export function supportingOf(s: Span): Supporting {
|
|
62
|
+
return {
|
|
63
|
+
reviewPerPr: s.reviewPerPr,
|
|
64
|
+
over200kShare: s.over200kShare,
|
|
65
|
+
orchestratorShare: s.orchestratorShare,
|
|
66
|
+
opusShare: s.opusShare,
|
|
67
|
+
fixRoundsPerPr: s.fixRoundsPerPr,
|
|
68
|
+
escalationsPerPr: per(s.escalations, s.merged),
|
|
69
|
+
scopeChangesPerPr: per(s.scopeChanges, s.merged),
|
|
70
|
+
escapesPerPr: per(s.escapes, s.merged),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface Baseline {
|
|
75
|
+
values: Record<HeadlineKey, number | null>;
|
|
76
|
+
supporting: Supporting;
|
|
77
|
+
setAt: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// The next baseline. None until `cfg.baselineDays` complete days exist with
|
|
81
|
+
// at least `cfg.minMerges` merges among them: a quiet week would lock in a
|
|
82
|
+
// baseline nothing can meet, so the seed waits for the first week that has
|
|
83
|
+
// enough work, and a ratchet step skips a week that doesn't. The first is
|
|
84
|
+
// those first days, and each night after it, the trailing week of days is
|
|
85
|
+
// ratcheted in. The supporting snapshot follows cost per merged PR:
|
|
86
|
+
// it is replaced only when that improves.
|
|
87
|
+
export function nextBaseline(
|
|
88
|
+
prev: Baseline | null,
|
|
89
|
+
complete: Rollup[],
|
|
90
|
+
cfg: UsageConfig,
|
|
91
|
+
now = Date.now()
|
|
92
|
+
): Baseline | null {
|
|
93
|
+
const days = [...complete].sort((a, b) => a.day.localeCompare(b.day));
|
|
94
|
+
if (days.length < cfg.baselineDays) return prev;
|
|
95
|
+
if (!prev) {
|
|
96
|
+
let s = spanOf(days.slice(0, cfg.baselineDays));
|
|
97
|
+
if (s.merged < cfg.minMerges) s = spanOf(days.slice(-cfg.baselineDays));
|
|
98
|
+
if (s.merged < cfg.minMerges) return null;
|
|
99
|
+
return {
|
|
100
|
+
values: {
|
|
101
|
+
costPerMerged: s.costPerMerged,
|
|
102
|
+
costPerClean: s.costPerClean,
|
|
103
|
+
cleanRate: s.cleanRate,
|
|
104
|
+
},
|
|
105
|
+
supporting: supportingOf(s),
|
|
106
|
+
setAt: now,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const s = spanOf(days.slice(-cfg.baselineDays));
|
|
110
|
+
if (s.merged < cfg.minMerges) return prev;
|
|
111
|
+
const values = { ...prev.values };
|
|
112
|
+
for (const k of Object.keys(HEADLINE) as HeadlineKey[])
|
|
113
|
+
values[k] = ratchet(prev.values[k], s[k], HEADLINE[k]);
|
|
114
|
+
const improved =
|
|
115
|
+
s.costPerMerged !== null &&
|
|
116
|
+
(prev.values.costPerMerged === null ||
|
|
117
|
+
s.costPerMerged < prev.values.costPerMerged);
|
|
118
|
+
return {
|
|
119
|
+
values,
|
|
120
|
+
supporting: improved ? supportingOf(s) : prev.supporting,
|
|
121
|
+
setAt: improved ? now : prev.setAt,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface Alert {
|
|
126
|
+
kind: "cost" | "quality";
|
|
127
|
+
message: string;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const round = (x: number) => Math.round(x * 1e6) / 1e6;
|
|
131
|
+
|
|
132
|
+
// The supporting metric that moved furthest the wrong way since the baseline.
|
|
133
|
+
export function biggestMover(
|
|
134
|
+
base: Supporting,
|
|
135
|
+
now: Supporting
|
|
136
|
+
): { label: string; change: string } | null {
|
|
137
|
+
let best: { key: keyof Supporting; score: number } | null = null;
|
|
138
|
+
for (const key of Object.keys(base) as (keyof Supporting)[]) {
|
|
139
|
+
const b = base[key];
|
|
140
|
+
const c = now[key];
|
|
141
|
+
if (b === null || c === null) continue;
|
|
142
|
+
const score = b > 0 ? (c - b) / b : c > 0 ? 1 : 0;
|
|
143
|
+
if (score > 0 && (!best || score > best.score)) best = { key, score };
|
|
144
|
+
}
|
|
145
|
+
if (!best) return null;
|
|
146
|
+
const b = base[best.key] as number;
|
|
147
|
+
return {
|
|
148
|
+
label: SUPPORTING_LABELS[best.key],
|
|
149
|
+
change: b > 0 ? `+${Math.round(best.score * 100)}%` : "up from none",
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Conditions that hold now, over the last `avgDays` complete days.
|
|
154
|
+
export function evaluateAlerts(
|
|
155
|
+
complete: Rollup[],
|
|
156
|
+
baseline: Baseline | null,
|
|
157
|
+
cfg: UsageConfig
|
|
158
|
+
): Alert[] {
|
|
159
|
+
if (!baseline) return [];
|
|
160
|
+
const days = [...complete]
|
|
161
|
+
.sort((a, b) => a.day.localeCompare(b.day))
|
|
162
|
+
.slice(-cfg.avgDays);
|
|
163
|
+
const s = spanOf(days);
|
|
164
|
+
if (days.length < cfg.avgDays || s.merged < cfg.minMerges) return [];
|
|
165
|
+
const mover = biggestMover(baseline.supporting, supportingOf(s));
|
|
166
|
+
const moved = mover ? ` Biggest mover: ${mover.label} ${mover.change}.` : "";
|
|
167
|
+
const out: Alert[] = [];
|
|
168
|
+
const baseCost = baseline.values.costPerMerged;
|
|
169
|
+
if (s.costPerMerged !== null && baseCost !== null && baseCost > 0) {
|
|
170
|
+
const over = round(((s.costPerMerged - baseCost) / baseCost) * 100);
|
|
171
|
+
if (over > cfg.costPct)
|
|
172
|
+
out.push({
|
|
173
|
+
kind: "cost",
|
|
174
|
+
message: `Usage: cost per merged PR, ${cfg.avgDays}-day average, is ${Math.round(over)}% over baseline.${moved}`,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
const baseClean = baseline.values.cleanRate;
|
|
178
|
+
if (s.cleanRate !== null && baseClean !== null) {
|
|
179
|
+
const under = round((baseClean - s.cleanRate) * 100);
|
|
180
|
+
if (under > cfg.cleanPoints)
|
|
181
|
+
out.push({
|
|
182
|
+
kind: "quality",
|
|
183
|
+
message: `Usage: clean-merge rate, ${cfg.avgDays}-day average, is ${Math.round(under)} points under baseline.${moved}`,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Which firing alerts are new (to send), and which open ones have cleared.
|
|
190
|
+
export function nextAlertState(
|
|
191
|
+
open: string[],
|
|
192
|
+
firing: Alert[]
|
|
193
|
+
): { notify: Alert[]; clear: string[] } {
|
|
194
|
+
const now = new Set(firing.map((a) => a.kind));
|
|
195
|
+
return {
|
|
196
|
+
notify: firing.filter((a) => !open.includes(a.kind)),
|
|
197
|
+
clear: open.filter((k) => !now.has(k as Alert["kind"])),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function loadBaseline(): Baseline | null {
|
|
202
|
+
const rows = db
|
|
203
|
+
.prepare(`SELECT metric, value, set_at, supporting FROM usage_baseline`)
|
|
204
|
+
.all() as {
|
|
205
|
+
metric: string;
|
|
206
|
+
value: number;
|
|
207
|
+
set_at: number;
|
|
208
|
+
supporting: string | null;
|
|
209
|
+
}[];
|
|
210
|
+
if (!rows.length) return null;
|
|
211
|
+
const values: Baseline["values"] = {
|
|
212
|
+
costPerMerged: null,
|
|
213
|
+
costPerClean: null,
|
|
214
|
+
cleanRate: null,
|
|
215
|
+
};
|
|
216
|
+
let supporting: Supporting | null = null;
|
|
217
|
+
let setAt = 0;
|
|
218
|
+
for (const r of rows) {
|
|
219
|
+
if (r.metric in values) values[r.metric as HeadlineKey] = r.value;
|
|
220
|
+
if (r.metric === "costPerMerged" && r.supporting)
|
|
221
|
+
supporting = JSON.parse(r.supporting) as Supporting;
|
|
222
|
+
setAt = Math.max(setAt, r.set_at);
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
values,
|
|
226
|
+
supporting: supporting ?? supportingOf(spanOf([])),
|
|
227
|
+
setAt,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function saveBaseline(b: Baseline): void {
|
|
232
|
+
db.transaction(() => {
|
|
233
|
+
for (const k of Object.keys(HEADLINE) as HeadlineKey[]) {
|
|
234
|
+
const v = b.values[k];
|
|
235
|
+
if (v === null) continue;
|
|
236
|
+
db.prepare(
|
|
237
|
+
`INSERT INTO usage_baseline (metric, value, set_at, supporting) VALUES (?, ?, ?, ?)
|
|
238
|
+
ON CONFLICT(metric) DO UPDATE SET value = excluded.value, set_at = excluded.set_at,
|
|
239
|
+
supporting = excluded.supporting`
|
|
240
|
+
).run(
|
|
241
|
+
k,
|
|
242
|
+
v,
|
|
243
|
+
b.setAt,
|
|
244
|
+
k === "costPerMerged" ? JSON.stringify(b.supporting) : null
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
})();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function openAlerts(): string[] {
|
|
251
|
+
return (
|
|
252
|
+
db
|
|
253
|
+
.prepare(`SELECT kind FROM usage_alerts WHERE cleared_at IS NULL`)
|
|
254
|
+
.all() as { kind: string }[]
|
|
255
|
+
).map((r) => r.kind);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function applyAlertState(
|
|
259
|
+
state: { notify: Alert[]; clear: string[] },
|
|
260
|
+
now = Date.now()
|
|
261
|
+
): void {
|
|
262
|
+
db.transaction(() => {
|
|
263
|
+
for (const a of state.notify)
|
|
264
|
+
db.prepare(
|
|
265
|
+
`INSERT INTO usage_alerts (kind, opened_at, cleared_at, message) VALUES (?, ?, NULL, ?)
|
|
266
|
+
ON CONFLICT(kind) DO UPDATE SET opened_at = excluded.opened_at, cleared_at = NULL,
|
|
267
|
+
message = excluded.message`
|
|
268
|
+
).run(a.kind, now, a.message);
|
|
269
|
+
for (const k of state.clear)
|
|
270
|
+
db.prepare(`UPDATE usage_alerts SET cleared_at = ? WHERE kind = ?`).run(
|
|
271
|
+
now,
|
|
272
|
+
k
|
|
273
|
+
);
|
|
274
|
+
})();
|
|
275
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The account's limit windows as Claude Code reports them: its
|
|
3
|
+
* rate_limit_event carries what the anthropic-ratelimit-unified-* headers
|
|
4
|
+
* said on a response, for the window that matters most at that moment. Each
|
|
5
|
+
* reading is kept with its time; between readings the weekly figure is an
|
|
6
|
+
* estimate from recorded usage, and labelled as one.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { db } from "../db";
|
|
10
|
+
import { weighted, type TokenRow } from "./weights";
|
|
11
|
+
import { readUsageConfig } from "./config";
|
|
12
|
+
|
|
13
|
+
export type LimitWindow = "five_hour" | "seven_day";
|
|
14
|
+
|
|
15
|
+
export interface Reading {
|
|
16
|
+
window: LimitWindow;
|
|
17
|
+
// Percent used, 0-100.
|
|
18
|
+
utilization: number;
|
|
19
|
+
raw: number;
|
|
20
|
+
// Epoch ms.
|
|
21
|
+
resetsAt: number | null;
|
|
22
|
+
status: string | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const WINDOWS: LimitWindow[] = ["five_hour", "seven_day"];
|
|
26
|
+
const num = (v: unknown) =>
|
|
27
|
+
typeof v === "number" && Number.isFinite(v) && v >= 0 ? v : null;
|
|
28
|
+
|
|
29
|
+
// A rate_limit_event carries what the headers said: utilization as a
|
|
30
|
+
// fraction (0.42) and the reset in epoch seconds. A plan's per-window object
|
|
31
|
+
// carries a percent (42) and an ISO time. Each source has one unit.
|
|
32
|
+
const eventPercent = (fraction: number) => fraction * 100;
|
|
33
|
+
|
|
34
|
+
// Readings in what the CLI emits: a rate_limit_event's info, or a plan's
|
|
35
|
+
// per-window object. Anything else gives none.
|
|
36
|
+
export function parseRateLimit(info: unknown): Reading[] {
|
|
37
|
+
if (!info || typeof info !== "object") return [];
|
|
38
|
+
const i = info as Record<string, unknown>;
|
|
39
|
+
const out: Reading[] = [];
|
|
40
|
+
const status = typeof i.status === "string" ? i.status : null;
|
|
41
|
+
const type = i.rateLimitType;
|
|
42
|
+
const raw = num(i.utilization);
|
|
43
|
+
if ((type === "five_hour" || type === "seven_day") && raw !== null) {
|
|
44
|
+
const reset = num(i.resetsAt);
|
|
45
|
+
out.push({
|
|
46
|
+
window: type,
|
|
47
|
+
utilization: eventPercent(raw),
|
|
48
|
+
raw,
|
|
49
|
+
resetsAt: reset === null ? null : reset * 1000,
|
|
50
|
+
status,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
for (const w of WINDOWS) {
|
|
54
|
+
const o = i[w] as Record<string, unknown> | null | undefined;
|
|
55
|
+
const u = o && num(o.utilization);
|
|
56
|
+
if (!o || u === null || u === undefined) continue;
|
|
57
|
+
const reset =
|
|
58
|
+
typeof o.resets_at === "string" ? Date.parse(o.resets_at) : NaN;
|
|
59
|
+
out.push({
|
|
60
|
+
window: w,
|
|
61
|
+
utilization: u,
|
|
62
|
+
raw: u,
|
|
63
|
+
resetsAt: Number.isFinite(reset) ? reset : null,
|
|
64
|
+
status,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Stores a reading only when it differs from that window's last, so a busy
|
|
71
|
+
// day adds a handful of rows, not one per response.
|
|
72
|
+
export function recordReadings(
|
|
73
|
+
readings: Reading[],
|
|
74
|
+
source: string,
|
|
75
|
+
sessionId: string | null = null,
|
|
76
|
+
at = Date.now()
|
|
77
|
+
): number {
|
|
78
|
+
let added = 0;
|
|
79
|
+
for (const r of readings) {
|
|
80
|
+
const last = db
|
|
81
|
+
.prepare(
|
|
82
|
+
`SELECT utilization, resets_at, status FROM rate_limit_readings
|
|
83
|
+
WHERE window = ? ORDER BY id DESC LIMIT 1`
|
|
84
|
+
)
|
|
85
|
+
.get(r.window) as
|
|
86
|
+
| { utilization: number; resets_at: number | null; status: string | null }
|
|
87
|
+
| undefined;
|
|
88
|
+
if (
|
|
89
|
+
last &&
|
|
90
|
+
last.utilization === r.utilization &&
|
|
91
|
+
last.resets_at === r.resetsAt &&
|
|
92
|
+
last.status === r.status
|
|
93
|
+
)
|
|
94
|
+
continue;
|
|
95
|
+
db.prepare(
|
|
96
|
+
`INSERT INTO rate_limit_readings (at, window, utilization, raw, resets_at, status, session_id, source)
|
|
97
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
|
|
98
|
+
).run(
|
|
99
|
+
at,
|
|
100
|
+
r.window,
|
|
101
|
+
r.utilization,
|
|
102
|
+
r.raw,
|
|
103
|
+
r.resetsAt,
|
|
104
|
+
r.status,
|
|
105
|
+
sessionId,
|
|
106
|
+
source
|
|
107
|
+
);
|
|
108
|
+
added++;
|
|
109
|
+
}
|
|
110
|
+
return added;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface WindowReading {
|
|
114
|
+
pct: number;
|
|
115
|
+
resetsAt: number | null;
|
|
116
|
+
// When it was read (or, for an estimate, worked out).
|
|
117
|
+
at: number;
|
|
118
|
+
estimated: boolean;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function lastReading(
|
|
122
|
+
window: LimitWindow,
|
|
123
|
+
before = Number.MAX_SAFE_INTEGER
|
|
124
|
+
): { at: number; utilization: number; resetsAt: number | null } | null {
|
|
125
|
+
const r = db
|
|
126
|
+
.prepare(
|
|
127
|
+
`SELECT at, utilization, resets_at FROM rate_limit_readings
|
|
128
|
+
WHERE window = ? AND at <= ? ORDER BY at DESC, id DESC LIMIT 1`
|
|
129
|
+
)
|
|
130
|
+
.get(window, before) as
|
|
131
|
+
| { at: number; utilization: number; resets_at: number | null }
|
|
132
|
+
| undefined;
|
|
133
|
+
return r
|
|
134
|
+
? { at: r.at, utilization: r.utilization, resetsAt: r.resets_at }
|
|
135
|
+
: null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const WEEK = 7 * 24 * 3600 * 1000;
|
|
139
|
+
|
|
140
|
+
export function weightedBetween(from: number, to: number): number {
|
|
141
|
+
const rows = db
|
|
142
|
+
.prepare(
|
|
143
|
+
`SELECT model, provider, input_tokens, cache_read_tokens, cache_write_5m_tokens,
|
|
144
|
+
cache_write_1h_tokens, output_tokens FROM usage_calls WHERE at > ? AND at <= ?`
|
|
145
|
+
)
|
|
146
|
+
.all(from, to) as TokenRow[];
|
|
147
|
+
let t = 0;
|
|
148
|
+
for (const r of rows) t += weighted(r);
|
|
149
|
+
return t;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// The weekly percent now, worked out from the last real reading: the usage
|
|
153
|
+
// recorded in the week up to that reading stands for its percent, so the
|
|
154
|
+
// week up to now stands for this one. Null before any reading.
|
|
155
|
+
export function estimateWeekly(now: number): WindowReading | null {
|
|
156
|
+
const last = lastReading("seven_day", now);
|
|
157
|
+
if (!last || last.utilization <= 0) return null;
|
|
158
|
+
const then = weightedBetween(last.at - WEEK, last.at);
|
|
159
|
+
if (then <= 0) return null;
|
|
160
|
+
const pct = (weightedBetween(now - WEEK, now) / then) * last.utilization;
|
|
161
|
+
return {
|
|
162
|
+
pct: Math.round(pct * 10) / 10,
|
|
163
|
+
resetsAt: last.resetsAt,
|
|
164
|
+
at: now,
|
|
165
|
+
estimated: true,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// The window from a reading when it's fresh enough, else (weekly only) the
|
|
170
|
+
// estimate, labelled.
|
|
171
|
+
export function currentWindow(
|
|
172
|
+
window: LimitWindow,
|
|
173
|
+
now = Date.now()
|
|
174
|
+
): WindowReading | null {
|
|
175
|
+
const last = lastReading(window, now);
|
|
176
|
+
const maxAge =
|
|
177
|
+
window === "five_hour"
|
|
178
|
+
? 30 * 60_000
|
|
179
|
+
: readUsageConfig().weeklyStaleHours * 3_600_000;
|
|
180
|
+
// A reading from before its window reset describes the old window.
|
|
181
|
+
const live =
|
|
182
|
+
last && now - last.at <= maxAge && (!last.resetsAt || last.resetsAt > now);
|
|
183
|
+
if (last && live)
|
|
184
|
+
return {
|
|
185
|
+
pct: last.utilization,
|
|
186
|
+
resetsAt: last.resetsAt,
|
|
187
|
+
at: last.at,
|
|
188
|
+
estimated: false,
|
|
189
|
+
};
|
|
190
|
+
if (window === "seven_day") return estimateWeekly(now);
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One day of usage and quality, from the ledger and the quality readers.
|
|
3
|
+
* Rows keep their sums as well as their ratios, so any span of days
|
|
4
|
+
* aggregates as a ratio of sums: three days' cost per PR is their total cost
|
|
5
|
+
* over their total PRs, never a mean of three ratios.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { db } from "../db";
|
|
9
|
+
import { currentWindow } from "./rate-limits";
|
|
10
|
+
import {
|
|
11
|
+
escalationsIn,
|
|
12
|
+
escapesIn,
|
|
13
|
+
mergedTasks,
|
|
14
|
+
qualityOf,
|
|
15
|
+
scopeChangesIn,
|
|
16
|
+
type Gh,
|
|
17
|
+
} from "./quality";
|
|
18
|
+
|
|
19
|
+
export * from "./span";
|
|
20
|
+
import { computeRollup, type CallGroup, type Rollup } from "./span";
|
|
21
|
+
|
|
22
|
+
export const dayKey = (ms: number) => {
|
|
23
|
+
const d = new Date(ms);
|
|
24
|
+
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
|
25
|
+
};
|
|
26
|
+
export const dayStart = (day: string) => {
|
|
27
|
+
const [y, m, d] = day.split("-").map(Number);
|
|
28
|
+
return new Date(y, m - 1, d).getTime();
|
|
29
|
+
};
|
|
30
|
+
export const dayEnd = (day: string) => {
|
|
31
|
+
const [y, m, d] = day.split("-").map(Number);
|
|
32
|
+
return new Date(y, m - 1, d + 1).getTime();
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function groupsIn(from: number, to: number): CallGroup[] {
|
|
36
|
+
return db
|
|
37
|
+
.prepare(
|
|
38
|
+
`SELECT kind, role, model, provider, COUNT(*) AS calls,
|
|
39
|
+
SUM(input_tokens) AS input_tokens, SUM(cache_read_tokens) AS cache_read_tokens,
|
|
40
|
+
SUM(cache_write_5m_tokens) AS cache_write_5m_tokens,
|
|
41
|
+
SUM(cache_write_1h_tokens) AS cache_write_1h_tokens,
|
|
42
|
+
SUM(output_tokens) AS output_tokens,
|
|
43
|
+
SUM(ctx_tokens IS NOT NULL) AS ctxKnown, SUM(ctx_tokens > 200000) AS over200k
|
|
44
|
+
FROM usage_calls WHERE at >= ? AND at < ?
|
|
45
|
+
GROUP BY kind, role, model, provider`
|
|
46
|
+
)
|
|
47
|
+
.all(from, to) as CallGroup[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function rollupDay(
|
|
51
|
+
day: string,
|
|
52
|
+
now = Date.now(),
|
|
53
|
+
gh?: Gh
|
|
54
|
+
): Promise<Rollup> {
|
|
55
|
+
const from = dayStart(day);
|
|
56
|
+
const to = dayEnd(day);
|
|
57
|
+
const tasks = mergedTasks(from, to);
|
|
58
|
+
const q = qualityOf(tasks);
|
|
59
|
+
const weekly = currentWindow("seven_day", Math.min(now, to));
|
|
60
|
+
return computeRollup({
|
|
61
|
+
day,
|
|
62
|
+
groups: groupsIn(from, to),
|
|
63
|
+
merged: tasks.length,
|
|
64
|
+
clean: q.filter((t) => t.clean).length,
|
|
65
|
+
fixRounds: q.reduce((t, x) => t + x.fixRounds, 0),
|
|
66
|
+
escalations: escalationsIn(from, to),
|
|
67
|
+
scopeChanges: scopeChangesIn(from, to),
|
|
68
|
+
escapes: await escapesIn(tasks, gh),
|
|
69
|
+
weeklyPct: weekly?.pct ?? null,
|
|
70
|
+
weeklyEstimated: weekly?.estimated ?? false,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function saveRollup(r: Rollup, final: boolean, now = Date.now()): void {
|
|
75
|
+
db.prepare(
|
|
76
|
+
`INSERT INTO usage_rollups (day, data, final, computed_at) VALUES (?, ?, ?, ?)
|
|
77
|
+
ON CONFLICT(day) DO UPDATE SET data = excluded.data, final = excluded.final,
|
|
78
|
+
computed_at = excluded.computed_at`
|
|
79
|
+
).run(r.day, JSON.stringify(r), final ? 1 : 0, now);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function loadRollups(limit = 60): Rollup[] {
|
|
83
|
+
return (
|
|
84
|
+
db
|
|
85
|
+
.prepare(`SELECT data FROM usage_rollups ORDER BY day DESC LIMIT ?`)
|
|
86
|
+
.all(limit) as { data: string }[]
|
|
87
|
+
)
|
|
88
|
+
.map((r) => JSON.parse(r.data) as Rollup)
|
|
89
|
+
.reverse();
|
|
90
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pure half of the daily rollup: one day from its inputs, and any span
|
|
3
|
+
* of days as ratios of sums. No database, so the Usage view can use it too.
|
|
4
|
+
* A day keeps its sums as well as its ratios, so three days' cost per PR is
|
|
5
|
+
* their total cost over their total PRs, never a mean of three ratios.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { weighted, family, type TokenRow } from "./weights";
|
|
9
|
+
|
|
10
|
+
export interface CallGroup extends TokenRow {
|
|
11
|
+
kind: string;
|
|
12
|
+
role: string | null;
|
|
13
|
+
calls: number;
|
|
14
|
+
// Calls with a known context size, and those of them above 200k.
|
|
15
|
+
ctxKnown: number;
|
|
16
|
+
over200k: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface Rollup {
|
|
20
|
+
day: string;
|
|
21
|
+
weighted: number;
|
|
22
|
+
// Everything but a person's own sessions.
|
|
23
|
+
work: number;
|
|
24
|
+
review: number;
|
|
25
|
+
orchestrator: number;
|
|
26
|
+
mix: { opus: number; sonnet: number; haiku: number };
|
|
27
|
+
calls: number;
|
|
28
|
+
ctxKnown: number;
|
|
29
|
+
over200k: number;
|
|
30
|
+
merged: number;
|
|
31
|
+
clean: number;
|
|
32
|
+
fixRounds: number;
|
|
33
|
+
escalations: number;
|
|
34
|
+
scopeChanges: number;
|
|
35
|
+
escapes: number | null;
|
|
36
|
+
weeklyPct: number | null;
|
|
37
|
+
weeklyEstimated: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface DayInputs {
|
|
41
|
+
day: string;
|
|
42
|
+
groups: CallGroup[];
|
|
43
|
+
merged: number;
|
|
44
|
+
clean: number;
|
|
45
|
+
fixRounds: number;
|
|
46
|
+
escalations: number;
|
|
47
|
+
scopeChanges: number;
|
|
48
|
+
escapes: number | null;
|
|
49
|
+
weeklyPct: number | null;
|
|
50
|
+
weeklyEstimated: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function computeRollup(i: DayInputs): Rollup {
|
|
54
|
+
const r: Rollup = {
|
|
55
|
+
day: i.day,
|
|
56
|
+
weighted: 0,
|
|
57
|
+
work: 0,
|
|
58
|
+
review: 0,
|
|
59
|
+
orchestrator: 0,
|
|
60
|
+
mix: { opus: 0, sonnet: 0, haiku: 0 },
|
|
61
|
+
calls: 0,
|
|
62
|
+
ctxKnown: 0,
|
|
63
|
+
over200k: 0,
|
|
64
|
+
merged: i.merged,
|
|
65
|
+
clean: i.clean,
|
|
66
|
+
fixRounds: i.fixRounds,
|
|
67
|
+
escalations: i.escalations,
|
|
68
|
+
scopeChanges: i.scopeChanges,
|
|
69
|
+
escapes: i.escapes,
|
|
70
|
+
weeklyPct: i.weeklyPct,
|
|
71
|
+
weeklyEstimated: i.weeklyEstimated,
|
|
72
|
+
};
|
|
73
|
+
for (const g of i.groups) {
|
|
74
|
+
const w = weighted(g);
|
|
75
|
+
if (w <= 0 && g.provider && g.provider !== "anthropic") continue;
|
|
76
|
+
r.weighted += w;
|
|
77
|
+
r.mix[family(g.model)] += w;
|
|
78
|
+
r.calls += g.calls;
|
|
79
|
+
r.ctxKnown += g.ctxKnown;
|
|
80
|
+
r.over200k += g.over200k;
|
|
81
|
+
if (g.role !== "interactive") r.work += w;
|
|
82
|
+
if (g.kind === "review" || g.kind === "gate") r.review += w;
|
|
83
|
+
if (g.role === "orchestrator") r.orchestrator += w;
|
|
84
|
+
}
|
|
85
|
+
return r;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// A span of days, as ratios of sums.
|
|
89
|
+
export interface Span {
|
|
90
|
+
days: number;
|
|
91
|
+
weighted: number;
|
|
92
|
+
work: number;
|
|
93
|
+
merged: number;
|
|
94
|
+
clean: number;
|
|
95
|
+
costPerMerged: number | null;
|
|
96
|
+
costPerClean: number | null;
|
|
97
|
+
cleanRate: number | null;
|
|
98
|
+
reviewPerPr: number | null;
|
|
99
|
+
over200kShare: number | null;
|
|
100
|
+
orchestratorShare: number | null;
|
|
101
|
+
opusShare: number | null;
|
|
102
|
+
fixRoundsPerPr: number | null;
|
|
103
|
+
escalations: number;
|
|
104
|
+
scopeChanges: number;
|
|
105
|
+
escapes: number | null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const ratio = (a: number, b: number) => (b > 0 ? a / b : null);
|
|
109
|
+
|
|
110
|
+
export function spanOf(rows: Rollup[]): Span {
|
|
111
|
+
const sum = (f: (r: Rollup) => number) => rows.reduce((t, r) => t + f(r), 0);
|
|
112
|
+
const work = sum((r) => r.work);
|
|
113
|
+
const merged = sum((r) => r.merged);
|
|
114
|
+
const clean = sum((r) => r.clean);
|
|
115
|
+
const weightedTotal = sum((r) => r.weighted);
|
|
116
|
+
const known = rows.filter((r) => r.escapes !== null);
|
|
117
|
+
return {
|
|
118
|
+
days: rows.length,
|
|
119
|
+
weighted: weightedTotal,
|
|
120
|
+
work,
|
|
121
|
+
merged,
|
|
122
|
+
clean,
|
|
123
|
+
costPerMerged: ratio(work, merged),
|
|
124
|
+
costPerClean: ratio(work, clean),
|
|
125
|
+
cleanRate: ratio(clean, merged),
|
|
126
|
+
reviewPerPr: ratio(
|
|
127
|
+
sum((r) => r.review),
|
|
128
|
+
merged
|
|
129
|
+
),
|
|
130
|
+
over200kShare: ratio(
|
|
131
|
+
sum((r) => r.over200k),
|
|
132
|
+
sum((r) => r.ctxKnown)
|
|
133
|
+
),
|
|
134
|
+
orchestratorShare: ratio(
|
|
135
|
+
sum((r) => r.orchestrator),
|
|
136
|
+
weightedTotal
|
|
137
|
+
),
|
|
138
|
+
opusShare: ratio(
|
|
139
|
+
sum((r) => r.mix.opus),
|
|
140
|
+
weightedTotal
|
|
141
|
+
),
|
|
142
|
+
fixRoundsPerPr: ratio(
|
|
143
|
+
sum((r) => r.fixRounds),
|
|
144
|
+
merged
|
|
145
|
+
),
|
|
146
|
+
escalations: sum((r) => r.escalations),
|
|
147
|
+
scopeChanges: sum((r) => r.scopeChanges),
|
|
148
|
+
escapes: known.length
|
|
149
|
+
? known.reduce((t, r) => t + (r.escapes ?? 0), 0)
|
|
150
|
+
: null,
|
|
151
|
+
};
|
|
152
|
+
}
|