Skip to content

refactor(llc): consolidate duplicated key-validation, API-key placeholder, and terminal-status checks #9777

Closed

Description

@mrveiss

LLC DRY / consolidation cleanup

Three related duplications in the LLC module, surfaced while implementing the heartbeat run-scoped key lifecycle (#9622/#9623, PR #9772). None are bugs today, but each is a "edit-two-places-in-sync" hazard.

1. Agent-auth middleware reimplements ApiKeyService.validate_key (highest value)

llc/middleware/agent_auth.py hand-rolls the same key-validation query as ApiKeyService.validate_key — key_hash + revoked_at IS NULL + (now) expires_at TTL check — plus an inline hashlib.sha256(...) duplicating services/api_key.py._hash_key.

Note: PR #9772 had to add the expires_at clause to both places to keep them consistent — direct evidence of the hazard (miss one → expired keys accepted = security hole).

Fix: middleware calls ApiKeyService().validate_key(session, raw_key); keep the last_used_at update + request.state wiring in the middleware. Share _hash_key. Add middleware tests (none exist today) before/with the refactor.

2. "<injected-at-runtime>" placeholder duplicated

Defined as _AGENT_API_KEY_PLACEHOLDER in llc/adapters/claude_code_adapter.py but also written as a raw literal in llc/kb/context_builder.py (×2). The adapter skips this exact string when injecting AUTOBOT_LLC_API_KEY — if the builder's literal drifts, the real key leaks/placeholder leaks.

Fix: single shared constant (e.g. llc/config), imported by both.

3. No terminal-status helper on LLCRunStatus

LLCRunStatus (llc/models/enums.py) has no terminal/nonterminal helper. The concept is re-derived ad hoc:

  • llc/scheduler/heartbeat_scheduler.py: _NONTERMINAL_STATUSES = {QUEUED, RUNNING}
  • llc/adapters/copilot_subscription_adapter.py:162 and claude_code_subscription_adapter.py:183: in (LLCRunStatus.COMPLETED, LLCRunStatus.FAILED)

Fix: add LLCRunStatus.is_terminal() (or TERMINAL / NONTERMINAL frozensets) on the enum and replace the scattered checks.

References

Discovered during #9622/#9623 (PR #9772). Related dead-module cleanup: #9776.

Labels: enhancement · backend · priority: low

Activity

  1. mrveiss commented on Jun 9, 2026

    @mrveiss
    OwnerAuthor

    Partially addressed by PR #9811 (merged): the <injected-at-runtime> placeholder is now a single shared constant in llc/config (AGENT_API_KEY_PLACEHOLDER), used by both the producer (HeartbeatContextBuilder) and the adapters. Remaining (this issue stays open): (1) middleware agent_auth.py still re-implements ApiKeyService.validate_key + _hash_key; (2) LLCRunStatus still has no is_terminal() helper (scattered in (COMPLETED, FAILED) checks + scheduler _NONTERMINAL_STATUSES). Both will be done in the next PR.

  2. mrveiss commented on Jun 9, 2026

    @mrveiss
    OwnerAuthor

    Fixed in PR #9829 (merged to Dev_new_gui).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions