Direction shift 2026-04-22: docs/CONTROL_PLANE_PRD.md becomes the long-horizon architecture target. Existing Lakehouse (docs/PRD.md, Phases 0-37) is preserved as the reference implementation and first consumer. New 6-layer architecture: L1 Universal API /v1/chat /v1/usage /v1/sessions /v1/tools /v1/context L2 Routing & Policy Engine (rules, fallback chains, cost gating) L3 Provider Adapter Layer (Ollama + OpenRouter + Gemini + Claude) L4 Knowledge + Memory + Playbooks (already built) L5 Execution Loop (scenarios + bot/cycle.ts instances) L6 Observability + token accounting Phases 38-44 sequenced with detailed per-phase specs in the PRD. Current scope: staffing domain (synthetic workers_500k, contracts, emails, SMS, playbooks). DevOps (Terraform/Ansible) is long-horizon target — architecture-compatible but not current. Files added: - docs/CONTROL_PLANE_PRD.md — 6-layer architecture, Phase 38-44 sequencing with staffing-first Truth Layer + Validation pipeline - bot/ — manual-only PR bot scaffold. First consumer test-bed for /v1/chat (Phase 38). Mem0-aligned ADD/UPDATE/NOOP apply semantics; KB feedback loop reads prior cycles on same gap and injects into cloud prompt so bot cycles compound like scenario.ts runs do. - tests/multi-agent/run_stress.ts — the 6-task diverse stress test referenced in the previous commit but missing from its staging Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
// Daily cost tracker. Resets at UTC midnight by keying the file by date.
|
|
// Hard ceilings are the policy surface — the bot refuses to call the
|
|
// cloud once the budget is exhausted for the day.
|
|
|
|
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import type { CostState } from "./types.ts";
|
|
|
|
const COST_DIR = "/home/profit/lakehouse/data/_bot";
|
|
|
|
export const DAILY_CALLS_BUDGET = 20;
|
|
export const DAILY_TOKENS_BUDGET = 8000 * DAILY_CALLS_BUDGET; // 160k tokens/day ceiling
|
|
|
|
function todayUtc(): string {
|
|
return new Date().toISOString().slice(0, 10);
|
|
}
|
|
|
|
function costFile(date: string): string {
|
|
return join(COST_DIR, `cost-${date}.json`);
|
|
}
|
|
|
|
export async function readCost(): Promise<CostState> {
|
|
const date = todayUtc();
|
|
try {
|
|
const raw = await readFile(costFile(date), "utf8");
|
|
return JSON.parse(raw) as CostState;
|
|
} catch {
|
|
return { date, calls: 0, tokens: 0 };
|
|
}
|
|
}
|
|
|
|
export async function recordCost(calls: number, tokens: number): Promise<CostState> {
|
|
await mkdir(COST_DIR, { recursive: true });
|
|
const current = await readCost();
|
|
const updated: CostState = {
|
|
date: current.date,
|
|
calls: current.calls + calls,
|
|
tokens: current.tokens + tokens,
|
|
};
|
|
await writeFile(costFile(updated.date), JSON.stringify(updated, null, 2));
|
|
return updated;
|
|
}
|
|
|
|
export function budgetCheck(c: CostState): { ok: boolean; reason: string } {
|
|
if (c.calls >= DAILY_CALLS_BUDGET) {
|
|
return { ok: false, reason: `daily call budget exhausted (${c.calls}/${DAILY_CALLS_BUDGET})` };
|
|
}
|
|
if (c.tokens >= DAILY_TOKENS_BUDGET) {
|
|
return { ok: false, reason: `daily token budget exhausted (${c.tokens}/${DAILY_TOKENS_BUDGET})` };
|
|
}
|
|
return { ok: true, reason: `${c.calls}/${DAILY_CALLS_BUDGET} calls, ${c.tokens}/${DAILY_TOKENS_BUDGET} tokens used today` };
|
|
}
|