Optimize signal read paths and enforce score invariants

This commit is contained in:
2026-07-11 16:04:13 +02:00
parent fdc49d0e28
commit 9450831ef3
13 changed files with 426 additions and 170 deletions
+76 -2
View File
@@ -95,7 +95,17 @@ async def test_score_drop_seeds_then_alerts(session):
msgs = await svc._collect_score_drops(session)
await session.commit()
assert msgs == []
assert await svc._watermark(session, "AAA") == 80.0
watermarks = (
await session.execute(
select(AlertLog.value)
.where(
AlertLog.alert_type == svc.WATERMARK_TYPE,
AlertLog.dedup_key == "AAA",
)
.order_by(AlertLog.created_at.desc(), AlertLog.id.desc())
)
).scalars().all()
assert watermarks == [80.0]
# Drop the composite well past the threshold
row = (await session.execute(
@@ -111,7 +121,71 @@ async def test_score_drop_seeds_then_alerts(session):
assert key == "scoredrop:AAA"
assert "AAA" in text
# rebaselined to the new (lower) level
assert await svc._watermark(session, "AAA") == 60.0
watermarks = (
await session.execute(
select(AlertLog.value)
.where(
AlertLog.alert_type == svc.WATERMARK_TYPE,
AlertLog.dedup_key == "AAA",
)
.order_by(AlertLog.created_at.desc(), AlertLog.id.desc())
)
).scalars().all()
assert watermarks[0] == 60.0
async def test_score_drop_uses_latest_watermark_when_timestamps_tie(session):
await _seed_watchlisted_ticker(session, "AAA", 50.0)
timestamp = datetime.now(timezone.utc)
session.add_all([
AlertLog(
alert_type=svc.WATERMARK_TYPE,
dedup_key="AAA",
value=90.0,
created_at=timestamp,
),
AlertLog(
alert_type=svc.WATERMARK_TYPE,
dedup_key="AAA",
value=70.0,
created_at=timestamp,
),
])
await session.commit()
msgs = await svc._collect_score_drops(session)
assert len(msgs) == 1
assert "from 70" in msgs[0][1]
async def test_score_drop_seeds_when_latest_watermark_has_no_value(session):
await _seed_watchlisted_ticker(session, "AAA", 80.0)
session.add(
AlertLog(
alert_type=svc.WATERMARK_TYPE,
dedup_key="AAA",
value=None,
created_at=datetime.now(timezone.utc),
)
)
await session.commit()
msgs = await svc._collect_score_drops(session)
await session.commit()
assert msgs == []
values = (
await session.execute(
select(AlertLog.value)
.where(
AlertLog.alert_type == svc.WATERMARK_TYPE,
AlertLog.dedup_key == "AAA",
)
.order_by(AlertLog.created_at.desc(), AlertLog.id.desc())
)
).scalars().all()
assert values[0] == 80.0
def test_format_qualified_includes_current_price_and_target_move():