"""A compaction whose session's last request is still cached asks for its summaries at the end of that request, so the provider reads the history it summarizes from cache; once the cache has lapsed, the summary goes out as a request of its own.""" import json import time import unittest from harness import Albedo, Provider, exclusive, text LEAF = "Write the memory of one stretch of this conversation" NOTES = "Rewrite your compaction notes now" EVICTED = "" def cache_table(seconds): """A local cache-table entry for the fixture host: a refresh TTL.""" return { "version": 1, "entries": [ { "id": "fixture", "match": {"host": "127.0.0.1"}, "policy": "refresh", "clock": "response", "tiers": [{"seconds": seconds, "write": 1.25}], "read": 0.1, "evidence": "measured", } ], } def reply(request): sent = json.dumps(request["input"]) if LEAF in sent: return text("replayed leaf") if NOTES in sent: return text("replayed notes") if EVICTED in sent: return text("separate summary") return text("ordinary answer") @exclusive class SummaryReplayTests(unittest.TestCase): def setUp(self): self.provider = Provider(reply) self.addCleanup(self.provider.close) self.app = Albedo(self.provider, protocol="responses") self.app.__enter__() self.addCleanup(self.app.__exit__, None, None, None) def converse(self, ttl_seconds): (self.app.home / "cache-ttl.json").write_text( json.dumps(cache_table(ttl_seconds)) ) session = self.app.session() for prompt in ("older unit one", "older unit two", "newest unit"): self.app.prompt(session, prompt).close() self.app.idle(session) return session def compact(self, session): before = len(self.provider.requests) with self.app.api( f"/sessions/{session}/compaction", {"strategy": "coding-lcm"} ) as response: self.assertEqual(json.load(response)["state"], "compacted") return [entry["request"] for entry in self.provider.requests[before:]] def test_a_cached_request_is_replayed_with_the_summary_asked_at_its_end(self): session = self.converse(3600) turn = self.provider.requests[-1]["request"] calls = self.compact(session) replayed = [call for call in calls if LEAF in json.dumps(call["input"])] self.assertEqual(len(replayed), 1, calls) leaf = replayed[0] # Everything the last turn sent comes first, unchanged, so the # provider reads it from cache; the request asked for the summary # with the turn's own tools and tool choice. self.assertEqual(leaf["input"][: len(turn["input"])], turn["input"]) self.assertIn(LEAF, json.dumps(leaf["input"][-1])) self.assertEqual(leaf.get("tools"), turn.get("tools")) self.assertEqual(leaf.get("tool_choice"), turn.get("tool_choice")) self.assertEqual(leaf.get("instructions"), turn.get("instructions")) self.assertFalse(any(EVICTED in json.dumps(call["input"]) for call in calls)) notes = [call for call in calls if NOTES in json.dumps(call["input"])] self.assertEqual(len(notes), 1, calls) self.assertEqual(notes[0]["input"][: len(turn["input"])], turn["input"]) self.app.prompt(session, "after fold").close() self.app.idle(session) shown = json.dumps(self.provider.requests[-1]["request"]["input"]) self.assertIn("replayed leaf", shown) self.assertIn("replayed notes", shown) def test_a_lapsed_cache_summarizes_separately(self): session = self.converse(1) # The lifetime counts with a tenth spare, so 0.9s is already too late. time.sleep(1.2) calls = self.compact(session) self.assertFalse(any(LEAF in json.dumps(call["input"]) for call in calls)) separate = [call for call in calls if EVICTED in json.dumps(call["input"])] self.assertTrue(separate, calls) self.assertEqual(separate[0].get("tool_choice"), "none") if __name__ == "__main__": unittest.main()