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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282"""Compaction decisions use the last request's usage without corrupting replay."""
import jsonimport unittest
from harness import Albedo, Provider, Reply, error, exclusive, text
class CompactionProviderUsageTest(unittest.TestCase): def session_with_window(self, app, tokens=50000): session = app.session() route = f"/sessions/{session}?view=configuration" with app.api(route) as response: etag = response.headers["ETag"] app.api( route, {"context_window": tokens}, method="PATCH", headers={"If-Match": etag}, ).close() return session
def exercise( self, usage, payload_size, expect_compaction, missing_after_low=False, between_turns=lambda _app: None, ): opaque = "opaque:" + "x" * payload_size summaries = [] followups = [] observations = [] armed = False called = 0
def reply(request): nonlocal called inputs = request["input"] last = inputs[-1] if inputs else {} content = last.get("content", "") if isinstance(content, list): content = "".join(part.get("text", "") for part in content) replayed_summary = last.get("role") == "user" and ( '<system-note origin="compaction">' in content ) if request.get("tool_choice") == "none" or replayed_summary: summaries.append(called) return text("older history summarized") if armed and (called == 0 or (missing_after_low and called == 1)): called += 1 response = { "id": "response", "status": "completed", "output": [ { "type": "reasoning", "id": "reasoning", "summary": [], "encrypted_content": opaque, }, { "type": "function_call", "id": "call", "call_id": f"call-{called}", "name": "python", "arguments": '{"code":"print(123)"}', "status": "completed", }, ], } if usage is not None and not (missing_after_low and called == 2): response["usage"] = usage return Reply( "raw", events=[{"type": "response.completed", "response": response}] ) if armed: followups.append(called) if armed and not expect_compaction: with app.api(f"/sessions/{session}/context") as response: observations.append(json.load(response)["compaction"]) return text("done")
provider = Provider(reply, chunk_size=64000) self.addCleanup(provider.close) with Albedo(provider, protocol="responses") as app: session = self.session_with_window(app) for prompt in ("older one", "older two", "older three"): prompt += " history" * 4000 app.prompt(session, prompt).close() app.idle(session) summaries.clear() armed = True app.prompt(session, "run a tool").close() app.idle(session) if missing_after_low: self.assertEqual(called, 2) if expect_compaction: self.assertTrue(summaries) self.assertEqual(summaries[0], 2 if missing_after_low else 1) self.assertIn(called, followups) self.assertEqual(called, 2 if missing_after_low else 1) self.assertEqual(bool(summaries), expect_compaction) if not expect_compaction: self.assertTrue(observations) self.assertGreater(observations[-1]["estimated_input_tokens"], 12) self.assertLess(observations[-1]["estimated_input_tokens"], 1000) self.assertIn("provider-reported", observations[-1]["estimate_method"]) request = provider.requests[-1]["request"] replay = [ item for item in request["input"] if item.get("type") == "reasoning" ] self.assertTrue( any(item.get("encrypted_content") == opaque for item in replay) ) self.assertTrue( any( item.get("type") == "function_call_output" for item in request["input"] ) ) requests_before = len(provider.requests) between_turns(app) app.prompt(session, "keep going in a new turn").close() app.idle(session) self.assertFalse( summaries, "the new turn discarded usable provider usage" ) self.assertGreater(len(provider.requests), requests_before) self.assertIn("done", json.dumps(app.history(session)))
def test_low_usage_preserves_large_opaque_replay(self): self.exercise({"input_tokens": 10, "output_tokens": 2}, 400000, False)
@exclusive def test_low_usage_survives_a_daemon_restart(self): self.exercise( {"input_tokens": 10, "output_tokens": 2}, 400000, False, between_turns=lambda app: app.restart(), )
def test_high_usage_compacts_small_projection(self): self.exercise({"input_tokens": 50000, "output_tokens": 2}, 10, True)
def test_missing_usage_falls_back_to_estimate(self): self.exercise( {"input_tokens": 10, "output_tokens": 2}, 60000, True, missing_after_low=True, )
def test_high_usage_without_evictable_history_keeps_the_turn_running(self): def reply(request): if request["input"][-1].get("type") == "function_call_output": return text("continued without dropping the newest unit") return Reply( "python", "print(123)", usage={"input_tokens": 40000, "output_tokens": 2}, )
provider = Provider(reply) self.addCleanup(provider.close) with Albedo(provider, protocol="responses") as app: session = self.session_with_window(app) app.prompt(session, "run a tool").close() app.idle(session) self.assertIn( "continued without dropping the newest unit", json.dumps(app.history(session)), ) self.assertEqual(len(provider.requests), 2)
def test_new_tool_results_are_counted_before_the_next_request(self): armed = False summaries = [] summaries_before_tools = []
def reply(request): if request.get("tool_choice") == "none": summaries.append(request) return text("older history summarized") if not armed: return text("ready") results = [ item for item in request["input"] if item.get("type") == "function_call_output" ] if results: if not summaries: return error(400, "new tool results exceed the remaining context") return text("continued after making room for the tool results") summaries_before_tools.append(len(summaries)) calls = [ { "type": "function_call", "id": f"call-{index}", "call_id": f"call-{index}", "name": "python", "arguments": json.dumps({"code": "print('x' * 32000)"}), "status": "completed", } for index in range(2) ] return Reply( "raw", events=[ { "type": "response.completed", "response": { "id": "response", "status": "completed", "output": calls, "usage": {"input_tokens": 35000, "output_tokens": 2}, }, } ], )
provider = Provider(reply, chunk_size=64000) self.addCleanup(provider.close) with Albedo(provider, protocol="responses") as app: session = self.session_with_window(app) for prompt in ("older one", "older two", "older three"): app.prompt(session, prompt + " history" * 4000).close() app.idle(session) summaries.clear() armed = True app.prompt(session, "run both tools").close() app.idle(session) self.assertTrue(summaries) self.assertEqual(summaries_before_tools, [0]) self.assertIn( "continued after making room for the tool results", json.dumps(app.history(session)), )
def test_a_changed_projection_or_model_discards_the_saved_measurement(self): observations = [] inspect = False
def reply(_request): if inspect: with app.api(f"/sessions/{session}/context") as response: observations.append(json.load(response)["compaction"]) return text("ready", usage={"input_tokens": 10, "output_tokens": 2})
provider = Provider(reply) self.addCleanup(provider.close) with Albedo(provider, protocol="responses") as app: session = self.session_with_window(app) for prompt in ("older one", "older two", "older three"): app.prompt(session, prompt).close() app.idle(session) with app.api(f"/sessions/{session}/compaction", {}) as response: self.assertEqual(json.load(response)["state"], "compacted") app.idle(session) inspect = True app.prompt(session, "after compaction").close() app.idle(session) self.assertIn("local byte-based", observations[-1]["estimate_method"]) app.prompt(session, "same projection again").close() app.idle(session) self.assertIn("provider-reported", observations[-1]["estimate_method"]) route = f"/sessions/{session}?view=configuration" with app.api(route) as response: etag = response.headers["ETag"] app.api( route, {"model": "changed-model"}, method="PATCH", headers={"If-Match": etag}, ).close() app.prompt(session, "after model change").close() app.idle(session) self.assertIn("local byte-based", observations[-1]["estimate_method"])