@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
286 lines (285 loc) • 17.6 kB
JavaScript
// Which Claude account a launch runs under when none is named, and which one
// a worker that hit its usage limit continues on. See docs/accounts.md.
//
// Every account stays in its own home and is used only by Claude Code there:
// this chooses a CLAUDE_CONFIG_DIR, it never moves a token or a request.
import { readFile } from "node:fs/promises";
import path from "node:path";
import { z } from "zod";
import { readInstallPreferences } from "../init/preferences.js";
import { logger } from "../logger.js";
import { findPhrenPath } from "../phren-paths.js";
import { claudeAuthStatus, claudeHomes, claudeLoginFingerprint, DEFAULT_ACCOUNT } from "./claude-accounts.js";
import { knownLogin, readLastKnown, rememberLogin, rememberPolicy } from "./account-last-known.js";
import { harnessInventoryWithin } from "./harnesses.js";
import { atomic, BridgeError, bridgeRoot } from "./protocol.js";
import { choosePolicyAccount, computerRule, policyApplies, projectRule, withPolicy } from "./account-policy.js";
import { accountPolicy } from "./account-pools.js";
import { phrenStoreRoot } from "./transcripts.js";
import { detectProject } from "../shared/index.js";
/** Claude's 5-hour session window and its 7-day window over all models. */
export const FIVE_HOUR = "five_hour", WEEK = "seven_day";
function windowRoom(window, now) {
if (!window)
return undefined;
const reset = window.resetsAt ? Date.parse(window.resetsAt) : NaN;
if (window.reset || Number.isFinite(reset) && reset <= now)
return { leftPercent: 100 };
const left = window.limited ? 0 : typeof window.usedPercent === "number" ? Math.max(0, Math.min(100, Math.round(100 - window.usedPercent))) : undefined;
return { ...(left !== undefined ? { leftPercent: left } : {}), ...(window.resetsAt ? { resetsAt: window.resetsAt } : {}) };
}
/** The 5-hour and weekly rooms of one usage row. */
export function accountWindows(usage, now) {
const fiveHour = windowRoom(usage.windows.find(w => w.id === FIVE_HOUR), now);
const week = windowRoom(usage.windows.find(w => w.id === WEEK), now);
return { ...(fiveHour ? { fiveHour } : {}), ...(week ? { week } : {}) };
}
const left = (room) => room?.leftPercent;
/** Unknown room ranks below any known room: an account never reported is tried after one that is. */
const rank = (room) => left(room) ?? -1;
const describe = (room) => {
const part = (name, window) => left(window) === undefined ? `${name} unknown` : `${left(window)}% ${name} left`;
return `${room.account} (${part("5-hour", room.fiveHour)}, ${part("weekly", room.week)})`;
};
/**
* The account with the most headroom: the most left on the 5-hour window,
* then on the weekly one, then `default`, then by id. An account that is not
* usable (signed out), is out of quota, or that `blocked` names (it hit its
* limit and its window has not reset) is never chosen. Undefined when none is left.
*/
export function chooseAccount(rooms, blocked = () => undefined) {
const skipped = [];
const open = rooms.filter(room => {
const why = room.usable === false ? "not signed in" : room.exhausted ? `out of quota${room.until ? ` until ${room.until}` : ""}` : blocked(room);
if (why)
skipped.push(`${room.account}: ${why}`);
return !why;
});
const sorted = [...open].sort((a, b) => rank(b.fiveHour) - rank(a.fiveHour) || rank(b.week) - rank(a.week)
|| Number(b.account === DEFAULT_ACCOUNT) - Number(a.account === DEFAULT_ACCOUNT) || a.account.localeCompare(b.account));
const best = sorted[0];
if (!best)
return { skipped };
const others = sorted.slice(1).map(describe);
const reason = `most headroom: ${describe(best)}${others.length ? `; over ${others.join(", ")}` : ""}${skipped.length ? `; skipped ${skipped.join(", ")}` : ""}`;
return { choice: { account: best.account, reason: reason.slice(0, 400) }, skipped };
}
/** This computer's signed-in Claude accounts with their room, from the Hook's inventory and usage rows. */
export function claudeRooms(inventory, usage, now) {
const accounts = inventory?.harnesses.find(entry => entry.source === "claude")?.accounts ?? [];
return accounts.map(account => {
const row = usage.find(item => item.source === "claude" && (item.account?.id ?? DEFAULT_ACCOUNT) === account.id);
const windows = row ? accountWindows(row, now) : {};
const out = [windows.fiveHour, windows.week].filter(w => w?.leftPercent === 0);
const until = out.map(w => w?.resetsAt).filter((value) => Boolean(value)).sort().at(-1);
return { account: account.id, ...(account.key ? { key: account.key } : {}), usable: account.usable, ...windows,
...(out.length ? { exhausted: true, ...(until ? { until } : {}) } : {}) };
});
}
// ── Accounts that hit their limit ────────────────────────────────────────────
/** How long an account that hit its limit is left alone when its reset time is unknown: one 5-hour window. */
export const UNKNOWN_RESET_MS = 5 * 60 * 60_000;
const limitsFile = () => path.join(bridgeRoot(), "account-limits.json");
const limitSchema = z.object({ key: z.string().max(200), account: z.string().max(64), until: z.string().datetime(), at: z.string().datetime() }).strict();
const ledgerSchema = z.object({ limits: z.array(limitSchema).max(64) }).strict();
/** How a login is told apart: its key, else its id on that computer (two unknown logins are not one). */
export const limitKey = (room, computer) => room.key && !room.key.includes(":home:") ? room.key : `${computer}/${room.account}`;
export async function readAccountLimits(now = Date.now()) {
try {
const parsed = ledgerSchema.parse(JSON.parse(await readFile(limitsFile(), "utf8")));
return parsed.limits.filter(entry => Date.parse(entry.until) > now);
}
catch (error) {
if (error.code !== "ENOENT")
logger.warn("accounts", `Could not read account-limits.json: ${error instanceof Error ? error.message : String(error)}`);
return [];
}
}
let ledgerWrites = Promise.resolve();
/** Records that a login hit its limit: no launch or failover chooses it again before `until`. */
export function noteAccountLimit(entry, now = Date.now()) {
const run = ledgerWrites.then(async () => {
const until = entry.until && Date.parse(entry.until) > now ? entry.until : new Date(now + UNKNOWN_RESET_MS).toISOString();
const kept = (await readAccountLimits(now)).filter(item => item.key !== entry.key);
await atomic(limitsFile(), { limits: [...kept, { key: entry.key, account: entry.account, until, at: new Date(now).toISOString() }].slice(-64) });
});
ledgerWrites = run.catch(() => undefined);
return run;
}
/** Why `room` on `computer` is held back by the ledger, or undefined. */
export function limitedBy(limits, computer) {
return room => {
const hit = limits.find(entry => entry.key === limitKey(room, computer));
return hit ? `hit its limit, held until ${hit.until}` : undefined;
};
}
let usageReader;
let inventoryReader;
/** The Hook's usage reader (`AccountUsageReader.limits`); tests pass fakes for both. */
export function setChoiceReaders(readers) {
usageReader = readers.usage;
inventoryReader = readers.inventory;
}
/** The value, or undefined once `ms` passes or it fails: a launch never waits on a slow reader. */
async function within(ms, value) {
let timer;
const late = new Promise(resolve => { timer = setTimeout(() => resolve(undefined), ms); timer.unref(); });
try {
return await Promise.race([value.catch(() => undefined), late]);
}
finally {
clearTimeout(timer);
}
}
function scopeRule(scope) {
const store = scope.phrenPath ?? phrenStoreRoot();
let project = scope.project;
if (!project && scope.cwd) {
try {
project = detectProject(store, scope.cwd) ?? undefined;
}
catch { /* No store here: no project rule. */ }
}
return project ? projectRule(store, project) : undefined;
}
const AUTH_BUDGET_MS = 2_500;
/** A home's login type: the probe's definitive answer, else the last one seen. Never waits past the budget. */
async function loginKind(home, last) {
// The fingerprint names the login this probe is about: a later one makes this answer stale.
const fingerprint = claudeLoginFingerprint(home);
const status = claudeAuthStatus(home);
// A definitive answer still records the classification when it lands after the budget.
void status.then(answer => {
if (answer.unknown !== true && answer.signedIn)
return rememberLogin(home, fingerprint, answer.console ? "console" : "subscription");
}).catch(() => undefined);
const answer = await within(AUTH_BUDGET_MS, status);
if (answer && answer.unknown !== true)
return answer.signedIn && answer.console ? "console" : "subscription";
return knownLogin(last, home);
}
/**
* The account a Claude launch that names none runs under on this computer, or
* undefined to launch as before (`default`). With no pool, role or Console login
* in play: only when more than one account is signed in here and their room can
* be read. Otherwise as dispatch picks (account-policy.ts): the project's and this
* computer's pools, never `off`, `last-resort` last; 409 `account_pool` when no
* account fits. Logged with the reason.
*/
export async function pickClaudeAccount(what, scope = {}, computer = "this computer", now = Date.now()) {
const project = scopeRule(scope);
const last = await readLastKnown();
// This computer's accounts.yaml roles and pools, read here rather than only through the inventory: a slow or
// failed inventory read must not hide a restriction it would have applied. When the file can't be read, keep
// its last successfully parsed policy, but only when that policy restricted something.
const local = localPolicy();
let effective;
if (local.policy) {
effective = { settings: local.policy.settings, ...(local.policy.allowedPools ? { allowedPools: local.policy.allowedPools } : {}), restricted: restricts(local.policy) };
void rememberPolicy({ settings: local.policy.settings, ...(local.policy.allowedPools ? { allowedPools: local.policy.allowedPools } : {}) });
}
else if (last.policy && restricts(last.policy)) {
effective = { settings: last.policy.settings, ...(last.policy.allowedPools ? { allowedPools: last.policy.allowedPools } : {}), restricted: true };
logger.warn("accounts", "accounts.yaml can't be read; keeping its last-known Claude roles and pools.");
}
else {
effective = { settings: {}, restricted: false };
logger.warn("accounts", "accounts.yaml can't be read; launching as before because no Claude role or pool was ever set.");
}
// One Claude home and no rule that could refuse it means nothing to choose. Checking the folders first keeps a
// launch from running `claude auth status` against the config it is about to pretrust: that process can write
// .claude.json back over the new trust entry. A Console (API-billed) login is the exception: it is never chosen
// automatically without a role, so the sole account's login is confirmed before taking the shortcut.
const homes = claudeHomes();
const knownKind = (account) => {
const home = homes.find(item => item.id === account);
return home ? knownLogin(last, home) : undefined;
};
if (!inventoryReader && homes.length < 2 && !project?.pools && !effective.restricted) {
if (await loginKind(homes[0], last) !== "console")
return undefined;
throw new BridgeError(409, `The only Claude account on ${computer} is an API (Console) login with no role, so it is not used automatically. Give it a role or name it by hand.`.slice(0, 500), { code: "account_pool", skipped: [`${homes[0].id}: an API account with no role (chosen only by hand)`] });
}
const inventory = await within(2_500, (inventoryReader ?? (() => harnessInventoryWithin(2_500)))());
const entry = inventory?.harnesses.find(item => item.source === "claude");
// A restriction is only enforceable against a listing of this computer's accounts. With none, refuse rather
// than launch under default, which may be exactly the account the restriction forbids.
if (effective.restricted && !entry?.accounts?.length) {
throw new BridgeError(409, `This computer's Claude account roles and pools${local.policy ? "" : " (its last-known ones)"} can't be evaluated because the harness inventory listed no accounts; refusing rather than launch under an account they may forbid.`.slice(0, 500), { code: "account_pool" });
}
const rules = [...(project ? [project] : []), computerRule(computer, effective.allowedPools)];
// An unanswered sign-in check leaves console off the listing: use the login last seen for that home, so a
// Console account is never chosen automatically just because this probe did not answer.
const policyRooms = (usage) => claudeRooms(inventory, usage, now).map(room => {
const listed = entry?.accounts?.find(item => item.id === room.account);
const unknown = Boolean(listed && !listed.signedIn && listed.usable);
const console = listed?.console === true || (unknown && knownKind(room.account) === "console");
return withPolicy(room, { ...listed, ...(console ? { console: true } : {}), ...effective.settings[room.account] });
});
// Falling back to `default` is also a choice: never launch a last-known Console login without a role.
const defaultListed = entry?.accounts?.find(item => item.id === DEFAULT_ACCOUNT);
const defaultUnknown = Boolean(defaultListed && !defaultListed.signedIn && defaultListed.usable);
const defaultConsole = defaultListed?.console === true || ((!defaultListed || defaultUnknown) && knownKind(DEFAULT_ACCOUNT) === "console");
const fallback = (choice) => {
if (choice)
return choice;
if (defaultConsole)
throw new BridgeError(409, `The default Claude account on ${computer} is an API (Console) login with no role, so it is not used automatically. Give it a role or name it by hand.`.slice(0, 500), { code: "account_pool", skipped: [`${DEFAULT_ACCOUNT}: an API account with no role (chosen only by hand)`] });
return undefined;
};
if (!policyApplies(policyRooms([]), rules)) {
if (policyRooms([]).filter(room => room.usable !== false).length < 2)
return fallback();
const usage = usageReader ? await within(2_500, usageReader()) : undefined;
if (!usage)
return fallback();
const { choice, skipped } = chooseAccount(claudeRooms(inventory, usage, now), limitedBy(await readAccountLimits(now), computer));
if (choice) {
logger.info("accounts", `${what}: Claude account ${choice.account}, ${choice.reason}.`);
return choice;
}
if (defaultConsole)
return fallback();
logger.info("accounts", `${what}: no Claude account has room (${skipped.join("; ")}); launching under default.`);
return undefined;
}
// Unknown room ranks last: an absent or slow usage reader must not bypass pools or roles.
const usage = (usageReader ? await within(2_500, usageReader()) : undefined) ?? [];
const { choice, skipped } = choosePolicyAccount(policyRooms(usage), rules, limitedBy(await readAccountLimits(now), computer));
if (!choice) {
throw new BridgeError(409, `No Claude account on ${computer} may do this work${skipped.length ? `: ${skipped.join("; ")}` : ""}.`.slice(0, 500), { code: "account_pool", skipped });
}
logger.info("accounts", `${what}: Claude account ${choice.account}, ${choice.reason}.`);
return choice;
}
/** This computer's accounts.yaml policy, or that it can't be read. */
function localPolicy() {
try {
return { policy: accountPolicy(), unreadable: false };
}
catch {
return { unreadable: true };
}
}
/** Whether a policy sets a pool, role or allowed pools. */
function restricts(policy) {
return Boolean(policy?.allowedPools) || Object.values(policy?.settings ?? {}).some(item => item.pool || item.role);
}
// ── The switch ───────────────────────────────────────────────────────────────
const TRUE = new Set(["1", "true", "on", "yes"]), FALSE = new Set(["0", "false", "off", "no"]);
/** Whether a dispatched Claude worker stopped at its usage limit continues on another signed-in account.
* Off unless the owner opts in with `phren config account-failover on` or PHREN_ACCOUNT_FAILOVER=on. */
export function resolveAccountFailover(env = process.env, store = findPhrenPath()) {
const raw = env.PHREN_ACCOUNT_FAILOVER?.trim().toLowerCase();
if (raw && TRUE.has(raw))
return { on: true, source: "PHREN_ACCOUNT_FAILOVER" };
if (raw && FALSE.has(raw))
return { on: false, source: "PHREN_ACCOUNT_FAILOVER" };
try {
const value = store ? readInstallPreferences(store).accountFailover : undefined;
if (typeof value === "boolean")
return { on: value, source: "install preferences" };
}
catch { /* No preferences yet: the default applies. */ }
return { on: false, source: "default" };
}