Skip to content

Self-healing scaffolding: recreate missing agent files on first read#412

Merged
chubes4 merged 2 commits into
mainfrom
feature/self-healing-scaffolding
Feb 25, 2026
Merged

Self-healing scaffolding: recreate missing agent files on first read#412
chubes4 merged 2 commits into
mainfrom
feature/self-healing-scaffolding

Conversation

@chubes4
Copy link
Copy Markdown
Member

@chubes4 chubes4 commented Feb 25, 2026

Summary

Closes #408 — Agent files (SOUL.md, USER.md, MEMORY.md) were only created during register_activation_hook. If the plugin was deployed via Homeboy, rsync, or git pull without triggering activation, the agent silently operated without its identity files.

Changes

  • DirectoryManager::ensure_agent_files() — New static method with a once-per-request flag. Calls datamachine_ensure_default_memory_files() lazily on the first access. Repeated calls are free (static bool guard).

  • CoreMemoryFilesDirective::get_outputs() — Calls ensure_agent_files() before reading memory files. This is the universal funnel for all AI calls. Also now logs a warning if files are still missing after the scaffolding attempt.

  • AgentMemory constructor — Calls ensure_agent_files() on instantiation. Covers all CLI and API memory operations.

  • FileAbilities::listAgentFiles() / getAgentFile() — Calls ensure_agent_files() before listing or retrieving agent files via REST API.

  • AgentMemory::ensure_file_exists() — Upgraded from bare # Agent Memory\n stub to using datamachine_get_scaffold_defaults() content, so a recreated MEMORY.md includes the standard sections (State, Lessons Learned, Context).

  • datamachine_ensure_default_memory_files() — Added notice-level logging when files are recreated, providing visibility into self-healing events.

How it works

Request touches agent files (AI call, CLI, REST API)
  → DirectoryManager::ensure_agent_files()     [static flag: once per request]
    → datamachine_ensure_default_memory_files() [creates missing files, skips existing]
      → Logs "Self-healing: created missing agent file X" for each recreated file
  → Normal read continues with files guaranteed to exist

Coverage

Access path Self-healing point
Every AI call (chat, pipelines) CoreMemoryFilesDirective::get_outputs()
CLI wp datamachine memory * AgentMemory constructor
REST API GET /files/agent FileAbilities::listAgentFiles()
REST API GET /files/agent/{file} FileAbilities::getAgentFile()
REST API PUT /files/agent/{file} AgentMemory::ensure_file_exists() (write path)

get_option('active_plugins') only returns subsite-specific plugins on
multisite. Network-activated plugins are stored separately in
active_sitewide_plugins site option. Merges both lists so the scaffolded
SOUL.md reflects all plugins the agent will encounter.
…t read

Agent files (SOUL.md, USER.md, MEMORY.md) were only created during the
WordPress activation hook. If the plugin was deployed via Homeboy, rsync,
or git pull without triggering activation, the agent silently operated
without its identity files.

Add lazy self-healing via DirectoryManager::ensure_agent_files() — a
static once-per-request check that recreates missing files from scaffold
defaults. Hooked into all read paths:

- CoreMemoryFilesDirective (every AI call)
- AgentMemory constructor (CLI/API memory operations)
- FileAbilities listAgentFiles/getAgentFile (REST API)

Also upgrades AgentMemory::ensure_file_exists() to use rich scaffold
content instead of a bare stub, and adds logging for both creation
events and persistent missing-file warnings.

Closes #408
@chubes4 chubes4 merged commit d3b81da into main Feb 25, 2026
@chubes4 chubes4 deleted the feature/self-healing-scaffolding branch February 25, 2026 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Self-healing scaffolding: create agent files on first request, not just activation

1 participant