Skip to content

Commit 0d3ea14

Browse files
authored
Python: feat(core): add max_duration_seconds bound and stop_reason signal to tool loop (#7587) (#7772)
* feat(core): add max_duration_seconds bound and stop_reason signal to tool loop (#7587) * fix(core): unify batch limit decision precedence and propagate stop_reason to AgentResponse * test: add missing tests for duration bounds, batch limits, and session persistence * Fix _apply_batch_limit_decision limits bug and Phase 1 test execution - **agent-framework-core**: Refactored _apply_batch_limit_decision to compute perf_counter exactly once per decision point, eliminating the structural fragility where the threshold check and log message used separate clock samples. - **agent-framework-core**: Re-ordered limit checking in Phase 1 to execute before approval response resolution, successfully preventing execution during approved replays when limits are reached. A post-approval check ensures consecutive error limits (�ction == stop) remain handled. - **agent-framework-core**: Rewrote 6 tests in est_function_invocation_logic.py that used a fragile call_count mock. The tests now use a mutable clock array that advances directly during the tool execution semantic step, providing true robustness against internal engine refactors. Note: The fallback response trigger (_ensure_function_invocation_limit_fallback_response) remains scoped strictly to the function call limit, preserving pre-existing behavior. Expanding this to cover consecutive errors (�ction == stop) or duration timeouts is intentionally left out of scope for this fix. * fix(core): preserve budget start_time across approval resumes, dedupe streaming limit decision, fix double-counted approval calls against max_function_calls * chore: remove CHANGELOG entry, updated centrally on release per review * chore: fix ruff formatting in test_function_invocation_logic.py (exact CI-pinned ruff 0.16.3) * fix(core): remove _agent_framework_stop_reason signal, keep max_duration_seconds only * fix(core): satisfy pre-commit and typing CI checks
1 parent 63edba2 commit 0d3ea14

3 files changed

Lines changed: 532 additions & 39 deletions

File tree

‎python/packages/core/agent_framework/_harness/_tool_approval.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ async def process(self, context: AgentContext, call_next: Callable[[], Awaitable
385385
raise RuntimeError("ToolApprovalMiddleware requires an AgentSession.")
386386

387387
state = _get_state(context.session, source_id=self.source_id)
388-
context.client_kwargs.setdefault(_FUNCTION_INVOCATION_BUDGET_STATE_KEY, {})
388+
session_budget = context.session.state.setdefault(_FUNCTION_INVOCATION_BUDGET_STATE_KEY, {})
389+
context.client_kwargs.setdefault(_FUNCTION_INVOCATION_BUDGET_STATE_KEY, session_budget)
389390
context.messages = self._prepare_inbound_messages(context.messages, state, context.session)
390391
await self._drain_auto_approvable_queue(state)
391392
if next_queued := self._pop_next_queued_request(state):

0 commit comments

Comments
 (0)