UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

130 lines • 5.97 kB
import { withoutOrderedAccount } from "@earendil-works/pi-ai/auth/pool/select"; /** * Whether a Claude CLI slot carries an `auth_error` block that no Claude CLI verdict backs (senpi#5623). An engine from * before #4436 lists the slot, finds no token in it, and writes `blockReason`/`blockedUntil`/`blockedAt` onto it * without contacting Claude; such a block says nothing about the login in the slot's dir. Takes `object`: the generic * listing passes raw pool slots. */ export function isForeignAuthBlock(slot) { const fields = slot; if (fields.store !== "claude-cli" || fields.blockReason !== "auth_error") return false; // Only this engine marks a login expired (senpi#5350), so that block is never another engine's. if (fields.signInRequired === "login_expired") return false; const verdict = fields.authVerdict; return verdict?.dirId !== fields.dirId || verdict?.at !== fields.blockedAt; } function withoutForeignAuthBlock(slot) { if (!isForeignAuthBlock(slot)) return slot; const { blockedUntil: _until, blockReason: _reason, blockedAt: _at, authVerdict: _verdict, ...rest } = slot; return rest; } /** Narrows a slot to an env token account; a claude CLI slot's login lives in the CLI's own store. */ export function isEnvSlot(slot) { return slot.store === undefined; } export const SENTINEL_OAUTH_FIELDS = { access: "claude-sdk-oauth-managed", refresh: "claude-sdk-oauth-managed", expires: 4102444800000, }; export function emptyCredential() { return { type: "oauth", ...SENTINEL_OAUTH_FIELDS, accounts: [] }; } function storedSlots(credential) { return credential.accounts ?? []; } /** * Whether a stored slot is listed: only a claude CLI account is. A stored account that still holds tokens (one the * retired senpi-owned login wrote, which the load-time sign-out could not rewrite, or a sentinel projection) is never * listed, so no turn or usage poll serves or refreshes its token (senpi#4436). Every listing of stored Claude * accounts (this lane's, the generic one RPC uses, and the configured check) applies this one rule, so none of them * shows or counts an account the others hide. Takes `object`: the generic listing passes raw pool slots. */ export function isListedStoredSlot(slot) { const candidate = slot; const name = candidate.name; return candidate.store === "claude-cli" && typeof name === "string" && name.trim() !== ""; } /** * A stored claude CLI account the listing rule drops, so a lane can refuse the turn instead of silently falling back to * ambient host credentials when every such account is unusable (oh-my-openagent#12228). Accounts of any other shape are * not counted: a legacy token slot is hidden on purpose (senpi#4436) and is not a broken login this rule can report. */ export function unusableStoredSlotCount(credential) { if (credential === null || typeof credential !== "object" || !("accounts" in credential)) return 0; const stored = credential.accounts; return Array.isArray(stored) ? stored.filter((slot) => slot !== null && typeof slot === "object" && slot.store === "claude-cli" && !isListedStoredSlot(slot)).length : 0; } /** The listed accounts: every stored claude CLI account (`isListedStoredSlot`), then the env token accounts. */ export function listAccounts(credential, env) { const slots = storedSlots(credential).filter(isListedStoredSlot).map(withoutForeignAuthBlock); if (env) { const state = credential.slotState ?? {}; for (const slot of envSlots(env)) { const persisted = state[slot.name]; slots.push(persisted ? { ...slot, ...persisted } : slot); } } return slots; } const ACCOUNT_NAME_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/; export function assertValidAccountName(name) { if (!ACCOUNT_NAME_PATTERN.test(name)) { throw new Error(`Invalid account name '${name}': use letters, digits, '-' or '_', starting with a letter or digit`); } } export function addAccount(credential, slot) { assertValidAccountName(slot.name); if (storedSlots(credential).some((existing) => existing.name === slot.name)) { throw new Error(`Account '${slot.name}' already exists`); } return { ...credential, accounts: [...storedSlots(credential), slot] }; } export function removeAccount(credential, name) { const accounts = storedSlots(credential).filter((slot) => slot.name !== name); const next = { ...credential, accounts }; if (credential.pinned === name) delete next.pinned; return withoutOrderedAccount(next, name); } export function pinAccount(credential, name) { return { ...credential, pinned: name }; } export function assertSentinelInvariant(credential) { if (credential.access !== SENTINEL_OAUTH_FIELDS.access || credential.refresh !== SENTINEL_OAUTH_FIELDS.refresh || credential.expires !== SENTINEL_OAUTH_FIELDS.expires) { throw new Error("top-level OAuth fields must remain sentinel values"); } } export function envSlots(env) { const slots = []; const read = (suffix) => env(suffix === undefined ? "CLAUDE_CODE_OAUTH_TOKEN" : `CLAUDE_CODE_OAUTH_TOKEN_${suffix}`); const names = [{ slot: "env" }]; for (let index = 2; index <= 16; index++) names.push({ suffix: String(index), slot: `env-${index}` }); for (const { suffix, slot } of names) { const token = read(suffix); if (token) { slots.push({ name: slot, refresh: "", access: "", expires: 0, source: "env" }); } } return slots; } export function envSlotToken(env, slotName) { const match = /^env(?:-(\d+))?$/.exec(slotName); if (!match) return undefined; const suffix = match[1]; return env(suffix === undefined ? "CLAUDE_CODE_OAUTH_TOKEN" : `CLAUDE_CODE_OAUTH_TOKEN_${suffix}`); } //# sourceMappingURL=accounts.js.map