Repository navigation
Description
Summary
When multiple agents are created in quick succession (e.g., via deploy_system with a multi-agent manifest, or rapid sequential create_agent calls), the backend's credential-injection step races against agent container HTTP startup. The retry loop gives up before the agent's FastAPI server is listening, and the credentials never land. If a scheduled task fires within the first minute or two after deploy, it hits the agent with no credentials and fails with HTTP error: ConnectError, cascading across every new agent in the fleet.
System self-heals on subsequent ticks (later calls succeed once the server is up and the import retries on-demand), but the post-deploy window is noisy and produces misleading failed executions in the UI.
Component
Backend / Agent Lifecycle
Priority
P2
Error
ERROR services.agent_service.lifecycle: Failed to import credentials into agent [AGENT] after 3 attempts: All connection attempts failed
ERROR services.task_execution_service: Failed to execute task on [AGENT]: HTTP error: ConnectError
WARNING services.agent_client: Circuit OPENED for agent [AGENT] after 3 failures
Location
- File:
services/agent_service/lifecycle.py - Function:
import_credentials
Root Cause
The retry loop for import_credentials waits on the Docker container being in "running" state, not on the in-container HTTP server accepting connections. Under load (creating 5+ agents in ~2 minutes), Up precedes listening on :8000 by several seconds, and the 3-attempt retry window exhausts before the server is ready.
Evidence from a fresh 10-agent deploy: every agent's first credential push failed, then 10 scheduled tasks fired at :00 in the same minute and all got ConnectError. Recovery happened silently on subsequent ticks.
Reproduction Steps
- Deploy a system that creates N ≥ 5 agents in rapid succession (e.g., via
deploy_systemwith a multi-agent manifest) - Configure each agent with a schedule that fires within 1-2 minutes of deploy
- Observe in
trinity-backendlogs:Failed to import credentials into agent [X] after 3 attempts - Observe first scheduled tick:
HTTP error: ConnectErroron every affected agent - Wait ~5 minutes and re-trigger — works fine
Suggested Fix
Add a readiness probe before the first credential-injection attempt. Poll GET /health on the agent's internal hostname until it returns 200 (or timeout, e.g., 60 s with 1 s interval + exponential backoff):
async def _wait_for_agent_ready(agent_name: str, timeout_s: int = 60) -> bool:
deadline = time.monotonic() + timeout_s
while time.monotonic() < deadline:
try:
r = await http_client.get(f"http://{agent_name}:8000/health", timeout=2.0)
if r.status_code == 200:
return True
except (ConnectError, ReadTimeout):
pass
await asyncio.sleep(1.0)
return FalseAlternatively, push credentials via the agent's filesystem mount (write encrypted file into the workspace volume) and have the agent read-on-startup, removing the HTTP round-trip entirely.
Environment
- Trinity version:
8106fe9 - Docker: compose v2, prod profile
Related
- Circuit breaker in
services/agent_client.py— also trips during this window and can mask recovery
Activity
fix(lifecycle): gate post-start injections on HTTP readiness (#406)
fix(lifecycle): gate post-start injections on HTTP readiness (#406)
fix(lifecycle): gate post-start injections on HTTP readiness (#406) (#…