Skip to content

Commit 24deec7

Browse files
committed
tests: add adaptive coverage tests and raise gate to 74%
1 parent ceb6d6f commit 24deec7

5 files changed

Lines changed: 69 additions & 3 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
run: mypy src
4646

4747
- name: Pytest (with coverage gate)
48-
run: pytest -q --maxfail=1 --disable-warnings --cov=neuralcache --cov-report=xml --cov-fail-under=71
48+
run: pytest -q --maxfail=1 --disable-warnings --cov=neuralcache --cov-report=xml --cov-fail-under=74
4949

5050
- name: Upload coverage artifact
5151
uses: actions/upload-artifact@v4

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
[![CodeQL](https://github.com/Maverick0351a/neuralcache/actions/workflows/codeql.yml/badge.svg)](https://github.com/Maverick0351a/neuralcache/actions/workflows/codeql.yml)
1212
[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
1313
[![GitHub stars](https://img.shields.io/github/stars/Maverick0351a/neuralcache?style=social)](https://github.com/Maverick0351a/neuralcache/stargazers)
14-
[![Coverage](https://img.shields.io/badge/coverage-71%25-yellow)](./coverage-policy)
14+
[![Coverage](https://img.shields.io/badge/coverage-74%25-yellow)](./coverage-policy)
1515

1616
NeuralCache is a lightweight reranker for RAG pipelines that *actually remembers what helped*. It blends dense semantic similarity with a narrative memory of past wins and stigmergic pheromones that reward helpful passages while decaying stale ones—then spices in MMR diversity and ε-greedy exploration. The result: more relevant context for your LLM without rebuilding your stack.
1717

@@ -323,7 +323,7 @@ ruff check && mypy && pytest --cov=neuralcache --cov-report=term-missing
323323

324324
- Look for [good first issues](https://github.com/Maverick0351a/neuralcache/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22).
325325
- Add test coverage for user-visible changes.
326-
- Coverage gate currently enforces >=71%. We'll continue to ratchet this upward as core adaptive components gain additional tests.
326+
- Coverage gate currently enforces >=74%. We'll continue to ratchet this upward as core adaptive components gain additional tests.
327327
- PRs with docs, demos, and eval improvements are extra appreciated.
328328

329329
Optionally, join the discussion in **#neuralcache** on Discord (coming soon—watch this space).

tests/test_encoder_hashing.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import numpy as np
2+
from neuralcache.encoder import create_encoder, _hash_to_vector # type: ignore
3+
4+
5+
def test_hashing_encoder_deterministic():
6+
enc = create_encoder("hash", dim=32, model=None)
7+
v1 = enc.encode("Hello World")
8+
v2 = enc.encode("Hello World")
9+
assert np.allclose(v1, v2)
10+
assert v1.shape == (32,)
11+
12+
13+
def test_hashing_encoder_batch_empty():
14+
enc = create_encoder("hash", dim=16, model=None)
15+
batch = enc.encode_batch([])
16+
assert batch.shape == (0, 16)
17+
18+
19+
def test_hash_to_vector_non_empty():
20+
vec = _hash_to_vector("", 8)
21+
# empty string should still produce a non-all-zero vector (fallback sets index 0)
22+
assert vec.shape == (8,)
23+
assert vec[0] != 0.0

tests/test_narrative_tracker.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import numpy as np
2+
import time
3+
from neuralcache.narrative import NarrativeTracker
4+
5+
6+
def test_narrative_update_and_coherence():
7+
nt = NarrativeTracker(dim=4, alpha=0.5, success_gate=0.0, backend="memory")
8+
emb = np.array([1.0, 0.0, 0.0, 0.0], dtype=np.float32)
9+
nt.update(emb, success=1.0)
10+
sims = nt.coherence(np.stack([emb, np.array([0.0,1.0,0.0,0.0], dtype=np.float32)]))
11+
assert sims.shape == (2,)
12+
assert sims[0] >= sims[1]
13+
14+
15+
def test_narrative_purge_if_stale():
16+
nt = NarrativeTracker(dim=4, alpha=0.5, success_gate=0.0, backend="memory")
17+
emb = np.array([1.0, 0.0, 0.0, 0.0], dtype=np.float32)
18+
nt.update(emb, success=1.0)
19+
# Force timestamp into past
20+
nt._updated_ts -= 100.0
21+
nt.purge_if_stale(retention_seconds=10.0)
22+
assert np.allclose(nt.v, np.zeros_like(nt.v))

tests/test_pheromone_store.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import time
2+
from neuralcache.pheromone import PheromoneStore
3+
4+
5+
def test_pheromone_reinforce_and_decay():
6+
store = PheromoneStore(half_life_s=1.0, exposure_penalty=0.0, backend="memory")
7+
store.reinforce(["a"], reward=1.0)
8+
v_initial = store.get_bonus("a")
9+
assert v_initial > 0.0
10+
time.sleep(1.1)
11+
v_later = store.get_bonus("a")
12+
assert v_later < v_initial # decayed
13+
14+
15+
def test_pheromone_exposure_penalty():
16+
store = PheromoneStore(half_life_s=1000.0, exposure_penalty=0.5, backend="memory")
17+
store.reinforce(["b"], reward=1.0)
18+
v0 = store.get_bonus("b")
19+
store.record_exposure(["b"]) # one exposure halves remaining multiplier (1 - 0.5*1)
20+
v1 = store.get_bonus("b")
21+
assert v1 <= v0

0 commit comments

Comments
 (0)