import { VaultErrorCode, type SecretPrincipal, type SecretSaveTo, type VaultFieldKind, type VaultItemType, type VaultMode, type VaultOwnerKind, type VaultPolicy, type VaultProviderOperations, type VaultSource, type VaultSyncStatus, type VaultVersionCreatedByKind } from "@opengeni/contracts"; import { type Database } from "./database.js"; export type VaultDbContext = { kind: "subject"; accountId: string; workspaceId: string; subjectId: string; } | { kind: "principal"; principal: SecretPrincipal; }; /** A vault routine refused the call; `code` is the API error code. */ export declare class VaultError extends Error { readonly code: VaultErrorCode; readonly details?: Record | undefined; readonly name = "VaultError"; constructor(code: VaultErrorCode, message: string, details?: Record | undefined); } export declare function isVaultError(error: unknown): error is VaultError; /** Map a database error raised by a vault routine to VaultError; null otherwise. */ export declare function vaultErrorFromDatabase(error: unknown): VaultError | null; /** * What a provider (or its bridge) said about its own upstream freshness at * the last successful check (0789). Opengeni's own check time is * `lastSyncedAt`; it never stands in for `sourceSyncedAt`. */ export type StoredProviderFreshness = { source: "live" | "cache" | "unknown"; sourceSyncedAt: string | null; }; export type VaultRow = { id: string; workspaceId: string; ownerKind: VaultOwnerKind; ownerLabel: string | null; rootSessionId: string | null; name: string; description: string | null; source: VaultSource; mode: VaultMode; syncStatus: VaultSyncStatus; syncErrorCode: string | null; syncErrorMessage: string | null; lastSyncedAt: string | null; rateLimitedUntil: string | null; providerConnectionId: string | null; providerVaultRef: string | null; /** Effective operations the provider last reported (0789); null before the first check. */ providerOperations?: VaultProviderOperations | null; /** The provider's own freshness observation from the last successful check (0789). */ providerFreshness?: StoredProviderFreshness | null; /** * Durable value-cache epoch (0789, bigint as text). Every complete mirror * replacement advances it; provider value caches compare it before reuse. */ providerCacheEpoch?: string; availability: { allowedWorkspaceIds: string[] | null; allowPersonalWorkspaces: boolean; } | null; isDefault: boolean; itemCount: number; /** A user vault owned by the caller's membership. */ ownedByCaller: boolean; generation: number; createdBySubjectId: string | null; createdAt: string; updatedAt: string; /** Lookup tier: 0 task, 1 Only me, 2 workspace, 3 organization. */ tier: number; }; export type VaultFieldRow = { id: string; name: string; kind: VaultFieldKind; position: number; currentVersion: number | null; providerFieldRef: string | null; fileName: string | null; fileSize: number | null; updatedAt: string; /** Current ciphertext for text fields only (all text fields on item reads; username/rpId/credentialId on lists). */ valueEncrypted: string | null; }; export type VaultItemRow = { id: string; vaultId: string; vaultName: string; vaultOwnerKind: VaultOwnerKind; vaultSource: VaultSource; vaultMode: VaultMode; type: VaultItemType; /** Provider-specific kind of item (0956); null for the plain type. */ category: string | null; name: string; nameKey: string; websites: string[]; /** Encrypted provider display hint; never a locally resolvable field value. */ usernameHintEncrypted?: string | null; expiresAt: string | null; archivedAt: string | null; lastUsedAt: string | null; updatedAt: string; createdAt: string; createdBySubjectId: string | null; generation: number; primaryFieldId: string | null; passkeySignCount: number | null; passkeyLeasedUntil: string | null; /** * Passkeys a connected item holds (0986), without key material; null when it * holds none or for hosted items. Absent on rows from older servers. */ providerPasskeys?: VaultProviderPasskeyRow[] | null; aliases: string[]; fields: VaultFieldRow[]; }; /** One passkey a connected item holds, as mirrored (0986). */ export type VaultProviderPasskeyRow = { /** base64url without padding. */ credentialId: string; rpId: string; userName: string | null; }; export type VaultFieldCiphertext = { valueEncrypted: string | null; version: number | null; currentVersion: number | null; kind: VaultFieldKind; fieldId: string; fieldName: string; fileName: string | null; itemId: string; itemName: string; itemType: VaultItemType; vaultId: string; vaultName: string; vaultOwnerKind: VaultOwnerKind; source: VaultSource; providerConnectionId: string | null; providerVaultRef: string | null; /** The vault's value-cache epoch (0789). */ providerCacheEpoch?: string; providerItemRef: string | null; providerFieldRef: string | null; }; export type VaultFieldVersionRow = { version: number; current: boolean; createdAt: string; createdByKind: VaultVersionCreatedByKind; createdBySubjectId: string | null; }; /** A parsed reference target as the lookup routine takes it. */ export type SecretReferenceLookup = { index: number; form: "path" | "id"; segments?: string[]; itemId?: string; field?: string | null; otp: boolean; previous: boolean; }; export type SecretReferenceCandidateInterpretation = "item" | "item_field" | "vault_item" | "vault_item_field" | "id"; export type SecretReferenceCandidate = { refIndex: number; interpretation: SecretReferenceCandidateInterpretation; tier: number; matchedBy: "name" | "alias" | "id"; vaultId: string; vaultName: string; vaultOwnerKind: VaultOwnerKind; source: VaultSource; providerConnectionId: string | null; providerVaultRef: string | null; /** The vault's value-cache epoch (0789). */ providerCacheEpoch?: string; itemId: string; itemName: string; itemType: VaultItemType; archived: boolean; providerItemRef: string | null; fieldId: string | null; fieldName: string | null; fieldKind: VaultFieldKind | null; fileName: string | null; providerFieldRef: string | null; currentVersion: number | null; version: number | null; valueEncrypted: string | null; /** Why no value: the named field, the TOTP field, the primary field, the previous version or the value is absent. */ missing: "field" | "totp_field" | "primary_field" | "previous_version" | "value" | null; }; export type SecretResolutionMeta = { kind: SecretPrincipal["kind"] | "subject"; subjectId: string | null; humanExpected: boolean; humanValid: boolean; hasSecretsUse: boolean; agentAllowed: boolean; includeUser: boolean; includeShared: boolean; policyVaultIds: string[] | null; reachableVaultCount: number; }; export type SessionSecretsViewRow = { policy: VaultPolicy; effectiveVaultIds: string[]; narrowedBySessionId: string | null; taskVault: VaultRow | null; taskItems: VaultItemRow[]; reachableVaults: Array; }; export type VaultFieldWrite = { fieldId?: string; name: string; newName?: string; kind?: VaultFieldKind; valueEncrypted?: string; /** Plaintext size in bytes, checked against the text and file limits. */ valueSize?: number; restoreVersion?: number; remove?: true; fileName?: string; }; export type VaultItemWrite = { name?: string; type?: VaultItemType; /** A category token, or null to clear it (0956). */ category?: string | null; websites?: string[]; expiresAt?: string | null; primaryFieldName?: string; expectedGeneration?: number; createdByKind?: VaultVersionCreatedByKind; passkeySignCount?: number; fields?: VaultFieldWrite[]; }; export declare function listVaultsForSubject(db: Database, context: VaultDbContext, input: { workspaceId: string; }): Promise; /** * Every organization vault, wherever it works (migration 1218), for the * people who manage organization vaults; the caller checks organization * administration (the owner or an admin). * Summaries only: a vault's items still need it to reach the workspace. */ export declare function listOrganizationVaults(db: Database, context: VaultDbContext, input: { workspaceId: string; }): Promise; export declare function getVault(db: Database, context: VaultDbContext, input: { vaultId: string; }): Promise; export declare function createVault(db: Database, context: VaultDbContext, input: { name: string; description?: string | null; ownerKind: "user" | "workspace" | "organization"; availability?: { allowedWorkspaceIds: string[] | null; allowPersonalWorkspaces: boolean; }; /** The caller administers the organization (the owner or an admin). */ allowOrganization?: boolean; }): Promise; export declare function updateVault(db: Database, context: VaultDbContext, input: { vaultId: string; expectedGeneration: number; name?: string; description?: string | null; availability?: { allowedWorkspaceIds: string[] | null; allowPersonalWorkspaces: boolean; }; allowOrganization?: boolean; }): Promise; export declare function deleteVault(db: Database, context: VaultDbContext, input: { vaultId: string; confirmName: string; allowOrganization?: boolean; }): Promise; export declare function ensureDefaultVault(db: Database, context: VaultDbContext, input: { workspaceId: string; ownerKind: "user" | "workspace"; }): Promise; export declare function ensureTaskVault(db: Database, context: VaultDbContext, input: { workspaceId: string; sessionId: string; }): Promise; export declare function vaultItemCursor(item: Pick): string; export declare function listVaultItems(db: Database, context: VaultDbContext, input: { vaultId: string; q?: string; type?: VaultItemType; includeArchived?: boolean; cursor?: string; limit?: number; }): Promise<{ items: VaultItemRow[]; nextCursor: string | null; }>; export declare function getVaultItem(db: Database, context: VaultDbContext, input: { itemId: string; }): Promise; /** * Create (`vaultId`), upsert by name (`vaultId` + `mode: "upsert"`) or update * (`itemId`) one hosted item in one transaction. */ export declare function upsertVaultItem(db: Database, context: VaultDbContext, input: VaultItemWrite & ({ itemId: string; } | { vaultId: string; mode?: "create" | "upsert"; })): Promise; export declare function archiveVaultItem(db: Database, context: VaultDbContext, input: { itemId: string; }): Promise; export declare function restoreVaultItem(db: Database, context: VaultDbContext, input: { itemId: string; }): Promise; export declare function deleteVaultItemPermanently(db: Database, context: VaultDbContext, input: { itemId: string; }): Promise; export declare function moveVaultItem(db: Database, context: VaultDbContext, input: { itemId: string; targetVaultId: string; mode: "move" | "copy"; newName?: string; }): Promise; /** * One field value with its audit row in the same statement. Purposes: * `reveal` (human reveal, `vault.field.revealed`), `agent_read` (`secret_get`, * `vault.secret.read`) and `internal` (service-side copies; not audited). */ export declare function readVaultFieldCiphertext(db: Database, context: VaultDbContext, input: { fieldId: string; version?: number; purpose: "reveal" | "agent_read" | "internal"; metadata?: Record; }): Promise; export declare function listVaultFieldVersions(db: Database, context: VaultDbContext, input: { fieldId: string; }): Promise; /** One roundtrip: principal facts plus every candidate for every reference. */ export declare function resolveSecretReferenceCandidates(db: Database, context: VaultDbContext, input: { refs: SecretReferenceLookup[]; }): Promise<{ meta: SecretResolutionMeta; candidates: SecretReferenceCandidate[]; }>; export declare function recordVaultSecretUse(db: Database, context: VaultDbContext, input: { use: { surface: string; toolName: string; operationId?: string; sourceCallId?: string; destination?: string; /** The approvals (0952) this use relied on. */ approvalIds?: string[]; /** Who and what caused a non-person use (audit only). */ onBehalfOf?: { subjectId?: string; sessionId?: string; turnId?: string; }; }; items: Array<{ itemId: string; vaultId: string; fieldId: string | null; fieldName: string | null; reference: string; }>; }): Promise; export declare function setSessionVaultPolicy(db: Database, context: VaultDbContext, input: { sessionId: string; policy: VaultPolicy; }): Promise; export declare function getSessionSecretsView(db: Database, context: VaultDbContext, input: { sessionId: string; }): Promise; export declare function updatePasskeySignCount(db: Database, context: VaultDbContext, input: { itemId: string; count: number; }): Promise; export declare function leaseVaultPasskeys(db: Database, context: VaultDbContext, input: { holder: string; ttlSeconds: number; }): Promise; export declare function releaseVaultPasskeyLeases(db: Database, context: VaultDbContext, input: { holder: string; }): Promise; export type ProviderVaultItemMirror = { providerItemRef: string; usernameHintEncrypted?: string | null; providerVersion?: string | null; type: Exclude; /** Provider-specific kind of item (0956); null or absent for the plain type. */ category?: string | null; name: string; websites?: string[]; primaryFieldRef?: string | null; /** Archived upstream by the connected account (0956); null or absent when active. */ archivedAt?: string | null; /** * The item's passkeys without key material (0986). Absent keeps the mirrored * list (a provider that does not report passkeys); empty clears it. */ passkeys?: Array<{ credentialId: string; rpId: string; userName?: string | null; }>; fields: Array<{ providerFieldRef: string; name: string; kind: VaultFieldKind; }>; }; /** * Replace a connected vault's metadata mirror with one complete inventory * (at most `VAULT_LIMITS.itemsPerVault` items; more is refused with * `provider_inventory_incomplete` and the old mirror kept). Items missing * from `items` are removed, and the vault's cache epoch advances (0789). */ export declare function upsertProviderVaultItems(db: Database, context: VaultDbContext, input: { vaultId: string; contentVersion: string | null; items: ProviderVaultItemMirror[]; }): Promise; /** * Record a connected vault's sync state. `operations` and `freshness` are * optional (0789): absent keeps the stored value, null clears it. The optional * mirror and status settle atomically, comparing `expectedCacheEpoch` under * the vault lock; a mismatch raises provider_conflict with superseded=true. * Every applied settlement advances the epoch, even unchanged or failed checks. */ export declare function setVaultSyncStatus(db: Database, context: VaultDbContext, input: { vaultId: string; status: VaultSyncStatus; errorCode?: string | null; errorMessage?: string | null; rateLimitedUntil?: string | null; /** The provider's effective operations for this vault. */ operations?: VaultProviderOperations | null; /** The provider's own upstream freshness observation (never Opengeni's check time). */ freshness?: StoredProviderFreshness | null; /** Reject a late refresh when another settlement advanced this epoch. */ expectedCacheEpoch?: string; /** Complete inventory committed atomically with this successful status. */ mirror?: { contentVersion: string | null; items: ProviderVaultItemMirror[]; }; }): Promise; /** * Link a connected vault (W5, 0789). Every source but `host` needs an active * `api_key` connection in the caller's workspace whose provider identity * matches (`1password.com` with the 1Password service-account role, or * `vault:` with the `external_vault_provider` role and * `metadata.vaultProvider.provider = source`, version 1, not pending * verification) and whose ownership matches `ownerKind`: a personal * connection backs only its owner's Only-me vaults. The mirror is filled by * the first sync. */ export declare function linkProviderVault(db: Database, context: VaultDbContext, input: { ownerKind: "user" | "workspace" | "organization"; name: string; description?: string | null; source: Exclude; providerConnectionId?: string | null; providerVaultRef: string; mode: VaultMode; availability?: { allowedWorkspaceIds: string[] | null; allowPersonalWorkspaces: boolean; }; /** The caller administers the organization (the owner or an admin). */ allowOrganization?: boolean; }): Promise; /** * Exercise the exact link policy before an irreversible provider create. The * temporary link, personal authority and audit rows are all rolled back. This * is a preflight, not a reservation: a race after it must return the confirmed * upstream identity so the caller can link that vault instead of creating again. * No database transaction is held open across provider I/O. */ export declare function preflightProviderVaultLink(db: Database, context: VaultDbContext, input: Omit[2], "providerVaultRef">): Promise; export type ProviderVaultSyncTargetRow = { vaultId: string; workspaceId: string; source: Exclude; mode: VaultMode; providerConnectionId: string | null; providerVaultRef: string; providerContentVersion: string | null; rateLimitedUntil: string | null; /** The vault's value-cache epoch (0789). */ providerCacheEpoch?: string; }; /** The provider refs and mirror version of one reachable connected vault. */ export declare function getProviderVaultSyncTarget(db: Database, context: VaultDbContext, vaultId: string): Promise; export type ProviderVaultItemRefRow = { itemId: string; providerItemRef: string; providerVersion: string | null; generation: number; }; /** A mirrored item's provider refs, looked up by item id or by provider item ref. */ export declare function getProviderVaultItemRef(db: Database, context: VaultDbContext, input: { vaultId: string; itemId?: string; providerItemRef?: string; }): Promise; export type SecretAnswerWrite = { questionId: string; saveTo?: "user" | "task" | "workspace"; vaultId?: string; item: VaultItemWrite & { name: string; }; }; /** Where this person's answer could be read by the turn that asked for it. */ export declare function listHumanInputSecretSaveTargets(db: Database, context: Extract, input: { sessionId: string; turnId: string; }): Promise; /** * Write secret ask-card answers inside the caller's transaction (the * human-input accept transaction). `tx` may already be a transaction; the * vault scope is applied in a savepoint and the parent scope restored. */ export declare function applySecretAnswersInTransaction(tx: Database, context: VaultDbContext, input: { sessionId: string; turnId: string; answers: SecretAnswerWrite[]; }): Promise>;