@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
104 lines (103 loc) • 5.87 kB
JavaScript
// Account pools and roles applied to an automatic account choice (dispatch placement, Continue in…, the
// accounts a stopped worker may continue on). See docs/accounts.md.
//
// - A pool rule (a project's or a computer's `allowedPools`) refuses an account outside it, chosen or named.
// - `off` is never chosen automatically, and neither is a Console (API-billed) login with no role: API accounts
// are off for automatic use until the owner gives them a role. Both can still be named by hand.
// - `last-resort` accounts are offered only when no other account (ordinary or `helpers`) fits.
// With no pools, no roles and no Console logins nothing here changes a choice. A config that can't be read
// allows no pool rather than any.
import { readFileSync } from "node:fs";
import { load } from "js-yaml";
import { resolveProjectConfigPath } from "../project-config.js";
import { chooseAccount } from "./account-choice.js";
import { isAccountRole, isPoolTag, poolAllowed } from "./account-pools.js";
const REFUSAL_MAX = 280;
/** A project's rule, from its `phren.project.yaml` in the store (or the team store holding it). */
export function projectRule(phrenPath, project) {
const owner = `project ${project}`;
const file = resolveProjectConfigPath(phrenPath, project);
if (!file)
return { owner };
let text;
try {
text = readFileSync(file, "utf8");
}
catch (error) {
if (error.code === "ENOENT" || error.code === "EISDIR")
return { owner };
return { owner: `${owner} (its phren.project.yaml can't be read)`, pools: [] };
}
let parsed;
// A broken file that never named allowedPools had no list: it stays "any pool", as before pools existed.
try {
parsed = text.trim() ? load(text) : {};
}
catch {
return text.includes("allowedPools") ? { owner: `${owner} (its phren.project.yaml can't be read)`, pools: [] } : { owner };
}
const pools = parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed.allowedPools : undefined;
return { owner, ...(Array.isArray(pools) ? { pools: pools.filter(isPoolTag) } : {}) };
}
/** A computer's rule from what its harness listing reports (its Claude entry's `allowedPools`, or that its
* accounts.yaml can't be read). */
export function computerRule(computer, pools, unreadable = false) {
if (unreadable)
return { owner: `computer ${computer} (its accounts.yaml can't be read)`, pools: [] };
return { owner: `computer ${computer}`, ...(Array.isArray(pools) ? { pools: pools.filter(isPoolTag) } : {}) };
}
/** The pool, role and Console flag a listed account carries, checked, onto its room. */
export function withPolicy(room, listed) {
return { ...room, ...(isPoolTag(listed?.pool) ? { pool: listed.pool } : {}), ...(isAccountRole(listed?.role) ? { role: listed.role } : {}),
...(listed?.console === true ? { console: true } : {}) };
}
/** Why `room` may not do this work under `rules` at all, or undefined. Applies to a named account too. */
export function poolRefusal(room, rules) {
const rule = rules.find(item => !poolAllowed(item.pools, room.pool));
if (!rule)
return undefined;
const listed = rule.pools.length ? `'s allowed pools (${rule.pools.join(", ")})` : "'s allowed pools";
return `${room.pool ? `its pool ${room.pool}` : "it has no pool, and"} is not in ${rule.owner}${listed}`.slice(0, REFUSAL_MAX);
}
/** Why `room` is never chosen automatically, or undefined. */
export function notAutomatic(room) {
if (room.role === "off")
return "role off (chosen only by hand)";
if (!room.role && room.console)
return "an API account with no role (chosen only by hand)";
return undefined;
}
/** Whether any pool, role or Console login could change a choice among `rooms` under `rules`. */
export function policyApplies(rooms, rules) {
return rules.some(rule => rule.pools) || rooms.some(room => room.role || room.pool || room.console);
}
/** The rooms a rule and the roles allow an automatic choice to take. */
export function allowedRooms(rooms, rules) {
return rooms.filter(room => !poolRefusal(room, rules) && !notAutomatic(room));
}
/** Whether an allowed room has room to take work: signed in, not out of quota, not `held` (the limits ledger, a
* worker that just stopped there). */
export const openRoom = (room, held = () => false) => room.usable !== false && !room.exhausted && !held(room);
/**
* `chooseAccount` with pools and roles: rooms a rule refuses or that are not automatic are skipped, with why;
* the rest are ranked by headroom as before, `last-resort` ones only when no other account has room.
*/
export function choosePolicyAccount(rooms, rules = [], blocked = () => undefined) {
const skipped = [];
const allowed = rooms.filter(room => {
const why = poolRefusal(room, rules) ?? notAutomatic(room);
if (why)
skipped.push(`${room.account}: ${why}`);
return !why;
});
const ruledOut = skipped.length ? `; ruled out ${skipped.join(", ")}` : "";
const first = chooseAccount(allowed.filter(room => room.role !== "last-resort"), blocked);
if (first.choice)
return { choice: { account: first.choice.account, reason: `${first.choice.reason}${ruledOut}`.slice(0, 400) }, skipped: [...skipped, ...first.skipped] };
const last = chooseAccount(allowed.filter(room => room.role === "last-resort"), blocked);
const all = [...skipped, ...first.skipped, ...last.skipped];
if (!last.choice)
return { skipped: all };
const without = first.skipped.length ? ` (${first.skipped.join(", ")})` : "";
return { choice: { account: last.choice.account, reason: `last resort, no other account has room${without}; ${last.choice.reason}${ruledOut}`.slice(0, 400) }, skipped: all, lastResort: true };
}