UNPKG

@code-yeongyu/senpi

Version:

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

508 lines • 26.1 kB
import { lstat } from "node:fs/promises"; import { dirname, join } from "node:path"; import { normalizeProviderId } from "@earendil-works/pi-ai"; import { slotsOfOwnKind } from "@earendil-works/pi-ai/auth/pool/kinds"; import { applyAccountOrder, isSlotBlocked, withoutOrderedAccount } from "@earendil-works/pi-ai/auth/pool/select"; import { accountDisplayName, listSlots, pinSlot, projectSlot, removeSlot, renameSlotDisplayName, } from "@earendil-works/pi-ai/auth/pool/slots"; import { getAgentDir } from "../config.js"; import { discoverEnvSlots } from "./credential-pool/env-slots.js"; import { isAvailable } from "./credential-pool/failover.js"; import { modelBlockApplies, pruneModelBlocks } from "./credential-pool/model-scope.js"; import { applicableStoredState, CredentialSlotRepository, } from "./credential-pool/state-store.js"; import { emitProviderAccountsChanged } from "./extensions/builtin/anthropic-subscription/account-events.js"; import { isForeignAuthBlock, isListedStoredSlot, SENTINEL_OAUTH_FIELDS, } from "./extensions/builtin/anthropic-subscription/accounts.js"; import { isAccountBlocked, isLimitLiftedEarly } from "./extensions/builtin/anthropic-subscription/affinity.js"; import { accountDirFor, isValidAccountDirId, removeAccountDir, } from "./extensions/builtin/anthropic-subscription/claude-cli-account-dir.js"; import { defaultExecutableDeps, resolveClaudeCodeExecutable, } from "./extensions/builtin/anthropic-subscription/executable.js"; /** * The accounts no usage reading can bring back, so usage they report must not release a session's hold on the * provider (senpi#6536): a slot of the other credential kind (senpi#3486), a signed-out account, and every account * blocked for anything but a usage limit. A usage limit is a `rate_limit` block or a `paused` one (a ChatGPT * subscription's usage-limit pause, which carries no reason). The Claude lane's billing and organization blocks also * reach a summary with no reason but are not paused, so they count as needing the user. */ export function accountsNeedingUser(summaries) { return summaries .filter((account) => account.unused === true || account.signedOut === true || (account.blocked && account.blockedReason !== "rate_limit" && account.paused !== true)) .map((account) => account.name); } /** * Whether a pooled account can serve `modelId` now: not blocked, not signed out (the pool skips those slots), and * no live block on that model. A block keyed by a family binds the family's models; any other key binds that exact * model id only, matched by the pool's own rule (senpi#5590). */ export function accountCanServe(account, modelId, now) { if (account.blocked || account.signedOut === true) return false; return !(account.blockedModels ?? []).some((block) => block.until > now && modelBlockApplies(block.model, modelId)); } function objectField(value, key) { const found = Object.entries(value).find(([candidate]) => candidate === key)?.[1]; return typeof found === "object" && found !== null && !Array.isArray(found) ? found : undefined; } function storedCheck(slot) { const check = objectField(slot, "check"); if (check === undefined) return undefined; const model = stringField(check, "model"); const outcome = stringField(check, "outcome"); const at = numberField(check, "at"); if (model === undefined || at === undefined || (outcome !== "ok" && outcome !== "failed")) return undefined; const reason = stringField(check, "reason"); const message = stringField(check, "message"); const status = numberField(check, "status"); return { model, outcome, ...(reason === undefined ? {} : { reason }), ...(message === undefined ? {} : { message }), ...(status === undefined ? {} : { status }), at, }; } /** The listing fields of a stored `claude-cli` slot, read defensively: auth.json is a trust boundary. */ function claudeCliSlotFields(slot) { if (stringField(slot, "store") !== "claude-cli") return {}; const profile = objectField(slot, "profile") ?? {}; const signedInAt = numberField(slot, "signedInAt"); const email = stringField(profile, "email"); const orgName = stringField(profile, "orgName"); const subscriptionType = stringField(profile, "subscriptionType"); const check = storedCheck(slot); return { store: "claude-cli", ...(signedInAt === undefined ? {} : { signedInAt }), ...(email === undefined ? {} : { email }), ...(orgName === undefined ? {} : { orgName }), ...(subscriptionType === undefined ? {} : { subscriptionType }), ...(check === undefined ? {} : { check }), signIn: stringField(slot, "signInRequired") === undefined ? "signed_in" : "needs_sign_in", }; } 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`); } } /** Resolve the immutable account ID, never its display name or its position in a list. */ export function selectedOAuthAccount(storage, provider, name) { assertValidAccountName(name); const credential = projectSlot(storage.get(provider), name); if (credential?.type !== "oauth") throw new Error(`OAuth account not found: ${name}`); return credential; } function pooledFrom(credential) { return credential === undefined ? undefined : credential; } function pinnedName(credential) { return credential?.pinned; } function numberField(value, key) { const found = Object.entries(value).find(([candidate]) => candidate === key)?.[1]; return typeof found === "number" ? found : undefined; } export function credentialPoolRepository(storage) { const authPath = storage.getStoragePath(); return new CredentialSlotRepository(authPath ? join(dirname(authPath), "credential-pool-state.json") : undefined); } function stringField(value, key) { const found = Object.entries(value).find(([candidate]) => candidate === key)?.[1]; return typeof found === "string" ? found : undefined; } const UNBLOCKED = { blocked: false }; function accountBlockedReason(reason) { switch (reason) { case "auth_error": case "rate_limit": return reason; case "account_disabled": return "disabled"; default: return undefined; } } /** A live block with its reason and, unless disabled, the time it lifts. */ function liveBlock(reason, blockedUntil, now) { const blockedReason = accountBlockedReason(reason); if (blockedReason === "disabled") return { blocked: true, blockedReason }; if (blockedUntil === undefined || blockedUntil <= now) return UNBLOCKED; return { blocked: true, ...(blockedReason === undefined ? {} : { blockedReason }), blockedUntil }; } function sidecarBlock(sidecar, now) { if (sidecar?.usageLimit) return { blocked: true, paused: true, usageUrl: "https://chatgpt.com/settings/usage" }; if (sidecar === undefined || isAvailable(sidecar, now)) return UNBLOCKED; return liveBlock(sidecar?.blockReason, sidecar?.blockedUntil, now); } /** * A block a provider lane stored on its own slot holds exactly while that lane's selector skips the slot, so a * listing never calls a skipped account available (senpi#4365 follow-up). The Claude lane re-probes an auth block * whose deadline passed; the Cursor lane keeps one until re-login, with no deadline to report. */ function storedSlotBlock(provider, slot, now) { const blockReason = stringField(slot, "blockReason"); const blockedUntil = numberField(slot, "blockedUntil"); const blockedReason = accountBlockedReason(blockReason); if (blockedReason === "disabled") return { blocked: true, blockedReason }; // The Claude lane does not skip a slot for an auth block no Claude CLI verdict backs (senpi#5623). if (normalizeProviderId(provider) === "anthropic-subscription" && isForeignAuthBlock(slot)) return UNBLOCKED; const skipped = normalizeProviderId(provider) === "anthropic-subscription" ? isAccountBlocked({ blockedUntil }, now) && !isLimitLiftedEarly({ name: stringField(slot, "name") ?? "", blockReason, blockedAt: numberField(slot, "blockedAt"), blockedUntil, }, now) : isSlotBlocked({ blockReason, blockedUntil }, now); if (!skipped) return UNBLOCKED; return { blocked: true, ...(blockedReason === undefined ? {} : { blockedReason }), ...(blockedUntil === undefined || blockedUntil <= now ? {} : { blockedUntil }), }; } function slotBlocked(provider, slot, sidecar, now) { const fromSidecar = sidecarBlock(sidecar, now); const lane = normalizeProviderId(provider); // rotation-slots overlays only sidecar health; generic failover never sees auth.json block fields. if (lane !== "cursor-cli-oauth" && lane !== "anthropic-subscription") return fromSidecar; const fromSlot = storedSlotBlock(provider, slot, now); // A disabled account outranks a timed block from the other source: it never lifts on its own. if (fromSlot.blockedReason === "disabled") return fromSlot; if (fromSidecar.blockedReason === "disabled" || !fromSlot.blocked) return fromSidecar; // So does a stored block with no deadline: only a re-login lifts it, so no expiry may be borrowed for it. if (!fromSidecar.blocked || fromSlot.blockedUntil === undefined) return fromSlot; // Both sources hold a timed block: the account is usable only once the later one lifts, and a // rejected key stays the reason so a client still skips it. const later = (fromSlot.blockedUntil ?? 0) > (fromSidecar.blockedUntil ?? 0) ? fromSlot : fromSidecar; const rejected = [fromSidecar, fromSlot].some((block) => block.blockedReason === "auth_error"); return rejected ? { ...later, blockedReason: "auth_error" } : later; } function storedModelBlocks(slot) { const found = Object.entries(slot).find(([candidate]) => candidate === "modelBlocks")?.[1]; if (found === null || typeof found !== "object") return undefined; const entries = Object.entries(found).flatMap(([model, block]) => { const until = block !== null && typeof block === "object" ? numberField(block, "blockedUntil") : undefined; return until === undefined ? [] : [[model, { blockedUntil: until }]]; }); return entries.length === 0 ? undefined : Object.fromEntries(entries); } /** Live model blocks from every source, the latest expiry winning per model. */ function blockedModels(now, ...sources) { const until = new Map(); for (const blocks of sources) { for (const [model, block] of Object.entries(pruneModelBlocks(blocks, now) ?? {})) { until.set(model, Math.max(until.get(model) ?? 0, block.blockedUntil)); } } return until.size === 0 ? {} : { blockedModels: [...until].map(([model, at]) => ({ model, until: at })) }; } /** * Lists a provider's credential accounts for ANY provider, not just one lane. * Stored slots own the listing when a credential exists; env slots are listed * only when nothing is stored, mirroring resolution precedence exactly. Health * comes from the pool sidecar, so a cooldown persisted by a failover shows up * here without auth.json ever carrying block state. */ export async function getCredentialAccounts(storage, provider, env = process.env, repository, activeName) { const accounts = await summarizeCredentialAccounts(provider, storage.get(provider), env, repository ?? credentialPoolRepository(storage)); if (normalizeProviderId(provider) !== "chatgpt-subscription") return accounts; // `active` is additive: present only on the account a request actually used, so other summaries keep their shape. return accounts.map((account) => account.name === activeName && !account.blocked && !account.signedOut && !account.unused ? { ...account, active: true } : account); } /** Storage-free variant for callers that already hold the credential (e.g. auth check). */ export async function summarizeCredentialAccounts(provider, stored, env = process.env, repository = new CredentialSlotRepository()) { const credential = pooledFrom(stored); return applyAccountOrder(credential?.order, await summarizeInStoredOrder(provider, credential, env, repository)); } async function summarizeInStoredOrder(provider, credential, env, repository) { const now = Date.now(); const pinned = pinnedName(credential); const summaries = []; if (credential) { const state = await repository.listSlots(provider, "stored"); // Read boundary (senpi#1989): a caller may still pass the legacy provider // id (an older session, a stored account payload), so compare normalized. const isClaudeSubscription = normalizeProviderId(provider) === "anthropic-subscription"; // The Claude lane's own listing rule, so both listings show the same accounts (senpi#4436). const storedAccounts = isClaudeSubscription ? Array.isArray(credential.accounts) ? listSlots(credential).filter(isListedStoredSlot) : [] : listSlots(credential); const usable = new Set(slotsOfOwnKind(credential).map((slot) => slot.name)); for (const slot of storedAccounts) { const displayName = accountDisplayName(slot.displayName); const persisted = state[slot.name]; const revisions = await repository.storedSlotRevisions(provider, slot.name, { key: slot.key, access: slot.access, refresh: slot.refresh, }); // The same rule the pool selects by: an account's block survives a token refresh (senpi#4093). const applicable = applicableStoredState(persisted, revisions); summaries.push({ name: slot.name, ...(normalizeProviderId(provider) === "chatgpt-subscription" && credential.type === "oauth" ? { route: slot.route === "siwc" ? "siwc" : "legacy", ...(slot.route === "siwc" && typeof slot.planUsageEnabled === "boolean" ? { planUsageEnabled: slot.planUsageEnabled } : {}), ...(slot.signedOut === true ? { signedOut: true } : {}), } : {}), ...(displayName === undefined ? {} : { displayName }), source: slot.source ?? "login", ...slotBlocked(provider, slot, applicable, now), pinned: pinned === slot.name, ...blockedModels(now, applicable?.modelBlocks, storedModelBlocks(slot)), ...(usable.has(slot.name) ? {} : { unused: true }), ...(isClaudeSubscription ? claudeCliSlotFields(slot) : {}), }); } if (normalizeProviderId(provider) !== "anthropic-subscription") return summaries; } const state = await repository.listSlots(provider, "env"); for (const slot of discoverEnvSlots(provider, (name) => env[name])) { const persisted = state[slot.name]; const revision = await repository.envCredentialRevision(slot.envVarName, slot.key); // A block belongs to the value that earned it; a rotated env key starts clean. const applicable = persisted?.credentialRevision === revision ? persisted : undefined; summaries.push({ name: slot.name, source: "env", ...sidecarBlock(applicable, now), pinned: pinned === slot.name, ...blockedModels(now, applicable?.modelBlocks), }); } return summaries; } /** Atomically rename/clear stored metadata without changing identity, health or environment state. */ export async function renameCredentialAccount(storage, provider, name, displayName) { await storage.modify(provider, async (current) => { if (!current) throw new Error(`No stored credential for provider: ${provider}`); // Provider-managed OAuth sentinels without an accounts array are not legacy flat accounts. // Key this on the credential shape, not one provider id, so sibling managed lanes cannot be promoted. if (current.type === "oauth" && !Array.isArray(current.accounts) && current.access === current.refresh && current.access.endsWith("-managed")) { throw new Error(`Stored provider account not found: ${name}`); } return renameSlotDisplayName(current, name, displayName); }); emitProviderAccountsChanged(provider); } /** Pins one slot, or clears the pin when `name` is null. */ export async function pinCredentialAccount(storage, provider, name, env = process.env, repository) { const repo = repository ?? credentialPoolRepository(storage); if (name !== null) { assertValidAccountName(name); const accounts = await getCredentialAccounts(storage, provider, env, repo); const account = accounts.find((candidate) => candidate.name === name); if (!account) { throw new Error(`Provider account not found: ${name}`); } if (account.unused) { throw new Error(`Account ${name} holds a login of the other kind and is never used, so it cannot be pinned. To switch ${provider} to it, save it again with /login.`); } } await storage.modify(provider, async (current) => { if (current === undefined) { if (normalizeProviderId(provider) !== "anthropic-subscription" || name === null) { throw new Error(`No stored credential for provider: ${provider}`); } return pinSlot({ type: "oauth", ...SENTINEL_OAUTH_FIELDS, accounts: [] }, name); } if (name === null) { if (pinnedName(current) === undefined) return current; const { pinned: _pinned, ...unpinned } = { ...current, pinned: undefined }; return unpinned; } return pinSlot(current, name); }); // Subscribed clients re-read the list after any mutation; skipping this leaves // a desktop account picker showing a stale pin. emitProviderAccountsChanged(provider); } /** * Removes one stored slot. Env-backed accounts are refused: they are owned by * the environment, and deleting one here would silently disagree with the * variable that still defines it. */ export async function removeCredentialAccount(storage, provider, name, env = process.env, repository) { const repo = repository ?? credentialPoolRepository(storage); const accounts = await getCredentialAccounts(storage, provider, env, repo); const account = accounts.find((candidate) => candidate.name === name); if (!account) throw new Error(`Provider account not found: ${name}`); if (account.source === "env") { throw new Error(`Environment provider account cannot be removed: ${name}`); } const notice = await signOutAndDropSlot(storage, provider, name, env, repo); emitProviderAccountsChanged(provider); return notice === undefined ? {} : { notice }; } /** Drops one stored slot with its pin, order entry and pool sidecar state, from the credential as stored now. */ async function dropStoredSlot(storage, provider, name, repo) { const removed = removeSlot(await storage.read(provider), name); const remaining = removed === undefined ? undefined : withoutOrderedAccount(removed, name); if (remaining === undefined) await storage.delete(provider); else await storage.modify(provider, async () => remaining); await repo.mutateSlotState(provider, "stored", name, () => undefined); } async function pathExists(path) { try { await lstat(path); return true; } catch { return false; } } /** * The one CLI-safe removal of a stored slot, shared by every entry point (senpi#4436): an Anthropic Subscription * `claude-cli` slot is signed out and its dir removed first, then the slot is dropped. When the sign-out throws * (an abort, a filesystem error), the slot is kept with its dir, unless the dir was already removed, in which case * the slot is dropped too. If the drop itself then fails, the slot stays stored without its dir until the next * removal of it, which finds the dir gone and drops the slot. */ async function signOutAndDropSlot(storage, provider, name, env, repo, signal) { const credential = await storage.read(provider); if (credential === undefined) throw new Error(`No stored credential for provider: ${provider}`); let notice; if (normalizeProviderId(provider) === "anthropic-subscription") { const accountDir = claudeCliAccountDir(storage, credential, name); const dirExisted = accountDir !== undefined && (await pathExists(accountDir)); try { notice = await signOutClaudeCliSlot(storage, credential, name, env, signal); } catch (error) { if (dirExisted && !(await pathExists(accountDir))) await dropStoredSlot(storage, provider, name, repo); throw error; } } await dropStoredSlot(storage, provider, name, repo); return notice; } /** * A provider logout deletes every stored account at once. For Anthropic Subscription each `claude-cli` slot is first * signed out of the CLI's own store by its account dir, through the same per-slot path as a single removal (link * guard, never stuck on the CLI), so a logout never orphans a CLI sign-in (senpi#4436). Returns the notices of the * slots that could not be signed out cleanly; any other provider returns none. */ export async function signOutClaudeCliSlotsForLogout(storage, provider, env = process.env, signal, /** Sign out only this stored account (a one-account logout); every account when absent. */ slotName) { if (normalizeProviderId(provider) !== "anthropic-subscription") return []; const credential = await storage.read(provider); if (credential === undefined) return []; const repo = credentialPoolRepository(storage); const notices = []; let dropped = false; try { // Each account is dropped from the store as soon as it is signed out, so an abort or a failure on a later // account never leaves an earlier one stored without its dir. for (const slot of listSlots(credential)) { if (stringField(slot, "store") !== "claude-cli") continue; if (slotName !== undefined && slot.name !== slotName) continue; signal?.throwIfAborted(); const notice = await signOutAndDropSlot(storage, provider, slot.name, env, repo, signal); dropped = true; if (notice !== undefined) notices.push(notice); } } finally { if (dropped) emitProviderAccountsChanged(provider); } return notices; } /** The agent dir the account dirs live under: the auth file's dir, else the default agent dir. */ function claudeCliAgentDir(storage) { const authPath = storage.getStoragePath(); return authPath ? dirname(authPath) : getAgentDir(); } /** The account dir of a `claude-cli` slot with a valid dir id; undefined for any other slot. */ function claudeCliAccountDir(storage, credential, name) { const slot = listSlots(credential).find((candidate) => candidate.name === name); if (slot === undefined || stringField(slot, "store") !== "claude-cli") return undefined; const dirId = stringField(slot, "dirId"); return dirId !== undefined && isValidAccountDirId(dirId) ? accountDirFor(claudeCliAgentDir(storage), dirId) : undefined; } /** * A `claude-cli` slot is signed out of the CLI's own store and its account dir removed, so the caller * (`signOutAndDropSlot`) can drop the slot afterwards. Removal never gets stuck on the CLI: when `claude` cannot be * resolved or its sign-out fails, the dir is still removed, with a notice that the sign-out did not run. A slot that * names no valid account dir, or whose dir is a link or file, cannot be signed out safely; the caller drops it anyway * and the notice says so. */ async function signOutClaudeCliSlot(storage, credential, name, env, signal) { const slot = listSlots(credential).find((candidate) => candidate.name === name); if (slot === undefined || stringField(slot, "store") !== "claude-cli") return undefined; const dirId = stringField(slot, "dirId"); const agentDir = claudeCliAgentDir(storage); if (dirId === undefined || !isValidAccountDirId(dirId)) { return `Removed Claude account ${name} without a Claude sign-out: it names no valid account dir.`; } let executable; try { executable = resolveClaudeCodeExecutable({ ...defaultExecutableDeps(), env: (key) => env[key] }); } catch { executable = undefined; } const outcome = await removeAccountDir({ agentDir, dirId, executable, parentEnv: env, ...(signal === undefined ? {} : { signal }), }); if (outcome === "removed") return undefined; if (outcome === "removed_without_sign_out") { return `Removed Claude account ${name}, but the Claude CLI sign-out could not run; its stored sign-in may remain in the Claude CLI's credential store.`; } return `Removed Claude account ${name} without a Claude sign-out: its account dir is a link or a file, left untouched at ${accountDirFor(agentDir, dirId)}.`; } //# sourceMappingURL=credential-accounts.js.map