"""A turn whose steps keep calling the same bindings with nearly the same code. Catches: a model looping across the steps of one turn (each reply fine on its own) going unnoticed, and the opposite, a note for a turn whose steps change their code or only wait. Only the real loop, kernel and trace together decide which bindings a step called and when a run of repeats is long enough. """ import difflib import unittest from harness import Albedo, Provider, python, text NOTE = "you may be repeating yourself" def repeating(step): return ( f"found = await files.find('needle', '.', max_results=5)\n" f"print('pass', {step}, len(str(found)))" ) # Each step searches too, but in code too different from the one before it. VARIED = [ "\n".join( [f"{name} = await files.find({name!r}, '.', max_results={step + 1})"] + [f"print({name!r} * {k}, len(str({name})))" for k in range(step % 3 + 1)] ) for step, name in enumerate( ["alpha", "brook", "cinder", "dune", "ember", "fjord", "grove", "heath"] ) ] class RepetitionTest(unittest.TestCase): def run_turn(self, cells): def script(request): done = sum( 1 for item in request["input"] if item.get("type") == "function_call" ) if done < len(cells): return python(cells[done]) return text("done") provider = Provider(script) try: with Albedo(provider, protocol="responses") as app: session = app.session() app.prompt(session, "find the needle").close() app.idle(session) history = app.history(session)["items"] finally: provider.close() notes = [ [ item["content"] for item in entry["request"]["input"] if item.get("role") == "user" and NOTE in str(item.get("content")) ] for entry in provider.requests ] # The note is a transcript entry, so the user sees it too. shown = [ part["text"] for entry in history if entry["kind"] == "note" for part in entry["content"] if part["kind"] == "text" and NOTE in part["text"] ] return notes, shown def test_eight_repeating_steps_tell_the_model_and_the_user(self): notes, shown = self.run_turn([repeating(step) for step in range(8)]) self.assertEqual(len(notes), 9) # Only the request after the eighth step carries the note. self.assertEqual([bool(found) for found in notes], [False] * 8 + [True]) self.assertIn("files.find", notes[8][0]) self.assertTrue(shown, "the user sees no repetition note") def test_steps_whose_code_changes_are_not_repeats(self): for before, after in zip(VARIED, VARIED[1:]): self.assertLess(difflib.SequenceMatcher(None, before, after).ratio(), 0.8) notes, shown = self.run_turn(VARIED) self.assertEqual(len(notes), 9) self.assertFalse(any(notes)) self.assertEqual(shown, []) def test_steps_that_only_wait_are_not_repeats(self): notes, shown = self.run_turn( [f"print(len(await cells.list()), {step})" for step in range(8)] ) self.assertEqual(len(notes), 9) self.assertFalse(any(notes)) self.assertEqual(shown, []) def test_repeating_syntax_error_steps_still_trigger_the_note(self): notes, shown = self.run_turn( [ f"# attempt {step}\n" "src = 'fn main() {}\n'\n" "errs = [line[:150] for line ;None]\n" for step in range(8) ] ) self.assertEqual(len(notes), 9) self.assertEqual([bool(found) for found in notes], [False] * 8 + [True]) self.assertIn("syntax_error", notes[8][0]) self.assertTrue(shown, "the user sees no repetition note") if __name__ == "__main__": unittest.main()