Repository navigation
Description
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:
- Container recreation — recreates the container with the new subscription's
CLAUDE_CODE_OAUTH_TOKENas an env var. This works correctly. - Workspace credential import — separately calls
import_credentialswhich looks for a.credentials.encfile in the agent workspace. This is a legacy path for credential-file-based injection and does not apply to subscriptions registered viaclaude 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
- Register a subscription via
claude setup-token(OAuth token, not API key) - Assign it to an agent
- Trigger an execution that causes the subscription to hit its rate limit (429)
- Observe SUB-003 auto-switch firing in backend logs
- Observe
Failed to import credentials into agent X after 3 attempts: No .credentials.enc file foundimmediately after container restart - Observe execution status =
FAILEDdespiterestart_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
AndriiPasternak31 commentedon May 11, 2026
/claim
github-actions commentedon May 11, 2026
Claimed by @AndriiPasternak31. Label
status-in-progressadded.Next steps:
feature/606-<short-slug>Fixes #606AndriiPasternak31 commentedon May 11, 2026
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.
docs(feature-flows): sync session-tab cold-turn lock + subscription a…
fix(tests): adopt sys.modules snapshot/restore in #606 chain test
docs(lint): mention named-helper escape hatch in sys.modules error me…
fix(tests): adopt sys.modules snapshot/restore in #783 cleanup test
test(subscription): pin auto-switch chain skips .credentials.enc impo…
docs(feature-flows): sync session-tab cold-turn lock + subscription a…
fix(tests): adopt sys.modules snapshot/restore in #606 chain test
9 remaining items
vybe commentedon Jun 1, 2026
Shipped in v0.6.0 (released via #998).
fix(sub-003): per-agent lock for auto-switch — serialize concurrent f…
feat: rotate subscription credentials via hot-reload, not container r…
test(#1771): restore sys.modules the four 1771c files evict (#762 lint)
test(#1771): restore sys.modules the four 1771c files evict (#762 lint)
test(db/schedules): edge-case + property coverage for CAS status writ…
fix(credentials): subscription agents import a committed .credentials…
fix(agent-runtime): drop the startup credentials auto-import that cal…