Skip to content

bug: auto-switch (SUB-003) marks triggering execution failed due to spurious .credentials.enc injection after container restart #606

Closed

Description

@vybe

Summary

When SUB-003 auto-switch fires (rate-limit 429 or auth error), it correctly recreates the agent container with the new subscription's CLAUDE_CODE_OAUTH_TOKEN env var. However, after the container is ready, the lifecycle code also attempts to import a .credentials.enc file from the agent workspace. This file does not exist for OAuth token-based subscriptions — those use env var injection, not file-based credential import. The import fails after 3 retries, causing the triggering execution to be marked as FAILED even though the container switch succeeded and subsequent executions on the new subscription work correctly.

Component

Backend / Subscription Auto-Switch (SUB-003) / Agent Lifecycle

Priority

P2

Error

Failed to import credentials into agent {agent_name} after 3 attempts: No .credentials.enc file found in agent workspace

Logger: services.agent_service.lifecycle

Immediately followed by:

[SUB-003] Auto-switch complete: {'switched': True, 'agent_name': '...', 'restart_result': 'success'}
Async task completed for {agent_name}: status=TaskExecutionStatus.FAILED

Root Cause

The auto-switch flow performs two credential-injection steps after a rate-limit event:

  1. Container recreation — recreates the container with the new subscription's CLAUDE_CODE_OAUTH_TOKEN as an env var. This works correctly.
  2. Workspace credential import — separately calls import_credentials which looks for a .credentials.enc file in the agent workspace. This is a legacy path for credential-file-based injection and does not apply to subscriptions registered via claude setup-token (OAuth token flow).

Step 2 is unnecessary for OAuth token subscriptions (the container already has the correct token from step 1), but its failure is propagated up and causes the triggering execution to be marked FAILED. The restart_result: 'success' in the auto-switch result confirms step 1 succeeded, but the execution status reflects the spurious step 2 failure.

Impact

Every auto-switch event causes one unnecessary execution failure. Without this bug, the triggering execution could potentially be retried on the new subscription rather than being terminated. For high-frequency scheduled agents, this results in measurable false-failure rates in the execution history.

Reproduction Steps

  1. Register a subscription via claude setup-token (OAuth token, not API key)
  2. Assign it to an agent
  3. Trigger an execution that causes the subscription to hit its rate limit (429)
  4. Observe SUB-003 auto-switch firing in backend logs
  5. Observe Failed to import credentials into agent X after 3 attempts: No .credentials.enc file found immediately after container restart
  6. Observe execution status = FAILED despite restart_result: 'success'

Suggested Fix

In the auto-switch credential injection path, skip the .credentials.enc workspace import step when the agent's subscription uses OAuth token injection (i.e., CLAUDE_CODE_OAUTH_TOKEN env var). The container recreation already handles credential injection correctly for this case.

# Pseudocode — in the auto-switch post-restart credential step:
if subscription.auth_type == "oauth_token":
    # Container already has CLAUDE_CODE_OAUTH_TOKEN injected via env var
    # No .credentials.enc import needed
    pass
else:
    # Legacy credential file import for API-key-based agents
    await import_credentials_from_workspace(agent_name)

Alternatively, the import_credentials function could be made non-fatal (log a warning instead of propagating the error) when the agent is confirmed running with a valid OAuth token.

Environment

  • Trinity version: 7d8abe8
  • Docker version: 29.3.0
  • OS: Ubuntu 22.04

Related

  • Feature: Subscription Auto-Switch (SUB-003)
  • Feature: Subscription Management (SUB-002) — OAuth token injection via CLAUDE_CODE_OAUTH_TOKEN
  • Backend service: services/agent_service/lifecycle.py (import_credentials function)
  • Backend service: services/subscription_auto_switch.py

Activity

  1. AndriiPasternak31 commented on May 11, 2026

    @AndriiPasternak31
    Contributor

    /claim

  2. added
    status-in-progress
    on May 11, 2026
  3. github-actions commented on May 11, 2026

    @github-actions

    Claimed by @AndriiPasternak31. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/606-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #606
  4. AndriiPasternak31 commented on May 11, 2026

    @AndriiPasternak31
    Contributor

    Filed retry-after-auto-switch follow-up as #792 — covers the residual gap for one-shot scheduled executions, with the full design surface (idempotency, retry budget, capacity-slot lifecycle, subscription usage attribution, 2h-skip-list interaction, per-agent concurrency lock). Will link bidirectionally in the close PR.

  5. 9 remaining items

  6. added
    status-in-dev
    and removed
    status-in-progress
    on May 28, 2026
  7. vybe commented on Jun 1, 2026

    @vybe
    ContributorAuthor

    Shipped in v0.6.0 (released via #998).

  8. added
    complexity-low
    theme-reliability
    and removed
    status-in-dev
    on Jun 1, 2026
  9. added 2 commits that reference this issue on Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

complexity-lowpriority-p2theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions