Ops bot: remember the last exchanges so follow-ups have context

Each message started a fresh claude -p with only the database snapshot, so
«да, поправь» had nothing to refer to. Keep the last 6 exchanges (2 h, in
memory, shared by Telegram and BroV) and put them into the prompt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maksarsanjeev
2026-10-28 21:54:19 +03:00
parent 0f4e110512
commit 977665806f
2 changed files with 57 additions and 2 deletions
+21
View File
@@ -174,5 +174,26 @@ class Company(unittest.TestCase):
os.remove(path)
class HistoryTest(unittest.TestCase):
def test_follow_up_sees_previous_exchange(self):
brain._history.clear()
o = Ops(connect(":memory:"))
seen = []
def fake(prompt, *a, **k):
seen.append(prompt)
return '{"actions":[],"reply":"Ок"}'
brain.ask_claude = fake
brain.handle_text(o, "задача посмотреть бегущий по лезвию 2033", threading.Lock())
brain.handle_text(o, "да, поправь", threading.Lock())
self.assertNotIn("Недавний разговор", seen[0])
self.assertIn("бегущий по лезвию 2033", seen[1])
self.assertLess(seen[1].index("Недавний разговор"), seen[1].index("да, поправь"))
def test_old_history_expires(self):
brain._history.clear()
brain.remember("старое", "ответ", now=0)
self.assertEqual(brain.history_text(now=brain.HISTORY_TTL + 1), "")
if __name__ == "__main__":
unittest.main()