## Change description > Whole-repo lint + pre-commit convention, CONFIG ONLY (no source reformat in this PR). Python (ruff) and TypeScript (eslint + prettier) share a 120-col margin via the committed pre-commit gate. This upgrades main's existing basic ruff setup and adds the TS lint stack (none existed on main). > > What this PR adds: > - ruff: line-length 120; security rules S101/S105/S106/S107/S506/S605/S607; policy per-file-ignores (tests + accepted CLI deferred-imports + product asserts + N818, each commented); ruff pinned <0.16. > - eslint/prettier: shared base (`eslint.base.mjs` + `prettier.config.mjs`); ui-tui extends it with React rules, bridge with a Node-only config; hermes-ink vendor and generated RPC types excluded. > - pre-commit: keeps main's hooks (conventional-commit, detect-private-key, etc.), adds a top-level exclude and 4 local eslint/prettier hooks; bumps the pre-commit dev dep. > - 2 lint-surfaced fixes: PlannerContext import (F821) in the sentinel runner; lower-cased file-local cron-heartbeat constants (N806). (raven/config/raven.py's F821 was already fixed on main.) > > Sequencing: config only. `ruff check` is intentionally red on ~307 auto-fixables (I001/F401/F541/E701/E702); a SEPARATE reformat branch runs `ruff --fix` + `ruff format` + eslint/prettier --fix to clear them (100% auto-resolvable, zero manual). Enforcement (pre-commit install / CI) turns on only after the reformat lands, so do not expect a green tree from this PR alone. > > CLI-owned files (the reformat and several per-file-ignores touch `raven/cli/*`) are under review by the CLI team. Judgment items (TS no-explicit-any, no-unused-expressions, S101 convert-vs-keep, N818 rename, C90, mypy) are deferred to v2. ## Type of change - [ ] Bug fix - [x] New feature - [ ] Document - [x] Others ## Related issues (if there is) > none ## Checklists ### Development - [ ] Lint rules pass locally - [ ] Application changes have been tested thoroughly - [ ] Automated tests covering modified code pass > config-only: `ruff check` is red on auto-fixables by design (the reformat branch clears them); eslint/prettier configs load in both ui-tui and bridge. ### Security - [x] Security impact of change has been considered - [ ] Code follows company security practices and guidelines > Only dependency change is the dev-only pre-commit tool plus the ruff pin; the detect-private-key hook from main is retained. ### Code review - [x] Pull request has a descriptive title and context useful to a reviewer - [ ] Pull request linked to JIRA ticket when applicable --------- Co-authored-by: Sheng Zhao <sheng.zhao@evermind.ai> Co-authored-by: Claude (claude-opus-4-8) <noreply@anthropic.com>
276 lines
8.4 KiB
Python
276 lines
8.4 KiB
Python
"""Unit tests for RoutineLearner.
|
|
|
|
Pin the contract: deterministic, no LLM, candidate-only routines,
|
|
date-windowed, quiet on malformed input.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
import pytest
|
|
|
|
from raven.proactive_engine.sentinel.predictor.routine_learner import RoutineLearner, parse_history_entries
|
|
|
|
|
|
def _now_fixed() -> datetime:
|
|
return datetime(2026, 4, 21, 14, 0, 0)
|
|
|
|
|
|
def _learner(**kwargs) -> RoutineLearner:
|
|
defaults = dict(min_occurrences=3, hour_slot_size=3, learning_window_days=60, now_fn=_now_fixed)
|
|
defaults.update(kwargs)
|
|
return RoutineLearner(**defaults)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Parsing
|
|
|
|
|
|
def test_parse_multiple_formats_ok():
|
|
raw = """
|
|
[2026-04-21 08:00] morning check-in
|
|
[2026-04-21T08:30] inbox review
|
|
(2026/04/21 09:15) calendar open
|
|
not a timestamped line
|
|
2026-04-21 09:30 something else
|
|
"""
|
|
entries = parse_history_entries(raw)
|
|
# All 4 timestamped lines should parse.
|
|
assert len(entries) == 4
|
|
# Sorted chronologically.
|
|
assert entries[0].ts < entries[-1].ts
|
|
|
|
|
|
def test_parse_ignores_malformed_lines():
|
|
raw = "random text\n2026-13-50 24:99 bogus\n[2026-04-21 10:00] ok"
|
|
entries = parse_history_entries(raw)
|
|
assert len(entries) == 1
|
|
assert entries[0].content == "ok"
|
|
|
|
|
|
def test_parse_empty_returns_empty():
|
|
assert parse_history_entries("") == []
|
|
assert parse_history_entries("\n\n\n") == []
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Learning — basic
|
|
|
|
|
|
def test_learn_no_history_returns_empty():
|
|
assert _learner().learn("") == []
|
|
|
|
|
|
def test_learn_below_threshold_returns_empty():
|
|
# Only 2 entries in one bin — threshold is 3.
|
|
raw = "\n".join(
|
|
[
|
|
"[2026-04-07 08:00] morning duolingo session",
|
|
"[2026-04-14 08:15] morning duolingo session",
|
|
]
|
|
)
|
|
assert _learner().learn(raw) == []
|
|
|
|
|
|
def test_learn_at_threshold_emits_candidate():
|
|
raw = "\n".join(
|
|
[
|
|
"[2026-04-07 08:00] morning duolingo session",
|
|
"[2026-04-14 08:10] morning duolingo lesson",
|
|
"[2026-04-21 08:05] morning duolingo review",
|
|
]
|
|
)
|
|
routines = _learner().learn(raw)
|
|
assert len(routines) == 1
|
|
r = routines[0]
|
|
assert r.status == "candidate"
|
|
assert r.user_confirmed is False
|
|
assert r.occurrence_count == 3
|
|
# Tuesday, 6-9am bin (hour_slot_size=3 → 06..09).
|
|
assert r.day_of_week == 1 # Tuesday
|
|
assert r.time_slot == (6, 9)
|
|
assert "duolingo" in r.pattern.lower()
|
|
assert "duolingo" in r.keywords
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Window filtering
|
|
|
|
|
|
def test_learn_old_entries_outside_window_ignored():
|
|
# entries 90 days ago + only 1 recent → below threshold.
|
|
old = "\n".join(
|
|
[
|
|
"[2026-01-15 08:00] morning duolingo",
|
|
"[2026-01-22 08:00] morning duolingo",
|
|
"[2026-01-29 08:00] morning duolingo",
|
|
]
|
|
)
|
|
recent = "\n[2026-04-14 08:00] morning duolingo"
|
|
routines = _learner(learning_window_days=60).learn(old + recent)
|
|
# Only 1 entry in window; below threshold.
|
|
assert routines == []
|
|
|
|
|
|
def test_learn_wide_window_catches_everything():
|
|
old = "\n".join(
|
|
[
|
|
"[2026-01-15 08:00] morning duolingo",
|
|
"[2026-01-22 08:00] morning duolingo",
|
|
"[2026-01-29 08:00] morning duolingo",
|
|
]
|
|
)
|
|
routines = _learner(learning_window_days=365).learn(old)
|
|
assert len(routines) == 1
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Multiple bins / ordering
|
|
|
|
|
|
def test_multiple_routines_sorted_by_frequency():
|
|
# Pattern A: 3 Monday mornings
|
|
# Pattern B: 5 Thursday evenings
|
|
lines = []
|
|
for d in (6, 13, 20): # Mondays in Apr 2026
|
|
lines.append(f"[2026-04-{d:02d} 08:00] inbox review morning")
|
|
for d in (2, 9, 16, 23, 30): # Thursdays in Apr 2026
|
|
lines.append(f"[2026-04-{d:02d} 21:00] evening journaling reflection")
|
|
routines = _learner().learn("\n".join(lines))
|
|
assert len(routines) == 2
|
|
# More frequent bin first.
|
|
assert routines[0].occurrence_count == 5
|
|
assert routines[0].day_of_week == 3 # Thursday
|
|
assert routines[0].time_slot == (21, 24)
|
|
assert routines[1].occurrence_count == 3
|
|
assert routines[1].day_of_week == 0 # Monday
|
|
|
|
|
|
def test_no_keywords_still_emits_routine():
|
|
raw = "\n".join(
|
|
[
|
|
"[2026-04-07 08:00]",
|
|
"[2026-04-14 08:00]",
|
|
"[2026-04-21 08:00]",
|
|
]
|
|
)
|
|
routines = _learner().learn(raw)
|
|
assert len(routines) == 1
|
|
# pattern string should not crash on empty keywords.
|
|
assert routines[0].keywords == []
|
|
assert "no dominant keywords" in routines[0].pattern
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Keyword extraction
|
|
|
|
|
|
def test_keyword_extraction_drops_stopwords_and_digits():
|
|
raw = "\n".join(
|
|
[
|
|
"[2026-04-07 08:00] the user did 7 pushups and opened the app",
|
|
"[2026-04-14 08:00] the user did 10 pushups in the app",
|
|
"[2026-04-21 08:00] the user did 15 pushups with the app",
|
|
]
|
|
)
|
|
routines = _learner().learn(raw)
|
|
assert len(routines) == 1
|
|
kw = routines[0].keywords
|
|
# Stopwords "the", "and" should not appear; "pushups" and "app" should.
|
|
assert "the" not in kw
|
|
assert "and" not in kw
|
|
assert any("pushup" in k for k in kw)
|
|
|
|
|
|
def test_keyword_extraction_handles_chinese():
|
|
# Use same day-of-week + same 3-hour slot across 3 dates.
|
|
raw = "\n".join(
|
|
[
|
|
"[2026-04-07 22:00] 晚上 小红书 发帖 打卡",
|
|
"[2026-04-14 22:10] 晚上 小红书 发帖 打卡",
|
|
"[2026-04-21 22:15] 晚上 小红书 发帖 打卡",
|
|
]
|
|
)
|
|
routines = _learner().learn(raw)
|
|
assert len(routines) == 1
|
|
r = routines[0]
|
|
# At least one Chinese keyword should surface.
|
|
assert any("小红书" in k or "打卡" in k for k in r.keywords)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Safety
|
|
|
|
|
|
def test_invalid_hour_slot_size_rejected():
|
|
with pytest.raises(ValueError):
|
|
_learner(hour_slot_size=5) # 24 % 5 != 0
|
|
with pytest.raises(ValueError):
|
|
_learner(hour_slot_size=0)
|
|
|
|
|
|
def test_never_emits_active_or_confirmed():
|
|
"""Invariant: RoutineLearner only produces candidates, never active ones.
|
|
Only an explicit user confirmation upgrades status."""
|
|
raw = "\n".join([f"[2026-04-{7 + 7 * i:02d} 08:00] morning check" for i in range(10)])
|
|
routines = _learner(min_occurrences=2).learn(raw)
|
|
assert routines # should find something
|
|
for r in routines:
|
|
assert r.status == "candidate", f"got {r.status}"
|
|
assert r.user_confirmed is False
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Min-history floor — prevent noisy "0 candidates" work when user has too
|
|
# little history to possibly form a routine.
|
|
|
|
|
|
def test_learn_under_min_history_short_circuits():
|
|
"""3 entries, all in the same bin, min_occurrences=3 → would normally
|
|
emit a routine. With min_history_entries=10 we short-circuit before
|
|
binning."""
|
|
raw = "\n".join(
|
|
[
|
|
"[2026-04-07 08:00] morning duolingo",
|
|
"[2026-04-14 08:00] morning duolingo",
|
|
"[2026-04-21 08:00] morning duolingo",
|
|
]
|
|
)
|
|
assert _learner(min_history_entries=10).learn(raw) == []
|
|
# Sanity: same raw with floor=0 (default) DOES emit.
|
|
assert _learner().learn(raw)
|
|
|
|
|
|
def test_learn_with_decay_under_min_history_short_circuits():
|
|
raw = "\n".join(
|
|
[
|
|
"[2026-04-07 08:00] morning duolingo",
|
|
"[2026-04-14 08:00] morning duolingo",
|
|
"[2026-04-21 08:00] morning duolingo",
|
|
]
|
|
)
|
|
assert _learner(min_history_entries=10).learn_with_decay(raw) == []
|
|
assert _learner().learn_with_decay(raw)
|
|
|
|
|
|
def test_learn_from_file_missing(tmp_path):
|
|
missing = tmp_path / "nope.md"
|
|
assert _learner().learn_from_file(missing) == []
|
|
|
|
|
|
def test_learn_from_file_ok(tmp_path):
|
|
f = tmp_path / "HISTORY.md"
|
|
f.write_text(
|
|
"\n".join(
|
|
[
|
|
"[2026-04-07 08:00] morning run",
|
|
"[2026-04-14 08:00] morning run",
|
|
"[2026-04-21 08:00] morning run",
|
|
]
|
|
)
|
|
)
|
|
routines = _learner().learn_from_file(f)
|
|
assert len(routines) == 1
|
|
assert "run" in routines[0].keywords
|