feat(task-5): scope matrix context state per room

This commit is contained in:
Mikhail Putilovskij 2026-04-19 17:41:04 +03:00
parent 03160a3b37
commit c11c8ecfbf
7 changed files with 189 additions and 72 deletions

View file

@ -13,7 +13,12 @@ from adapter.matrix.handlers.context_commands import (
make_handle_reset,
make_handle_save,
)
from adapter.matrix.store import get_load_pending, get_reset_pending, set_load_pending, set_reset_pending
from adapter.matrix.store import (
get_load_pending,
get_reset_pending,
set_load_pending,
set_room_meta,
)
from core.protocol import IncomingCommand, OutgoingMessage
from core.store import InMemoryStore
from sdk.interface import MessageResponse
@ -40,6 +45,11 @@ class MatrixCommandPlatform(MockPlatformClient):
async def test_save_command_auto_name_records_session():
platform = MatrixCommandPlatform()
store = InMemoryStore()
await set_room_meta(
store,
"!room:example.org",
{"chat_id": "C1", "matrix_user_id": "u1", "platform_chat_id": "matrix:room-1"},
)
handler = make_handle_save(
agent_api=platform._agent_api,
store=store,
@ -57,16 +67,22 @@ async def test_save_command_auto_name_records_session():
assert len(result) == 1
assert isinstance(result[0], OutgoingMessage)
assert "Сохранение запущено" in result[0].text
assert "Запрос на сохранение отправлен агенту" in result[0].text
sessions = await platform._prototype_state.list_saved_sessions("u1")
assert len(sessions) == 1
assert sessions[0]["name"].startswith("context-")
assert sessions[0]["source_context_id"] == "matrix:room-1"
@pytest.mark.asyncio
async def test_save_command_with_name_uses_given_name():
platform = MatrixCommandPlatform()
store = InMemoryStore()
await set_room_meta(
store,
"!room:example.org",
{"chat_id": "C1", "matrix_user_id": "u1", "platform_chat_id": "matrix:room-1"},
)
handler = make_handle_save(
agent_api=platform._agent_api,
store=store,
@ -164,15 +180,28 @@ async def test_reset_endpoint_unavailable_reports_error():
@pytest.mark.asyncio
async def test_context_command_shows_current_snapshot():
platform = MatrixCommandPlatform()
store = InMemoryStore()
await platform._prototype_state.set_current_session("u1", "session-a")
await platform._prototype_state.set_last_tokens_used("u1", 99)
runtime = build_runtime(platform=platform)
await runtime.chat_mgr.get_or_create(
user_id="u1",
chat_id="C1",
platform="matrix",
surface_ref="!room:example.org",
name="Chat 1",
)
await set_room_meta(
runtime.store,
"!room:example.org",
{"chat_id": "C1", "matrix_user_id": "u1", "platform_chat_id": "matrix:room-1"},
)
await platform._prototype_state.set_current_session("matrix:room-1", "session-a")
await platform._prototype_state.set_last_tokens_used("matrix:room-1", 99)
await platform._prototype_state.add_saved_session("u1", "session-a")
handler = make_handle_context(store=store, prototype_state=platform._prototype_state)
handler = make_handle_context(store=runtime.store, prototype_state=platform._prototype_state)
event = IncomingCommand(user_id="u1", platform="matrix", chat_id="C1", command="context", args=[])
result = await handler(event, None, platform, None, None)
result = await handler(event, runtime.auth_mgr, platform, runtime.chat_mgr, runtime.settings_mgr)
assert "Контекст чата: matrix:room-1" in result[0].text
assert "Сессия: session-a" in result[0].text
assert "Токены (последний ответ): 99" in result[0].text
assert "session-a" in result[0].text
@ -182,6 +211,15 @@ async def test_context_command_shows_current_snapshot():
async def test_bot_intercepts_numeric_load_selection():
platform = MatrixCommandPlatform()
runtime = build_runtime(platform=platform)
await set_room_meta(
runtime.store,
"!room:example.org",
{
"chat_id": "C1",
"matrix_user_id": "@alice:example.org",
"platform_chat_id": "matrix:room-1",
},
)
client = SimpleNamespace(
user_id="@bot:example.org",
room_send=AsyncMock(),
@ -199,39 +237,10 @@ async def test_bot_intercepts_numeric_load_selection():
await bot.on_room_message(room, event)
platform.send_message.assert_awaited_once()
assert await platform._prototype_state.get_current_session("@alice:example.org") == "session-a"
assert await platform._prototype_state.get_current_session("matrix:room-1") == "session-a"
assert await platform._prototype_state.get_current_session("C1") == "session-a"
client.room_send.assert_awaited_once_with(
"!room:example.org",
"m.room.message",
{"msgtype": "m.text", "body": "Загрузка: session-a"},
)
@pytest.mark.asyncio
async def test_bot_intercepts_reset_yes_before_dispatch():
platform = MatrixCommandPlatform()
runtime = build_runtime(platform=platform)
client = SimpleNamespace(
user_id="@bot:example.org",
room_send=AsyncMock(),
)
bot = MatrixBot(client, runtime)
runtime.dispatcher.dispatch = AsyncMock()
await set_reset_pending(runtime.store, "@alice:example.org", "!room:example.org", {"active": True})
room = SimpleNamespace(room_id="!room:example.org")
event = SimpleNamespace(sender="@alice:example.org", body="!yes")
with patch("adapter.matrix.handlers.context_commands.httpx.AsyncClient") as client_cls:
http_client = client_cls.return_value
http_client.__aenter__ = AsyncMock(return_value=http_client)
http_client.__aexit__ = AsyncMock(return_value=False)
http_client.post = AsyncMock(return_value=SimpleNamespace(status_code=200))
await bot.on_room_message(room, event)
runtime.dispatcher.dispatch.assert_not_awaited()
client.room_send.assert_awaited_once_with(
"!room:example.org",
"m.room.message",
{"msgtype": "m.text", "body": "Контекст сброшен."},
{"msgtype": "m.text", "body": "Запрос на загрузку отправлен агенту: session-a"},
)