Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111"""A compaction whose session's last request is still cached asks for itssummaries at the end of that request, so the provider reads the history itsummarizes from cache; once the cache has lapsed, the summary goes out as arequest of its own."""
import jsonimport timeimport 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 = "<newly-evicted-history>"
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")
@exclusiveclass 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()