Skip to content

test(cognitive-load): pin cache eviction behavior for both in-memory caches#82

Open
tongshu2023 wants to merge 1 commit into
zijinz456:mainfrom
tongshu2023:claude/issue-30-cache-eviction-tests
Open

test(cognitive-load): pin cache eviction behavior for both in-memory caches#82
tongshu2023 wants to merge 1 commit into
zijinz456:mainfrom
tongshu2023:claude/issue-30-cache-eviction-tests

Conversation

@tongshu2023

Copy link
Copy Markdown

Summary

Addresses the remaining acceptance item on #30.

The two caches named in the issue already enforce hard bounds in the current code (the issue predates the fix): cognitive_load_nlp._analysis_cache caps at 500 and flushes the oldest half when full (under an asyncio.Lock), and cognitive_load_calibrator._cache caps at 200 and evicts the least-active (lowest message_count) baseline. So of the acceptance criteria, the first two are already satisfied — what was missing was the unit test verifying eviction behavior, without which a regression back to unbounded growth would land silently.

Changes

tests/test_cognitive_load_cache_bounds.py (6 tests):

NLP analysis cache

  • never exceeds its cap under sustained distinct messages
  • crossing the cap drops the oldest entries and keeps the newest
  • a cache hit returns the cached object without re-analysis

Calibrator baseline cache

  • never exceeds its cap
  • eviction removes exactly the least-active baseline and admits the newcomer
  • same user always receives the same baseline instance

The fixture monkeypatches the caps small (the eviction logic reads the module attribute at call time), keeping the suite at ~6s instead of ~3min with production-size caps.

Tests

test_cognitive_load_cache_bounds.py + test_cognitive_load_calibrator.py: 23 passed locally.

Acceptance criteria

  • Both caches have a defined max size — already in code (500 / 200)
  • Eviction happens automatically when max size is reached — already in code
  • Existing tests pass
  • Add a unit test verifying cache eviction behavior — this PR

🤖 Generated with Claude Code

…caches (zijinz456#30)

The two caches named in zijinz456#30 already enforce hard bounds in the current
code (the issue predates the fix): _analysis_cache caps at 500 and
flushes the oldest half, _cache caps at 200 and evicts the least-active
baseline. What was still missing from the acceptance criteria was a
unit test verifying eviction, so a regression back to unbounded growth
would fail loudly.

tests/test_cognitive_load_cache_bounds.py (6 tests):
- NLP cache never exceeds its cap under sustained distinct messages
- crossing the cap drops the oldest entries and keeps the newest
- cache hits return the cached object without re-analysis
- calibrator cache never exceeds its cap
- eviction removes exactly the least-active (lowest message_count)
  baseline and keeps the newcomer
- same user always gets the same baseline instance

Caps are monkeypatched small in the fixture (the eviction logic reads
the module attribute at call time), keeping the suite at ~6s instead of
~3min with production-size caps.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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