UNPKG

@code-yeongyu/senpi

Version:

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

180 lines • 8.45 kB
/** * Polls each signed-in subscription account's usage endpoint into the process's limit store (senpi#3684), * so `get_subscription_limits`, the `subscription_limits` record and `pi.getSubscriptionLimits()` carry an * account's windows before its first turn. Turns keep reporting through the same store (response headers, * the Claude SDK `rate_limit_event`); a poll replaces an account's window set, a turn updates windows in it. * Every process on the machine shares one cache file, so a machine makes one request per account per * interval however many sessions are open. */ import { randomUUID } from "node:crypto"; import { join } from "node:path"; import { AuthStorage } from "../auth-storage.js"; import { subscriptionLimits } from "./limit-store.js"; import { listPollAccounts, SIGN_IN_NEEDED } from "./usage-accounts.js"; import { claimedBy, isDue, readUsageCache, usageKey, withClaims, withResults, writeUsageCache, } from "./usage-cache.js"; import { fetchUsageJson, readUsage, UsageHttpError } from "./usage-endpoints.js"; /** Claude's numbers move in five-hour blocks and its endpoint is the stricter one, so it is read half as often. */ export const POLL_INTERVAL_MS = { "anthropic-subscription": 300_000, "chatgpt-subscription": 150_000, }; const TICK_MS = 30_000; /** A person's turn on a held provider re-reads its accounts' usage at most this often (senpi#6536). */ const REFRESH_MIN_AGE_MS = 60_000; const MAX_BACKOFF_MS = 60 * 60_000; /** Intervals to wait before asking again about a login only `/login` can fix. */ const SIGNED_OUT_INTERVALS = 5; export class UsagePoller { #deps; /** The cached reading each account last handed the store, so an unchanged reading is not re-announced. */ #adopted = new Map(); #running = false; constructor(deps) { this.#deps = deps; } /** * One pass: adopt what siblings already read, fetch what is due and claimed by this process, publish. `refresh` * also makes that provider's accounts due when their reading is at least REFRESH_MIN_AGE_MS old (senpi#6536). */ async pollOnce(signal = new AbortController().signal, refresh) { if (this.#running) return; this.#running = true; try { const accounts = new Map((await this.#deps.listAccounts()).map((account) => [keyOf(account), account])); const path = this.#deps.cachePath; const before = readUsageCache(path); this.#adopt(before, accounts); const now = this.#deps.now(); const due = [...accounts].flatMap(([key, account]) => isDue(before, key, now) || (account.provider === refresh?.provider && isDue(before, key, now, REFRESH_MIN_AGE_MS)) ? [key] : []); if (due.length === 0) return; const token = randomUUID(); writeUsageCache(path, withClaims(before, due, now, token)); const owned = claimedBy(readUsageCache(path), due, token); const results = new Map(); await Promise.all(owned.flatMap((key) => { const account = accounts.get(key); if (account === undefined) return []; return [this.#read(account, before.accounts[key], signal).then((reading) => results.set(key, reading))]; })); const after = withResults(readUsageCache(path), results, token); writeUsageCache(path, after); this.#adopt(after, accounts); } finally { this.#running = false; } } #adopt(cache, accounts) { for (const [key, account] of accounts) { const reading = cache.accounts[key]; if (reading === undefined || reading.pending === true || this.#adopted.get(key) === reading.updatedAt) continue; this.#adopted.set(key, reading.updatedAt); const reportedAt = new Date(reading.updatedAt); this.#deps.store.report(account.provider, account.account, reading.error !== undefined ? { error: reading.error, reportedAt } : { windows: reading.windows, ...(reading.plan === undefined ? {} : { plan: reading.plan }), reportedAt, }); } } async #read(account, previous, signal) { const interval = POLL_INTERVAL_MS[account.provider]; const now = this.#deps.now(); const credential = await account.access(signal); if (credential === undefined) { // Nothing to ask with yet: keep what is known and look again next interval. return { ...(previous ?? { windows: [], updatedAt: now, pending: true }), nextAt: now + interval }; } if ("error" in credential) { return { windows: [], error: credential.error, updatedAt: now, nextAt: now + SIGNED_OUT_INTERVALS * interval }; } try { const reading = await readUsage(account.provider, credential.access, this.#deps.fetch); return { ...reading, updatedAt: now, nextAt: now + interval }; } catch (error) { const failure = describeFailure(error, interval); return { windows: [], error: failure.error, updatedAt: now, nextAt: now + Math.min(failure.waitMs, MAX_BACKOFF_MS), }; } } } function keyOf(account) { return usageKey(account.provider, account.account); } /** A 429 with `retry-after: 0` means the window is about to roll, so only a positive header earns a long wait. */ function describeFailure(error, interval) { if (error instanceof UsageHttpError) { if (error.status === 401 || error.status === 403) return { error: SIGN_IN_NEEDED, waitMs: SIGNED_OUT_INTERVALS * interval }; if (error.status === 429) return { error: "rate limited", waitMs: error.retryAfterMs ?? interval }; return { error: `HTTP ${error.status}`, waitMs: error.retryAfterMs ?? 2 * interval }; } const message = error instanceof Error ? error.message : String(error); return { error: /abort|timeout|timed out/i.test(message) ? "timed out" : "unreachable", waitMs: 2 * interval }; } let running; /** * Re-reads `provider`'s account usage now instead of at the next interval, so a limit the provider lifted early * shows on the next turn a person starts (senpi#6536). A no-op where nothing polls (print mode, `--offline`, a * session isolate); a failure keeps the last reading and never fails the turn. */ export async function refreshSubscriptionUsage(provider) { if (running === undefined || (provider !== "anthropic-subscription" && provider !== "chatgpt-subscription")) return; await running.refresh(provider); } /** * Starts the process's poller once (interactive and RPC modes; never print mode or `--offline`). The timer * is unref'd, so it never holds the process open; `report` receives a pass that failed as a whole. */ export function startSubscriptionUsagePolling(options) { if (running !== undefined) return running; const storage = AuthStorage.create(join(options.agentDir, "auth.json")); const poller = new UsagePoller({ store: subscriptionLimits, cachePath: join(options.agentDir, "subscription-usage.json"), listAccounts: () => listPollAccounts({ store: storage, env: (name) => process.env[name], now: Date.now, agentDir: options.agentDir, }), fetch: fetchUsageJson, now: Date.now, }); const controller = new AbortController(); const tick = () => void poller.pollOnce(controller.signal).catch((error) => { options.report(`senpi subscription limits: usage poll failed: ${String(error)}`); }); tick(); const timer = setInterval(tick, TICK_MS); timer.unref(); running = { stop() { controller.abort(); clearInterval(timer); running = undefined; }, refresh: (provider) => poller.pollOnce(controller.signal, { provider }).catch((error) => { options.report(`senpi subscription limits: usage refresh failed: ${String(error)}`); }), }; return running; } //# sourceMappingURL=usage-poller.js.map