Implementation:23blocks OS Ai maestro Agent Registry CRUD
| Knowledge Sources | |
|---|---|
| Domains | Agent_Orchestration, Persistence |
| Last Updated | 2026-10-26 10:00 GMT |
Overview
Concrete tool for agent creation, lookup, and deletion in the agent registry, provided by AI Maestro's `lib/agent-registry.ts`.
Description
The Agent Registry is the single source of truth for agent data: `~/.aimaestro/agents/registry.json`. Agents are the core entity of AI Maestro; a tmux session is an optional property of one. The registry persists agents as a JSON array on disk with an mtime-based in-memory cache, and provides CRUD plus a family of lookup functions (`getAgent` by UUID, `getAgentByName` per-host, `getAgentBySession` via tmux session name parsing). Agent names are unique per host (like email addresses). Deletion is two-phase: soft-delete marks `deletedAt` and cleans AMP directories while preserving data, hard-delete backs up first and then permanently removes all data.
Key functions:
- loadAgents(): read all agents from disk (with mtime cache, migration, and runtime backfill)
- saveAgents(agents): persist the full agent array and invalidate the cache
- getAgent(id, includeDeleted): find by UUID, excluding soft-deleted by default
- createAgent(request): validate name (anti-shell-injection regex), check per-host uniqueness, auto-generate persona label and avatar, write to registry
- deleteAgent(id, hard): kill tmux sessions, then soft-delete (mark + AMP cleanup) or hard-delete (backup + full removal)
- getAgentSkills(agentId): skills config, defaulting to AI Maestro built-in skills
Usage
- Call `createAgent` when a new agent must exist in the fleet (local, Docker, or cloud deployment types).
- Call `getAgent(id) || getAgentByName(name)` to read agent properties — never derive them from tmux.
- Call `deleteAgent(id, false)` for a reversible delete (restore possible), `deleteAgent(id, true)` for permanent removal.
- Call `getAgentSkills` when exporting an agent or rendering its skill list.
Code Reference
Source Location
- Repository: ai-maestro
- File: lib/agent-registry.ts
- Lines: L154-202 (loadAgents), L207-223 (saveAgents), L320-326 (getAgent), L466-560 (createAgent), L848-946 (deleteAgent), L2011-2023 (getAgentSkills)
Signature
export function loadAgents(): Agent[]
export function saveAgents(agents: Agent[]): boolean
export function getAgent(id: string, includeDeleted: boolean = false): Agent | null
export function getAgentByName(name: string, hostId?: string): Agent | null
export function createAgent(request: CreateAgentRequest): Agent
export function deleteAgent(id: string, hard: boolean = false): boolean
export function getAgentSkills(agentId: string): AgentSkillsConfig | null
interface CreateAgentRequest {
name?: string // agent name; validated against ^[a-zA-Z0-9/_.-]+$
alias?: string // deprecated fallback for name
id?: string // caller-supplied UUID (offline-first)
workingDirectory?: string
program?: string
model?: string
taskDescription?: string
programArgs?: string
programKind?: string
permissionMode?: AgentPermissionMode // default 'supervised'
runtime?: string // default 'tmux'
deploymentType?: DeploymentType // default 'local'
hostId?: string
tags?: string[]
owner?: string
createSession?: boolean
sessionIndex?: number
launchOptions?: Record<string, unknown>
}
Import
import { loadAgents, saveAgents, getAgent, getAgentByName, createAgent, deleteAgent, getAgentSkills } from '@/lib/agent-registry'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| request | CreateAgentRequest | Yes (create) | Name, program, working directory, permission mode |
| id | string | Yes (get/delete) | Agent UUID |
| hard | boolean | No | Soft-delete (default) vs permanent removal |
| agents | Agent[] | Yes (save) | Full agent list to persist |
Outputs
| Name | Type | Description |
|---|---|---|
| loadAgents() returns | Agent[] | All agents (empty array on error) |
| saveAgents() returns | boolean | true on success |
| getAgent() returns | null | Agent object; soft-deleted excluded unless includeDeleted |
| createAgent() returns | Agent | New agent with generated UUID, label, avatar, host info |
| createAgent() throws | Error | Missing name, invalid name chars, duplicate name on host, duplicate UUID |
| deleteAgent() returns | boolean | true on success, false if not found |
| getAgentSkills() returns | null | Marketplace, AI Maestro, and custom skills |
Usage Examples
Create and look up an agent
import { createAgent, getAgent, getAgentByName } from '@/lib/agent-registry'
// 1. Create an agent with a recorded tool and working directory
const agent = createAgent({
name: 'website',
program: 'claude',
workingDirectory: '/home/user/projects/website',
permissionMode: 'supervised',
runtime: 'tmux',
})
// 2. Read it back — by UUID (the canonical identity)
const byId = getAgent(agent.id)
// 3. Or by name (per-host: names are unique like email addresses)
const byName = getAgentByName('website')
Soft-delete vs hard-delete
import { deleteAgent } from '@/lib/agent-registry'
// Soft-delete: kills tmux sessions, marks deletedAt, cleans AMP inbox,
// but preserves data on disk for restore
deleteAgent(agentId, false)
// Hard-delete: creates a backup first, then removes registry entry,
// agent directory (database), messages, and AMP directories
deleteAgent(agentId, true)
Related Pages
Implements Principle
- Principle:23blocks_OS_Ai_maestro_Agent_Registry
- Principle:23blocks_OS_Ai_maestro_Agent_Deletion
- Principle:23blocks_OS_Ai_maestro_Agent_Portability