Skip to content

fix(llc): keep a claude_code auth failure's reason on the run and pause the agent (#20202) - #21055

Closed
mrveiss wants to merge 5 commits into
mainfrom
issue-20202
Closed

mrveiss wants to merge 5 commits into
mainfrom
issue-20202

Conversation

@mrveiss

@mrveiss mrveiss commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

Thinking Path

Single-issue rationale

Both parts touch the llc/ scheduler and budget files and the same test directories, and the two #20930 items were left over from a train that already carries the rest of that issue; a separate PR would repeat a full CI run for two small changes in files this PR already edits.

What Changed

  • llc/exceptions.py: AdapterRunFailed takes an optional reason, appended to its message; new AdapterAuthFailed subclass.
  • llc/scheduler/auth_failure.py (new): raises the subclass when the adapter result equals the shared AUTH_FAILED_REASON constant (no output string matching), and pauses the agent through ControlsService.pause_agent(actor_type="system") after the run row is committed. A pause failure is logged as a warning. The company_id guard is kept. No new status, no migration.
  • llc/scheduler/heartbeat_scheduler.py: uses the two helpers; the file stays at its size baseline.
  • llc/models/budget.py, llc/api/budget.py (follow-up: minor review findings from train PR #20911 for company-os work-item hierarchy (#20478) #20930): token_limit gets a lower bound (TOKEN_LIMIT_MIN), so a value below the signed 64-bit minimum is a 422 instead of a database error.
  • llc/scheduler/liveness_monitor.py (follow-up: minor review findings from train PR #20911 for company-os work-item hierarchy (#20478) #20930): the unfiled-recovery notice is also written to the company activity feed; the existing router publish is kept.
  • Tests: new test_auth_failure_pause_20202.py; extended test_budget_raise_approval_8214.py and test_work_item_hierarchy_18978.py.
  • Review fix: the unfiled-recovery activity entry (_log_unfiled_recovery) runs in its own savepoint under try/except, so a failed write is logged as a warning and can no longer fail the recovery (test added).

Deviations that need the owner's review

  1. The pause applies to both Claude CLI adapters (plain and subscription), not to the claude_code adapter type alone. AUTH_FAILED_REASON is produced only by those two, the subscription adapter is the one that depends on a login, and other adapters' failures stay plain (bug(company-os): native AutoBot-agent runs are never charged to a budget (cost forwarding unwired) #20883). Narrowing it is a one-line change.
  2. The unfiled-recovery notice goes to the company activity feed, not to a per-user in-app notification. The user-facing notification pieces that exist are not wired to anything a user reads: no component uses the live-notification composable, and the Redis-backed in-app store has no API or UI reader. The activity feed is the existing surface company users see, and each record is pushed live to the dashboard. The unwired notifications are tracked in bug(company-os): router notifications never reach a user — useLLCNotifications is unwired and the in-app store has no reader #21045.

Please object here if either should change.

Verification

Model Used

Claude Sonnet 5.5

Closes #20202
Refs #20930, #21045

🤖 Generated with Claude Code

@mrveiss mrveiss added this to the M2 - Company OS workflow-ready milestone Nov 7, 2026
@mrveiss mrveiss added area: company-os Wave 4 · cluster A — Company OS & LLC canvas backend phase:M2.5 Heartbeats fire reliably labels Nov 7, 2026
@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Notice: 29 open PRs — past the runaway threshold (25)

This is not a request to defer this PR. There is no cap on open PRs (#19397) -- nothing blocks opening or pushing a PR.

This notice only means the count is high enough to be worth a glance for a runaway — something opening PRs in a loop, or a merge pipeline that has stalled so nothing is draining.

Currently open:

If the queue is draining normally, ignore this. Otherwise:

  1. Check whether CI is dispatching at all — see the ci-dispatch-watchdog status on these PRs
  2. Approved PRs land through the merge train the coordinator sequences (merge commits, never squash) -- check the open chore(merge-train) PR is moving, not each PR on its own
  3. Look for a loop opening near-identical PRs

Warn-only runaway detector — .github/workflows/pr-queue-gate.yml. It never blocks a merge.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

✅ SSOT Configuration Compliance: Passing

🎉 No new hardcoded values of either class — ssot and other both block.

Known backlog in pipeline-scripts/hardcoded_values_baseline.txt is suppressed and tracked in #14371.

@mrveiss

mrveiss commented Nov 7, 2026

Copy link
Copy Markdown
Owner Author

Code review (code-reviewer pass) at b55f14f

Verdict: APPROVE. Every claim checks out against the code. No blocking finding. Two items are owner decisions, listed below, and are not judged here. Behind main: 0 (main is an ancestor of the head).

# Severity Where Finding
1 Verified scheduler/heartbeat_scheduler.py _run_adapter, _finish_run; scheduler/auth_failure.py Ordering holds. _finish_run commits the FAILED row in its own session and swallows its own errors. pause_after_auth_failure then opens a second session and wraps the whole pause in try/except Exception, logging a warning. A pause failure cannot roll back the row or raise to the caller. The tests assert the commit count before the pause (1) and after a failing pause (row 1, pause session 0).
2 Verified auth_failure.py Only AdapterAuthFailed is paused, decided by equality with the shared AUTH_FAILED_REASON constant (no output matching). Other adapters' failures stay plain. The company_id guard skips the pause and logs.
3 Verified ControlsService.pause_agent The system-actor pause matches the existing quota-exhaustion path (actor_type="system", actor_user_id=None). The agent row is looked up and updated by company_id and agent_id. It writes a control.agent_paused activity record, so it is audited. It runs inside a scheduler, not a request, so there is no user to authorise.
4 Verified api/budget.py, models/budget.py UpdateLimitRequest.token_limit now has ge=TOKEN_LIMIT_MIN, so a value below the minimum gives a Pydantic 422, not a DB error. BudgetOverridePayload already has gt=0. The literal boundary test covers -263 (accepted) and -263-1 (rejected).
5 Verified scheduler/liveness_monitor.py The activity-feed write uses the run's company_id, ActorType.SYSTEM and the existing audit helper, in the recovery savepoint. The test filters by company and expects exactly one heartbeat.failed row. No cross-tenant path. The router publish is kept.
6 Low ControlsService.pause_agent (shared) If an operator pauses the agent while the run is in flight, the system pause stores pre_pause_status = paused, and a later resume restores paused. This is the same behaviour as the existing quota auto-pause, so it is not new here. The pause belongs to the fix for the shared method, not this PR. Suggest a follow-up issue that skips or preserves the status when the agent is already paused.
7 Low liveness_monitor.py _log_unfiled_recovery The new audit write is in the recovery savepoint. A failed activity write now fails that run's recovery, where before only the best-effort router publish ran. This follows the file's own "audit fails the recovery" design, so it is consistent, but it is a behaviour change worth knowing.
8 Info UpdateLimitRequest ge=TOKEN_LIMIT_MIN allows negative token limits (the issue asked for the 64-bit floor). A gt=0 rule, as on the approval payload, would be stricter. This is not required by #20930.
9 Verified Hygiene No new env-backed values are needed, and there are no fixed sleeps or line-number comments in code. heartbeat_scheduler.py is 979 lines with net 0 lines (6 added, 6 removed), so it stays at its baseline. liveness_monitor.py is 353 lines and auth_failure.py is 60. New and changed functions are at most 30 lines. The _run_adapter growth is one line, inside an already-baselined function.
10 Verified #20202 ACs AC1 (failed with an auth-specific reason): the reason is on the run row error. AC3 is already ticked. AC2 (visible without reading transcripts): HeartbeatMonitor.vue renders run.error for a run. Acceptable to close. AC2 depends on the monitor view being the operator surface.
11 Verified #20930 The two remaining boxes (token lower bound, boundary test) are delivered. The notice box is already ticked and this PR reroutes it to the activity feed (owner decision 2 below). Refs rather than Closes is correct.
12 Info Threads No inline review comments or reviews exist on this PR. The only comments are bot notices (a rate-limited review bot, an open-PR count notice, an SSOT check that passed). The GraphQL API was rate limited, so thread state was read from the REST comment and review lists. No unresolved threads found.

Owner decisions (not judged by this review)

  1. The auto-pause covers both Claude CLI adapters (plain and subscription), not only the claude_code adapter type. Narrowing it is a one-line change.
  2. The unfiled-recovery notice goes to the company activity feed instead of a per-user in-app notification, because nothing reads the in-app store (bug(company-os): router notifications never reach a user — useLLCNotifications is unwired and the in-app store has no reader #21045).

@mrveiss

mrveiss commented Nov 7, 2026

Copy link
Copy Markdown
Owner Author

Code review (delta check) at ff3aa6d, after the APPROVE at b55f14f. One own commit since then. _log_unfiled_recovery now writes the activity row inside session.begin_nested(), wrapped in try/except Exception with logger.warning(..., exc_info=True), so a failed write rolls back only its own savepoint and the recovery completes. New test test_a_failed_unfiled_recovery_activity_write_does_not_fail_the_recovery covers that path. The finding is resolved and the verdict stays APPROVE. Follow-up for the missing already-paused guard: #21056.

This was referenced Nov 7, 2026
@mrveiss

mrveiss commented Nov 8, 2026

Copy link
Copy Markdown
Owner Author

Code review (code-reviewer pass) at af0e0bd
Verdict: APPROVE (0 blocking). Closes #20202 is correct; Refs #20930, #21045 is correct.

# Severity File:line Issue Fix
1 LOW llc/scheduler/liveness_monitor.py:279 _note_unfiled_recovery now takes 9 positional parameters. Bundle (session, svc, log) or pass the monitor, in a later touch.
2 LOW llc/models/budget.py:18, llc/api/budget.py:84 TOKEN_LIMIT_MIN is the signed 64-bit minimum, so negative limits still pass validation. This is what #20930 asked for (no database error), not a semantic bound. If a negative limit is meaningless, use ge=0 in a follow-up.
3 info llc/scheduler/auth_failure.py:32-37 Auth failure is detected by equality with the shared AUTH_FAILED_REASON (no output matching). ClaudeCodeSubscriptionAdapter subclasses ClaudeCodeAdapter (claude_code_subscription_adapter.py:59), so deviation 1 (both adapters pause) holds. The pause is best-effort, after the FAILED row commit (heartbeat_scheduler.py _finish_run then pause_after_auth_failure). A failing pause is logged as a warning. none
4 info llc/scheduler/liveness_monitor.py:303-326 The activity entry is in a savepoint under try/except with a logged warning and exc_info. HEARTBEAT_FAILED exists (models/enums.py:473). The router publish is kept. none

Deviations 1 and 2 in the PR body are owner decisions; the code does what is stated, and both are one-line changes if the owner objects.

CodeRabbit threads: 0 inline threads; the only CodeRabbit comment is a rate-limit notice.
AC coverage (Closes #20202):

  • AC1 ✓ The reason is carried by AdapterAuthFailed/AdapterRunFailed.reason (llc/exceptions.py), raised at heartbeat_scheduler.py _dispatch_registry_adapter via adapter_run_failure (auth_failure.py:29-37). It is stored through error_msg = str(exc). Asserted in test_auth_failure_pause_20202.py test_the_run_row_is_failed_with_the_reason....
  • AC2 ✓ The run's error is rendered in autobot-frontend/src/views/llc/HeartbeatMonitor.vue:76-84. The agent is also paused.
  • AC3 ✓ (already ticked) llc/adapters/tests/test_claude_code_auth_failure_20202.py.
    Main-merge commits: clean (git show --cc is empty for the four main-merge commits). heartbeat_scheduler.py is net 0 lines (6 additions, 6 deletions).

@mrveiss

mrveiss commented Nov 8, 2026

Copy link
Copy Markdown
Owner Author

Landed via merge train #21184, merge commit 209d547 (frozen head af0e0bd is an ancestor of it).

@mrveiss mrveiss closed this Nov 8, 2026
timothybrush pushed a commit to timothybrush/AutoBot-AI that referenced this pull request Nov 9, 2026
@mrveiss mrveiss reopened this Nov 18, 2026
@mrveiss

mrveiss commented Nov 18, 2026

Copy link
Copy Markdown
Owner Author

Branch audit (zero-change deletion). This PR shows 0 changed files against main (head c1855cff6c): every change it carried is already in main. Closing and deleting the branch through this PR; it can be restored with the Restore branch button below.

@mrveiss mrveiss closed this Nov 18, 2026
@mrveiss
mrveiss deleted the issue-20202 branch November 18, 2026 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: company-os Wave 4 · cluster A — Company OS & LLC canvas backend phase:M2.5 Heartbeats fire reliably

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(company-os): claude_code heartbeats fail silently when the agent's OAuth session has expired, and keep firing every 15 minutes

1 participant