|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import pytest |
| 4 | +from ag_ui.core import EventType |
| 5 | +from langgraph.errors import GraphRecursionError |
| 6 | +from step_budget import STEP_BUDGET_MESSAGE, StepBudgetAwareAgent |
| 7 | + |
| 8 | + |
| 9 | +class FakeInput: |
| 10 | + def __init__(self, messages=None): |
| 11 | + self.thread_id = "thread-1" |
| 12 | + self.run_id = "run-1" |
| 13 | + self.messages = messages or [] |
| 14 | + |
| 15 | + |
| 16 | +class FakeCall: |
| 17 | + def __init__(self, name): |
| 18 | + self.name = name |
| 19 | + |
| 20 | + |
| 21 | +class FakeMessage: |
| 22 | + def __init__(self, names): |
| 23 | + self.tool_calls = [FakeCall(n) for n in names] |
| 24 | + |
| 25 | + |
| 26 | +def drain(agent, run_input): |
| 27 | + async def collect(): |
| 28 | + return [event async for event in agent.run(run_input)] |
| 29 | + |
| 30 | + return asyncio.run(collect()) |
| 31 | + |
| 32 | + |
| 33 | +def run_with(monkeypatch, events, error=None): |
| 34 | + """Drive StepBudgetAwareAgent.run over a stubbed parent generator.""" |
| 35 | + agent = StepBudgetAwareAgent.__new__(StepBudgetAwareAgent) |
| 36 | + |
| 37 | + async def fake_parent(_self, _input): |
| 38 | + for event in events: |
| 39 | + yield event |
| 40 | + if error is not None: |
| 41 | + raise error |
| 42 | + |
| 43 | + monkeypatch.setattr( |
| 44 | + "step_budget.LangGraphAGUIAgent.run", fake_parent, raising=True |
| 45 | + ) |
| 46 | + return drain(agent, FakeInput([FakeMessage(["search_pull_requests"])])) |
| 47 | + |
| 48 | + |
| 49 | +def test_events_pass_through_untouched_when_the_run_succeeds(monkeypatch): |
| 50 | + events = run_with(monkeypatch, ["a", "b"]) |
| 51 | + |
| 52 | + assert events == ["a", "b"] |
| 53 | + |
| 54 | + |
| 55 | +def test_a_blown_step_budget_becomes_an_answer_not_a_broken_stream(monkeypatch): |
| 56 | + events = run_with( |
| 57 | + monkeypatch, ["a"], GraphRecursionError("Recursion limit of 60 reached") |
| 58 | + ) |
| 59 | + |
| 60 | + # Whatever the run produced before the limit survives, then the run is |
| 61 | + # closed the way the protocol expects rather than dying mid-stream. |
| 62 | + assert events[0] == "a" |
| 63 | + types = [getattr(e, "type", None) for e in events[1:]] |
| 64 | + assert types == [ |
| 65 | + EventType.TEXT_MESSAGE_START, |
| 66 | + EventType.TEXT_MESSAGE_CONTENT, |
| 67 | + EventType.TEXT_MESSAGE_END, |
| 68 | + EventType.RUN_FINISHED, |
| 69 | + ] |
| 70 | + |
| 71 | + |
| 72 | +def test_the_step_budget_message_tells_the_user_what_to_do(monkeypatch): |
| 73 | + events = run_with(monkeypatch, [], GraphRecursionError("boom")) |
| 74 | + content = next( |
| 75 | + e for e in events if getattr(e, "type", None) == EventType.TEXT_MESSAGE_CONTENT |
| 76 | + ) |
| 77 | + |
| 78 | + assert content.delta == STEP_BUDGET_MESSAGE |
| 79 | + assert "narrow" in content.delta.lower() |
| 80 | + # The user needs to know the write side is clean, not half-applied. |
| 81 | + assert "half-written" in content.delta |
| 82 | + |
| 83 | + |
| 84 | +def test_the_closing_events_carry_the_run_identity(monkeypatch): |
| 85 | + events = run_with(monkeypatch, [], GraphRecursionError("boom")) |
| 86 | + finished = events[-1] |
| 87 | + start = events[0] |
| 88 | + end = events[2] |
| 89 | + |
| 90 | + assert finished.thread_id == "thread-1" |
| 91 | + assert finished.run_id == "run-1" |
| 92 | + # One message, opened and closed under the same id. |
| 93 | + assert start.message_id == end.message_id |
| 94 | + |
| 95 | + |
| 96 | +def test_other_failures_still_propagate(monkeypatch): |
| 97 | + with pytest.raises(ValueError): |
| 98 | + run_with(monkeypatch, [], ValueError("something else")) |
| 99 | + |
| 100 | + |
| 101 | +def test_the_tool_trail_is_logged_for_diagnosis(monkeypatch, caplog): |
| 102 | + run_with(monkeypatch, [], GraphRecursionError("boom")) |
| 103 | + |
| 104 | + logged = [r for r in caplog.records if "step_budget" in str(r.msg)] |
| 105 | + assert logged, "the failure must be diagnosable from the logs alone" |
| 106 | + assert "search_pull_requests" in str(logged[0].msg) |
0 commit comments