UNPKG

@phren/cli

Version:

Knowledge layer for AI agents - CLI, MCP server, and data layer

125 lines (124 loc) • 5.34 kB
// What a launch last knew about this computer's Claude accounts: each home's login type (Console or // subscription), bound to a privacy-preserving login fingerprint, and the last accounts.yaml policy // that parsed. Kept in the bridge dir so a slow probe or an unreadable file does not force a launch // back to default. See docs/accounts.md. import { randomUUID } from "node:crypto"; import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"; import path from "node:path"; import { claudeLoginFingerprint } from "./claude-accounts.js"; import { withFileLock } from "../governance/locks.js"; import { isAccountRole, isPoolTag } from "./account-pools.js"; import { bridgeRoot } from "./protocol.js"; const SLUG = /^[a-z0-9][a-z0-9-]{0,31}$/; const FINGERPRINT = /^[a-f0-9]{64}$/; const MAX_POOLS = 32; const file = () => path.join(bridgeRoot(), "account-last-known.json"); function loginsOf(raw) { if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined; const out = {}; for (const [id, value] of Object.entries(raw)) { if (!SLUG.test(id) || !value || typeof value !== "object" || Array.isArray(value)) continue; const { kind, fingerprint } = value; if ((kind === "console" || kind === "subscription") && typeof fingerprint === "string" && FINGERPRINT.test(fingerprint)) { out[id] = { kind, fingerprint }; } } return Object.keys(out).length ? out : undefined; } function policyOf(raw) { if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined; const value = raw; const rawSettings = value.settings && typeof value.settings === "object" && !Array.isArray(value.settings) ? value.settings : {}; const settings = {}; for (const [id, item] of Object.entries(rawSettings)) { if (!SLUG.test(id) || !item || typeof item !== "object" || Array.isArray(item)) continue; const { pool, role } = item; const next = { ...(isPoolTag(pool) ? { pool } : {}), ...(isAccountRole(role) ? { role } : {}) }; if (Object.keys(next).length) settings[id] = next; } const allowedPools = Array.isArray(value.allowedPools) ? [...new Set(value.allowedPools.filter(isPoolTag))].slice(0, MAX_POOLS) : undefined; return { settings, ...(allowedPools ? { allowedPools } : {}) }; } function parse(raw) { const value = raw && typeof raw === "object" && !Array.isArray(raw) ? raw : {}; const known = {}; const logins = loginsOf(value.logins); if (logins) known.logins = logins; const policy = policyOf(value.policy); if (policy) known.policy = policy; return known; } function readSync() { try { return parse(JSON.parse(readFileSync(file(), "utf8"))); } catch { return {}; } } /** The last-known logins and policy, or empty when there is no readable file. */ export async function readLastKnown() { return readSync(); } /** A home's last-known login type: only when it belongs to the current login, or the config names none. */ export function knownLogin(last, home) { const entry = last.logins?.[home.id]; if (!entry) return undefined; const current = claudeLoginFingerprint(home); // No identity to compare against: keep the previous classification rather than discarding it. return current === undefined || entry.fingerprint === current ? entry.kind : undefined; } function writeSync(value) { const target = file(); mkdirSync(path.dirname(target), { recursive: true, mode: 0o700 }); const temporary = `${target}.${randomUUID()}.tmp`; try { writeFileSync(temporary, JSON.stringify(value), { mode: 0o600, flag: "wx" }); renameSync(temporary, target); } finally { rmSync(temporary, { force: true }); } } let writes = Promise.resolve(); /** Read, change and write under the file's shared lock, one change at a time; a failure never rejects. */ function update(change) { const run = writes.then(() => { try { withFileLock(file(), () => { const value = readSync(); const before = JSON.stringify(value); change(value); if (JSON.stringify(value) !== before) writeSync(value); }); } catch { /* Best effort: a lost record only costs one more background probe. */ } }); writes = run; return run; } /** Remembers a home's login type once Claude answered for the fingerprint seen before the probe. */ export function rememberLogin(home, seenFingerprint, kind) { // No identity to bind the answer to: keep the previous classification instead of storing an unbound one. if (!SLUG.test(home.id) || !seenFingerprint) return Promise.resolve(); return update(value => { // A later login already changed the home: this older answer is not about it. if (claudeLoginFingerprint(home) !== seenFingerprint) return; value.logins = { ...value.logins, [home.id]: { kind, fingerprint: seenFingerprint } }; }); } /** Remembers the last accounts.yaml policy that parsed. */ export function rememberPolicy(policy) { return update(value => { value.policy = policy; }); }