mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-05 17:28:00 +01:00
* ops(fleet-env): retire a key everywhere in one command; GEMINI_API_KEY is no longer a fleet key; ai-kit 1.26 GEMINI_API_KEY sat in /opt/fleet/env as a free tier. The Google project had billing and every app read the key: about $567 on 10-11 Nov 2026. On 2026-11-13 it was still set there, with eight apps on an ai-kit that puts Google in the free chain. - fleet-env.sh where KEY: every env file on the box that sets KEY, by fingerprint. retire KEY: its line in /opt/fleet/env and in every app env becomes a dated comment (the value is kept), and the app units restart one at a time so none keeps the value it started with. - fleet-keys.conf drops GEMINI_API_KEY, so seed never brings it back. - fleet-env.yml offers where and retire with a key input. - @bitbaum/ai-kit ^1.26.0: Google out of the free chain, paid AI off unless an app opts in, daily caps. test-fleet-env.sh covers where, retire (value kept, other lines untouched, no value printed, seed does not restore) and the refusal of a non-credential. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq * test(ops): four .env cases that need an unreadable file are not run as root chmod 000 is how the section makes a .env unreadable, and root reads any file. In an agent's sandbox (uid 0) the four cases could only fail, on main too, and stopped every push that touched scripts/hetzner. They print as not run; CI and a developer's machine run them as an ordinary user. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq * ops(launch): every app on the box writes to one AI usage log; ai-kit 1.27.0 launch.sh.tmpl exports AI_USAGE_DIR=/opt/fleet/ai-usage where that directory exists and AI_APP_ID from the app's directory name, unless the app's own .env sets them. @bitbaum/ai-kit 1.27.0 then writes one line per model call there (app, vendor, model, outcome; never content). fleet-env.yml creates the directory. Kivvi canaries 1.27.0, so the rollout gate no longer passes 1.26.0. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq * feat(settings): every app on this server and every AI vendor, today The owner, 2026-11-13: 'I never know what is used up, what isn't, what it was used up for, and when. Is it just one app, or is it all the apps?' About $567 had gone to Google and nobody could say which app spent it. Settings > AI gains the operator's card 'Every app on this server'. It reads the one usage log every app writes (@bitbaum/ai-kit usage-log, AI_USAGE_DIR) and shows, per vendor: free or billing, whether its key is on this server, what today looked like, the last trouble in words, what that means for the next request, and which apps used it; then the same by app. A log that could not be read is never drawn as nothing used; a vendor nobody called is 'not called today', not full and not empty; a person's own key is counted apart. - lib/ai/fleet-usage.ts (pure view), /api/ai/fleet (operator only, key NAMES only), components/settings/AiFleetSettings.tsx - scripts/test/fleet-usage.ts: 12 checks Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq * docs(env): AI_USAGE_DIR in .env.example Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq * fix(fleet-env): retire edits the file a symlinked .env points at, never the link /opt/<app>/app/.env is a symlink to shared/.env. An in-place sed on the link replaces it with a regular copy, which later changes to shared/.env never reach. where and retire resolve each file first, so the 13 paths the box listed for GEMINI_API_KEY are the 8 files they really are. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq * test(npm-publish): tamper with the first character of the sealed body, not the last The last base64url character of a body carries bits that belong to no byte, so replacing only the tail could decode to the same bytes, and openSecret rightly accepted it: 'tampering is refused' failed on an unlucky random IV (CI on #1705, 2026-11-13). The first character always changes a byte. 40 of 40 runs pass. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq --------- Co-authored-by: Claude <noreply@anthropic.com>
197 lines
6.5 KiB
TypeScript
197 lines
6.5 KiB
TypeScript
/**
|
|
* Every app on the server, every AI vendor, one picture (lib/ai/fleet-usage).
|
|
*
|
|
* The cases that matter are the ones a dashboard gets wrong: a log that could
|
|
* not be read drawn as "nothing used", a vendor nobody called drawn as full or
|
|
* empty, a paid vendor sitting among free ones, and a refusal shown as a code.
|
|
*
|
|
* Run: npx tsx scripts/test/fleet-usage.ts
|
|
*/
|
|
import assert from "node:assert/strict";
|
|
import type { UsageEvent } from "@bitbaum/ai-kit";
|
|
import { appLabel, fleetUsageView, problemWords } from "@/lib/ai/fleet-usage";
|
|
|
|
let passed = 0;
|
|
const check = (label: string, fn: () => void) => {
|
|
fn();
|
|
passed++;
|
|
console.log(` ✓ ${label}`);
|
|
};
|
|
|
|
const now = Date.UTC(2026, 10, 13, 12, 0, 0);
|
|
const e = (
|
|
minAgo: number,
|
|
app: string,
|
|
provider: string,
|
|
outcome: UsageEvent["outcome"],
|
|
more: Partial<UsageEvent> = {},
|
|
): UsageEvent => ({
|
|
t: now - minAgo * 60_000,
|
|
app,
|
|
provider,
|
|
model: `${provider}-m`,
|
|
outcome,
|
|
paid: provider === "google" || provider === "xai",
|
|
byok: false,
|
|
...more,
|
|
});
|
|
const keys = new Set(["GROQ_API_KEY", "OPENROUTER_API_KEY"]);
|
|
|
|
check("a log that could not be read is not 'nothing used'", () => {
|
|
const v = fleetUsageView({ events: null, keysSet: keys, now });
|
|
assert.equal(v.readable, false);
|
|
assert.match(v.headline, /could not be read/);
|
|
assert.equal(v.vendors.find((x) => x.id === "groq")?.line, "Not readable");
|
|
});
|
|
|
|
check("a vendor with a key and no calls is 'not called today', neither full nor empty", () => {
|
|
const v = fleetUsageView({ events: [], keysSet: keys, now });
|
|
const groq = v.vendors.find((x) => x.id === "groq");
|
|
assert.equal(groq?.state, "not-called");
|
|
assert.match(groq?.consequence ?? "", /no reading/);
|
|
assert.match(v.headline, /No app has made an AI call/);
|
|
});
|
|
|
|
check("a vendor with no key and no calls is left out; Google is not listed once retired", () => {
|
|
const v = fleetUsageView({ events: [], keysSet: keys, now });
|
|
assert.equal(
|
|
v.vendors.some((x) => x.id === "google"),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
v.vendors.some((x) => x.id === "xai"),
|
|
false,
|
|
);
|
|
});
|
|
|
|
check("who used a vendor is named per app, busiest first", () => {
|
|
const v = fleetUsageView({
|
|
events: [
|
|
e(30, "ORANGECAT", "groq", "served"),
|
|
e(20, "ORANGECAT", "groq", "served"),
|
|
e(10, "LOKI", "groq", "served"),
|
|
],
|
|
keysSet: keys,
|
|
now,
|
|
});
|
|
const groq = v.vendors.find((x) => x.id === "groq");
|
|
assert.deepEqual(
|
|
groq?.apps.map((a) => a.app),
|
|
["Orangecat", "Loki"],
|
|
);
|
|
assert.equal(groq?.state, "answering");
|
|
assert.match(
|
|
v.headline,
|
|
/2 apps made 3 answered calls today\. None went to a vendor that bills\./,
|
|
);
|
|
});
|
|
|
|
check(
|
|
"a refusal after the last answer reads as not answering, in words, with what happens next",
|
|
() => {
|
|
const v = fleetUsageView({
|
|
events: [
|
|
e(50, "HEIDI", "groq", "served"),
|
|
e(5, "HEIDI", "groq", "failed", { status: 429, kind: "daily" }),
|
|
],
|
|
keysSet: keys,
|
|
now,
|
|
});
|
|
const groq = v.vendors.find((x) => x.id === "groq");
|
|
assert.equal(groq?.state, "refusing");
|
|
assert.match(groq?.problem ?? "", /today's free allowance is used up \(5 min ago\)/);
|
|
assert.match(groq?.consequence ?? "", /move on to the next vendor/);
|
|
assert.ok(!/429|daily/.test(`${groq?.problem} ${groq?.stateLabel}`), "no code, no flag");
|
|
assert.match(v.headline, /Groq is not answering right now/);
|
|
},
|
|
);
|
|
|
|
check("an answer after a refusal reads as answering again", () => {
|
|
const v = fleetUsageView({
|
|
events: [e(50, "A", "groq", "failed", { status: 429 }), e(5, "A", "groq", "served")],
|
|
keysSet: keys,
|
|
now,
|
|
});
|
|
assert.equal(v.vendors.find((x) => x.id === "groq")?.state, "answering");
|
|
});
|
|
|
|
check(
|
|
"a paid vendor that answered is said to bill, with the count, and reaches the headline",
|
|
() => {
|
|
const v = fleetUsageView({
|
|
events: [
|
|
e(9, "HEIDI", "google", "served"),
|
|
e(8, "HEIDI", "google", "served"),
|
|
e(7, "LOKI", "groq", "served"),
|
|
],
|
|
keysSet: new Set([...keys, "GEMINI_API_KEY"]),
|
|
now,
|
|
});
|
|
const google = v.vendors.find((x) => x.id === "google");
|
|
assert.equal(google?.paid, true);
|
|
assert.match(google?.consequence ?? "", /bills per call\. 2 calls were answered today/);
|
|
assert.match(v.headline, /2 calls went to a vendor that bills\./);
|
|
assert.deepEqual(
|
|
google?.apps.map((a) => a.app),
|
|
["Heidi"],
|
|
);
|
|
},
|
|
);
|
|
|
|
check("a paid vendor stopped by the spend rules says nothing was spent", () => {
|
|
const v = fleetUsageView({
|
|
events: [e(3, "SOLON", "google", "refused", { reason: "paid-not-opted-in" })],
|
|
keysSet: new Set([...keys, "GEMINI_API_KEY"]),
|
|
now,
|
|
});
|
|
const google = v.vendors.find((x) => x.id === "google");
|
|
assert.match(google?.problem ?? "", /paid AI is switched off for that app/);
|
|
assert.match(google?.consequence ?? "", /Nothing is being spent here/);
|
|
});
|
|
|
|
check("calls with no key left on the server say the next one will not be made", () => {
|
|
const v = fleetUsageView({ events: [e(3, "HEIDI", "google", "served")], keysSet: keys, now });
|
|
const google = v.vendors.find((x) => x.id === "google");
|
|
assert.equal(google?.keySet, false);
|
|
assert.match(google?.consequence ?? "", /no longer holds its key/);
|
|
});
|
|
|
|
check("a person's own key is counted apart, never against the server's vendors", () => {
|
|
const v = fleetUsageView({
|
|
events: [{ ...e(3, "LOKI", "xai", "served"), byok: true }],
|
|
keysSet: keys,
|
|
now,
|
|
});
|
|
assert.match(v.ownKeyLine ?? "", /1 call today used a person's own key/);
|
|
assert.equal(
|
|
v.vendors.some((x) => x.id === "xai"),
|
|
false,
|
|
);
|
|
assert.equal(v.apps.length, 0);
|
|
});
|
|
|
|
check("per app: which vendors answered it", () => {
|
|
const v = fleetUsageView({
|
|
events: [
|
|
e(3, "ORANGECAT", "groq", "served"),
|
|
e(2, "ORANGECAT", "openrouter", "failed", { status: 503 }),
|
|
],
|
|
keysSet: keys,
|
|
now,
|
|
});
|
|
assert.deepEqual(v.apps[0].vendors.map((p) => p.label).sort(), ["Groq", "OpenRouter"]);
|
|
assert.match(v.apps[0].line, /1 answered · 1 failed/);
|
|
});
|
|
|
|
check("words for every way a link does not answer; an app id reads as a name", () => {
|
|
assert.match(problemWords({ at: 0, outcome: "failed", status: 401 }), /rejected the key/);
|
|
assert.match(problemWords({ at: 0, outcome: "failed", status: 502 }), /error on its side/);
|
|
assert.match(
|
|
problemWords({ at: 0, outcome: "refused", reason: "fleet-cap" }),
|
|
/server's daily paid allowance/,
|
|
);
|
|
assert.equal(appLabel("SBB_FUNDBUERO"), "Sbb fundbuero");
|
|
});
|
|
|
|
console.log(`fleet-usage: ${passed} checks passed`);
|