ai-registry 0.1.0

AI Registry on Solana - On-chain agent identity and knowledge management
Documentation
use ai_registry_api::{
    consts::{AGENT, FLAG_KNOWLEDGE_FINALIZED},
    error::AgentError,
    event::KnowledgeFinalizedEvent,
    state::Agent,
};
use steel::*;

/// Process a finalize knowledge instruction
pub fn process_finalize_knowledge(
    accounts: &[AccountInfo<'_>],
    _data: &[u8],
) -> ProgramResult {
    // Load accounts (signer first, following Solana best practices)
    let [controller_info, agent_info] = accounts else {
        return Err(ProgramError::NotEnoughAccountKeys);
    };

    // Validate accounts
    agent_info.is_writable()?.has_seeds(
        &[AGENT, agent_info.as_account::<Agent>(&ai_registry_api::ID)?.creator.as_ref()],
        &ai_registry_api::ID,
    )?;
    controller_info.is_signer()?;

    // Load agent and verify controller
    let agent = agent_info.as_account::<Agent>(&ai_registry_api::ID)?;
    if controller_info.key != &agent.controller {
        return Err(AgentError::UnauthorizedController.into());
    }

    // Check if already finalized
    if agent.state_flags & FLAG_KNOWLEDGE_FINALIZED != 0 {
        return Err(AgentError::KnowledgeFinalized.into());
    }

    // Finalize knowledge
    let clock = Clock::get()?;
    let agent = agent_info.as_account_mut::<Agent>(&ai_registry_api::ID)?;
    agent.state_flags |= FLAG_KNOWLEDGE_FINALIZED;
    agent.finalized_at = clock.unix_timestamp;
    agent.updated_at = clock.unix_timestamp;

    // Emit event
    KnowledgeFinalizedEvent {
        creator: agent.creator,
        agent: *agent_info.key,
        timestamp: clock.unix_timestamp,
    }
    .log();

    Ok(())
}