From 0d76a6b2d698a8808a79e6669b76a6d02dec8c4b Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Mon, 27 Apr 2026 02:25:07 -0400 Subject: [PATCH] refactor: consolidate legacy record_turn_memory into unified API (T90.3) The Phase 1 single-bot ``record_turn_memory`` lingered next to the unified ``record_turn_memory_for_present`` introduced in T84. Only test fixtures still called the legacy entry point. - Remove ``record_turn_memory`` from ``chat/services/memory_write.py``. - Update the two test_memory_write.py callers to use ``record_turn_memory_for_present(..., guest_bot_id=None)``, which produces the same ``[you=1, host=1, guest=0]`` witness mask. The unified API returns ``dict[bot_id, (event_id, memory_id)]``; tests extract the host entry. No production callers were affected. --- chat/services/memory_write.py | 56 ----------------------------------- tests/test_memory_write.py | 14 +++++++-- 2 files changed, 11 insertions(+), 59 deletions(-) diff --git a/chat/services/memory_write.py b/chat/services/memory_write.py index 12eed5d..d60c3d9 100644 --- a/chat/services/memory_write.py +++ b/chat/services/memory_write.py @@ -22,62 +22,6 @@ from sqlite3 import Connection from chat.eventlog.log import append_and_apply -def record_turn_memory( - conn: Connection, - *, - chat_id: str, - host_bot_id: str, - narrative_text: str, - scene_id: int | None = None, - chat_clock_at: str | None = None, - source: str = "direct", - significance: int = 1, -) -> tuple[int, int | None]: - """Append a ``memory_written`` event for the host bot's POV of this turn. - - Uses :func:`chat.eventlog.log.append_and_apply` (not raw - :func:`append_event`) so the new memory row is projected immediately - without re-running prior non-idempotent handlers (e.g. ``edge_update`` - deltas). - - Returns ``(event_id, memory_id)``. ``event_id`` is the row id of the - just-appended ``memory_written`` event in ``event_log``. ``memory_id`` - is the autoincrement PK of the corresponding ``memories`` row — these - are *different* numbers (event_log and memories use independent - rowid sequences) so callers needing to update significance or pin - state must use ``memory_id``. Falls back to ``None`` if the projected - row can't be located, which shouldn't happen but keeps the return - shape stable. - """ - payload: dict = { - "owner_id": host_bot_id, - "chat_id": chat_id, - "pov_summary": narrative_text, - "witness_you": 1, - "witness_host": 1, - "witness_guest": 0, - "source": source, - "reliability": 1.0, - "significance": significance, - "pinned": 0, - "auto_pinned": 0, - } - if scene_id is not None: - payload["scene_id"] = scene_id - if chat_clock_at is not None: - payload["chat_clock_at"] = chat_clock_at - - event_id = append_and_apply(conn, kind="memory_written", payload=payload) - row = conn.execute( - "SELECT id FROM memories " - "WHERE owner_id = ? AND chat_id = ? " - "ORDER BY id DESC LIMIT 1", - (host_bot_id, chat_id), - ).fetchone() - memory_id = row[0] if row else None - return event_id, memory_id - - def _write_one_memory( conn: Connection, *, diff --git a/tests/test_memory_write.py b/tests/test_memory_write.py index 77132ae..8c5253a 100644 --- a/tests/test_memory_write.py +++ b/tests/test_memory_write.py @@ -22,7 +22,7 @@ from chat.db.migrate import apply_migrations from chat.eventlog.log import append_event from chat.eventlog.projector import project from chat.llm.mock import MockLLMClient -from chat.services.memory_write import record_turn_memory, record_turn_memory_for_present +from chat.services.memory_write import record_turn_memory_for_present import chat.state.entities # noqa: F401 - register handlers import chat.state.memory # noqa: F401 import chat.state.world # noqa: F401 @@ -64,14 +64,19 @@ def test_record_turn_memory_writes_event_and_projects(tmp_path): apply_migrations(db) _seed_minimal(db) with open_db(db) as conn: - eid, mid = record_turn_memory( + # T90.3: legacy ``record_turn_memory`` was removed; the unified + # ``record_turn_memory_for_present`` with ``guest_bot_id=None`` + # produces the same single-bot witness mask [1,1,0]. + result = record_turn_memory_for_present( conn, chat_id="chat_bot_a", host_bot_id="bot_a", + guest_bot_id=None, narrative_text="BotA looks up. 'You're back late.'", scene_id=None, chat_clock_at="2026-04-26T20:00:00+00:00", ) + eid, mid = result["bot_a"] assert eid > 0 assert mid is not None and mid > 0 @@ -111,12 +116,15 @@ def test_record_turn_memory_omits_optional_fields(tmp_path): _seed_minimal(db) with open_db(db) as conn: # Call without scene_id/chat_clock_at — should default to None. - eid, mid = record_turn_memory( + # T90.3: migrated from legacy ``record_turn_memory``. + result = record_turn_memory_for_present( conn, chat_id="chat_bot_a", host_bot_id="bot_a", + guest_bot_id=None, narrative_text="A simple memory.", ) + eid, mid = result["bot_a"] assert eid > 0 assert mid is not None and mid > 0