feat: branching read-side filter — event readers consult active branch range (T113)
Wire the active branch's [origin_event_id, head_event_id] window into every user-facing event/memory reader so switching branches actually changes what dialogue and memories the user sees. Phase 4 T89/T94 shipped branches as metadata-only — this closes the loop. Helper: - chat/state/branches.py: add `active_branch_event_ids(conn)` returning the active branch's id range, with two defensive fall-throughs to `(0, BIG_INT)`: (a) no active branch row at all, and (b) the bootstrap "main" sentinel (name="main", origin=0, head=0). Production never bumps main's head_event_id today, so this preserves existing reader behaviour for every test that doesn't explicitly switch. Readers updated (all user-facing dialogue / retrieval surfaces): - chat/services/turn_common.py::read_recent_dialogue — chat-history prompt context + the chat-view template path (via web/turns.py + web/chat.py). - chat/services/scene_summarize.py::_read_recent_dialogue — scene-close per-POV summary input. - chat/state/memory.py::search_memories — FTS leg filters via m.event_id (T109's column); legacy NULL event_id rows are *included* unconditionally so the filter doesn't break pre-0014 retrieval. The fused (FTS + RRF + vector) path also drops vector hits whose event_id falls outside the branch window. - chat/web/meanwhile.py::_read_recent_meanwhile_dialogue — meanwhile prompt context. Projector queries (chat/state/world.py et al.) and admin/management surfaces (drawer hide-panel, cross-chat search, regenerate's row lookups by id) are intentionally NOT branch-filtered: projection must see the full log to build state correctly, and the admin surfaces operate across branches by design. Tests (10 new, 446 total): - tests/test_branches_state.py: 3 tests for `active_branch_event_ids` itself (bootstrap-main, no-active-branch, non-main literal range). - tests/test_branching.py: 7 cross-feature tests covering the spec's five required scenarios plus scene_summarize and meanwhile readers.
This commit is contained in:
@@ -7,7 +7,13 @@ from chat.db.migrate import apply_migrations
|
||||
from chat.eventlog.log import append_event
|
||||
from chat.eventlog.projector import project
|
||||
import chat.state.branches # registers handlers
|
||||
from chat.state.branches import active_branch, get_branch, list_branches
|
||||
from chat.state.branches import (
|
||||
_NO_HEAD_CLAMP,
|
||||
active_branch,
|
||||
active_branch_event_ids,
|
||||
get_branch,
|
||||
list_branches,
|
||||
)
|
||||
|
||||
|
||||
def test_main_branch_bootstrapped_by_migration(tmp_path):
|
||||
@@ -174,3 +180,83 @@ def test_branch_switched_unknown_name_warns(tmp_path, caplog):
|
||||
|
||||
# The unknown name was not inserted as a side effect.
|
||||
assert get_branch(conn, "does_not_exist") is None
|
||||
|
||||
|
||||
def test_active_branch_event_ids_bootstrap_main_returns_no_clamp(tmp_path):
|
||||
"""Bootstrap "main" (origin=0, head=0) reads as the no-clamp sentinel.
|
||||
|
||||
Migration 0013 seeds main with both event-id columns at 0; production
|
||||
today never emits ``branch_head_updated`` for main, so head stays at 0
|
||||
even as events accumulate. The helper treats this exact bootstrap
|
||||
state as "all events visible" (lower bound 0, upper bound BIG_INT) so
|
||||
every existing reader stays branch-agnostic until a non-main branch
|
||||
becomes active.
|
||||
"""
|
||||
db = tmp_path / "t.db"
|
||||
apply_migrations(db)
|
||||
with open_db(db) as conn:
|
||||
origin, head = active_branch_event_ids(conn)
|
||||
assert origin == 0
|
||||
assert head == _NO_HEAD_CLAMP
|
||||
|
||||
|
||||
def test_active_branch_event_ids_no_active_branch_falls_through(tmp_path):
|
||||
"""No active branch row at all → defensive ``(0, BIG_INT)``.
|
||||
|
||||
A switch to an unknown branch leaves zero rows with ``is_active=1``;
|
||||
``active_branch`` returns None. The helper must still hand readers a
|
||||
workable range (the full log) so the read pipeline doesn't crash on
|
||||
an inconsistent metadata state.
|
||||
"""
|
||||
db = tmp_path / "t.db"
|
||||
apply_migrations(db)
|
||||
with open_db(db) as conn:
|
||||
# Switching to a nonexistent branch clears is_active flags
|
||||
# without setting any other branch active.
|
||||
append_event(
|
||||
conn,
|
||||
kind="branch_switched",
|
||||
payload={"name": "does_not_exist"},
|
||||
)
|
||||
project(conn)
|
||||
assert active_branch(conn) is None
|
||||
|
||||
origin, head = active_branch_event_ids(conn)
|
||||
assert origin == 0
|
||||
assert head == _NO_HEAD_CLAMP
|
||||
|
||||
|
||||
def test_active_branch_event_ids_returns_actual_range_for_non_main(tmp_path):
|
||||
"""Non-main branches return their literal ``(origin, head)`` window.
|
||||
|
||||
A branch created at origin=10 + bumped to head=20 must surface as
|
||||
(10, 20) so readers' ``BETWEEN`` clamp scopes to that window.
|
||||
"""
|
||||
db = tmp_path / "t.db"
|
||||
apply_migrations(db)
|
||||
with open_db(db) as conn:
|
||||
append_event(
|
||||
conn,
|
||||
kind="branch_created",
|
||||
payload={
|
||||
"name": "experiment",
|
||||
"origin_event_id": 10,
|
||||
"head_event_id": 10,
|
||||
"chat_id": "c1",
|
||||
},
|
||||
)
|
||||
append_event(
|
||||
conn,
|
||||
kind="branch_head_updated",
|
||||
payload={"name": "experiment", "head_event_id": 20},
|
||||
)
|
||||
append_event(
|
||||
conn,
|
||||
kind="branch_switched",
|
||||
payload={"name": "experiment"},
|
||||
)
|
||||
project(conn)
|
||||
|
||||
origin, head = active_branch_event_ids(conn)
|
||||
assert origin == 10
|
||||
assert head == 20
|
||||
|
||||
Reference in New Issue
Block a user