Repository navigation
Conversation
|
| await self.session.wait_if_paused() | ||
| if self.session.cancelled: | ||
| break | ||
| budget_reason = self._budget_exceeded(loop_started) |
There was a problem hiding this comment.
Retries ignore the runtime limit
The budget check runs before the first model call, but not before retries after a context-length error. If the first request takes longer than agent_max_runtime_s before failing, the loop still sends another request after the deadline. Check the budget before each retry and return the same budget-stop result when it is reached.
Prompt To Fix With AI
This is a comment left during a code review.
Path: openhack/agents/base.py
Line: 446
Comment:
**Retries ignore the runtime limit**
The budget check runs before the first model call, but not before retries after a context-length error. If the first request takes longer than `agent_max_runtime_s` before failing, the loop still sends another request after the deadline. Check the budget before each retry and return the same budget-stop result when it is reached.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| max_cost = getattr(settings, "agent_max_cost_usd", 0.0) or 0.0 | ||
| if max_cost > 0 and self.session.total_cost >= max_cost: |
There was a problem hiding this comment.
Coordinator calls bypass the budget
The session-wide cost cap only protects calls made through _agent_loop. CoordinatorAgent.run bypasses that loop, and _extract_high_risk_features and _write_app_specific_researchers call the model directly. If recon finishes below the cap and hunting reaches it, feature extraction still sends a paid request. Apply the cost check to these calls too, or stop the coordinator when the session budget is reached.
Prompt To Fix With AI
This is a comment left during a code review.
Path: openhack/agents/base.py
Line: 247-248
Comment:
**Coordinator calls bypass the budget**
The session-wide cost cap only protects calls made through `_agent_loop`. `CoordinatorAgent.run` bypasses that loop, and `_extract_high_risk_features` and `_write_app_specific_researchers` call the model directly. If recon finishes below the cap and hunting reaches it, feature extraction still sends a paid request. Apply the cost check to these calls too, or stop the coordinator when the session budget is reached.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.|
|
||
| assert result["error"] == "Budget exceeded" | ||
| assert "agent_max_runtime_s" in result["detail"] | ||
| assert 2 <= llm.calls <= 6 |
There was a problem hiding this comment.
This test depends on real scheduling time. On a busy runner, one asyncio.sleep(0.08) can resume after the full 0.2-second limit. Correct code can then stop after one call and fail the llm.calls >= 2 assertion. Use a controlled clock instead of real sleeps so runner load does not cause false failures.
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/test_agent_budget.py
Line: 85
Comment:
**Runtime test fails under load**
This test depends on real scheduling time. On a busy runner, one `asyncio.sleep(0.08)` can resume after the full 0.2-second limit. Correct code can then stop after one call and fail the `llm.calls >= 2` assertion. Use a controlled clock instead of real sleeps so runner load does not cause false failures.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| def test_defaults_are_unlimited(): | ||
| assert settings.agent_max_cost_usd == 0.0 | ||
| assert settings.agent_max_runtime_s == 0.0 |
There was a problem hiding this comment.
Local budgets change test results
These tests read the user's loaded settings instead of isolating the new limits. If either limit is set through the environment or saved config, test_defaults_are_unlimited fails even though the declared defaults are correct. A configured runtime limit can also stop test_cost_cap_stops_the_loop early. Check defaults through Settings.model_fields and explicitly set both limits in each behavioral test.
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/test_agent_budget.py
Line: 54-56
Comment:
**Local budgets change test results**
These tests read the user's loaded `settings` instead of isolating the new limits. If either limit is set through the environment or saved config, `test_defaults_are_unlimited` fails even though the declared defaults are correct. A configured runtime limit can also stop `test_cost_cap_stops_the_loop` early. Check defaults through `Settings.model_fields` and explicitly set both limits in each behavioral test.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
Summary
Add optional cost and runtime limits to prevent agent loops from running indefinitely or exceeding a configured budget.
Changes
agent_max_cost_usdto limit total session spend across agents.agent_max_runtime_sto limit the wall-clock runtime of an individual agent loop.Backward Compatibility
Both limits default to
0(unlimited), preserving existing behavior unless explicitly configured.Testing
Add unit tests for budget enforcement and disabled-limit behavior.