/** * Claude reads on the shared subscription core (design 5.3, Claude track * C2a): usage (EP-N24, EP-S13), catalog readiness (EP-T18, EP-S25, EP-N26) * and organization custom-model admission. Every entry point is reached only * after the caller read the organization's Claude provider route as `core`; * the Claude binding is looked up in the registry, which fails closed until * the Claude cutover (C3) registers it. * * Legacy model ids keep their meaning on the core: an * `organization-claude-subscription/` model is served by the organization * inference pool, a `workspace-claude-subscription/` model by the workspace * pool or the subject's own personal connection in their own Personal * workspace. */ import type { Settings } from "@opengeni/config"; import type { ClaudeSubscriptionUsage } from "@opengeni/contracts"; import { type Database } from "./database.js"; import { type SubscriptionCoreConnectionRow } from "./subscription-core/administration.js"; import { type SubscriptionCoreConnectionProbeDeps } from "./subscription-core/operation-connections.js"; /** The legacy refresh throttle: one live usage read per connection per 30 seconds. */ export declare const SUBSCRIPTION_CORE_CLAUDE_USAGE_REFRESH_INTERVAL_MS = 30000; /** A Claude connection one usage route may read, with the legacy authority scope it maps to. */ export type SubscriptionCoreClaudeUsageTarget = { connectionId: string; /** * `workspace` and `organization`: a shared connection of the workspace's * pool by its source; `user`: the caller's own personal connection. */ scope: "workspace" | "organization" | "user"; row: SubscriptionCoreConnectionRow; }; /** * The Claude connection a workspace usage route names (a canonical id or a * cutover alias), when the caller may see it here: a shared connection of * the workspace's effective pool, or the caller's own personal connection * in their own Personal workspace (the owner-only reader). Null otherwise. * The row carries its row version (shared reads select it; a personal row * gets it from the owner-only version reader, and is not returned without * one). */ export declare function readSubscriptionCoreClaudeWorkspaceUsageTarget(db: Database, input: { accountId: string; workspaceId: string; subjectId: string; accountIdParam: string; }): Promise; /** * The organization's own Claude connection an organization usage route * names, for an organization administrator (null for anyone else, and for * a connection outside the organization pool). */ export declare function readSubscriptionCoreClaudeOrganizationUsageRow(db: Database, input: { organizationId: string; subjectId: string; accountIdParam: string; }): Promise; /** * Every Claude product model the workspace's catalog lists now (the * organization's Claude models and the workspace's own), passed to the * usage decoder so a model-family window cools each listed model. */ export declare function listSubscriptionCoreClaudeCatalogModelIds(db: Database, input: { accountId: string; workspaceId: string; }): Promise; /** Forget remembered attempts (tests). */ export declare function resetSubscriptionCoreClaudeUsageThrottle(): void; /** * A live usage refresh of one Claude connection a usage route may refresh: * a shared connection of the workspace's pool through the core connection * seam (workspace scope), or the caller's own personal connection in their * own Personal workspace through the owner-only seam (design 5.3, C2c). * Returns the connection's projected usage after the read and whether the * reading ended a stored exhaustion (the caller wakes the Claude core * waiters; a failed wake never fails the committed read). * * Throttle (legacy: one read per 30 seconds): a stored usage-endpoint * reading younger than the interval answers first; else this replica's * remembered attempt, when it is younger than the interval and newer than * the stored reading. A reading the core did not apply (the generation * fence, or a failed write) is reported `unavailable`, never `available`; * one a newer stored observation superseded (a turn's response headers that * arrived meanwhile) is a successful check of an account whose stored quota * is at least as current. Every check that ends `unavailable` is logged with * its cause (`logClaudeUsageCheckFailure`). * `deps.force` reads live whatever the throttle says: a limit reset claim * has just changed the provider's answer. */ export declare function refreshSubscriptionCoreClaudeWorkspaceUsage(db: Database, settings: Settings, input: { accountId: string; workspaceId: string; subjectId: string; target: SubscriptionCoreClaudeUsageTarget; }, deps?: Pick & { now?: () => number; force?: boolean; }): Promise<{ usage: ClaudeSubscriptionUsage; recovered: boolean; }>; /** * A live usage refresh of one organization-pool Claude connection on the * organization route, for an organization administrator, through the * administrator seam (design 5.3, C2c). The same throttle, projection and * wake rule as the workspace path. The decoder knows the organization's * Claude models (the organization route has no workspace catalog; a * workspace's own models get their cooldowns from that workspace's reads, * and a stored cooldown is never shortened by a later reading). When the * administrator's model list cannot be read, nothing is read live. * `deps.force` reads live whatever the throttle says. */ export declare function refreshSubscriptionCoreClaudeOrganizationUsage(db: Database, settings: Settings, input: { organizationId: string; subjectId: string; row: SubscriptionCoreConnectionRow; }, deps?: { fetchImpl?: SubscriptionCoreConnectionProbeDeps["fetchImpl"]; now?: () => number; force?: boolean; }): Promise<{ usage: ClaudeSubscriptionUsage; recovered: boolean; }>; /** * Claude catalog readiness on the core. One answer for every Claude * subscription model id form (docs/design/subscription-model-ids-without-pools.md): * the forms name the same models, served by the same connections, so each * field below repeats the same rule under the name its callers read. */ export type SubscriptionCoreClaudeCatalogReadiness = { /** `workspace-claude-subscription/` models: some serving connection here. */ workspace: boolean; /** `organization-claude-subscription/` models: the same answer. */ organization: boolean; /** * Each id form's model restriction: the union of the serving allowlists * in every id form (null: every model), the same under every key. */ restrictions: { readonly "claude/": readonly string[] | null; readonly "organization-claude-subscription/": readonly string[] | null; readonly "workspace-claude-subscription/": readonly string[] | null; }; }; /** Not ready: what maintenance answers (deeply frozen; shared by every answer). */ export declare const SUBSCRIPTION_CORE_CLAUDE_NOT_READY: SubscriptionCoreClaudeCatalogReadiness; /** * Claude readiness for new work of `subjectId` in this workspace on the core: * the core eligibility projection (shared connections reaching the * workspace; the subject's own personal connections only in their own * Personal workspace), the same connections placement considers. Every id * form gets the same answer; who manages a connection is not read. Frozen * v1 Claude snapshots are not read. */ export declare function readSubscriptionCoreClaudeCatalogReadiness(db: Database, input: { accountId: string; workspaceId: string; subjectId: string | null; }): Promise; /** * Organization custom-model admission on the core: a connection that serves * this workspace for the established subject (the same connections catalog * readiness counts) and allows the model, in any id form, admits it. A * subject without a Personal workspace here (a service actor, a borrowed * subject) sees shared capacity only; no session creator is read or * inferred. */ export declare function subscriptionCoreClaudeOrganizationModelAdmitted(db: Database, input: { accountId: string; workspaceId: string; upstreamModelId: string; subjectId: string | null; }): Promise; /** * The carrier whose accepted Claude authority an admission re-reads on the * core, when the work it admits was accepted before this request: * * - `session_initial`: a new session's initial authority (session creation); * - `session_turn`: an edit's source turn, whose authority the edit copies; * - `receiving_session`: agent-submitted work, which takes the receiving * session's accepted source (its execution-context turn, else its latest * turn, else its initial authority); * - `scheduled_task`: a scheduled-task update. * * A human's own new work and a service actor's work carry none: they * resolve the workspace's current source. */ export type SubscriptionCoreClaudeAdmissionCarrier = { kind: "session_initial"; sessionId: string; } | { kind: "session_turn"; turnId: string; } | { kind: "receiving_session"; sessionId: string; } | { kind: "scheduled_task"; scheduledTaskId: string; }; /** * Whether a carrier's accepted Claude authority (0716's * `read_subscription_authority_compat`) lets the core admit an organization * model for it: * * - a compatibility record (a carrier accepted before the cutover) admits * when it accepted shared capacity at all (`sharedPool` `workspace` or * `organization`): both pools are now the same shared connections that * reach the workspace, so the pool it named no longer narrows them; * - a carrier accepted after the cutover (`v2` or `none`: it has no record) * follows the same source rule as new work; * - anything else (not visible, `missing`, `v1`, an unknown answer, or a * database without the reader) admits nothing. */ export declare function subscriptionCoreClaudeAcceptedAuthorityAdmitsOrganization(compat: unknown): boolean; /** * Claude organization custom-model admission on the core route (the * `claude_subscription` branch of * `lockActiveOrganizationModelProviderCustomModelForAdmission`), inside the * caller's workspace-scoped transaction: * * - the transaction's subject must be established by the admission caller * (as on the legacy route); it is never replaced by a session creator and * no person is inferred for a service actor; * - work accepted before this request (a carrier: a new session's initial * authority, an edit's source turn, the receiving session of * agent-submitted work, a scheduled task) first reads that accepted * authority (`subscriptionCoreClaudeAcceptedAuthorityAdmitsOrganization`); * without the reader nothing is admitted; * - then a connection serving this workspace for that subject must allow * the model (`subscriptionCoreClaudeOrganizationModelAdmitted`). Work with * a carrier keeps its own accepted personal authority, which this check * does not re-read, so it counts shared connections only, as before. */ export declare function subscriptionCoreClaudeCustomModelAdmitted(tx: Database, input: { accountId: string; workspaceId: string; upstreamModelId: string; carrier: SubscriptionCoreClaudeAdmissionCarrier | null; }): Promise;