import { type RoleName } from "./access-control/role-primitives.js"; import type { World } from "./types/environment.js"; import type { Memory } from "./types/memory.js"; import type { UUID } from "./types/primitives.js"; import type { IAgentRuntime } from "./types/runtime.js"; export type { RoleName } from "./access-control/role-primitives.js"; /** * Provenance of an explicit `roles[entityId]` grant. "session" marks a grant * minted at chat ingress for an authenticated machine-session (paired-device) * principal: it is capped at USER by construction, kept distinct from "manual" * so it never confers manual-grant private access, and its reachability is * gated per turn by the HTTP session boundary (a revoked/expired session can * no longer act as the granted entity). */ export type RoleGrantSource = "owner" | "manual" | "connector_admin" | "session"; /** Shared role ranking. USER and MEMBER denote the same tier; NONE is the floor. */ export { CANONICAL_ROLE_RANK, hasAtLeastRole, isAdminRank, ROLE_RANK, } from "./access-control/role-primitives.js"; export type RolesWorldMetadata = { ownership?: { ownerId?: string; }; roles?: Record; roleSources?: Record; }; export type ConnectorAdminWhitelist = Record; export type RolesConfig = { connectorAdmins?: ConnectorAdminWhitelist; }; export type RoleCheckResult = { entityId: UUID; role: RoleName; isOwner: boolean; isAdmin: boolean; canManageRoles: boolean; }; declare const CONNECTOR_STABLE_ID_FIELDS: readonly ["userId", "id"]; type ConnectorStableIdField = (typeof CONNECTOR_STABLE_ID_FIELDS)[number]; type ConnectorAdminMatch = { connector: string; matchedValue: string; matchedField: ConnectorStableIdField; }; type ResolveEntityRoleOptions = { liveEntityMetadata?: Record | null; liveEntityId?: string; }; /** * Role floor used when a real sender exists but no world role can be resolved. * Connector messages must not outrank a fully resolved stranger, so unknown * non-local sources fall to GUEST. Local, owner-app, and harness traffic keeps * USER so no-world control surfaces keep their historical behavior. */ export declare function getUnresolvedSenderRoleFloor(message: Memory): RoleName; /** * Connector identity for role resolution. Only identity stamped into the * Memory itself is trusted: `content.metadata` can come from untrusted chat * clients, so it never participates. */ export declare function getLiveEntityMetadataFromMessage(message: Memory): Record | undefined; export declare function findWorldsForOwner(runtime: IAgentRuntime, entityId: string): Promise; export declare function getConfiguredOwnerEntityIds(runtime: Pick): string[]; export declare function hasConfiguredCanonicalOwner(runtime: IAgentRuntime): boolean; export declare function resolveCanonicalOwnerId(runtime: Pick, metadata?: RolesWorldMetadata): string | null; /** * Deterministic owner-entity id for an agent that has no configured canonical * owner. Seeded from the agent ID — never the character name, which is * user-editable and differs between surfaces that read it (`character.name`, * `agents.defaults.name`, a hard-coded "Eliza") — so every owner-scoped surface * lands on one `subject_id`. Prefer {@link resolveOwnerEntityIdOrDefault} * whenever a runtime is available; call this directly only when the caller has * already established that no canonical owner is configured. */ export declare function deterministicOwnerEntityId(agentId: string): UUID; /** * The single owner-entity derivation shared by the client-chat write path, the * personal-assistant routes, LifeOps reads and the scheduler, the * outbound owner target, and connector ownership metadata: the configured * canonical owner (`ELIZA_ADMIN_ENTITY_ID` / owner contacts) when it is a * UUID, otherwise {@link deterministicOwnerEntityId}. Surfaces that derive the * owner any other way fork the owner `subject_id`, making rows written on one * surface invisible to the others. */ export declare function resolveOwnerEntityIdOrDefault(runtime: Pick): UUID; export declare function setConnectorAdminWhitelist(runtime: IAgentRuntime, whitelist: ConnectorAdminWhitelist | Record | undefined): void; export declare function getConnectorAdminWhitelist(runtime: IAgentRuntime): ConnectorAdminWhitelist; export declare function matchEntityToConnectorAdminWhitelist(entityMetadata: Record | null | undefined, whitelist: ConnectorAdminWhitelist | Record | undefined): ConnectorAdminMatch | null; export declare function normalizeRole(raw: string | undefined | null): RoleName; export declare function getEntityRole(metadata: RolesWorldMetadata | undefined, entityId: string): RoleName; export declare function resolveEntityRole(runtime: IAgentRuntime, _world: Awaited>, metadata: RolesWorldMetadata | undefined, entityId: string, options?: ResolveEntityRoleOptions): Promise; export declare function checkSenderPrivateAccess(runtime: IAgentRuntime, message: Memory): Promise<(RoleCheckResult & { hasPrivateAccess: boolean; accessRole: RoleName | null; accessSource: "owner" | "manual" | "linked_manual" | null; }) | null>; export declare function canModifyRole(actorRole: RoleName, targetCurrentRole: RoleName, newRole: RoleName): boolean; export declare function resolveWorldById(runtime: IAgentRuntime, worldId: UUID): Promise<{ world: Awaited>; metadata: RolesWorldMetadata; } | null>; export declare function resolveWorldForMessage(runtime: IAgentRuntime, message: Memory): Promise<{ world: Awaited>; metadata: RolesWorldMetadata; } | null>; export declare function resolveCanonicalOwnerIdForMessage(runtime: IAgentRuntime, message: Memory): Promise; export declare function checkSenderRole(runtime: IAgentRuntime, message: Memory): Promise; export declare function isAgentSelf(runtime: IAgentRuntime | undefined, message: Memory | undefined): boolean; /** * Injectable role-resolution seam for {@link hasRoleAccess}. * Lets callers (e.g. plugin-manager/security.ts wrappers, and tests) substitute * the sender-role check / canonical-owner resolution without monkey-patching the * module. */ export type RoleAccessDeps = { checkSenderRole?: (runtime: IAgentRuntime, message: Memory) => Promise; resolveCanonicalOwnerIdForMessage?: (runtime: IAgentRuntime, message: Memory) => Promise; }; /** * Check whether the sender has at least the given role in the elizaOS * role hierarchy (OWNER > ADMIN > USER > GUEST). * * A caller must supply the runtime and an explicit sender, including trusted * local administration. Missing context never confers a role. When a real * sender's world role cannot be resolved, use the same source-aware floor as * Stage 1 context filtering. */ export declare function hasRoleAccess(runtime: IAgentRuntime | undefined, message: Memory | undefined, requiredRole: RoleName, deps?: RoleAccessDeps): Promise; /** * Records the owner role and its source on world metadata. Mutates metadata * and returns whether anything changed, allowing callers to persist only changes. */ export declare function recordOwnerGrant(metadata: RolesWorldMetadata, ownerId: string): boolean; /** * Record an explicit, auditable role grant on a world's metadata: pairs * `roles[entityId] = role` with `roleSources[entityId] = source` (GUEST clears the * source, matching {@link setEntityRole}). Use when you hold the metadata but not * a Memory — {@link setEntityRole} needs a message and {@link recordOwnerGrant} * only records the canonical OWNER. Pure + idempotent: mutates `metadata` in place * and returns `true` iff it changed something. */ export declare function recordRoleGrant(metadata: RolesWorldMetadata, entityId: string, role: RoleName, source?: RoleGrantSource): boolean; export declare function setEntityRole(runtime: IAgentRuntime, message: Memory, targetEntityId: string, newRole: RoleName, source?: RoleGrantSource): Promise>; /** * Typed outcome of one atomic role-write attempt. `conflict` means a * concurrent metadata write landed between the caller's authorized read and * the compare-and-swap; the caller must re-resolve and re-authorize before * retrying — never retry with the original snapshot. */ export type EntityRoleCasResult = { status: "committed"; roles: Record; } | { status: "conflict"; } | { status: "world_not_found"; } | { status: "unauthorized"; }; /** Bounded retries before a persistently contended role write gives up. */ export declare const ROLE_WRITE_CAS_MAX_ATTEMPTS = 3; /** * Atomically set one entity's world role under compare-and-swap. * * Unlike {@link setEntityRole} (a blind read-merge-write of whole-world * metadata), this resolves the world fresh on EVERY attempt, re-invokes the * caller's `authorize` predicate against that fresh state, applies the grant * via {@link recordRoleGrant}, and commits through * `adapter.compareAndSwapWorldMetadata`, which compares the exact prior * metadata snapshot in the same transaction that writes the replacement and * inserts the durable `role_audit` log row. A concurrent writer therefore * turns into a typed `conflict` the caller can retry after re-authorizing, * instead of being silently overwritten (which could resurrect a revoked * ADMIN). * * The `authorize` predicate is re-evaluated on every attempt against the * freshly resolved world — never trust an authorization verdict computed * against an earlier snapshot. The audit row uses the originating * `message.roomId` — a real room the request came from — and is committed in * the same adapter transaction as the metadata replacement, so a committed * authority change is never separable from its audit record. */ export declare function setEntityRoleCas(runtime: IAgentRuntime, message: Memory, targetEntityId: string, newRole: RoleName, options?: { source?: RoleGrantSource; /** * Resolve this explicit world instead of the message room's world. * The trust ROLE handler resolves the configured `WORLD_ID` setting, * which may differ from the room the message arrived in — its CAS * must write that world, not the room's. */ worldId?: UUID; /** Max CAS attempts before reporting conflict exhaustion. */ maxAttempts?: number; /** * Per-attempt authorization against the FRESH world state. Re-checked * before every CAS write, so a concurrent revocation of the requester * cannot be outrun by a retry. Deny surfaces as * {@link EntityRoleCasOutcome} `unauthorized` — never a write. */ authorize?: (fresh: Awaited>) => boolean | Promise; /** Additional authority metadata committed in the same audited CAS. */ mutateMetadata?: (replacement: RolesWorldMetadata) => void; }): Promise; export declare function hasOwnerAccess(runtime: IAgentRuntime | undefined, message: Memory | undefined, deps?: RoleAccessDeps): Promise; export declare function hasAdminAccess(runtime: IAgentRuntime | undefined, message: Memory | undefined, deps?: RoleAccessDeps): Promise;