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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279"""Every request carries the model's catalogued output ceiling, or albedo'sfallback for a model the catalogue gives none.
Catches a provider whose request falls back to a wire default instead of themodel's own ceiling: OpenCode Go and GitHub Copilot turns stopped at exactly8192 output tokens, the Anthropic Messages encoder's old default, though thecatalogue said 128000. The ceiling is filled once for every provider, so eachone the fake provider can drive is checked here with a ceiling no wire wouldpick by itself. GitHub Copilot is checked in github_copilot_test.py (itssign-in needs a fake GitHub) and Gemini's nested limit inprovider_protocols_test.py; Claude, Codex, Bedrock, Vertex and Antigravityreach fixed or signed endpoints the fake cannot stand in for.
The catalogue is daemon-wide, so the class is exclusive."""
import jsonimport unittest
from harness import Albedo, Provider, Reply, exclusive_class, text
# A profile name, its extension and protocol, the model, the field its wire# carries the limit in, and the model's catalogued ceiling.CASES = [ ( "openai-chat", "openai", "chat_completions", "chat-model", "max_completion_tokens", 11111, ), ( "openai-responses", "openai", "responses", "responses-model", "max_output_tokens", 22222, ), ( "opencode-go-chat", "opencode-go", "chat_completions", "glm-fixture", "max_completion_tokens", 33333, ), ( "opencode-go-messages", "opencode-go", "chat_completions", "claude-go-fixture", "max_tokens", 44444, ), ( "opencode-zen", "opencode-zen", "chat_completions", "zen-fixture", "max_completion_tokens", 55555, ), ("mistral", "mistral", "chat_completions", "mistral-fixture", "max_tokens", 66666), ( "perplexity", "perplexity", "responses", "openai/gpt-fixture", "max_output_tokens", 77777, ),]
# About 20000 tokens at three bytes each, a third of the tight models' window.LONG_PROMPT = "word " * 12000
LIMIT_FIELDS = {"max_tokens", "max_completion_tokens", "max_output_tokens"}
# extension.output_fallback: the median of models.dev's output limits.FALLBACK = 65536
def limits(context, output): return {"limit": {"context": context, "output": output}}
MODELS_DEV = { # Every fixture endpoint is on loopback, so this namespace answers for each # profile no catalogue of its own claims. "fixture": { "id": "fixture", "name": "Fixture", "api": "http://127.0.0.1", "models": { "chat-model": limits(200000, 11111), "responses-model": limits(200000, 22222), "glm-fixture": limits(200000, 33333), "claude-go-fixture": limits(200000, 44444), "zen-fixture": limits(200000, 55555), # Ceilings a long prompt leaves no room for. "tight-chat": limits(60000, 50000), "claude-tight": limits(60000, 50000), # A window but no output limit: the fallback must fit the window. "unlimited-chat": limits(60000, 0), }, }, "mistral": { "id": "mistral", "name": "Mistral", "models": {"mistral-fixture": limits(200000, 66666)}, }, # Perplexity's ids are its trainers'. "openai": { "id": "openai", "name": "OpenAI", "models": {"gpt-fixture": limits(200000, 77777)}, },}
def cut_off(_request): """A Chat Completions answer that stops at its output limit.""" chunk = {"id": "fixture", "choices": [{"index": 0, "delta": {"content": "half"}}]} stop = { "id": "fixture", "choices": [{"index": 0, "delta": {}, "finish_reason": "length"}], } return Reply("text", events=[chunk, stop])
@exclusive_classclass OutputCeilingTests(unittest.TestCase): def start(self, profiles, script=lambda _request: text("ok")): """Profiles `(name, extension, protocol, model)`, one fake provider per extension: OpenCode Go pools its profiles' keys, so two of its profiles may send to either one's base url.""" shared = {} providers = {} configured = {} models = [{"id": model} for *_, model in profiles] for name, extension, protocol, model in profiles: if extension not in shared: shared[extension] = Provider(script, catalog={"data": models}) self.addCleanup(shared[extension].close) provider = providers[name] = shared[extension] configured[name] = { "extension": extension, "baseUrl": provider.url, "apiKey": f"{name}-key", "model": model, "protocol": protocol, }
def offline(app): (app.home / "models.json").write_text(json.dumps(MODELS_DEV)) (app.home / "models.json.index").unlink(missing_ok=True) app.write_extensions({"models": {"refreshHours": 0}})
app = Albedo( next(iter(providers.values())), providers=configured, prepare=offline ) app.__enter__() self.addCleanup(app.__exit__, None, None, None) return app, providers
def turn(self, app, profile, prompt="say ok"): """Runs one turn on `profile` and returns the errors it ended with.""" app.profile = profile session = app.session() app.prompt(session, prompt).close() snapshot = app.idle(session) return [ line["text"] for line in snapshot["activity"]["lines"] if line["kind"] == "error" ]
def test_every_provider_sends_the_models_catalogued_ceiling(self): app, providers = self.start([case[:4] for case in CASES]) for name, _, _, model, field, ceiling in CASES: with self.subTest(provider=name): self.assertEqual(self.turn(app, name), []) [request] = [ sent for sent in providers[name].requests if sent["model"] == model ] body = request["request"] # Exactly the catalogue's number, in the one field its wire # names: a wire default would be some other number. self.assertEqual(body[field], ceiling) self.assertEqual(LIMIT_FIELDS & body.keys(), {field})
def test_a_model_without_a_ceiling_is_sent_the_fallback(self): app, providers = self.start( [ ("unknown-chat", "openai", "chat_completions", "unknown-model"), ("unknown-responses", "openai", "responses", "unknown-model"), # Anthropic Messages, which requires a limit. ("messages", "opencode-go", "chat_completions", "claude-uncatalogued"), ] ) for name, field in ( ("unknown-chat", "max_completion_tokens"), ("unknown-responses", "max_output_tokens"), ("messages", "max_tokens"), ): with self.subTest(provider=name): self.assertEqual(self.turn(app, name), []) body = providers[name].requests[-1]["request"] self.assertEqual(body[field], FALLBACK) self.assertEqual(LIMIT_FIELDS & body.keys(), {field})
def test_the_fallback_is_clamped_to_a_known_window(self): app, providers = self.start( [("chat", "openai", "chat_completions", "unlimited-chat")] ) self.assertEqual(self.turn(app, "chat", LONG_PROMPT), []) [request] = providers["chat"].requests sent = request["request"]["max_completion_tokens"] self.assertLessEqual(sent, 60000 - len(LONG_PROMPT) // 3) self.assertGreaterEqual(sent, 1024)
def test_a_turn_cut_off_at_the_fallback_names_it(self): app, _ = self.start( [("chat", "openai", "chat_completions", "unknown-model")], script=cut_off ) failure = self.turn(app, "chat")[0] self.assertIn(f"model stopped at the {FALLBACK}-token output limit", failure) self.assertIn( f"no output limit in the model list; albedo's fallback of {FALLBACK}", failure, )
def test_a_turn_cut_off_at_its_limit_names_the_limit_and_its_source(self): app, _ = self.start( [("chat", "openai", "chat_completions", "chat-model")], script=cut_off ) # The error's first line; the partial answer follows it. failure = self.turn(app, "chat")[0] self.assertIn( "model stopped at the 11111-token output limit albedo sent", failure ) self.assertIn("models.dev catalog", failure)
def test_a_prompt_near_the_window_clamps_the_limit_to_fit_it(self): app, providers = self.start( [ ("chat", "openai", "chat_completions", "tight-chat"), ("messages", "opencode-go", "chat_completions", "claude-tight"), ] ) for name, field in ( ("chat", "max_completion_tokens"), ("messages", "max_tokens"), ): with self.subTest(provider=name): self.assertEqual(self.turn(app, name, LONG_PROMPT), []) [request] = providers[name].requests sent = request["request"][field] # Under the 50000 ceiling, and with the prompt (at a # conservative three bytes a token) still inside the 60000 # window; never below the floor. self.assertLess(sent, 50000) self.assertLessEqual(sent, 60000 - len(LONG_PROMPT) // 3) self.assertGreaterEqual(sent, 1024)
def test_a_clamped_turn_cut_off_at_its_limit_says_it_was_clamped(self): app, _ = self.start( [("chat", "openai", "chat_completions", "tight-chat")], script=cut_off ) failure = self.turn(app, "chat", LONG_PROMPT)[0] self.assertIn("clamped to fit the window", failure) self.assertIn("the model's ceiling is 50000", failure)
if __name__ == "__main__": unittest.main()