Skip to content

bug: CLAUDE.local.md never injected into already-running agents on backend startup #135

Closed

Description

@vybe

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():

  1. Backend lifespan startup (main.py:217-288): Lists existing agents but never injects into running ones. If agents are already running (common after docker compose restart), injection never occurs.

  2. 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), calls container_start() directly instead of start_agent_internal(), also skipping injection.

  3. 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

  1. Deploy Trinity with agents running normally
  2. Restart the backend: docker restart trinity-backend (or docker compose restart)
  3. Check any agent: docker exec agent-{name} cat /home/developer/CLAUDE.local.md
  4. 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() and start_agent_internal()
  • src/backend/services/agent_client.py — AgentClient.inject_trinity_prompt()
  • docker/base-image/agent_server/routers/trinity.py — Agent-side /api/trinity/inject endpoint

Activity

  1. vybe commented on Mar 15, 2026

    @vybe
    ContributorAuthor

    Superseded by #136 — refactoring to runtime injection via --append-system-prompt instead of patching the file-based approach.

  2. added
    complexity-low
    on Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p1type-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions