Skip to content

fix(l1): delete dead backed-mode SWR machinery; docs stop claiming backed SWR (LAB-388) - #256

Merged
27Bslash6 merged 3 commits into
mainfrom
lab-388-swr-docs-dead-code
Aug 4, 2026
Merged

fix(l1): delete dead backed-mode SWR machinery; docs stop claiming backed SWR (LAB-388)#256
27Bslash6 merged 3 commits into
mainfrom
lab-388-swr-docs-dead-code

Conversation

@27Bslash6

@27Bslash6 27Bslash6 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR removes dead backend-mode SWR (stale-while-revalidate) code from the L1 cache and corrects documentation that incorrectly claimed SWR works with backends configured.

Changes

Code Removal (src/cachekit/l1_cache.py)

Deleted the unused SWR machinery from L1Cache:

  • Removed get_with_swr(), complete_refresh(), and cancel_refresh() methods
  • Removed SWR state tracking (_refreshing_keys, _entry_version) and config fields (_swr_enabled, _swr_threshold_ratio)
  • Removed the cached_at field from CacheEntry and the associated version-bump/refresh-cancel logic in _remove_entry() and invalidate_all()
  • Removed the now-unused random import

Documentation Corrections

Clarified that within-TTL SWR only runs in L1-only mode (backend=None) across README.md, docs/configuration.md, and docs/features/l1-invalidation.md:

  • swr_enabled/swr_threshold_ratio have no effect when a backend (Redis, File, Memcached) is configured, since Redis exposes no read-side freshness signal
  • Presets dev(), production(), secure() now show SWR as "L1-only" rather than universally supported
  • The only backed SWR is @cache.io's past-TTL stale_ttl mode
  • Documented that SWR requires a ttl (with ttl=None, entries never go stale)
  • Corrected the SWR refresh model: a successful background refresh re-runs your function and restarts both freshness and hard-expiry timers (previously docs claimed it refreshed content from L2 without extending TTL)
  • Updated jitter explanation to reflect per-key dedup via an in-flight marker

Test Cleanup

  • Removed SWR-specific performance benchmarks (get_with_swr, complete_refresh, version mismatch) and switched the SLA test to use get()
  • Removed unit tests for the deleted version-token and refresh-flag mechanisms

Purpose

Eliminates misleading documentation and removes dead code paths that were never exercised in backend mode, reducing per-entry memory overhead and simplifying the L1 hot path.


Summary

This PR removes dead stale-while-revalidate (SWR) machinery from the L1 cache that only functioned in backed modes (with a configured backend like Redis), and updates documentation to accurately reflect that within-TTL SWR only works in L1-only mode.

Changes

Code cleanup (src/cachekit/l1_cache.py)

  • Removes the get_with_swr(), complete_refresh(), and cancel_refresh() methods that implemented the backed-mode SWR flow.
  • Removes SWR state tracking fields: _refreshing_keys, _entry_version, _swr_enabled, and _swr_threshold_ratio.
  • Removes the cached_at field from CacheEntry and drops the version-increment logic from _remove_entry() and invalidate_all().
  • Removes the now-unused random import.

Documentation corrections

  • README.md: Relabels the L1 SWR feature as "L1 SWR (L1-only mode)" and adds a note clarifying that within-TTL background refresh only runs when backend=None.
  • docs/configuration.md: Marks swr_enabled/swr_threshold_ratio as L1-only-mode only, notes that SWR requires a ttl, and updates the presets table to indicate presets are "L1-only" for SWR.
  • docs/features/l1-invalidation.md: Adds an IMPORTANT callout clarifying that within-TTL SWR runs only in L1-only mode; corrects the description of refresh behavior — a successful background refresh re-runs your function and restarts both the freshness and hard-expiry timers (previously docs incorrectly claimed SWR refreshed content from L2 without extending TTL).

Test updates

  • tests/performance/test_l1_invalidation_benchmarks.py: Removes SWR and version-token benchmarks (get_with_swr, complete_refresh, version mismatch), and switches the SLA test to benchmark plain get() instead.
  • tests/unit/test_l1_invalidation.py: Removes tests covering version increments, refreshing-flag clearing, and SWR resurrection prevention.

Purpose

The removed SWR code was non-functional in backed modes since Redis exposes no read-side freshness signal, making it dead code. Documentation previously implied backed SWR worked, which was inaccurate. This change aligns the codebase and docs so that within-TTL SWR is correctly scoped to L1-only mode, while @cache.io's past-TTL stale_ttl remains the only backed SWR mechanism.


Based on the code changes provided, here's a description for this pull request:

Description

This PR removes dead code related to a "backed-mode" SWR (Stale-While-Revalidate) implementation from the L1 cache layer and updates documentation to no longer claim support for backed SWR functionality (LAB-388).

Changes

Benchmark correctness fix (tests/performance/test_l1_invalidation_benchmarks.py):

  • Updated the L1 invalidation SLA benchmark to properly unpack the tuple return value from cache.get() (found, _).
  • Added an assertion to verify each get() call actually results in an L1 hit. Previously, the benchmark discarded the return value, which meant an unnoticed cache miss could pass through undetected.

Why

As noted in the added inline comment, a cache miss returns early and is faster than a cache hit. Without asserting that a hit occurred, the benchmark could inadvertently measure the miss path (the wrong code path) while still passing the SLA threshold. The new assertion ensures the benchmark measures the intended hit latency and fails loudly if the test setup produces unexpected misses.

Note: The PR title indicates broader changes (deleting dead backed-mode SWR machinery and updating docs), but the only file included in the provided diff is the benchmark test correction. The remaining code and documentation changes described in the title are not visible in the supplied patch.

Summary by CodeRabbit

  • Changed

    • L1-only stale-while-revalidate behaviour is no longer provided by the L1 cache.
    • Backed configurations continue to support stale-while-revalidate through stale_ttl.
    • L1-only guidance now clarifies TTL requirements, refresh timing, jitter, deduplication, and failure handling.
  • Documentation

    • Updated configuration, feature, and comparison guides to distinguish L1-only and backed-mode refresh behaviour.
    • Clarified refresh outcomes, including timer resets after success and entry preservation after failure.

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

The change removes L1-managed SWR state and APIs. Documentation distinguishes L1-only SWR from backed-mode stale_ttl. Benchmarks now measure direct L1 get() latency and invalidation performance.

Changes

L1 SWR alignment

Layer / File(s) Summary
Remove L1-managed SWR
src/cachekit/l1_cache.py
Removes cached timestamps, refresh versions, refresh coordination, SWR methods, and related invalidation handling from L1Cache.
Document L1-only and backed-mode SWR
README.md, docs/configuration.md, docs/features/l1-invalidation.md
Documents TTL requirements, L1-only within-TTL refresh, backed-mode stale_ttl, refresh timer behaviour, failure handling, presets, and latency guidance.
Update invalidation benchmarks
tests/performance/test_l1_invalidation_benchmarks.py
Removes SWR and version-token benchmarks. The SLA test now measures direct cache.get() hit latency.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Suggested reviewers: kodus-27b

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the removal of dead backed-mode SWR machinery and the related documentation correction.
Description check ✅ Passed The description clearly covers the code, documentation, test changes, and purpose, although it omits several template checklist sections.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch lab-388-swr-docs-dead-code

Comment @coderabbitai help to get the list of available commands.

@kodus-27b

This comment has been minimized.

Comment thread tests/performance/test_l1_invalidation_benchmarks.py
@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@27Bslash6

Copy link
Copy Markdown
Contributor Author

Auto-rebase attempted a merge of main into this branch. One conflict in docs/configuration.md: the preset support-matrix table disagrees on substance, not just wording — this branch says SWR support for dev()/production()/secure() is L1-only¹ (with a footnote explaining Redis exposes no read-side freshness signal), main says (full support) for the same presets, plus io() is "closed alpha" here vs "closed beta" on main. Since this PR's own scope is SWR-docs correctness (lab-388-swr-docs-dead-code), picking either side risks either reverting the fix this PR is making or keeping stale content — needs the PR author's call. Left for a human; merge aborted, branch unchanged.

