Repository navigation
Description
Summary
services.agent_service.lifecycle attempts to inject Claude Code credentials into an agent container on the health-check / recovery path, even when the container is already running with valid env-var-based auth (CLAUDE_CODE_OAUTH_TOKEN per SUB-002). When the target container is busy serving other requests, the 3-attempt injection (~2s between attempts) fails with All connection attempts failed, adding log noise and unnecessary container load during exactly the moments when the container is already under stress.
Component
Backend / Agent lifecycle (services.agent_service.lifecycle)
Priority
P2 — not a user-visible failure by itself, but: (a) it is strictly redundant when env-var auth is in use; (b) it fires repeatedly under load exactly when the system is fragile; (c) the 3-retry-then-ERROR pattern pollutes logs and confuses investigations.
Error
WARNING services.agent_service.lifecycle: Credential import attempt 1 failed: All connection attempts failed
WARNING services.agent_service.lifecycle: Credential import attempt 2 failed: All connection attempts failed
WARNING services.agent_service.lifecycle: Credential import attempt 3 failed: All connection attempts failed
ERROR services.agent_service.lifecycle: Failed to import credentials into agent <name> after 3 attempts: All connection attempts failed
Location
- Logger:
services.agent_service.lifecycle - Likely file:
backend/services/agent_service/lifecycle.py - Triggered via:
trigger_health_check/start_agentrecovery path (after circuit-breaker OPEN → CLOSED transitions, or when another agent calls these MCP tools)
Root Cause
Since SUB-002, subscription tokens are injected as CLAUDE_CODE_OAUTH_TOKEN env var at container creation. The file-based credential injection into the running container's Claude Code credential file appears to be a legacy/dual-path operation. It is still being invoked on recovery/health-check paths (e.g. when another agent calls MCP trigger_health_check / start_agent), even though:
- The container is already running and already authenticated via env var.
- The target HTTP endpoint inside the agent is under load and unable to accept the injection connection.
This was observed during an overload window where delegate agents were serving long-running tasks and the orchestrator (cleon) called trigger_health_check; the health check succeeded, but an accompanying credential re-injection failed unnecessarily.
Reproduction Steps
- Deploy Trinity with SUB-002 env-var-based subscription auth.
- Put an agent container under load (e.g. 3/3 slots active on long-running tasks).
- From another agent, call MCP
trigger_health_checkorstart_agenton the loaded agent. - Observe
Credential import attempt N failed: All connection attempts failedin backend logs even though the target agent is running and authenticated.
Suggested Fix
Gate the file-based credential injection on one or more of:
is_fresh_container_create— only inject on actual container creation, not on every health-check / start call against an already-running container.auth_mode != 'env_var'— skip file-based injection entirely when env-var-based subscription auth is configured for the agent.- Make failure non-blocking and non-ERROR if the container is already running (downgrade to DEBUG or INFO, since the container is already authenticated via env var).
# Pseudo-code
if container_was_just_created or auth_mode == "credentials_json":
try:
inject_credentials(...)
except ConnectionError:
log.warning(...) # only a problem on fresh create
else:
log.debug("skipping credential injection; container already running with env-var auth")Environment
- Trinity version:
3b02871 - Deployment: Docker Compose (prod profile), SUB-002 env-var auth
Related
- SUB-002 — Subscription token injection as
CLAUDE_CODE_OAUTH_TOKEN - bug: inter-agent task timeout hardcoded at 600s ignores per-agent execution_timeout_seconds #418 — Inter-agent 600s timeout (this credential-injection noise fired during the same overload window and added to confusion during investigation)
Activity
fix(lifecycle): skip credential/skill injection on idempotent start (#…
fix(lifecycle): skip credential/skill injection on idempotent start (A…
fix(agent-lifecycle): stop showing credentials_injection: failed for …
test(subscription): pin auto-switch chain skips .credentials.enc impo…
test(subscription): pin auto-switch chain skips .credentials.enc impo…
test(subscription): pin auto-switch chain skips .credentials.enc impo…
fix(sub-003): per-agent lock for auto-switch — serialize concurrent f…
feat: rotate subscription credentials via hot-reload, not container r…
test(ent1108): the start-hook test leaves no task on a closed loop