@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
93 lines • 4.31 kB
JavaScript
import { AUTH_BLOCK_REPROBE_MS } from "../../../credential-pool/auth-reprobe.js";
import { logContinuityEvent } from "./session-observability.js";
/** The re-probe time passed (or the block predates re-probe times): the next request probes this account. */
function isProbeDue(slot, now) {
return slot.blockedUntil === undefined || slot.blockedUntil <= now;
}
/**
* A `login_expired` slot keeps its `auth_error` block as the self-heal cadence, so it is probed like any auth block.
* That cadence survives `/claude-account unblock` only because `unblockProviderAccount` skips `auth_error` blocks
* (account-management.ts:114); if unblock ever lifts them, a `login_expired` slot is never re-probed again.
*/
function isClaudeCliProbeDue(slot, now) {
return (slot.store === "claude-cli" &&
slot.blockReason === "auth_error" &&
(slot.signInRequired === undefined || slot.signInRequired === "login_expired") &&
isProbeDue(slot, now));
}
function probedSlot(stored, state, now) {
if (state === "signed_in") {
const { blockReason: _reason, blockedUntil: _until, blockedAt: _at, signInRequired: _sign, authVerdict: _verdict, ...unblocked } = stored;
return unblocked;
}
return {
...stored,
...(state === "signed_out" ? { signInRequired: "login_expired" } : {}),
blockReason: "auth_error",
blockedUntil: now + AUTH_BLOCK_REPROBE_MS,
blockedAt: now,
// The verdict names the sign-in it is about, so a block another engine writes is told apart (senpi#5623).
authVerdict: { dirId: stored.dirId, at: now },
};
}
function transitionOf(stored, state) {
if (state === "signed_out" && stored.signInRequired === undefined)
return "claude_cli_login_expired";
if (state === "signed_in" && stored.signInRequired === "login_expired")
return "claude_cli_login_restored";
return undefined;
}
async function probeClaudeCliSlot(input, slot, status) {
const state = await status(slot);
input.signal.throwIfAborted();
let transition;
await input.store.modify(input.providerId, async (current) => {
transition = undefined;
if (current?.type !== "oauth")
return current;
const credential = current;
const stored = credential.accounts?.find((candidate) => candidate.name === slot.name);
if (!stored || !isClaudeCliProbeDue(stored, input.now))
return current;
transition = transitionOf(stored, state);
const next = probedSlot(stored, state, input.now);
return {
...credential,
accounts: (credential.accounts ?? []).map((candidate) => (candidate.name === slot.name ? next : candidate)),
};
});
// Logged once the write landed: a store that retries the callback must not log the transition twice.
if (transition !== undefined)
logContinuityEvent(transition, { accountName: slot.name });
}
/**
* Re-probes before a turn: a claude CLI account whose auth block is due for its re-probe (senpi#3264) asks
* `claude auth status`; a login clears the block and a confirmed sign-out marks the login expired (senpi#5350).
*/
export async function recoverAuthBlockedSlots(input) {
const status = input.claudeCliStatus;
const probes = status === undefined ? [] : input.accounts.filter((slot) => isClaudeCliProbeDue(slot, input.now));
if (probes.length === 0)
return [...input.accounts];
let transientFailure;
for (const probe of probes) {
if (status === undefined)
break;
try {
await probeClaudeCliSlot(input, probe, status);
}
catch (error) {
input.signal.throwIfAborted();
transientFailure = error;
}
}
const accounts = await input.reload();
// A due `login_expired` slot counts as usable here (its block time has passed), so a thrown probe on a pool of
// only such slots does not throw `transientError`: the lane then drops them and throws the sign-in error, which
// stops retries and model hops.
if (transientFailure !== undefined && !accounts.some(input.isUsable)) {
throw input.transientError(transientFailure);
}
return accounts;
}
//# sourceMappingURL=auth-block-recovery.js.map