coderabbitai[bot]
coderabbitai Bot previously approved these changes Aug 4, 2026
@27Bslash6

Copy link
Copy Markdown
Contributor Author

@kody start-review

kodus-27b[bot]
kodus-27b Bot previously approved these changes Aug 4, 2026
…SWR (LAB-388)

The L1Cache SWR trio (get_with_swr / complete_refresh / cancel_refresh)
had zero callers — the decorator wrapper only ever drove the live
ObjectCache L1-only path. Its supporting state (_refreshing_keys,
_entry_version, _swr_enabled, _swr_threshold_ratio, CacheEntry.cached_at)
existed solely for the trio, so it goes with it: keeping version
bookkeeping on every removal for a reader that no longer exists is the
same trap the trio was.

Docs now state the post-LAB-381 SWR reality: within-TTL refresh-ahead is
L1-only mode (backend=None) only; production/secure have no backed SWR
(Redis has no read-side freshness signal); io() keeps past-TTL stale_ttl
SWR via headers (shipped in #228, untouched here). l1-invalidation.md's
'refresh from L2' story replaced with the live re-run-your-function
semantics, incl. the corrected both-timers-restart TTL behaviour.

Tests: unit+critical 2159 passed; docs markdown 121 passed; ruff +
basedpyright clean. Live L1-only SWR path and test_l1_only_swr.py
untouched.
…e; drop L2-era wording (LAB-388)

Expert-panel findings applied: the canonical SWR config examples omitted
ttl, configuring a mode where SWR never fires (ttl=None never goes
stale); the 'Disable SWR' docstring still told the deleted L2 story; the
second config table claimed a (0.1-1.0) threshold range vs the actual
(0.0, 1.0] validation and lacked the L1-only qualifier.

The panel also surfaced the inert namespace-index machinery (config
never reaches the production L1Cache; no namespace= puts; no bulk
invalidation callers) — filed as LAB-1433, out of this ticket's scope.
@27Bslash6
27Bslash6 force-pushed the lab-388-swr-docs-dead-code branch from c157261 to 92e977f Compare August 4, 2026 04:26
@kodus-27b

This comment has been minimized.

Comment thread tests/performance/test_l1_invalidation_benchmarks.py

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/performance/test_l1_invalidation_benchmarks.py`:
- Around line 468-475: Update the hot-path benchmark loop around L1Cache.get to
capture its found flag and assert each requested key is present, failing
immediately on any miss while retaining the existing latency measurement and
get_hit_p95 calculation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 71210e48-09f3-455e-9634-cfe7adcf088f

📥 Commits

Reviewing files that changed from the base of the PR and between 9a00972 and 92e977f.

📒 Files selected for processing (7)
  • README.md
  • docs/configuration.md
  • docs/features/l1-invalidation.md
  • src/cachekit/l1_cache.py
  • tests/performance/test_l1_invalidation_benchmarks.py
  • tests/unit/test_l1_invalidation.py
  • tests/unit/test_l1_swr.py
💤 Files with no reviewable changes (2)
  • tests/unit/test_l1_swr.py
  • tests/unit/test_l1_invalidation.py

Comment thread tests/performance/test_l1_invalidation_benchmarks.py
…s path (LAB-388)

A miss returns early from L1Cache.get and is faster than a hit, so the
loop discarding the return value could measure the miss path and still
pass the <1500ns SLA if population or the key pattern ever drifted.

Per CodeRabbit review on #256.
@kodus-27b

kodus-27b Bot commented Aug 4, 2026

Copy link
Copy Markdown

Kody Review Complete

Great news! 🎉
No issues were found that match your current review configurations.

Keep up the excellent work! 🚀

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Validate Business Logic: Ask Kody to validate your code against business rules by adding a comment with the @kody -v business-logic command.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug
Performance
Security
Business Logic

Access your configuration settings here.

@27Bslash6
27Bslash6 merged commit 878ad08 into main Aug 4, 2026
36 checks passed
@27Bslash6
27Bslash6 deleted the lab-388-swr-docs-dead-code branch August 4, 2026 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant