Repository navigation
Description
Summary
CLAUDE.local.md (Trinity platform instructions) is never injected into agent containers that are already running when the backend starts. The inject_trinity_meta_prompt() function is only called via start_agent_internal(), which is only triggered by the API start endpoint — not during backend startup. This means agents lose their platform instructions after any backend restart or redeploy.
Component
Backend / Agent Lifecycle
Priority
P1
Error
No error is raised — the injection is silently skipped. All agents show CLAUDE.local.md as MISSING despite being healthy and running.
Location
- File:
src/backend/main.py - Lines: 217-288 (
lifespan()function) - File:
src/backend/services/agent_service/lifecycle.py - Lines: 198-238 (
start_agent_internal()) - File:
src/backend/services/system_agent_service.py - Lines: 56-103 (
ensure_deployed())
Root Cause
Three code paths bypass inject_trinity_meta_prompt():
-
Backend lifespan startup (
main.py:217-288): Lists existing agents but never injects into running ones. If agents are already running (common afterdocker compose restart), injection never occurs. -
System agent
ensure_deployed()(system_agent_service.py:82-88): When the system agent is already running, returns immediately without injection. When starting a stopped one (line 92), callscontainer_start()directly instead ofstart_agent_internal(), also skipping injection. -
Docker container persistence: Agent containers survive backend restarts. Since the backend doesn't re-inject on startup, agents that were running before the restart never get
CLAUDE.local.md.
The injection endpoint on the agent side works correctly — calling /api/trinity/inject directly succeeds. The issue is purely that the backend never calls it for already-running agents.
Reproduction Steps
- Deploy Trinity with agents running normally
- Restart the backend:
docker restart trinity-backend(ordocker compose restart) - Check any agent:
docker exec agent-{name} cat /home/developer/CLAUDE.local.md - File is missing — agents have no platform instructions
Suggested Fix
Add a startup injection sweep in the lifespan() function to inject into all running agents:
# In lifespan(), after listing agents (line 228):
if docker_client:
try:
agents = list_all_agents_fast()
print(f"Found {len(agents)} existing Trinity agent containers")
# Inject Trinity meta-prompt into all running agents
running_agents = [a for a in agents if a.status == "running"]
if running_agents:
for agent in running_agents:
try:
result = await inject_trinity_meta_prompt(agent.name)
status = result.get("status", "unknown")
if status not in ("injected", "already_injected"):
print(f" Warning: injection into {agent.name}: {status}")
except Exception as e:
print(f" Warning: Failed to inject into {agent.name}: {e}")
print(f"Injected Trinity prompt into {len(running_agents)} running agents")
except Exception as e:
print(f"Error during startup injection: {e}")Also fix system_agent_service.py to call start_agent_internal() instead of container_start() directly when starting a stopped system agent.
Environment
- Trinity version:
546b7a7 - Docker version: 24.0.7
- OS: Ubuntu 22.04
Related
src/backend/services/agent_service/lifecycle.py—inject_trinity_meta_prompt()andstart_agent_internal()src/backend/services/agent_client.py—AgentClient.inject_trinity_prompt()docker/base-image/agent_server/routers/trinity.py— Agent-side/api/trinity/injectendpoint
Activity
vybe commentedon Mar 15, 2026
Superseded by #136 — refactoring to runtime injection via
--append-system-promptinstead of patching the file-based approach.