Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/aelfrice/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -3287,21 +3287,16 @@ class is unknown.
for cls in signal_weights:
if not is_valid_signal_class(cls):
raise ValueError(f"unknown signal class: {cls!r}")
cur = self._conn.execute(
"SELECT 1 FROM meta_beliefs WHERE key = ?", (key,)
)
if cur.fetchone() is not None:
return False
encoded = encode_signal_weights(signal_weights)
self._conn.execute(
"INSERT INTO meta_beliefs "
cur = self._conn.execute(
"INSERT OR IGNORE INTO meta_beliefs "
"(key, static_default, half_life_seconds, last_updated_ts, signal_classes) "
"VALUES (?, ?, ?, ?, ?)",
(key, float(static_default), int(half_life_seconds),
int(now_ts), encoded),
)
self._conn.commit()
return True
return cur.rowcount > 0

def update_meta_belief(
self,
Expand Down
32 changes: 32 additions & 0 deletions tests/test_meta_beliefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,38 @@ def test_doctor_meta_beliefs_json_is_parseable(tmp_path, monkeypatch):
assert any(p["signal_class"] == SIGNAL_RELEVANCE for p in row["posteriors"])


def test_install_concurrent_race_returns_false_not_raises(tmp_path):
"""INSERT OR IGNORE path: second install returns False without raising.

Simulates the outcome of two concurrent writers where the first write
wins. Single-threaded; exercises the rowcount > 0 return path rather
than actual concurrency.
"""
import sqlite3
db_path = tmp_path / "race.sqlite"
s = MemoryStore(str(db_path))
try:
first = s.install_meta_belief(
"meta:race", static_default=0.5, half_life_seconds=3600,
signal_weights={SIGNAL_RELEVANCE: 1.0}, now_ts=1,
)
assert first is True

# Second call: row already present — must return False, NOT raise.
try:
second = s.install_meta_belief(
"meta:race", static_default=0.5, half_life_seconds=3600,
signal_weights={SIGNAL_RELEVANCE: 1.0}, now_ts=2,
)
except sqlite3.IntegrityError as exc:
raise AssertionError(
f"install_meta_belief raised IntegrityError on duplicate key: {exc}"
) from exc
assert second is False
finally:
s.close()


def test_doctor_meta_beliefs_empty_store_reports_none(tmp_path, monkeypatch):
import os
db_path = tmp_path / "empty.sqlite"
Expand Down
Loading