import { SubscriptionPersonalAuthorityV2 } from "@opengeni/contracts"; import { type ModelDescriptor, type PlacementInput, type ReselectionPoint } from "@opengeni/subscriptions"; import { type Database } from "./database.js"; import { type SubscriptionCoreProvider } from "./subscription-core/provider.js"; export type SubscriptionCorePlacementWorldRequest = { accountId: string; workspaceId: string; sessionId: string; turnId: string; sessionOwnerSubjectId: string | null; sessionOwnerMembershipId: string | null; initiatingHumanSubjectId: string | null; /** Parsed immutable v2 value read from this exact accepted turn, never recomputed. */ acceptedAuthorityV2: SubscriptionPersonalAuthorityV2; preferredModelId: string; reasoningLevel: string; models: readonly ModelDescriptor[]; reselectionPoints: readonly ReselectionPoint[]; now: Date; /** * Read the session binding under its row lock (`FOR UPDATE`), held until * the transaction ends: placement decides from, and commits to, the * binding no pin or other placement can change meanwhile (SUB-STICK-02). * Taken before any per-connection lock (lease, request), the order a pin * also uses: binding row, then the connection. */ lockBinding?: boolean; }; export type SubscriptionCorePlacementWorldResult = { status: "not_visible"; } | { status: "completed"; value: T; }; export type SubscriptionCoreAcceptedTurnIdentity = Pick; export type SubscriptionCoreAcceptedTurnAccessResult = { status: "not_visible"; } | { status: "completed"; value: T; }; export type SubscriptionCoreRefreshResult = { status: "not_visible"; } | { status: "lease_lost"; } | { status: "refused"; } | { status: "completed"; value: T; }; /** The credential a refresh callback rotates, read under the refresh lock. */ export type SubscriptionCoreRefreshCredential = { refreshGeneration: number; credentialEncrypted: string; expiresAt: Date | null; }; /** * Load one provider's placement world and run the caller's placement/lease * operation in the same tenant- and session-scoped transaction. Database * authorization functions establish access from the exact accepted turn; * callers cannot use this as a general account or session reader. */ export declare function withSubscriptionCoreProviderPlacementWorld(db: Database, provider: SubscriptionCoreProvider, request: SubscriptionCorePlacementWorldRequest, operation: (tx: Database, input: PlacementInput) => Promise): Promise>; /** * Reusable exact-turn gate for provider-neutral placement and secret-bearing * operations. Call it inside the caller's already tenant/session-scoped RLS * transaction; a successful result proves both the DB capability and the * request's session-owner tuple match the immutable accepted turn. */ export declare function assertSubscriptionCoreAcceptedTurn(tx: Database, request: SubscriptionCoreAcceptedTurnIdentity): Promise; /** * Establish the core service actor and exact tenant/session RLS transaction, * then run an operation only for the immutable accepted turn. Secret-bearing * consumers should use this wrapper rather than opening workspace-only RLS. */ export declare function withSubscriptionCoreAcceptedTurn(db: Database, request: SubscriptionCoreAcceptedTurnIdentity, operation: (tx: Database) => Promise): Promise>; /** * Serialize one connection's credential refresh under the canonical * per-connection lock, while requiring both exact accepted-turn access and * its live lease generation in the same RLS transaction. * * Authorization happens once, before the callback's provider call, through * begin_subscription_core_refresh. The callback receives the credential to * rotate and must call persistSubscriptionCoreRefresh as soon as the * provider returns, before any other fallible work: a rolled-back * transaction discards the rotated credential. Persistence no longer depends * on the lease or visibility surviving the provider call. */ export declare function withSubscriptionCoreRefreshLock(db: Database, provider: SubscriptionCoreProvider, request: SubscriptionCoreAcceptedTurnIdentity & { connectionId: string; holderId: string; generation: number; }, operation: (tx: Database, credential: SubscriptionCoreRefreshCredential) => Promise): Promise>;