Skip to content

refactor(ai): the embedding LRU cache, the webhook replay guard and the Radar memory cache use cachetools, with a bound and a TTL #1993

Description

@dan-petty

Friction

  • Two bounded in-process caches are hand-written on OrderedDict, each with its own lock and its own eviction: the embedding LRU cache and the webhook replay guard.
  • The in-memory cache of the Cloudflare Radar client is a plain dict with no bound and no expiry.
  • cachetools is already a dependency, and the GitHub session transport uses its LRUCache.

Evidence

Checked at main a752a14.

Existing tool considered

  • cachetools (LRUCache, TTLCache) gives a bounded cache with eviction and expiry. It is already a pinned dependency.

Proposed remedy

  • Replace _EmbeddingLRUCache and the webhook replay guard with cachetools.LRUCache, kept under the locks that exist. Keep the hit and miss counters.
  • Bound the Radar memory cache with cachetools.TTLCache(maxsize=..., ttl=<the threat-intel TTL>).

Acceptance criteria

  • rg -n 'OrderedDict' src/devops_cli/ai/rag/embeddings.py src/devops_cli/server/routes/webhooks.py prints nothing.
  • Eviction at capacity, the hit and miss counts and the embedding-cache metrics behave as today. The tests that exist pass, moved where necessary.
  • A webhook delivery id seen within the capacity of the cache is still refused as a replay.
  • The Radar memory cache holds at most its bound and drops entries after the TTL. The test uses the timer of cachetools or a fake clock, never wall time.
  • Tests run offline, with fakes only at the HTTP edge or the process edge. No test patches a devops_cli name.
  • uv run devops ci passes.

Source: #1113

Proposed design

Problem statement

Three in-process caches are hand-written or unbounded. Two use OrderedDict with manual eviction. One is a plain dict with no bound and no expiry. cachetools is already a pinned dependency. This change replaces all three with cachetools.LRUCache or TTLCache. It keeps the existing locks, hit and miss counters, and replay refusal behavior.

Acceptance criteria

  • Criterion 1: No OrderedDict import or usage remains in the embedding cache or webhook replay guard files.
    Verification: Run rg -n 'OrderedDict' src/devops_cli/ai/rag/embeddings.py src/devops_cli/server/routes/webhooks.py. The command prints nothing.
  • Criterion 2: Eviction at capacity, hit and miss counts, and embedding-cache metrics behave as before the change.
    Verification: Run the existing tests for the embedding cache. All tests pass.
  • Criterion 3: A webhook delivery id seen within the cache capacity is still refused as a replay.
    Verification: Run the webhook replay test. The delivery is refused.
  • Criterion 4: The Radar memory cache holds at most its bound and drops entries after the TTL.
    Verification: Run the Radar cache test. It uses the timer of cachetools or a fake clock, never wall time.
  • Criterion 5: Tests run offline with fakes only at the HTTP edge or the process edge.
    Verification: Inspect the test code. No test patches a devops_cli name.
  • Criterion 6: The full CI pipeline passes.
    Verification: Run uv run devops ci. The command exits with code 0.

Key questions

  • What maxsize and TTL does the Radar memory cache use?: The body leaves both as placeholders. A person sets the maxsize and the TTL value before implementation. (proposed)
  • What maxsize does the webhook replay guard currently use?: The code defines a bound on the OrderedDict. The exact value is read from the existing code at that location. [sources: src/devops_cli/server/routes/webhooks.py:164]
  • Do the existing locks stay around the new cachetools objects?: Yes. The body states the caches are kept under the locks that exist. (proposed)
  • Does the embedding cache keep its hit and miss counters after the refactor?: Yes. The body states to keep the hit and miss counters. (proposed)

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/refactor
    on Oct 23, 2026
  2. dan-petty commented on Oct 23, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

    • Type: type/refactor. The candidate changes cache implementations to use cachetools.
    • Priority: P2-Medium. The change improves code quality and maintainability.
    • Value: Medium. The change helps with caching behavior in several parts of the system.
    • Effort: Medium. The change requires replacing hand-written caches with cachetools.
  3. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Status set to Ready at bb46660 (see proposed design in issue body).

  4. added
    priority/p2-medium
    status/ready
    value/medium
    effort/medium
    on Oct 28, 2026
  5. added a commit that references this issue on Oct 29, 2026
  6. removed
    status/ready
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumsource/agenttype/refactorvalue/medium

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions