Skip to content

bug: credential injection runs on health-check recovery path against already-running containers #421

Closed

Description

@vybe

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_agent recovery 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:

  1. The container is already running and already authenticated via env var.
  2. 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

  1. Deploy Trinity with SUB-002 env-var-based subscription auth.
  2. Put an agent container under load (e.g. 3/3 slots active on long-running tasks).
  3. From another agent, call MCP trigger_health_check or start_agent on the loaded agent.
  4. Observe Credential import attempt N failed: All connection attempts failed in backend logs even though the target agent is running and authenticated.

Suggested Fix

Gate the file-based credential injection on one or more of:

  1. is_fresh_container_create — only inject on actual container creation, not on every health-check / start call against an already-running container.
  2. auth_mode != 'env_var' — skip file-based injection entirely when env-var-based subscription auth is configured for the agent.
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p2type-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions