diff --git a/chat/db/migrations/0007_world.sql b/chat/db/migrations/0007_world.sql new file mode 100644 index 0000000..74af990 --- /dev/null +++ b/chat/db/migrations/0007_world.sql @@ -0,0 +1,45 @@ +CREATE TABLE chats ( + id TEXT PRIMARY KEY, + host_bot_id TEXT NOT NULL, + guest_bot_id TEXT, + created_at TEXT NOT NULL DEFAULT (datetime('now')) +); + +CREATE TABLE chat_state ( + chat_id TEXT PRIMARY KEY, + time TEXT NOT NULL, + weather TEXT NOT NULL DEFAULT '', + active_scene_id INTEGER, + narrative_anchor TEXT +); + +CREATE TABLE containers ( + id INTEGER PRIMARY KEY, + chat_id TEXT NOT NULL, + name TEXT NOT NULL, + type TEXT NOT NULL, + properties_json TEXT NOT NULL DEFAULT '{}', + parent_id INTEGER REFERENCES containers(id) +); + +CREATE TABLE scenes ( + id INTEGER PRIMARY KEY, + chat_id TEXT NOT NULL, + container_id INTEGER REFERENCES containers(id), + started_at TEXT NOT NULL, + ended_at TEXT, + significance INTEGER NOT NULL DEFAULT 0, + participants_json TEXT NOT NULL DEFAULT '[]' +); + +CREATE TABLE activity ( + entity_id TEXT PRIMARY KEY, + container_id INTEGER REFERENCES containers(id), + slot TEXT, + posture TEXT NOT NULL DEFAULT '', + action_json TEXT NOT NULL DEFAULT '{}', + attention TEXT NOT NULL DEFAULT '', + holding_json TEXT NOT NULL DEFAULT '[]', + status_json TEXT NOT NULL DEFAULT '{}', + updated_at TEXT NOT NULL DEFAULT (datetime('now')) +); diff --git a/chat/state/world.py b/chat/state/world.py new file mode 100644 index 0000000..5966924 --- /dev/null +++ b/chat/state/world.py @@ -0,0 +1,202 @@ +from __future__ import annotations +import json +from sqlite3 import Connection +from chat.eventlog.projector import on +from chat.eventlog.log import Event + + +def _row_to_dict(conn: Connection, table: str, row: tuple) -> dict: + cols = [c[1] for c in conn.execute(f"PRAGMA table_info({table})").fetchall()] + return dict(zip(cols, row)) + + +@on("chat_created") +def _apply_chat_created(conn: Connection, e: Event) -> None: + p = e.payload + conn.execute( + "INSERT INTO chats (id, host_bot_id, guest_bot_id) VALUES (?, ?, ?)", + (p["id"], p["host_bot_id"], p.get("guest_bot_id")), + ) + conn.execute( + "INSERT INTO chat_state (chat_id, time, weather, active_scene_id, narrative_anchor) " + "VALUES (?, ?, ?, NULL, ?)", + ( + p["id"], + p["initial_time"], + p.get("weather", ""), + p.get("narrative_anchor", ""), + ), + ) + + +@on("container_created") +def _apply_container_created(conn: Connection, e: Event) -> None: + p = e.payload + conn.execute( + "INSERT INTO containers (chat_id, name, type, properties_json, parent_id) " + "VALUES (?, ?, ?, ?, ?)", + ( + p["chat_id"], + p["name"], + p["type"], + json.dumps(p.get("properties", {})), + p.get("parent_id"), + ), + ) + + +@on("activity_change") +def _apply_activity_change(conn: Connection, e: Event) -> None: + p = e.payload + conn.execute( + "INSERT OR REPLACE INTO activity (" + "entity_id, container_id, slot, posture, action_json, " + "attention, holding_json, status_json, updated_at" + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))", + ( + p["entity_id"], + p.get("container_id"), + p.get("slot"), + p.get("posture", ""), + json.dumps(p.get("action", {})), + p.get("attention", ""), + json.dumps(p.get("holding", [])), + json.dumps(p.get("status", {})), + ), + ) + + +@on("scene_opened") +def _apply_scene_opened(conn: Connection, e: Event) -> None: + p = e.payload + cur = conn.execute( + "INSERT INTO scenes (chat_id, container_id, started_at, ended_at, " + "significance, participants_json) VALUES (?, ?, ?, NULL, 0, ?)", + ( + p["chat_id"], + p.get("container_id"), + p["started_at"], + json.dumps(p.get("participants", [])), + ), + ) + new_id = cur.lastrowid + conn.execute( + "UPDATE chat_state SET active_scene_id = ? WHERE chat_id = ?", + (new_id, p["chat_id"]), + ) + + +@on("scene_closed") +def _apply_scene_closed(conn: Connection, e: Event) -> None: + p = e.payload + scene_id = p["scene_id"] + significance = int(p.get("significance", 0)) + conn.execute( + "UPDATE scenes SET ended_at = ?, significance = ? WHERE id = ?", + (p["ended_at"], significance, scene_id), + ) + row = conn.execute( + "SELECT chat_id FROM scenes WHERE id = ?", (scene_id,) + ).fetchone() + if row is not None: + chat_id = row[0] + conn.execute( + "UPDATE chat_state SET active_scene_id = NULL WHERE chat_id = ?", + (chat_id,), + ) + + +def _chat_select_columns() -> str: + return ( + "c.id, c.host_bot_id, c.guest_bot_id, c.created_at, " + "s.time, s.weather, s.active_scene_id, s.narrative_anchor" + ) + + +def _chat_row_to_dict(row: tuple) -> dict: + return { + "id": row[0], + "host_bot_id": row[1], + "guest_bot_id": row[2], + "created_at": row[3], + "time": row[4], + "weather": row[5], + "active_scene_id": row[6], + "narrative_anchor": row[7], + } + + +def get_chat(conn: Connection, chat_id: str) -> dict | None: + row = conn.execute( + f"SELECT {_chat_select_columns()} FROM chats c " + "JOIN chat_state s ON s.chat_id = c.id WHERE c.id = ?", + (chat_id,), + ).fetchone() + if not row: + return None + return _chat_row_to_dict(row) + + +def list_chats(conn: Connection) -> list[dict]: + cur = conn.execute( + f"SELECT {_chat_select_columns()} FROM chats c " + "JOIN chat_state s ON s.chat_id = c.id ORDER BY c.id" + ) + return [_chat_row_to_dict(row) for row in cur.fetchall()] + + +def get_container(conn: Connection, container_id: int) -> dict | None: + row = conn.execute( + "SELECT * FROM containers WHERE id = ?", (container_id,) + ).fetchone() + if not row: + return None + d = _row_to_dict(conn, "containers", row) + d["properties"] = json.loads(d.pop("properties_json")) + return d + + +def find_container(conn: Connection, chat_id: str, name: str) -> dict | None: + row = conn.execute( + "SELECT * FROM containers WHERE chat_id = ? AND name = ?", + (chat_id, name), + ).fetchone() + if not row: + return None + d = _row_to_dict(conn, "containers", row) + d["properties"] = json.loads(d.pop("properties_json")) + return d + + +def get_activity(conn: Connection, entity_id: str) -> dict | None: + row = conn.execute( + "SELECT * FROM activity WHERE entity_id = ?", (entity_id,) + ).fetchone() + if not row: + return None + d = _row_to_dict(conn, "activity", row) + d["action"] = json.loads(d.pop("action_json")) + d["holding"] = json.loads(d.pop("holding_json")) + d["status"] = json.loads(d.pop("status_json")) + return d + + +def get_scene(conn: Connection, scene_id: int) -> dict | None: + row = conn.execute( + "SELECT * FROM scenes WHERE id = ?", (scene_id,) + ).fetchone() + if not row: + return None + d = _row_to_dict(conn, "scenes", row) + d["participants"] = json.loads(d.pop("participants_json")) + return d + + +def active_scene(conn: Connection, chat_id: str) -> dict | None: + row = conn.execute( + "SELECT active_scene_id FROM chat_state WHERE chat_id = ?", + (chat_id,), + ).fetchone() + if not row or row[0] is None: + return None + return get_scene(conn, row[0]) diff --git a/tests/test_memory.py b/tests/test_memory.py index 3d174b0..3675ce6 100644 --- a/tests/test_memory.py +++ b/tests/test_memory.py @@ -219,11 +219,11 @@ def test_memory_payload_defaults_when_optional_missing(tmp_path): assert mem["auto_pinned"] == 0 -def test_schema_version_after_migration_is_6(tmp_path): +def test_schema_version_after_migration_is_at_least_6(tmp_path): db = tmp_path / "t.db" apply_migrations(db) with open_db(db) as conn: row = conn.execute( "SELECT value FROM meta WHERE key = 'schema_version'" ).fetchone() - assert int(row[0]) == 6 + assert int(row[0]) >= 6 diff --git a/tests/test_world.py b/tests/test_world.py new file mode 100644 index 0000000..29a0bb0 --- /dev/null +++ b/tests/test_world.py @@ -0,0 +1,334 @@ +from __future__ import annotations + +from chat.db.connection import open_db +from chat.db.migrate import apply_migrations +from chat.eventlog.log import append_event +from chat.eventlog.projector import project +import chat.state.world # registers handlers +from chat.state.world import ( + active_scene, + find_container, + get_activity, + get_chat, + get_container, + get_scene, + list_chats, +) + + +def _chat_payload(**overrides): + payload = { + "id": "chat_bot_a", + "host_bot_id": "bot_a", + "guest_bot_id": None, + "initial_time": "2026-04-26T20:00:00+00:00", + "narrative_anchor": "Day 1 evening", + "weather": "clear", + } + payload.update(overrides) + return payload + + +def _container_payload(**overrides): + payload = { + "chat_id": "chat_bot_a", + "name": "office", + "type": "workplace", + "properties": { + "public": True, + "moving": False, + "audible_range": "normal", + "slots": [], + }, + "parent_id": None, + } + payload.update(overrides) + return payload + + +def test_chat_created_initializes_chats_and_chat_state(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + append_event(conn, kind="chat_created", payload=_chat_payload()) + project(conn) + chat = get_chat(conn, "chat_bot_a") + assert chat is not None + assert chat["id"] == "chat_bot_a" + assert chat["host_bot_id"] == "bot_a" + assert chat["guest_bot_id"] is None + assert chat["time"] == "2026-04-26T20:00:00+00:00" + assert chat["weather"] == "clear" + assert chat["narrative_anchor"] == "Day 1 evening" + assert chat["active_scene_id"] is None + + +def test_get_chat_returns_none_for_missing_id(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + assert get_chat(conn, "chat_missing") is None + + +def test_list_chats_returns_all_joined_rows(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + append_event(conn, kind="chat_created", payload=_chat_payload()) + append_event(conn, kind="chat_created", payload=_chat_payload( + id="chat_bot_b", host_bot_id="bot_b", + )) + project(conn) + chats = list_chats(conn) + assert len(chats) == 2 + ids = [c["id"] for c in chats] + assert ids == sorted(ids) + assert all("time" in c for c in chats) + + +def test_chat_created_with_optional_fields_defaulted(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + append_event(conn, kind="chat_created", payload={ + "id": "chat_bot_a", + "host_bot_id": "bot_a", + "initial_time": "2026-04-26T20:00:00+00:00", + }) + project(conn) + chat = get_chat(conn, "chat_bot_a") + assert chat is not None + assert chat["guest_bot_id"] is None + assert chat["weather"] == "" + assert chat["narrative_anchor"] == "" + assert chat["active_scene_id"] is None + + +def test_container_created_inserts_and_findable_by_name(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + append_event(conn, kind="chat_created", payload=_chat_payload()) + append_event(conn, kind="container_created", payload=_container_payload()) + project(conn) + c = find_container(conn, "chat_bot_a", "office") + assert c is not None + assert c["name"] == "office" + assert c["type"] == "workplace" + assert c["chat_id"] == "chat_bot_a" + assert c["parent_id"] is None + assert c["properties"] == { + "public": True, + "moving": False, + "audible_range": "normal", + "slots": [], + } + # get_container by id also works + c2 = get_container(conn, c["id"]) + assert c2 is not None + assert c2["name"] == "office" + assert c2["properties"]["public"] is True + + +def test_find_container_returns_none_when_missing(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + assert find_container(conn, "chat_bot_a", "nope") is None + assert get_container(conn, 999) is None + + +def test_activity_change_inserts_then_replaces(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + append_event(conn, kind="chat_created", payload=_chat_payload()) + append_event(conn, kind="container_created", payload=_container_payload()) + # First activity event then a second for the same entity that supersedes it. + append_event(conn, kind="activity_change", payload={ + "entity_id": "bot_a", + "container_id": 1, + "slot": "desk_chair", + "posture": "sitting", + "action": { + "verb": "writing email", + "interruptible": True, + "required_attention": "medium", + "expected_duration": "a few minutes", + "started_at": "2026-04-26T20:00:00+00:00", + }, + "attention": "the screen", + "holding": [], + "status": {}, + }) + append_event(conn, kind="activity_change", payload={ + "entity_id": "bot_a", + "container_id": 1, + "posture": "standing", + "action": {"verb": "pacing"}, + "attention": "the window", + }) + project(conn) + a = get_activity(conn, "bot_a") + assert a is not None + assert a["entity_id"] == "bot_a" + assert a["container_id"] == 1 + # second event replaced first + assert a["posture"] == "standing" + assert a["action"]["verb"] == "pacing" + assert a["attention"] == "the window" + # only one row exists for that entity + count = conn.execute( + "SELECT COUNT(*) FROM activity WHERE entity_id = ?", ("bot_a",) + ).fetchone()[0] + assert count == 1 + + +def test_activity_change_initial_values_persist_when_only_one_event(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + append_event(conn, kind="chat_created", payload=_chat_payload()) + append_event(conn, kind="container_created", payload=_container_payload()) + append_event(conn, kind="activity_change", payload={ + "entity_id": "bot_a", + "container_id": 1, + "slot": "desk_chair", + "posture": "sitting", + "action": {"verb": "writing email"}, + "attention": "the screen", + "holding": ["pen"], + "status": {"hungry": False}, + }) + project(conn) + a = get_activity(conn, "bot_a") + assert a is not None + assert a["slot"] == "desk_chair" + assert a["posture"] == "sitting" + assert a["action"]["verb"] == "writing email" + assert a["attention"] == "the screen" + assert a["holding"] == ["pen"] + assert a["status"] == {"hungry": False} + + +def test_activity_change_defaults_for_minimal_payload(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + append_event(conn, kind="activity_change", payload={ + "entity_id": "you", + }) + project(conn) + a = get_activity(conn, "you") + assert a is not None + assert a["container_id"] is None + assert a["slot"] is None + assert a["posture"] == "" + assert a["action"] == {} + assert a["attention"] == "" + assert a["holding"] == [] + assert a["status"] == {} + + +def test_get_activity_returns_none_for_missing(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + assert get_activity(conn, "ghost") is None + + +def test_scene_opened_marks_active_scene_id(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + append_event(conn, kind="chat_created", payload=_chat_payload()) + append_event(conn, kind="container_created", payload=_container_payload()) + append_event(conn, kind="scene_opened", payload={ + "chat_id": "chat_bot_a", + "container_id": 1, + "started_at": "2026-04-26T20:00:00+00:00", + "participants": ["you", "bot_a"], + }) + project(conn) + + s = active_scene(conn, "chat_bot_a") + assert s is not None + assert s["chat_id"] == "chat_bot_a" + assert s["container_id"] == 1 + assert s["started_at"] == "2026-04-26T20:00:00+00:00" + assert s["ended_at"] is None + assert s["significance"] == 0 + assert s["participants"] == ["you", "bot_a"] + + chat = get_chat(conn, "chat_bot_a") + assert chat["active_scene_id"] == s["id"] + + s2 = get_scene(conn, s["id"]) + assert s2 is not None + assert s2["participants"] == ["you", "bot_a"] + + +def test_scene_closed_clears_active_scene_id_and_records_end(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + append_event(conn, kind="chat_created", payload=_chat_payload()) + append_event(conn, kind="container_created", payload=_container_payload()) + append_event(conn, kind="scene_opened", payload={ + "chat_id": "chat_bot_a", + "container_id": 1, + "started_at": "2026-04-26T20:00:00+00:00", + "participants": ["you", "bot_a"], + }) + # The first scene insert will be id=1 (first row in scenes). + append_event(conn, kind="scene_closed", payload={ + "scene_id": 1, + "ended_at": "2026-04-26T21:00:00+00:00", + "significance": 2, + }) + project(conn) + + assert active_scene(conn, "chat_bot_a") is None + chat = get_chat(conn, "chat_bot_a") + assert chat["active_scene_id"] is None + s = get_scene(conn, 1) + assert s["ended_at"] == "2026-04-26T21:00:00+00:00" + assert s["significance"] == 2 + + +def test_scene_closed_significance_defaults_to_zero(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + append_event(conn, kind="chat_created", payload=_chat_payload()) + append_event(conn, kind="scene_opened", payload={ + "chat_id": "chat_bot_a", + "started_at": "2026-04-26T20:00:00+00:00", + "participants": ["you", "bot_a"], + }) + append_event(conn, kind="scene_closed", payload={ + "scene_id": 1, + "ended_at": "2026-04-26T21:00:00+00:00", + }) + project(conn) + s = get_scene(conn, 1) + assert s["significance"] == 0 + assert s["ended_at"] == "2026-04-26T21:00:00+00:00" + + +def test_get_scene_returns_none_for_missing(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + assert get_scene(conn, 999) is None + assert active_scene(conn, "chat_missing") is None + + +def test_schema_version_after_migration_is_7(tmp_path): + db = tmp_path / "t.db" + apply_migrations(db) + with open_db(db) as conn: + row = conn.execute( + "SELECT value FROM meta WHERE key = 'schema_version'" + ).fetchone() + assert int(row[0]) == 7