Skip to content

fix(roadmap): correct visibility, keys and bounds of Valkey caches #6611

Closed

Description

@dan-petty

Problem

PR #6583 (#4668) added the shared listing snapshot (ValkeySnapshotStore and TieredSnapshotStore in src/devops_cli/roadmap/stores/listing_shared.py) and the fact caches (TieredMapping in src/devops_cli/valkey/tiered.py). Its second review ended after the merge. It confirmed four defects on main (b2a277b):

  1. A failed visibility read removes the shared copy of a public repository. repository_is_public (line 457) gives False on DevOpsCLIError and keeps no answer (line 470). Then _public (line 555) calls shared.remove(repo) (line 560), which sends UNLINK of the whole snapshot key. The requirement "No key of a private repository" of openspec/specs/project-brain/shared-cache/spec.md asks for this ("A failed visibility read SHALL count as private"). The visibility read uses GraphQL, and the GraphQL quota runs out from time to time. A scratch test proved it: one read that fails with "API rate limit exceeded" removes the key. While the read fails, each lane round sends one more GraphQL request, no process writes the copy again, and each cold pod reads the full listing (about 66 pages).
  2. _seen is keyed by the raw repository text (lines 219, 235, 258, 329, 388 and 394), but the Valkey key and the visibility cache use RepoRef(repo).key. Two spellings of one repository in one process (a name from config and the full_name of a webhook) share one key but have two _seen entries. A scratch test proved it: 6 of 6 alternate saves write a full _replace (1 of 6 with one spelling).
  3. _decompressed (line 413) limits each field to DEFAULT_BRAIN_VALUE_MAX_BYTES, but no limit applies to the total. A scratch test proved it: 150 fields of 1,040 bytes of zlib, each of which expands to just under 1 MiB, gave a tracemalloc peak of 151 MiB. About 3,000 such fields (about 3 MB in Valkey) go above the 3Gi memory limit of roadmap-service. ADR 0023 says that shared values are untrusted.
  4. A Valkey hit puts a fact in the memory tier with a full new TTL (tiered.py line 129, TTLCache at line 102). The code does not use the time that is left on the Valkey key. So a long-lived process can give a PyPI or deps.dev fact up to about 48 h after the source read. The documented bound is 24 h (DEFAULT_BRAIN_FACT_TTL_SECONDS).

