UNPKG

@code-yeongyu/senpi

Version:

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

163 lines • 8.3 kB
import { z } from "zod"; export declare const CREDENTIAL_POOL_STATE_FILENAME = "credential-pool-state.json"; /** Every slot field this engine owns; any other field on a slot was written by another engine version. */ export declare const KNOWN_SLOT_STATE_FIELDS: ReadonlySet<string>; /** * The slot fields this engine reads and writes. Declared, not inferred from the loose schema: an inferred * type carries the schema's `[k: string]: unknown` index signature, which turned every writer's * `Omit<CredentialSlotState, "stateVersion">` into an unchecked bag, so a wrong-typed field would be * written and then reset the whole file on the next read. Parsing stays loose; writing stays typed. */ export type CredentialSlotState = Readonly<{ stateVersion: number; blockedUntil?: number; blockReason?: "auth_error" | "rate_limit" | "account_disabled"; /** SIWC usage-limit pause; it has no known reset time, so the cooldown is only a probe interval. */ usageLimit?: true; failureCount?: number; lastSuccessAt?: number; credentialRevision?: string; lease?: { id: string; expiresAt: number; }; modelBlocks?: Record<string, { blockedUntil: number; }>; }>; declare const documentSchema: z.ZodObject<{ schemaVersion: z.ZodLiteral<1>; installationKey: z.ZodString; providers: z.ZodRecord<z.ZodString, z.ZodObject<{ lanes: z.ZodRecord<z.ZodString, z.ZodObject<{ slots: z.ZodRecord<z.ZodString, z.ZodObject<{ stateVersion: z.ZodNumber; blockedUntil: z.ZodOptional<z.ZodNumber>; blockReason: z.ZodOptional<z.ZodEnum<{ account_disabled: "account_disabled"; auth_error: "auth_error"; rate_limit: "rate_limit"; }>>; usageLimit: z.ZodOptional<z.ZodLiteral<true>>; failureCount: z.ZodOptional<z.ZodNumber>; lastSuccessAt: z.ZodOptional<z.ZodNumber>; credentialRevision: z.ZodOptional<z.ZodString>; lease: z.ZodOptional<z.ZodObject<{ id: z.ZodString; expiresAt: z.ZodNumber; }, z.core.$loose>>; modelBlocks: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{ blockedUntil: z.ZodNumber; }, z.core.$loose>>>; }, z.core.$loose>>; }, z.core.$loose>>; }, z.core.$loose>>; }, z.core.$loose>; export type CredentialPoolStateDocument = z.infer<typeof documentSchema>; /** The local block log (senpi#3216), kept beside the state file. */ export declare const BLOCK_LOG_FILENAME = "credential-pool-blocks.jsonl"; export declare function credentialPoolStatePath(agentDir?: string): string; /** The two keys a stored slot's health can be recorded under (senpi#4093). */ export type StoredSlotRevisions = { /** The account: survives a token refresh. Every verdict on the account is recorded under it. */ readonly account: string; /** The exact token material: a rejected-token verdict is recorded under it, so new material retires it. */ readonly material: string; }; /** * The stored health that applies to a slot's current material. A rejected token is a verdict on that * token only, so an `auth_error` entry applies while the material is unchanged. Every other verdict * (a limit, a billing refusal, a failure run) is on the account and survives a refresh. An entry written * before account keys existed is keyed by material and keeps applying until its next write. */ export declare function applicableStoredState(persisted: CredentialSlotState | undefined, revisions: StoredSlotRevisions): CredentialSlotState | undefined; export type SlotHealth = "ready" | "blocked" | "half_open"; /** Deadlines are absolute so a restart never re-enables a cooling credential early. */ export declare function slotHealth(state: CredentialSlotState | undefined, now: number): SlotHealth; /** * File-locked health sidecar for credential pools. Never stores credential * material: env slots are keyed by an installation-local HMAC revision so a * rotated env value clears its own stale block without persisting a raw key * hash anywhere. */ export declare class CredentialSlotRepository { private readonly path; private readonly lockWaitBudgetMs; constructor(path?: string, options?: { readonly lockWaitBudgetMs?: number; }); /** The local block log sits beside the state file, so a test or alternate agent dir keeps both together. */ blockLogPath(): string; /** * Reads take no lock: writes land by atomic rename, so the file always holds * one complete document. Only a missing or invalid file falls through to the * locked path, which creates or resets it (senpi#3285). When that lock stays * busy too, the read answers with an empty, unsaved document: health is * advisory, and its random installation key matches no stored revision, so * every slot simply reads as healthy for this one request. */ private readDocument; /** * The sidecar holds only health, so an unreadable or invalid document resets to * a fresh one instead of failing auth resolution; the worst outcome of a reset * is a cleared cooldown, never lost credential material. */ private withDocument; installationKey(): Promise<string>; /** * Derives the persistence key for an env slot's health. HMAC over the * installation key means the revision is useless outside this installation * and never equals a raw hash of the key material. */ envCredentialRevision(envVarName: string, envValue: string): Promise<string>; /** * The stored-lane twin of the env revision rule: binds a slot's health to * the material that earned it, so a re-login or token refresh replacing the * material retires the block. HMAC over the installation key, never raw * material (omo#7084). */ storedCredentialRevision(providerId: string, slotName: string, material: { key?: string; access?: string; refresh?: string; }): Promise<string>; /** * Both keys for a stored slot. The account key binds health to the account (provider, slot, the * identity the token names, or the API key), so a token refresh keeps it; the material key is * {@link storedCredentialRevision}. HMAC over the installation key, never raw material. */ storedSlotRevisions(providerId: string, slotName: string, material: { key?: string; access?: string; refresh?: string; }): Promise<StoredSlotRevisions>; listSlots(providerId: string, laneId: string): Promise<Record<string, CredentialSlotState>>; /** * Drops every health entry recorded for a provider and reports whether there was one. A clean * file is only read, never locked or written. Used when a provider's own lane becomes the only * owner of its account selection, so health the generic pool recorded earlier (often a block * pinned on the wrong account) cannot outlive the change (senpi#2081). */ clearProvider(providerId: string): Promise<boolean>; /** * Atomic read-modify-write for one slot's health. `stateVersion` increments * on every write so concurrent mutators can detect lost updates; returning * undefined removes the slot state entirely. */ mutateSlotState(providerId: string, laneId: string, slotId: string, fn: (current: CredentialSlotState | undefined) => Omit<CredentialSlotState, "stateVersion"> | undefined): Promise<CredentialSlotState | undefined>; } export type HalfOpenLease = { leaseId: string; expiresAt: number; }; /** * Transitions an expired cooldown atomically to half_open: exactly one caller * wins the probe lease; everyone else keeps treating the slot as unavailable * until the lease expires or the probe settles the block. */ export declare function acquireHalfOpenLease(repository: Pick<CredentialSlotRepository, "mutateSlotState">, providerId: string, laneId: string, slotId: string, options?: { now?: number; leaseTtlMs?: number; }): Promise<HalfOpenLease | undefined>; export {}; //# sourceMappingURL=state-store.d.ts.map