import type { AgentEventSource, AgentIdentity, CheckpointStore, ProductionPersistenceStore } from "../contracts.js"; /** * Narrow router-state seam (structurally satisfied by * `@arnilo/prism-model-router`'s `ModelRouterStateStore`). Core stays * dependency-free; adapter packages pass their real stores. */ export interface StateConcurrencyRouterKey { readonly tenantId: string; readonly accountId?: string; readonly userId?: string; readonly principalId: string; readonly provider: string; readonly model: string; } export interface StateConcurrencyRouterStore { reserveBudget(input: { readonly key: StateConcurrencyRouterKey; readonly tokens?: number; readonly costUsd?: number; readonly maxTokens?: number; readonly maxCostUsd?: number; readonly windowMs: number; readonly reservationTtlMs: number; readonly now: number; readonly maxBudgetKeys?: number; }): Promise<{ readonly admitted: boolean; readonly reservationId?: string; readonly fencingToken?: string; readonly retryAfterMs?: number; }>; commitBudget(input: { readonly key: StateConcurrencyRouterKey; readonly reservationId: string; readonly fencingToken: string; readonly tokens?: number; readonly costUsd?: number; readonly windowMs: number; readonly now: number; }): Promise<{ readonly unknownUsage: boolean; }>; releaseBudget(input: { readonly key: StateConcurrencyRouterKey; readonly reservationId: string; readonly fencingToken: string; readonly windowMs: number; readonly now: number; }): Promise; readBudget(input: { readonly key: StateConcurrencyRouterKey; readonly windowMs: number; readonly now: number; }): Promise<{ readonly tokens: number; readonly costUsd: number; }>; } export interface StateConcurrencyRouterFactory { readonly create: () => StateConcurrencyRouterStore | Promise; /** * True when the store derives reservation expiry from the caller-supplied * `now` (in-memory stores). Durable stores compute expiry from their own * clock (`clock_timestamp()`), so the deterministic unknown-outcome probe * only runs when this is set; the durable leg is exercised by the Task 1 * enterprise-conformance integration probe (real 1ms TTL, `test:postgres`). */ readonly nowInjected?: boolean; } /** Narrow idempotency seam (structurally satisfied by `@arnilo/prism-work-tools`'s `IdempotencyStore`). */ export interface StateConcurrencyIdempotencyStore { get(input: StateConcurrencyIdempotencyKey): Promise; begin(input: StateConcurrencyIdempotencyKey): Promise<{ readonly outcome: "acquired" | "existing"; readonly record: StateConcurrencyIdempotencyRecord; }>; complete(input: StateConcurrencyIdempotencyKey & { readonly claimToken: string; readonly expectedVersion: number; readonly result: { readonly draftId: string; readonly resourceId?: string; }; }): Promise; } export interface StateConcurrencyIdempotencyKey { readonly identity: AgentIdentity; readonly key: string; readonly op: string; readonly signal?: AbortSignal; } export interface StateConcurrencyIdempotencyRecord { readonly tenantId?: string; readonly accountId?: string; readonly userId?: string; readonly principalId: string; readonly key: string; readonly op: string; readonly status: "in_progress" | "completed" | "failed_retryable" | "failed_terminal" | "unknown"; readonly attempt: number; readonly version: number; readonly claimToken?: string; readonly result?: { readonly draftId: string; readonly resourceId?: string; }; readonly failure?: { readonly code: string; readonly reference?: string; }; readonly createdAt: string; readonly updatedAt: string; readonly expiresAt?: string; } export type StateConcurrencyEventSource = AgentEventSource & { readonly close?: () => void | Promise; }; export interface StateConcurrencyEventSourceFactory { readonly create: () => StateConcurrencyEventSource | Promise; /** * True when the factory can re-open against the same backend (durable * stores). Memory stores are process-local; their probe covers cursor * resume on the same instance and durable factories additionally re-open. */ readonly reopenable?: boolean; } export interface StateConcurrencyFactories { /** Checkpoint seam: approval-determinism and checkpoint-CAS probes. */ readonly checkpoints?: () => CheckpointStore | Promise; /** Durable event seam: replay-cursor resume probe (memory and NATS/Postgres). */ readonly events?: StateConcurrencyEventSourceFactory; /** Session-record seam: conversation metadata CAS probe (appendSession). */ readonly sessions?: () => ProductionPersistenceStore | Promise; /** Work idempotency seam: retry-same-key probe. */ readonly idempotency?: () => StateConcurrencyIdempotencyStore | Promise; /** Router budget reservation seam: oversubscription + unknown-outcome probes. */ readonly routerState?: StateConcurrencyRouterFactory; } /** * Run the state-concurrency probes for every provided store family. Returns * the executed probe names so gates can assert coverage. Deterministic: * concurrent ops are awaited through `Promise.allSettled` and state is * asserted afterward; no timing-only sleeps anywhere in this file. */ export declare function assertStateConcurrencyConforms(factories: StateConcurrencyFactories): Promise;