src — identity

Module: src-identity Cohesion: 0.80 Members: 0

src — identity

The src/identity module, centered around the IdentityManager class, is responsible for managing critical "identity" files within the application. These files, such as SOUL.md, USER.md, and AGENTS.md, define the core persona, instructions, and context for the application's AI components.

This module handles the loading, prioritization, hot-reloading, and programmatic management of these files, ensuring that the application always has access to the most current and relevant identity information.

Purpose

The primary purpose of the IdentityManager is to:

  1. Load Identity Files: Read predefined markdown files from specific directories.
  2. Prioritize Files: Resolve conflicts by giving precedence to project-specific files over global configuration files.
  3. Hot-Reload Changes: Monitor these files for changes and automatically update their content in memory.
  4. Provide Access: Offer methods to retrieve individual or all loaded identity files.
  5. Format for Prompts: Generate a consolidated, structured string of all identity content suitable for direct injection into LLM prompts.
  6. Programmatic Updates: Allow identity files to be created or updated directly via code.

Core Concepts

Identity Files

These are markdown files (e.g., SOUL.md, USER.md, AGENTS.md, TOOLS.md, INSTRUCTIONS.md) that contain crucial context for the AI. Each file typically serves a specific purpose, defining aspects like the AI's core personality (SOUL.md), user preferences (USER.md), available agents (AGENTS.md), or specific instructions (INSTRUCTIONS.md).

The full list of default files is: SOUL.md, USER.md, AGENTS.md, TOOLS.md, IDENTITY.md, INSTRUCTIONS.md, BOOT.md, BOOTSTRAP.md, HEARTBEAT.md.

Project vs. Global Overrides

The IdentityManager operates with a clear hierarchy for identity files:

Project-level files always take precedence over global-level files with the same name. For example, if both project/.codebuddy/SOUL.md and ~/.codebuddy/SOUL.md exist, the project-level file's content will be loaded and used. This allows projects to define their own specific AI behavior without affecting global defaults.

Hot-Reloading

When watch() is enabled, the IdentityManager monitors both project and global identity directories for file changes. If a file is added, modified, or deleted, the manager automatically reloads its content and emits an identity:changed event. This ensures that the application's AI context remains up-to-date without requiring a restart.

IdentityManager Class

The IdentityManager class is an EventEmitter, allowing other parts of the application to subscribe to identity-related events.

export class IdentityManager extends EventEmitter { /* ... */ }

Configuration (IdentityManagerConfig)

The manager is configured via an IdentityManagerConfig object, which can be partially overridden during instantiation.

export interface IdentityManagerConfig {
  projectDir: string; class="hl-cmt">// Default: '.codebuddy'
  globalDir: string;  class="hl-cmt">// Default: '~/.codebuddy'
  watchForChanges: boolean; class="hl-cmt">// Default: true
  fileNames: string[]; class="hl-cmt">// Default: DEFAULT_IDENTITY_FILES
}

Identity File Structure (IdentityFile)

When an identity file is loaded, its data is stored in an IdentityFile object:

export interface IdentityFile {
  name: string;    class="hl-cmt">// e.g., 'SOUL.md'
  content: string; class="hl-cmt">// Trimmed file content
  source: 'project' | 'global'; class="hl-cmt">// Where the file was found
  path: string;    class="hl-cmt">// Absolute path to the file
  lastModified: Date; class="hl-cmt">// Last modification timestamp
}

Events (IdentityManagerEvents)

The IdentityManager emits the following events:

Key Public Methods

Private Methods

Singleton Pattern

The IdentityManager is typically accessed as a singleton throughout the application to ensure a single, consistent source of truth for identity files.

Architecture Diagram

The IdentityManager acts as a central hub for identity-related data, interacting with the file system and providing formatted content to prompt builders.

graph TD
    subgraph "File System"
        A[~/.codebuddy/]
        B[./.codebuddy/]
    end

    subgraph "IdentityManager"
        IM(IdentityManager)
        IM -- "1. load(cwd)" --> IM_LF(loadFile)
        IM_LF -- "2. readIdentityFile" --> A
        IM_LF -- "2. readIdentityFile" --> B
        IM -- "3. watch(cwd)" --> IM_WD(watchDirectory)
        IM_WD -- "4. fs.watch" --> A
        IM_WD -- "4. fs.watch" --> B
        IM -- "5. set(name, content)" --> B
        IM -- "6. getPromptInjection()" --> PB(PromptBuilder)
    end

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#f9f,stroke:#333,stroke-width:2px
    style IM fill:#bbf,stroke:#333,stroke-width:2px
    style IM_LF fill:#ccf,stroke:#333,stroke-width:1px
    style IM_WD fill:#ccf,stroke:#333,stroke-width:1px
    style PB fill:#bfb,stroke:#333,stroke-width:2px

Explanation:

  1. IdentityManager's load() method calls loadFile which in turn uses readIdentityFile to read from both global (A) and project (B) directories, prioritizing project files.
  2. watch() sets up watchDirectory which uses fs.watch to monitor changes in A and B.
  3. set() directly writes to the project directory (B).
  4. getPromptInjection() provides formatted content to consumers like the PromptBuilder.

Integration with the Codebase

The IdentityManager is a foundational module, integrated across various parts of the application:

Incoming Calls (Consumers)

Outgoing Calls (Dependencies)