Files
nanobot/tests/agent/conftest.py
T
chengyongru 8f6a5594fb refactor(session): centralize state ownership in SQLite (#5943)
* fix(session): move persistence off event loop

* refactor(session): isolate async persistence boundary

* refactor(session): keep a single session manager

* refactor(session): use stateless async adapters

* fix(session): serialize local persistence without file-lock contention

* refactor(session): own async persistence and snapshot commits

* refactor(session): narrow persistence changes to async adapters

* refactor(session): remove redundant async forwarding helpers

* fix(command): offload compact session persistence

* fix(session): adapt idle compaction to current main

* fix(webui): offload session metadata persistence

* refactor(session): centralize state ownership in SQLite

* fix(session): skip storage for non-recoverable followups

* test(webui): honor the full gateway event deadline

* perf(session): persist incremental changes and reuse owner snapshots

* test(agent): allow durable worker shutdown in burst test

* fix(gateway): drain session storage on Windows console shutdown

* fix(session): enforce bounded ownership and lifecycle-safe persistence

* fix: reject unresolved home paths and stabilize platform tests

* fix(session): preserve request context compatibility and owner-backed tests

* docs: focus configuration guide on user-facing behavior

* fix(session): adapt goal admission to async ownership after rebase

* fix(session): integrate current gateway and subagent contracts

* fix(session): align rebased workspace admission and async tests

* fix(session): support compatible SQLite schema evolution

* test(agent): use session owner in workspace context regressions

* test: stabilize Windows lifecycle and browser checks

* refactor(session): remove premature JSONL file export

* ci: bound Windows full-suite worker concurrency

* chore: remove session state refactor notes

* fix(session): queue writes and retain worker connections

* perf(webui): read detached metadata for session and task lists

* perf(agent): bound prompt preparation and local token counting

* docs: remove internal session storage guide

* perf(session): skip archived transcripts during idle scans

* fix(session): coordinate turns and isolate legacy session data

Bind WebUI input generations to the execution session and serialize idle
and SDK compaction with turns. Keep turn drafts through preparation so the
latest channel route persists.

Move imported workspace JSONL and checkpoints to durable private backups
before gateway admission, resuming cleanup after interruptions. Remove
committed child sessions when task creation is cancelled.

Validation: focused Python and frontend regressions, Ruff, BasedPyright,
import boundaries, WebUI build, and isolated gateway/browser flows.

* fix(agent): keep cancellation hooks independent of prompt preparation

* fix(session): preserve admission, delivery, and migration contracts

* fix(agent): preserve cancellation settlement after rebase

* fix(session): preserve request attribution through async ownership
2026-10-16 17:28:47 +08:00

95 lines
2.7 KiB
Python

"""Shared fixtures and helpers for agent tests."""
from __future__ import annotations
import os
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from nanobot.agent.loop import AgentLoop
from nanobot.bus.queue import MessageBus
from nanobot.providers.base import LLMProvider
@pytest.fixture
def cmd_python() -> str:
"""Return the Python command name available to ExecTool tests."""
return "python" if os.name == "nt" else "python3"
def make_provider(
default_model: str = "test-model",
*,
max_tokens: int = 4096,
spec: bool = True,
) -> MagicMock:
"""Create a spec-limited LLM provider mock."""
mock_type = MagicMock(spec=LLMProvider) if spec else MagicMock()
provider = mock_type
provider.get_default_model.return_value = default_model
provider.generation = SimpleNamespace(
max_tokens=max_tokens,
temperature=0.1,
reasoning_effort=None,
)
provider.estimate_prompt_tokens = MagicMock(return_value=(10_000, "test"))
return provider
def make_loop(
tmp_path: Path,
*,
model: str = "test-model",
context_window_tokens: int = 128_000,
session_ttl_minutes: int = 0,
unified_session: bool = False,
tools_config=None,
model_presets: dict | None = None,
hooks: list | None = None,
provider: MagicMock | None = None,
patch_deps: bool = False,
) -> AgentLoop:
"""Create a real AgentLoop for testing.
Args:
patch_deps: If True, patch ContextBuilder/SessionManager/SubagentManager
during construction (needed when workspace has no real files).
"""
bus = MessageBus()
if provider is None:
provider = make_provider(default_model=model)
kwargs = dict(
bus=bus,
provider=provider,
workspace=tmp_path,
model=model,
context_window_tokens=context_window_tokens,
session_ttl_minutes=session_ttl_minutes,
unified_session=unified_session,
)
if tools_config is not None:
kwargs["tools_config"] = tools_config
if model_presets is not None:
kwargs["model_presets"] = model_presets
if hooks is not None:
kwargs["hooks"] = hooks
if patch_deps:
with patch("nanobot.agent.loop.ContextBuilder"), \
patch("nanobot.agent.loop.SubagentManager") as mock_sub_mgr:
mock_sub_mgr.return_value.cancel_by_session = AsyncMock(return_value=0)
return AgentLoop(**kwargs)
return AgentLoop(**kwargs)
@pytest.fixture
def loop_factory(tmp_path):
"""Fixture providing a factory for creating AgentLoop instances."""
def _factory(**kwargs):
return make_loop(tmp_path, **kwargs)
return _factory