Files
loki/scripts/test/build-allowance.ts
CatoandClaude Fable 5.1 9c84c929e4 feat(engines): your own key builds when you say so; welcome builds, then the plan's, then your own; capacity you can read (#1347)
* feat(settings): your own key builds, when you say so — a key marked for building reaches your private builder

Track 1, PR 6 of docs/architecture/private-builders.md.

- user_model_keys.for_building (migration 0123), off by default; one
  switch per key in Settings → AI, offered only on keys that can drive an
  agent CLI (config/build-engines.ts: Anthropic → ANTHROPIC_API_KEY for
  Claude Code, OpenAI → Codex, xAI → Grok, Google → Gemini CLI) and only
  to an account with a builder on the cloud.
- scripts/write-tenant-engines.ts (root on the box, from the reconcile
  timer) opens the marked keys with the app's sealing secret and writes
  them to /etc/loki/runners/<short>.env; a unit whose file changed is
  drain-restarted, so the key reaches its next agent without ending a
  session mid-task. No route ever returns an opened key.
- Records: changelog {#your-engine}; roadmap milestones ticked.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

* fix(settings): the building switch sits in one row with its sentence

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

* feat(engines): welcome builds, then the plan's, then your own — and capacity you can read

Track 2 (2.1, 2.2, 2.5) and Track 4.1 of docs/master-plan-2026-10.md.

- users.build_engine (migration 0124): "own" by default — the person's
  sign-in or a key marked for building, never counted — or "loki": Loki's
  key within WELCOME_BUILDS (3, config/welcome-changes.ts) and the plan's
  PLAN_LIMITS.buildsMonthly (10 / 40 / 100). Every run records which
  engine paid for it (payload.engine); welcome and plan builds are counted
  from that marker. A dispatch past the last build is refused with the way
  forward (code engine-exhausted), and the kickoff screen shows it with
  one tap to Settings. Loki's welcome spending stops for everyone at
  WELCOME_MONTHLY_CAP_CHF (20), at CHF_PER_USD, a hand-set rate.
- The box writes Loki's key (LOKI_BUILD_ENGINE_ANTHROPIC_KEY from the
  app's env) into a tenant's unit only while they chose Loki's engine and
  have builds left; otherwise their own marked keys.
- Settings → AI: "Your engine for building", one Segmented choice with the
  numbers; /api/settings/build-engine. The kickoff's no-builder copy says
  the operator switches a private builder on per account.
- System → Capacity: how long work waited for a builder in the last day
  per channel (median, worst), this month's builds, tokens and cost per
  account, and welcome spend against its cap.
- /pricing and the whitepaper (0.3.3) state the builds a month on Loki's
  engine and the rule. Records: changelog {#welcome-changes}
  {#capacity-card}; roadmap milestones ticked.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

* style(engines): format resolve.ts the way prettier wants it

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-20 12:55:35 +02:00

81 lines
2.9 KiB
TypeScript

/**
* Builds on Loki's engine are a number, never an open-ended bill: welcome
* builds first under a cap in francs, then the plan's monthly builds, then
* the person's own engine. Run: npx tsx scripts/test/build-allowance.ts
*/
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { buildAllowance, RUN_ENGINE } from "../../src/lib/build-engine/allowance";
import { PLAN_LIMITS } from "../../src/lib/plan";
import {
CHF_PER_USD,
WELCOME_BUILDS,
WELCOME_MONTHLY_CAP_CHF,
} from "../../src/config/welcome-changes";
const now = new Date("2026-10-20T10:00:00Z");
const free = { plan: "free" as const, planStatus: null, planExpiresAt: null };
const pro = {
plan: "pro" as const,
planStatus: "active" as const,
planExpiresAt: new Date("2026-11-19"),
};
const base = { welcomeUsed: 0, planUsedThisMonth: 0, welcomeSpendUsdThisMonth: 0, now };
// A newcomer: welcome builds, then nothing on Free.
assert.equal(WELCOME_BUILDS, 3);
let a = buildAllowance({ ...base, account: free });
assert.equal(a.engine, RUN_ENGINE.LOKI_WELCOME);
assert.equal(a.welcomeLeft, 3);
a = buildAllowance({ ...base, account: free, welcomeUsed: 3 });
assert.equal(a.engine, null);
assert.match(a.reason!, /welcome builds are used up/);
assert.match(a.reason!, /own sign-in or key/, "the way forward is named");
// The cap in francs stops welcome builds for everyone, and says so.
assert.equal(WELCOME_MONTHLY_CAP_CHF, 20);
a = buildAllowance({
...base,
account: free,
welcomeSpendUsdThisMonth: WELCOME_MONTHLY_CAP_CHF / CHF_PER_USD,
});
assert.equal(a.engine, null);
assert.match(a.reason!, /for everyone this month/);
// A plan: its builds a month after the welcome ones; past them, its own engine.
assert.equal(PLAN_LIMITS.buildsMonthly.free, 0);
assert.equal(PLAN_LIMITS.buildsMonthly.pro, 40);
a = buildAllowance({ ...base, account: pro, welcomeUsed: 3 });
assert.equal(a.engine, RUN_ENGINE.LOKI_PLAN);
assert.equal(a.planLeft, 40);
a = buildAllowance({ ...base, account: pro, welcomeUsed: 3, planUsedThisMonth: 40 });
assert.equal(a.engine, null);
assert.match(a.reason!, /40 builds on Loki's engine are used up/);
// A lapsed plan has none.
a = buildAllowance({
...base,
account: { ...pro, planExpiresAt: new Date("2026-10-01") },
welcomeUsed: 3,
});
assert.equal(a.engine, null);
assert.equal(a.planLeft, 0);
// The marker is written at open and counted from, and the pricing copy says the numbers.
assert.match(
read("src/lib/inject-core.ts"),
/engine: engine\.engine/,
"every run records its engine",
);
assert.match(
read("src/lib/inject-core.ts"),
/code: "engine-exhausted"/,
"a refusal, not a silent queue",
);
assert.match(read("src/config/plans.ts"), /builds a month on Loki's engine; no limit on your own/);
assert.match(read("content/whitepaper.md"), /Three welcome builds run on Loki's engine/);
console.log("✓ build allowance: welcome, then the plan, then your own — in numbers");
function read(p: string) {
return readFileSync(p, "utf8");
}