The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(delegate): close the dedicated SessionDB if child construction fails; test degradation
thatssoheileef43e31608c
diff --git a/tests/tools/test_delegate.py b/tests/tools/test_delegate.pyindex 7132557b4d0..b0d50c85b92 100644--- a/tests/tools/test_delegate.py+++ b/tests/tools/test_delegate.py@@ -337,6 +337,33 @@ class TestDelegateTask(unittest.TestCase):finally:parent_db.close()+ def test_child_without_parent_db_still_degrades_to_none(self):+ """Parent without a SessionDB -> child gets None (pre-fix behaviour).++ The dedicated-handle path must not change the degradation contract:+ a parent that never opened a session store (headless/oneshot runs,+ test doubles) still yields ``session_db=None`` children.+ """+ parent = _make_mock_parent(depth=0)+ parent._session_db = None+ with patch("run_agent.AIAgent") as MockAgent:+ mock_child = MagicMock()+ MockAgent.return_value = mock_child++ _build_child_agent(+ task_index=0,+ goal="test",+ context=None,+ toolsets=None,+ model="test-model",+ max_iterations=5,+ parent_agent=parent,+ task_count=1,+ )++ _, kwargs = MockAgent.call_args+ self.assertIsNone(kwargs["session_db"])+def test_nous_child_rederives_api_mode_from_model(self):"""Portal is dual-wire — same provider + different model prefix mustnot inherit the parent's Messages/chat_completions mode verbatim."""diff --git a/tools/delegate_tool.py b/tools/delegate_tool.pyindex 814e666edc4..92afe58e49c 100644--- a/tools/delegate_tool.py+++ b/tools/delegate_tool.py@@ -1799,47 +1799,58 @@ def _build_child_agent(from agent.delegation_context import delegated_child_contextwith delegated_child_context():- child = AIAgent(- base_url=effective_base_url,- api_key=effective_api_key,- model=effective_model,- provider=effective_provider,- api_mode=effective_api_mode,- acp_command=effective_acp_command,- acp_args=effective_acp_args,- max_iterations=max_iterations,-- reasoning_config=child_reasoning,- prefill_messages=getattr(parent_agent, "prefill_messages", None),- fallback_model=parent_fallback,- enabled_toolsets=child_toolsets,- disabled_toolsets=child_disabled_toolsets,- quiet_mode=True,- ephemeral_system_prompt=child_prompt,- log_prefix=f"[subagent-{task_index}]",- platform="subagent",- skip_context_files=True,- skip_memory=True,- clarify_callback=None,- thinking_callback=child_thinking_cb,- session_db=child_session_db,- parent_session_id=getattr(parent_agent, "session_id", None),- providers_allowed=child_providers_allowed,- providers_ignored=child_providers_ignored,- providers_order=child_providers_order,- provider_sort=child_provider_sort,- provider_require_parameters=child_provider_require_parameters,- provider_data_collection=child_provider_data_collection,- request_overrides=(- dict(override_request_overrides or {})- if override_provider- else dict(getattr(parent_agent, "request_overrides", {}) or {})- ),- openrouter_min_coding_score=child_openrouter_min_coding_score,- tool_progress_callback=child_progress_cb,- iteration_budget=None, # fresh budget per subagent- **child_optional_kwargs,- )+ try:+ child = AIAgent(+ base_url=effective_base_url,+ api_key=effective_api_key,+ model=effective_model,+ provider=effective_provider,+ api_mode=effective_api_mode,+ acp_command=effective_acp_command,+ acp_args=effective_acp_args,+ max_iterations=max_iterations,++ reasoning_config=child_reasoning,+ prefill_messages=getattr(parent_agent, "prefill_messages", None),+ fallback_model=parent_fallback,+ enabled_toolsets=child_toolsets,+ disabled_toolsets=child_disabled_toolsets,+ quiet_mode=True,+ ephemeral_system_prompt=child_prompt,+ log_prefix=f"[subagent-{task_index}]",+ platform="subagent",+ skip_context_files=True,+ skip_memory=True,+ clarify_callback=None,+ thinking_callback=child_thinking_cb,+ session_db=child_session_db,+ parent_session_id=getattr(parent_agent, "session_id", None),+ providers_allowed=child_providers_allowed,+ providers_ignored=child_providers_ignored,+ providers_order=child_providers_order,+ provider_sort=child_provider_sort,+ provider_require_parameters=child_provider_require_parameters,+ provider_data_collection=child_provider_data_collection,+ request_overrides=(+ dict(override_request_overrides or {})+ if override_provider+ else dict(getattr(parent_agent, "request_overrides", {}) or {})+ ),+ openrouter_min_coding_score=child_openrouter_min_coding_score,+ tool_progress_callback=child_progress_cb,+ iteration_budget=None, # fresh budget per subagent+ **child_optional_kwargs,+ )+ except BaseException:+ # Construction failed: the dedicated handle has no owner and no+ # child close() will ever run — release it here so the sqlite fds+ # don't outlive the failed spawn.+ if child_session_db is not None:+ try:+ child_session_db.close()+ except Exception:+ pass+ raisechild._print_fn = getattr(parent_agent, "_print_fn", None)# Ownership transfer for the dedicated handle: the child's close() must# release it (nothing else holds a reference), and no parent teardown can