Expected

  1. The visibility check gives one of three results: public, private or unknown. Only a private answer removes the key. An unknown answer does not use, write or remove the shared copy. The store keeps a failure for a short time (for example 60 s, or until the reset time that the error gives), so that each lane round does not read the visibility again. The spec requirement changes to agree. The owner decision of 2026-11-21 (comment on PR perf(roadmap): share the listing snapshot and facts in Valkey #6583) accepts a window of up to 1 h for a repository that becomes private.
  2. _seen uses RepoRef(repo).key, as snapshot_key does.
  3. A named bound in config/defaults.py limits the total expanded bytes of one load. A load over the bound is a miss and counts as the other misses do.
  4. A memory entry expires when its Valkey key expires. For example, cachetools.TLRUCache with the TTL that is left (PTTL in the same pipeline), or a read time in the stored value.

Acceptance criteria

  • Test first for each point. Each test fails on the base.
  • A test: a public round, then a round whose visibility read fails. The key stays, and the second round does not use or write the shared copy.
  • A test: a private answer still removes the key and counts result="removed".
  • A test: two spellings of one repository alternate in one process. Only the first save writes a temporary key.
  • A test: many small fields that each pass the field bound but together pass the total bound make the load a miss.
  • A test: a fact read from Valkey near the end of its TTL leaves the memory tier when the Valkey key expires.

Dependencies

None.

Source

Source: #6583

Alternatives check of 2026-11-21: in-use.
The repository already uses this tool.
Tool: tkem/cachetools (https://github.com/tkem/cachetools), MIT, last activity 2026-11-19, maintained.
Reason: Searches found cachetools, a maintained MIT library whose TTLCache/TLRUCache supplies only the in-process expiry tier; the three-state visibility result, total expanded-byte bound and RepoRef key normalization stay project code.

Covers:

  • In-process cache with per-entry expiry times
  • LRU eviction that bounds memory use
  • Dict-like Mapping API for the memory tier

Lacks:

  • No bridge from Valkey PTTL to memory expiry
  • No three-state public/private/unknown visibility
  • No total expanded-byte bound on a snapshot load
  • No RepoRef key normalization for seen records

Proposed design

Problem statement

PR #6583 added the shared listing snapshot and tiered fact caches, and four defects remain on main. An unknown visibility read removes a public snapshot, _seen splits one repository by spelling, no total bound limits expanded snapshot bytes, and a memory fact can outlive its Valkey key. The fix adds a three-state visibility result with a short failure cache, normalizes _seen, adds a total load bound, and ties the memory TTL to the Valkey PTTL. The shared-cache spec changes to match.

Acceptance criteria

Requirement: Visibility check returns three states

The visibility check SHALL return Visibility.PUBLIC, Visibility.PRIVATE, or Visibility.UNKNOWN.

Scenario: Failed read returns unknown

  • WHEN repository_visibility reads a repository and GitHubRoadmapStore.repository_is_private raises DevOpsCLIError
  • THEN repository_visibility returns Visibility.UNKNOWN.

Requirement: Unknown visibility is cached for a short time

The store SHALL keep an unknown visibility answer for DEFAULT_BRAIN_VISIBILITY_FAILURE_TTL_SECONDS before it reads GitHub again.

Scenario: Second call inside the failure window

  • WHEN repository_visibility fails once, and a second call runs within DEFAULT_BRAIN_VISIBILITY_FAILURE_TTL_SECONDS
  • THEN the second call returns Visibility.UNKNOWN and sends no GitHub request.

Requirement: Unknown visibility leaves shared copy

A load or save with an unknown visibility answer SHALL NOT read the shared records, write the shared copy, or remove the shared key.

Scenario: Public round then failed visibility round

  • WHEN a public round saves a snapshot, then a second round runs with a visibility read that raises DevOpsCLIError
  • THEN the snapshot key remains, the second round sends no HGET, HSCAN, HSET or UNLINK for that key, and it counts devops_cli_brain_reads_total{store="snapshot",result="unknown"} and devops_cli_brain_writes_total{store="snapshot",result="unknown"}.

Requirement: Private visibility removes shared copy

A private visibility answer SHALL remove the shared snapshot and SHALL count devops_cli_brain_writes_total{store="snapshot",result="removed"}.

Scenario: Private answer removes key

  • WHEN the shared tier holds a snapshot and repository_visibility returns Visibility.PRIVATE
  • THEN the store sends UNLINK for the snapshot key and the removed counter rises by one.

Requirement: Snapshot store keys seen records by RepoRef

ValkeySnapshotStore SHALL key _seen by RepoRef(repo).key in every read, write, and removal path.

Scenario: Two spellings in one process

  • WHEN one process saves snapshots for Owner/Repo and owner/repo alternately after the first full save
  • THEN only the first save writes a temporary key, and each later save sends an HSET update.

Requirement: Total expanded-byte bound

A load of the shared snapshot SHALL return a miss when the total expanded bytes of its records pass DEFAULT_BRAIN_SNAPSHOT_LOAD_MAX_BYTES.

Scenario: Many small fields pass the total bound

  • WHEN the fake holds two records that each expand under DEFAULT_BRAIN_VALUE_MAX_BYTES but together pass a lowered DEFAULT_BRAIN_SNAPSHOT_LOAD_MAX_BYTES
  • THEN ValkeySnapshotStore.records returns None and TieredSnapshotStore.load counts devops_cli_brain_reads_total{store="snapshot",result="fallback"}.

Requirement: Memory entry expires with Valkey key

A memory entry filled from a Valkey hit SHALL expire no later than the Valkey key.

Scenario: Fact read near key expiry

  • WHEN a TieredMapping reads a fact whose Valkey PTTL is 5 s, and the test clock advances 6 s
  • THEN the next read misses the memory tier and sends a new GET to Valkey.

Requirement: Shared-cache spec records unknown visibility

The shared-cache spec SHALL state that a failed visibility read is unknown and leaves the shared copy in place.

Scenario: Spec validation

  • WHEN uv run openspec validate --all runs after the change
  • THEN the command exits 0 for the shared-cache spec delta that replaces the failed-read sentence with the unknown-visibility sentence.

Existing Tool Considered

The alternatives check in the issue body records verdict in-use for tkem/cachetools (MIT, maintained). See that check for what the tool covers and lacks. The proposal uses cachetools.TLRUCache for the memory tier and writes the remaining glue in project code: about 400 lines, 40k tokens to build, 8k to keep and 8k to fix, and 2 days.

Key questions

  • Which type should the visibility check return?: Add a Visibility enum with PUBLIC, PRIVATE and UNKNOWN in listing_shared.py. repository_visibility returns it. The cache stores each result with the matching TTL. (best practice) [sources: src/devops_cli/roadmap/stores/listing_shared.py, openspec/specs/project-brain/shared-cache/spec.md]
    • Rejected: Boolean with a sentinel. A sentinel mixes the unknown state with a false value and keeps the removal bug.
    • Chosen: Three-state enum. The expected behavior names three results, and each branch has a different action.
    • Rejected: Exception for unknown. The load and save paths must continue with the local tier, so an exception adds control flow.
  • How long should the store keep a failed visibility read?: Keep an unknown answer for DEFAULT_BRAIN_VISIBILITY_FAILURE_TTL_SECONDS = 60 s. Keep the positive visibility TTL unchanged and no more than 1 h, as the owner accepted. (best practice) [sources: src/devops_cli/config/defaults.py, src/devops_cli/roadmap/stores/listing_shared.py]
    • Chosen: 60 s. It stops one GraphQL request each lane round and stays inside the owner's 1 h window.
    • Rejected: Error reset time. The error does not always give a reset time, and a fallback is still needed.
    • Rejected: No cache. Each lane round would send another GraphQL request while the quota is exhausted.
  • How should telemetry count an unknown visibility answer?: Add BrainRead.UNKNOWN and BrainWrite.UNKNOWN. An unknown load counts unknown and returns the local copy. An unknown save counts unknown and writes nothing. (best practice) [sources: src/devops_cli/config/constants.py, src/devops_cli/telemetry/instruments.py, src/devops_cli/roadmap/stores/listing_shared.py]
    • Rejected: Reuse private. Unknown is not private, and the counter would hide a failed visibility read.
    • Rejected: Reuse fallback. Fallback means the shared tier is down, not that visibility is unknown.
    • Chosen: Add unknown results. The operator can tell a failed visibility read from a private repository and from a down tier.
  • How should the load bound the total expanded bytes?: Add DEFAULT_BRAIN_SNAPSHOT_LOAD_MAX_BYTES to config/defaults.py. _decompressed adds each expanded length and returns None when the sum passes the bound. (best practice) [sources: src/devops_cli/config/defaults.py, src/devops_cli/roadmap/stores/listing_shared.py]
    • Rejected: Per-field bound only. The body shows 150 fields pass the field bound and use 151 MiB.
    • Chosen: Total expanded-byte bound. It caps one load and keeps the 14 MB devops-cli snapshot well inside the bound.
    • Rejected: Streaming validation with no total bound. It still holds the expanded records in memory and does not cap the peak.
  • What value should the total expanded-byte bound have?: Set DEFAULT_BRAIN_SNAPSHOT_LOAD_MAX_BYTES to 64 MiB. (best practice) [sources: src/devops_cli/config/defaults.py, src/devops_cli/roadmap/stores/listing_shared.py]
    • Rejected: 16 MiB. The known devops-cli snapshot is about 14 MB, so 16 MiB leaves little headroom.
    • Chosen: 64 MiB. It gives about 4.5 times the known snapshot and stays far below the 3Gi memory limit.
    • Rejected: 256 MiB. It lets a few concurrent loads use close to 1 GiB and gives less protection.
  • How should a memory entry learn the remaining Valkey TTL?: Use cachetools.TLRUCache. Add ValkeyCacheTier.get_with_ttl that sends GET and PTTL in one pipeline. Set the memory expiry to the smaller of the mapping TTL and the PTTL. (best practice) [sources: src/devops_cli/valkey/tiered.py, src/devops_cli/valkey/cache.py]
    • Rejected: Store a read time in the value. It adds bytes to each value and depends on the process clock.
    • Chosen: PTTL in the same pipeline. Valkey gives the authoritative remaining time with no extra round trip.
    • Rejected: Do not fill memory on a Valkey hit. The spec requires the read to fill the in-process tier.
  • How should _seen handle repository spellings?: Use RepoRef(repo).key for every _seen lookup, store, and removal, as snapshot_key does. (best practice) [sources: src/devops_cli/roadmap/stores/listing_shared.py, src/devops_cli/github/repo_ref.py]
    • Rejected: Raw repository text. Two spellings share one Valkey key but get two _seen entries, so the second save replaces the key.
    • Chosen: RepoRef key. It matches snapshot_key and the visibility cache, so one repository has one entry.
  • How should the shared-cache spec change?: Replace the sentence 'A failed visibility read SHALL count as private' with 'A failed visibility read SHALL count as unknown and SHALL leave the shared copy in place.' (best practice) [sources: openspec/specs/project-brain/shared-cache/spec.md]
    • Rejected: Keep failed read as private. It removes a public snapshot and causes the defect.
    • Chosen: State unknown and leave the copy. It matches the three-state result and the expected behavior.
    • Rejected: Add a second requirement. The existing requirement already owns this behavior, so one change is clearer.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/bug
    source/agent
    on Nov 21, 2026
  2. dan-petty commented on Nov 21, 2026

    @dan-petty
    OwnerAuthor
  3. added
    scope/roadmap
    status/new
    effort/medium
    value/high
    priority/p1-high
    on Nov 21, 2026
  4. devops-cli-service commented on Nov 21, 2026

    @devops-cli-service
    Contributor

    Next step: the alternatives check records a verdict. See the section Proposed design of the body.

  5. devops-cli-service commented on Nov 21, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (9)

    Tools considered:

    Searches run:

    • cachetools TLRUCache per-entry TTL expire, results: 5
    • Python in-process cache with TTL synced to Redis key remaining TTL, results: 5
    • "cachetools" github tkem python library, results: 5
    • cachetools documentation TTLCache TLRUCache, results: 5
    • cachetools PyPI, results: 5
    • cachetools.readthedocs.io TTLCache, results: 5
    • python ttl cache library cachetools alternatives cachebox, results: 5
    • cachebox python cache library TTL, results: 5
    • "TLRUCache" python time aware least recently used cache, results: 5

    Sources:

  6. added
    status/ready
    and removed
    status/new
    on Nov 21, 2026
  7. devops-cli-service commented on Nov 21, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

  8. added
    owner/agent
    status/in-progress
    and removed
    status/ready
    on Nov 21, 2026
  9. added
    status/in-review
    and removed
    status/in-progress
    on Nov 21, 2026
  10. removed
    status/in-review
    owner/agent
    on Nov 21, 2026
  11. devops-cli-service commented on Nov 21, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-21 16:05 UTC

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/p1-highscope/roadmapsource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions