Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
refactor(agent): remove context block budget override (#5668)
chengyongruf37ba60ed489
diff --git a/nanobot/agent/context_governance.py b/nanobot/agent/context_governance.pyindex fef4c4b5c..e552ff133 100644--- a/nanobot/agent/context_governance.py+++ b/nanobot/agent/context_governance.py@@ -122,7 +122,6 @@ class ContextGovernanceConfig:session_key: str | Nonemax_tool_result_chars: intcontext_window_tokens: int | None = None- context_block_limit: int | None = Nonemax_tokens: int | None = None@@ -699,9 +698,7 @@ class ContextGovernor:max_output = config.max_tokens if isinstance(config.max_tokens, int) else (provider_max_tokens if isinstance(provider_max_tokens, int) else 4096)- budget = config.context_block_limit or (- config.context_window_tokens - max_output - SNIP_SAFETY_BUFFER- )+ budget = config.context_window_tokens - max_output - SNIP_SAFETY_BUFFERreturn budget if budget > 0 else 0@staticmethoddiff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.pyindex 14846f0be..583bd3d83 100644--- a/nanobot/agent/loop.py+++ b/nanobot/agent/loop.py@@ -274,7 +274,6 @@ class AgentLoop:max_iterations: int | None = None,max_concurrent_subagents: int | None = None,context_window_tokens: int | None = None,- context_block_limit: int | None = None,max_tool_result_chars: int | None = None,provider_retry_mode: str = "standard",tool_hint_max_length: int | None = None,@@ -355,7 +354,6 @@ class AgentLoop:preset_snapshot_loader=preset_snapshot_loader,)self.dream_model_preset = dream_model_preset- self.context_block_limit = context_block_limitself.max_tool_result_chars = (max_tool_result_charsif max_tool_result_chars is not None@@ -516,7 +514,6 @@ class AgentLoop:max_iterations=defaults.max_tool_iterations,max_concurrent_subagents=defaults.max_concurrent_subagents,context_window_tokens=context_window_tokens,- context_block_limit=defaults.context_block_limit,max_tool_result_chars=defaults.max_tool_result_chars,provider_retry_mode=defaults.provider_retry_mode,tool_hint_max_length=defaults.tool_hint_max_length,@@ -1201,7 +1198,6 @@ class AgentLoop:concurrent_tools=True,workspace=effective_scope.project_path,session_key=session.key if session else None,- context_block_limit=self.context_block_limit,provider_retry_mode=self.provider_retry_mode,retry_wait_callback=on_retry_wait,checkpoint_callback=_checkpoint,diff --git a/nanobot/agent/runner.py b/nanobot/agent/runner.pyindex 69aed146e..689b8d90b 100644--- a/nanobot/agent/runner.py+++ b/nanobot/agent/runner.py@@ -104,7 +104,6 @@ class AgentRunSpec:concurrent_tools: bool = Falseworkspace: Path | None = Nonesession_key: str | None = None- context_block_limit: int | None = Noneprovider_retry_mode: str = "standard"retry_wait_callback: RetryWaitCallback | None = Nonecheckpoint_callback: CheckpointCallback | None = None@@ -426,7 +425,6 @@ class AgentRunner:session_key=spec.session_key,max_tool_result_chars=spec.max_tool_result_chars,context_window_tokens=spec.runtime.context_window_tokens,- context_block_limit=spec.context_block_limit,max_tokens=spec.runtime.generation.max_tokens,)request_state = ModelRequestState(diff --git a/nanobot/config/schema.py b/nanobot/config/schema.pyindex 8438352f4..c3e75dcaf 100644--- a/nanobot/config/schema.py+++ b/nanobot/config/schema.py@@ -124,7 +124,6 @@ class AgentDefaults(Base):)max_tokens: int = 8192context_window_tokens: int = 200_000- context_block_limit: int | None = Nonetemperature: float = 0.1fallback_models: list[FallbackCandidate] = Field(default_factory=list)max_tool_iterations: int = 200diff --git a/tests/agent/test_loop_consolidation_tokens.py b/tests/agent/test_loop_consolidation_tokens.pyindex f443e106b..e950e9b40 100644--- a/tests/agent/test_loop_consolidation_tokens.py+++ b/tests/agent/test_loop_consolidation_tokens.py@@ -33,9 +33,6 @@ def _make_loop(workspace=tmp_path,model="test-model",context_window_tokens=context_window_tokens,- # These tests isolate Memory consolidation; Runner request fitting is- # covered separately with realistic context windows.- context_block_limit=10_000,)loop.tools.get_definitions = MagicMock(return_value=[])loop.consolidator._SAFETY_BUFFER = 0@@ -44,9 +41,9 @@ def _make_loop(@pytest.mark.asyncioasync def test_runner_pressure_commits_summary_and_current_delta(tmp_path) -> None:- loop = _make_loop(tmp_path, estimated_tokens=100, context_window_tokens=2_000)- loop.context_block_limit = 500- loop.provider.generation = GenerationSettings(max_tokens=100)+ loop = _make_loop(+ tmp_path, estimated_tokens=100, context_window_tokens=1_624, max_tokens=100,+ )loop.provider.can_resume_conversation_state.return_value = Falseloop.schedule_background = lambda coro: coro.close() # type: ignore[method-assign]diff --git a/tests/agent/test_runner_core.py b/tests/agent/test_runner_core.pyindex 1c4be1fa1..b68448115 100644--- a/tests/agent/test_runner_core.py+++ b/tests/agent/test_runner_core.py@@ -423,8 +423,7 @@ async def test_runner_preserves_tool_result_before_rejecting_unfit_followup():],tools=tools,model="gpt-5.6",- context_window_tokens=3_000,- context_block_limit=200,+ context_window_tokens=2_224,max_tokens=1_000,max_iterations=3,max_tool_result_chars=10_000,diff --git a/tests/agent/test_runner_governance.py b/tests/agent/test_runner_governance.pyindex ad292dc6f..5f9c32f22 100644--- a/tests/agent/test_runner_governance.py+++ b/tests/agent/test_runner_governance.py@@ -56,7 +56,6 @@ def _governance_config(session_key=spec.session_key,max_tool_result_chars=spec.max_tool_result_chars,context_window_tokens=spec.runtime.context_window_tokens,- context_block_limit=spec.context_block_limit,max_tokens=spec.runtime.generation.max_tokens,)@@ -136,8 +135,7 @@ async def test_runner_locally_fits_oversized_initial_transcript(monkeypatch):],tools=tools,model="local-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,@@ -210,8 +208,7 @@ async def test_runner_summarizes_history_and_preserves_current_input(monkeypatchprovider_state=prior_state,tools=tools,model="test-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,@@ -261,8 +258,7 @@ async def test_runner_rejects_oversized_delta_without_summarizable_history(monkeconsolidate_history=consolidate,tools=tools,model="test-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,@@ -313,8 +309,7 @@ async def test_runner_governs_history_before_summarizing_it(monkeypatch):consolidate_history=consolidate,tools=tools,model="test-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,@@ -395,8 +390,7 @@ async def test_native_compaction_uses_provider_request_boundary(consolidate_provider_compaction=consolidate_native,tools=tools,model="test-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,max_tokens=100,max_iterations=2,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,@@ -465,8 +459,7 @@ async def test_runner_keeps_current_tool_exchange_outside_summary(monkeypatch):consolidate_history=consolidate,tools=tools,model="test-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,max_tokens=100,max_iterations=2,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,@@ -537,8 +530,7 @@ async def test_repeated_pressure_advances_summary_boundary(monkeypatch):consolidate_history=consolidate,tools=tools,model="test-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,max_tokens=100,max_iterations=3,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,@@ -596,8 +588,7 @@ async def test_runner_refuses_checkpoint_that_cannot_fit_with_delta(monkeypatch)consolidate_history=consolidate,tools=tools,model="test-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,@@ -638,8 +629,8 @@ async def test_runner_governs_messages_added_by_before_iteration_hook(monkeypatcinitial_messages=[{"role": "user", "content": "hello"}],tools=tools,model="local-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,+ max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,hook=MutatingHook(),@@ -703,8 +694,8 @@ async def test_runner_drops_resumable_provider_state_when_request_is_fitted(monk],tools=tools,model="local-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,+ max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,provider_state=saved_state,@@ -760,8 +751,8 @@ async def test_runner_fits_each_malformed_retry_with_its_actual_tools(monkeypatcinitial_messages=[{"role": "user", "content": "use a tool"}],tools=tools,model="local-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,+ max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,))@@ -825,8 +816,8 @@ async def test_runner_fits_empty_response_finalization_before_dispatch(monkeypatinitial_messages=[{"role": "user", "content": "do task"}],tools=tools,model="local-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,+ max_tokens=100,max_iterations=3,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,))@@ -883,8 +874,8 @@ async def test_runner_fits_max_iteration_finalization_before_dispatch(monkeypatcinitial_messages=[{"role": "user", "content": "inspect"}],tools=tools,model="local-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,+ max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,))@@ -916,8 +907,8 @@ async def test_matching_reported_provider_usage_avoids_local_estimate(initial_messages=[{"role": "user", "content": "hello"}],tools=tools,model="local-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,+ max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,)@@ -957,8 +948,8 @@ async def test_changed_messages_use_local_estimate_after_reported_usage(monkeypainitial_messages=[{"role": "user", "content": "new tool output"}],tools=tools,model="local-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,+ max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,)@@ -998,8 +989,8 @@ def test_resumed_provider_context_avoids_full_transcript_estimate(monkeypatch):initial_messages=[{"role": "user", "content": "pending delta"}],tools=tools,model="local-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,+ max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,)@@ -1066,8 +1057,8 @@ async def test_runner_counts_resumed_provider_state_before_dispatch(monkeypatch)initial_messages=[current_message],tools=tools,model="local-model",- context_window_tokens=2_000,- context_block_limit=500,+ context_window_tokens=1_624,+ max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,provider_state=saved_state,@@ -1082,12 +1073,12 @@ async def test_runner_counts_resumed_provider_state_before_dispatch(monkeypatch)@pytest.mark.asyncio@pytest.mark.parametrize(- ("context_block_limit", "expected_budget"),- [(500, 500), (None, 0)],+ ("context_window_tokens", "expected_budget"),+ [(1_624, 500), (1_000, 0)],)async def test_runner_refuses_locally_fitted_request_that_still_cannot_fit(monkeypatch,- context_block_limit,+ context_window_tokens,expected_budget,):from nanobot.agent.runner import AgentRunner@@ -1110,8 +1101,8 @@ async def test_runner_refuses_locally_fitted_request_that_still_cannot_fit(],tools=tools,model="local-model",- context_window_tokens=1_000,- context_block_limit=context_block_limit,+ context_window_tokens=context_window_tokens,+ max_tokens=100,max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,))@@ -1142,8 +1133,8 @@ def test_snip_history_drops_orphaned_tool_results_from_trimmed_slice(monkeypatchmodel="test-model",max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,- context_window_tokens=2000,- context_block_limit=100,+ context_window_tokens=1_224,+ max_tokens=100,)monkeypatch.setattr(@@ -1193,8 +1184,8 @@ def test_snip_history_reserves_budget_for_tool_definitions(monkeypatch):model="test-model",max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,- context_window_tokens=2000,- context_block_limit=500,+ context_window_tokens=1_624,+ max_tokens=100,)def _estimate(_provider, _model, estimate_messages, estimate_tools):@@ -1581,8 +1572,8 @@ def test_snip_history_preserves_user_message_after_truncation(monkeypatch):model="test-model",max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,- context_window_tokens=2000,- context_block_limit=100,+ context_window_tokens=1_224,+ max_tokens=100,)# Make estimate_prompt_tokens_chain report above budget so _snip_history activates.@@ -1639,8 +1630,8 @@ def test_snip_history_no_user_at_all_falls_back_gracefully(monkeypatch):model="test-model",max_iterations=1,max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,- context_window_tokens=2000,- context_block_limit=100,+ context_window_tokens=1_224,+ max_tokens=100,)monkeypatch.setattr(