Create a new EmergentToolRegistry.
Emergent capability configuration. Missing fields are filled from DEFAULT_EMERGENT_CONFIG.
Optional db: EmergentRegistryStorageAdapterOptional SQLite storage adapter. When provided, agent and
shared tier tools are persisted to the agentos_emergent_tools table.
When omitted, all tiers use in-memory storage only.
Idempotent schema readiness guard.
Ensures ensureSchema() is called exactly once and all subsequent callers
await the same in-flight promise. This prevents the race condition where
concurrent DB operations start before DDL statements finish.
A promise that resolves when the schema is ready.
Initialize the database schema for emergent tool persistence.
Creates the agentos_emergent_tools and agentos_emergent_audit_log
tables along with their indexes. Safe to call multiple times — all
statements use CREATE TABLE IF NOT EXISTS / CREATE INDEX IF NOT EXISTS.
This method is a no-op when no storage adapter was provided.
If the storage adapter's exec or run method rejects.
Register a new emergent tool at the given tier.
Session-tier tools are stored in the in-memory session map and mirrored to SQLite when available. Agent and shared tier tools are stored in the persisted map (and written to SQLite when a storage adapter is available).
The emergent tool to register. Must have a unique id.
The tier to register the tool at. The tool's tier property
is updated to match.
If the maximum tool count for the target tier is exceeded
(checked against maxSessionTools or maxAgentTools from config).
If a tool with the same ID is already registered.
Retrieve a tool by its unique identifier.
Searches all tiers (session first, then persisted agent/shared).
The tool ID to look up.
The tool if found, or undefined if no tool with that ID exists.
Upsert a tool into the registry, replacing any prior in-memory copy.
Used to hydrate persisted/shared tools back into a live runtime so they can become executable again after process restart or admin promotion.
Get all tools registered at a specific tier, optionally filtered by scope.
The tier to query ('session', 'agent', or 'shared').
Optional scope: { Optional scope filter. When provided, results are narrowed:
sessionId: Match tools whose source string contains the session ID.agentId: Match tools whose createdBy equals the agent ID.Optional sessionOptional agentAn array of matching tools (may be empty).
Record a tool invocation, updating rolling usage statistics.
Updates the tool's ToolUsageStats in place:
totalUses.successCount or failureCount based on the success flag.avgExecutionTimeMs as a running average.confidenceScore as successCount / totalUses.lastUsedAt to the current ISO-8601 timestamp.The ID of the tool that was invoked.
The input arguments passed to the tool (logged for audit).
The output returned by the tool (logged for audit).
Whether the invocation completed successfully.
Wall-clock execution time in milliseconds.
If no tool with the given ID is registered.
Retrieve usage statistics for a registered tool.
The tool ID to look up.
The tool's ToolUsageStats, or undefined if the tool
is not registered.
Promote a tool to a higher lifecycle tier.
Moves the tool from its current tier to targetTier. If the tool was at
session tier, it is removed from the session map and added to the persisted
map. If a storage adapter is available and the target tier is agent or
shared, the tool is persisted to the database.
The ID of the tool to promote.
The target tier to promote to. Must be strictly higher than the tool's current tier.
Optional approvedBy: stringOptional identifier of the human or system entity that approved the promotion.
If the tool is not found.
If targetTier is not higher than the tool's current tier.
Demote or deactivate a tool.
Marks the tool as inactive by setting a sentinel on its usage stats
(confidenceScore set to 0) and logs the demotion event with a reason.
Inactive tools are still retrievable via get() but should be filtered
out by callers when building tool lists for the LLM.
The ID of the tool to demote.
Human-readable explanation for why the tool is being demoted.
If the tool is not found.
Remove all session-tier tools associated with a specific session.
Iterates the session map and deletes every tool whose source string
contains the given session ID. Logs a cleanup audit event for each
removed tool.
The session identifier to match against tool source
strings.
The number of tools removed.
Retrieve audit log entries, optionally filtered by tool ID.
Optional toolId: stringWhen provided, only entries for this tool are returned.
An array of AuditEntry objects in chronological order.
Manages the lifecycle of emergent tools across three trust tiers.
The registry stores session-tier tools in an in-memory Map (keyed by tool ID) and mirrors them to SQLite when available for audit/inspection. Agent/shared tier tools live in the persisted map and are written to SQLite (when a storage adapter is provided) or kept in-memory as fallback.
Key responsibilities:
Example