/** * Validates connector-stamped provenance and deduplicates by source, account, room, and * platform record. Production recall derives requester and destination authority from the * exact process-bound delivery turn, constrains storage before ranking, then applies * provenance, disclosure, and destination checks. Missing provenance or failed lookups never * become globally visible or falsely complete results. */ import type { AccessContext } from "../types/access-context.js"; import type { Memory, MemoryScope, MessageChatType } from "../types/memory.js"; import type { UUID } from "../types/primitives.js"; import type { IAgentRuntime } from "../types/runtime.js"; /** * How strongly the stored memory's sender identity is attested. * * - `self`: the agent's own message (entity is the agent). * - `sender-stamped`: the ingesting connector wrote a nested * `metadata[source]` identity object carrying `userId`/`id`. This records the * structural fact that a stable identity was stamped at ingestion — the same * evidence `roles.ts` reads. It is NOT labelled "connector-verified" because * ordinary stored metadata is not an unforgeable attestation. * - `unverified`: no stable connector identity was recorded. Content-supplied * metadata from a chat client lands here and must never be promoted. */ export type CanonicalTrust = "self" | "sender-stamped" | "unverified"; /** * The provenance envelope carried alongside every canonically-recalled item. */ export interface CanonicalProvenance { /** Canonical connector source (registry-normalized, e.g. `discord`). */ source: string; /** Raw `source` string as stored, before normalization. */ rawSource?: string; /** Connector account this arrived under. Distinguishes two accounts on one surface. */ accountId: string; /** Room the memory belongs to. */ roomId: UUID; /** World/tenant scope, when the memory recorded one. */ worldId?: UUID; /** Entity id of the sender. */ senderId: UUID; /** Sender's display name, when the connector recorded one. */ senderDisplayName?: string; /** Sender's stable platform id, when the connector stamped one. */ senderPlatformId?: string; /** Creation timestamp in epoch ms. */ timestampMs: number; /** Attestation strength of the sender identity. */ trust: CanonicalTrust; /** Chat type recorded by the connector (`dm`, `group`, …), when present. */ chatType?: MessageChatType; /** Platform-native message id, when the connector stamped one. */ platformMessageId: string; /** Explicit memory scope stamped by the ingesting connector. */ scope: MemoryScope; } /** Machine-readable reason a candidate was withheld from recall. */ export type RecallDenyCode = /** The stored memory is missing or has conflicting provenance required for fail-closed recall. */ "invalid_provenance" /** The item's own scope forbids this requester (delegated to the scope ladder). */ | "scope_denied" /** * The item lives in a different room than the destination. Cross-room * disclosure is audience policy and is owned by the trusted-delivery-audience * layer, not by this envelope. */ | "cross_room_denied"; export type CanonicalProvenanceResult = { valid: true; provenance: CanonicalProvenance; } | { valid: false; code: "invalid_provenance"; source?: string; reason: string; }; /** * Read source / account / room / sender / timestamp / trust / scope off a * stored memory, normalizing the surface through the connector-source registry. * * `agentId` identifies the agent so its own messages resolve to `self` trust. * Optional display fields may remain absent; missing required provenance, or * conflicting provenance fields (a source recorded under more than one path), * returns a typed invalid result. This derives stored facts and never * fabricates them. Sender attestation is recorded as a structural fact * (`sender-stamped`), never promoted to "connector-verified". */ export declare function deriveCanonicalProvenance(memory: Memory, agentId: UUID): CanonicalProvenanceResult; /** Builds a stable idempotency key including source, account, room, and platform record. Separator-free identifiers use a colon tuple; delimiter-bearing identifiers use versioned JSON to prevent collisions. */ export declare function canonicalDedupeKey(provenance: CanonicalProvenance): string; /** One item that survived provenance validation, the scope ladder, and containment. */ export interface RecalledItem { memory: Memory; provenance: CanonicalProvenance; dedupeKey: string; /** Candidate position of the record's best-ranked copy; lower is a closer match. */ rank: number; } /** * Keep the `count` best-ranked items and return them in transcript order. * Recall items are sorted by time, so truncating them directly keeps the * oldest matches instead of the closest ones. */ export declare function selectBestRankedRecallItems(items: readonly RecalledItem[], count: number): RecalledItem[]; /** * An item that was found but withheld. Carries only the machine-readable deny * code and an aggregate, identifier-free reason — never the source, account, * platform record id, or dedupe key of the withheld memory, which would leak * account/platform identifiers to a caller that was not authorized to read * them. */ export interface WithheldItem { code: RecallDenyCode; reason: string; } /** * Whether the answer is trustworthy as a complete picture. `partial` and * `unavailable` exist so a caller can never render a degraded result as a * confident empty state. */ export type RecallAvailability = "complete" | "partial" | "unavailable"; export interface CanonicalRecallResult { items: RecalledItem[]; withheld: WithheldItem[]; availability: RecallAvailability; /** * Whether the adapter candidate window covered the full eligible set. When * `false`, closer ineligible rows (wrong room, wrong source, malformed * provenance, duplicates) may have starved eligible results out of the * bounded top-K window, so the answer is honestly incomplete even if no * item was individually withheld. */ candidateWindowComplete: boolean; } export interface CanonicalRecallInput { /** Candidate memories already fetched from the store. */ candidates: Memory[]; /** The agent performing the recall. */ agentId: UUID; /** Who is asking. */ requester: AccessContext; /** Room the answer will land in (the inbound message's connector-stamped room). */ destinationRoomId: UUID; } export declare function buildCanonicalRecall(input: CanonicalRecallInput): Omit; interface CanonicalMemorySearchBaseInput { runtime: IAgentRuntime; embedding: number[]; /** False omits returned vectors, without changing ranking or source checks. */ includeEmbedding?: boolean; /** Additional room exclusions only narrow the authorized recall scope. */ excludeRoomIds?: UUID[]; query?: string; /** Caller metadata; runtime.agentId determines the retrieval agent. */ agentId?: UUID; /** Explicit result count. Omit when the complete eligible result set is required. */ count?: number; matchThreshold?: number; entityId?: UUID; /** Optional connector-source filter, normalized through the registry. */ source?: string; } /** * Production retrieval for canonical conversation recall. Requester identity * and destination are derived ONLY from the exact in-memory delivery turn * ({@link CanonicalMemorySearchDeliveryInput.deliveryMessage}) — the same * process-bound trusted delivery-audience evidence the disclosure gate uses. * Caller-supplied `requester` / `destinationRoomId` are NOT accepted: a caller * cannot mint authority over who is asking or where the answer lands. * * The adapter vector scan is constrained by the attested room before ranking, * so a global top-K cannot starve eligible same-room rows. When cross-room * recall is authorized by the trusted-delivery-audience layer the scan is not * room-constrained, but the bounded candidate window is refilled honestly and * a truncated window is never reported as complete. * * Adapter failures and access-context lookup failures propagate as a typed * `unavailable` availability — an error is an error, never an empty * "complete" result. */ export type CanonicalMemorySearchInput = CanonicalMemorySearchBaseInput & CanonicalMemorySearchDeliveryInput; export interface CanonicalMemorySearchDeliveryInput { /** Exact in-memory delivery turn the recalled context will render into. */ deliveryMessage: Memory; } /** * Search canonical conversation memories, deriving requester and destination * only from the exact trusted delivery turn. See * {@link CanonicalMemorySearchInput}. */ export declare function searchCanonicalConversationMemories(input: CanonicalMemorySearchInput): Promise; export {};