mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 05:17:01 +01:00
* docs(handoff): #1678 merged; the box still drops requests and Loki's deploys do not land Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi * docs(handoff): memory ceiling set at 10 GiB; Gemini spending cap in OrangeCat #1672 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi * fix(builder): five agent sessions at once, not eight; idle sealed sessions can be stopped to free memory Measured on the box on 2026-11-12: a sealed session holds about 1 GiB, not the 0.4 GB the cap of eight was sized at. With seven or eight open the builder held 6.3 to 8.3 GiB, the box had 0.6 GiB left, swap was full, and the kernel had stopped 37 processes for memory, live sites and the gateway among them. - AGENT_MAX_OPEN_DEFAULT is 5. LOKI_AGENT_MAX_OPEN raises it where there is room. - sealed-builds-ctl.sh stop-idle (sealed-builds.yml): stops the sealed sessions that relayed less than 512 bytes over two minutes, the line the idle rule already uses for "no work". A working agent prints far more (its spinner alone). runs=dry lists without stopping. idle_sessions is pinned by scripts/hetzner/test-sealed-builds-memory.sh. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi * fix(spend): the platform's Gemini key is not used where nothing caps it The audit after the Gemini spend (2026-11-12) found the same platform key in three places with no cap that refuses a call: - Loki's own chat chain called Google whenever GEMINI_API_KEY was in its env. Google is left out of CHAT_CHAIN (UNCAPPED_PAID_PROVIDERS) until Loki has a cap that refuses, like OrangeCat's reserve_vendor_call. - install-builder-agents.sh copied OrangeCat's key into the builder's env for the Antigravity CLI. An agent CLI cannot be capped in code; the builder now uses Gemini only with a key of its own, from a Google project with a per-day request quota. - openclaw-media.yml fell back to OrangeCat's key for the Telegram gateway's image and video reading. No fallback: with no key of its own, media understanding stays off. Keys already copied on the box stop mattering when the platform key is replaced; the new key is never copied anywhere. scripts/test/chat-chain.ts pins all three. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi * fix(builder): stop-idle stops only sessions whose project has no open run Bytes are not the signal: on 2026-11-12 eight sealed sessions at their prompt each relayed 3 to 10 KB a minute through podman, so a 512-byte line would have stopped none, and an agent waiting on CI prints nothing at all. A session whose project (or lane) has no open run in Loki's database is between tasks; one that does is kept, and so is any session under fifteen minutes old. between_tasks is pinned by test-sealed-builds-memory.sh. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi * fix(builder): between_tasks reads its list without a pipe into grep -q scripts/test/no-grep-q-in-a-pipe.ts: grep -q exits at the first match and can cut off the writer. A here-string has no writer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi * docs(handoff): the box recovered; spend caps; the owner's key steps Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi --------- Co-authored-by: Claude <noreply@anthropic.com>
198 lines
7.6 KiB
TypeScript
198 lines
7.6 KiB
TypeScript
/**
|
|
* Inline tests for the chat model chain (config/chat-models.ts).
|
|
*
|
|
* The chain is the difference between "one vendor's free tier runs dry and Loki
|
|
* is down" and "Loki keeps answering on someone else's". Its whole value is in
|
|
* the ORDER and the FILTERING, both of which are silent when wrong: a chain that
|
|
* drops every link still returns a plausible-looking empty array, and a chain
|
|
* that ignores a key still "works" right up until the vendor it forgot was the
|
|
* only one configured.
|
|
*
|
|
* Run: npm run test:chat-chain
|
|
*/
|
|
import { readFileSync } from "node:fs";
|
|
import {
|
|
CHAT_CHAIN,
|
|
usableChatChain,
|
|
chainFrom,
|
|
providerModels,
|
|
type ChatProvider,
|
|
} from "@/config/chat-models";
|
|
|
|
function assert(condition: boolean, message: string): void {
|
|
if (!condition) throw new Error(message);
|
|
}
|
|
|
|
let passed = 0;
|
|
const check = (label: string, fn: () => void) => {
|
|
fn();
|
|
passed += 1;
|
|
console.log(` ✓ ${label}`);
|
|
};
|
|
|
|
/** Run `fn` with exactly these env vars set, restoring whatever was there. */
|
|
function withEnv(vars: Record<string, string | undefined>, fn: () => void): void {
|
|
const before: Record<string, string | undefined> = {};
|
|
for (const [k, v] of Object.entries(vars)) {
|
|
before[k] = process.env[k];
|
|
if (v === undefined) delete process.env[k];
|
|
else process.env[k] = v;
|
|
}
|
|
try {
|
|
fn();
|
|
} finally {
|
|
for (const [k, v] of Object.entries(before)) {
|
|
if (v === undefined) delete process.env[k];
|
|
else process.env[k] = v;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Every key and model override the chain reads, from the chain itself: a list
|
|
// written by hand missed GEMINI_API_KEY once Gemini joined the chain, so a
|
|
// machine that held one failed "unkeyed provider leaked into the chain".
|
|
const unset = (names: (string | undefined)[]): Record<string, undefined> =>
|
|
Object.fromEntries(names.filter((k): k is string => !!k).map((k) => [k, undefined]));
|
|
const KEYS = unset(CHAT_CHAIN.map((p) => p.keyEnv));
|
|
const NO_OVERRIDES = unset(CHAT_CHAIN.map((p) => p.modelsEnv));
|
|
const groq = CHAT_CHAIN.find((p) => p.id === "groq") as ChatProvider;
|
|
const openrouter = CHAT_CHAIN.find((p) => p.id === "openrouter") as ChatProvider;
|
|
|
|
check("the shipped chain spans MORE THAN ONE vendor", () => {
|
|
// A "chain" within a single vendor is the bug this file exists to fix: every
|
|
// link would share that vendor's daily budget, so all of them die together.
|
|
const vendors = new Set(CHAT_CHAIN.map((p) => p.id));
|
|
assert(
|
|
vendors.size >= 2,
|
|
`only ${vendors.size} vendor(s) configured: ${[...vendors].join(", ")}`,
|
|
);
|
|
});
|
|
|
|
check("every provider declares a key env and at least one model", () => {
|
|
for (const p of CHAT_CHAIN) {
|
|
assert(Boolean(p.keyEnv), `${p.id} has no keyEnv`);
|
|
assert(p.models.length > 0, `${p.id} ships no models`);
|
|
assert(p.baseUrl.startsWith("https://"), `${p.id} baseUrl is not https: ${p.baseUrl}`);
|
|
}
|
|
});
|
|
|
|
check("a provider with no key is skipped silently, not thrown on", () => {
|
|
withEnv({ ...KEYS, ...NO_OVERRIDES, GROQ_API_KEY: "x" }, () => {
|
|
const chain = usableChatChain();
|
|
assert(chain.length > 0, "keyed provider produced no links");
|
|
assert(
|
|
chain.every((l) => l.provider.id === "groq"),
|
|
"unkeyed provider leaked into the chain",
|
|
);
|
|
});
|
|
});
|
|
|
|
check("no keys at all yields an empty chain rather than a broken link", () => {
|
|
withEnv({ ...KEYS, ...NO_OVERRIDES }, () => {
|
|
assert(usableChatChain().length === 0, "produced links with no keys configured");
|
|
});
|
|
});
|
|
|
|
check("links are provider-ordered, and each carries its own provider", () => {
|
|
withEnv({ ...KEYS, ...NO_OVERRIDES, GROQ_API_KEY: "x", OPENROUTER_API_KEY: "y" }, () => {
|
|
const chain = usableChatChain();
|
|
const firstOpenRouter = chain.findIndex((l) => l.provider.id === "openrouter");
|
|
const lastGroq = chain.map((l) => l.provider.id).lastIndexOf("groq");
|
|
assert(lastGroq < firstOpenRouter, "providers are interleaved — order must follow CHAT_CHAIN");
|
|
assert(
|
|
chain.every((l) => providerModels(l.provider).includes(l.model)),
|
|
"a link carries a model its own provider does not offer",
|
|
);
|
|
});
|
|
});
|
|
|
|
// ── env override: routing around a rotted model without a deploy ─────────────
|
|
check("an env override replaces a provider's models AT CALL TIME", () => {
|
|
withEnv(
|
|
{ ...KEYS, ...NO_OVERRIDES, GROQ_API_KEY: "x", LOKI_GROQ_MODELS: "only-this-one" },
|
|
() => {
|
|
// Read after the env was set — a value frozen at import would fail here,
|
|
// and would need a redeploy to route around a dead model.
|
|
assert(providerModels(groq).join() === "only-this-one", `got ${providerModels(groq).join()}`);
|
|
assert(
|
|
usableChatChain().every((l) => l.model === "only-this-one"),
|
|
"override not applied to the chain",
|
|
);
|
|
},
|
|
);
|
|
});
|
|
|
|
check("an override accepts commas, spaces, or both", () => {
|
|
withEnv({ LOKI_OPENROUTER_MODELS: "a/one:free, b/two:free c/three:free" }, () => {
|
|
assert(
|
|
providerModels(openrouter).join("|") === "a/one:free|b/two:free|c/three:free",
|
|
"bad split",
|
|
);
|
|
});
|
|
});
|
|
|
|
check("a blank override falls back to the shipped models, not to nothing", () => {
|
|
withEnv({ LOKI_GROQ_MODELS: " " }, () => {
|
|
assert(
|
|
providerModels(groq).length === groq.models.length,
|
|
"whitespace override emptied the provider",
|
|
);
|
|
});
|
|
});
|
|
|
|
// ── chainFrom: a pin must not reintroduce the single point of failure ────────
|
|
const FAKE: ChatLinkFixture = [
|
|
{ provider: groq, model: "m1" },
|
|
{ provider: groq, model: "m2" },
|
|
{ provider: openrouter, model: "m3" },
|
|
];
|
|
type ChatLinkFixture = Array<{ provider: ChatProvider; model: string }>;
|
|
|
|
check("no pin means the whole chain", () => {
|
|
assert(chainFrom(undefined, FAKE).length === 3, "dropped links with no pin");
|
|
assert(chainFrom(" ", FAKE).length === 3, "blank pin should behave as no pin");
|
|
});
|
|
|
|
check("a pinned model STARTS the chain and keeps everything after it", () => {
|
|
const out = chainFrom("m2", FAKE);
|
|
assert(out.length === 2, `expected 2 links, got ${out.length}`);
|
|
assert(out[0]!.model === "m2" && out[1]!.model === "m3", "pin did not start the chain");
|
|
});
|
|
|
|
check("pinning the last link still leaves a chain of one, not zero", () => {
|
|
assert(
|
|
chainFrom("m3", FAKE)
|
|
.map((l) => l.model)
|
|
.join() === "m3",
|
|
"lost the pinned link",
|
|
);
|
|
});
|
|
|
|
check("an UNKNOWN model is tried first, then falls through to the real chain", () => {
|
|
// The escape hatch ("run Loki on this model") must not become the single
|
|
// point of failure this file exists to remove.
|
|
const out = chainFrom("brand-new-model", FAKE);
|
|
assert(out[0]!.model === "brand-new-model", "unknown model was not tried first");
|
|
assert(out.length === 4, `expected fallthrough to the full chain, got ${out.length}`);
|
|
});
|
|
|
|
check("an unknown model with NO usable chain yields nothing to call", () => {
|
|
assert(chainFrom("anything", []).length === 0, "invented a link with no provider configured");
|
|
});
|
|
|
|
console.log(`\n${passed} passed`);
|
|
|
|
// The platform's Gemini key spent with no cap on 2026-11-12. Loki has no cap
|
|
// that refuses a call, so Google is not in its chain, and the platform key is
|
|
// not copied to the builder or to the gateway.
|
|
{
|
|
if (CHAT_CHAIN.some((p) => p.id === "google")) throw new Error("google is in Loki's chain");
|
|
const install = readFileSync("scripts/hetzner/install-builder-agents.sh", "utf8");
|
|
if (/\/opt\/orangecat\/app\/\.env/.test(install))
|
|
throw new Error("the builder copies OrangeCat's key");
|
|
const gateway = readFileSync(".github/workflows/openclaw-media.yml", "utf8");
|
|
if (/\/opt\/orangecat\/app\/\.env/.test(gateway))
|
|
throw new Error("the gateway copies OrangeCat's key");
|
|
console.log("✓ no uncapped platform Gemini key in Loki, the builder or the gateway");
|
|
}
|