Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/agent/capabilities/step_persistence/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,32 @@

from pydantic_ai_harness.step_persistence import StepPersistence

from agent.types.settings import StepPersistenceSettings
from conversations.agent_history import AgentHistoryService


class StepPersistenceFactory: # pylint: disable=too-few-public-methods
"""Factory for building step persistence capabilities."""

@staticmethod
def build_capability() -> Optional[StepPersistence]:
"""Build a step persistence capability from step persistence settings."""
def build_capability(
settings: StepPersistenceSettings,
) -> Optional[StepPersistence]:
"""Build a step persistence capability from step persistence settings.

Disabled settings produce no capability. Enabled settings return the
capability owned by the agent history service, which is ``None`` when
persistence is configured as a no-op.

Parameters:
settings: Step persistence settings, including whether persistence
is enabled for this agent.

Returns:
The history service capability, or ``None`` when persistence is
disabled or no store is configured.

"""
if not settings.enabled:
return None
return AgentHistoryService().step_persistence
2 changes: 1 addition & 1 deletion src/agent/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def create_agent(
SkillFactory.build_capabilities(settings.skills),
KnowledgeFactory.build_capabilities(settings.knowledge_sources),
CompactionFactory.build_capability(settings.compaction),
StepPersistenceFactory.build_capability(),
StepPersistenceFactory.build_capability(settings.step_persistence),
)
capabilities = [
capability for capability in raw_capabilities if capability is not None
Expand Down
2 changes: 2 additions & 0 deletions src/agent/types/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from agent.types.settings.mcp import MCPSettings
from agent.types.settings.shield import ShieldSettings
from agent.types.settings.skill import SkillSettings
from agent.types.settings.step_persistence import StepPersistenceSettings

__all__ = [
"AgentSettings",
Expand All @@ -14,4 +15,5 @@
"MCPSettings",
"ShieldSettings",
"SkillSettings",
"StepPersistenceSettings",
]
9 changes: 8 additions & 1 deletion src/agent/types/settings/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from agent.types.settings.mcp import MCPSettings
from agent.types.settings.shield import ShieldSettings
from agent.types.settings.skill import SkillSettings
from agent.types.settings.step_persistence import StepPersistenceSettings
from configuration import configuration
from models.api.requests import QueryRequest
from utils.prompts import get_system_prompt
Expand All @@ -26,6 +27,9 @@ class AgentSettings: # pylint: disable=too-many-instance-attributes
instructions: Optional[str] = None

compaction: CompactionSettings = field(default_factory=CompactionSettings)
step_persistence: StepPersistenceSettings = field(
default_factory=StepPersistenceSettings
)

skills: list[SkillSettings] = field(default_factory=list)
shields: list[ShieldSettings] = field(default_factory=list)
Expand All @@ -43,7 +47,9 @@ def from_query_request(
Shields, knowledge sources, and MCP servers always include every
configured entry. Omitted shield and knowledge-source ids leave them
enabled; a provided list disables the rest. ``no_tools`` disables
every MCP server. Skills and compaction come from configuration.
every MCP server. Skills, compaction, and step persistence come from
configuration. The query does not select step persistence, so it stays
enabled and the history service decides whether a store exists.
MCP headers are taken from the HTTP request and the auth token.

Parameters:
Expand All @@ -62,6 +68,7 @@ def from_query_request(
model=model,
instructions=get_system_prompt(query_request.system_prompt),
compaction=CompactionSettings.from_query_request(query_request),
step_persistence=StepPersistenceSettings.from_query_request(query_request),
skills=SkillSettings.from_query_request(query_request),
shields=ShieldSettings.from_query_request(query_request),
knowledge_sources=KnowledgeSourceSettings.from_query_request(query_request),
Expand Down
33 changes: 33 additions & 0 deletions src/agent/types/settings/step_persistence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Step persistence settings for an agent."""

from __future__ import annotations

from dataclasses import dataclass

from models.api.requests import QueryRequest


@dataclass
class StepPersistenceSettings:
"""Settings for step persistence."""

enabled: bool = True

@classmethod
def from_query_request(
cls, request: QueryRequest # pylint: disable=unused-argument
) -> StepPersistenceSettings:
"""Build step persistence settings for a query.

The query does not select step persistence. ``request`` is accepted so
every setting is built the same way. Persistence stays enabled; the
history service returns no capability when the store is a no-op.

Parameters:
request: Incoming query. Step persistence selection does not read it.

Returns:
Step persistence settings with ``enabled`` set to True.

"""
return cls()
34 changes: 28 additions & 6 deletions tests/unit/agent/capabilities/step_persistence/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,48 @@
from pytest_mock import MockerFixture

from agent.capabilities.step_persistence.factory import StepPersistenceFactory
from agent.types.settings import StepPersistenceSettings


def test_returns_the_agent_history_capability(mocker: MockerFixture) -> None:
"""The factory returns the capability owned by the agent history service."""
"""Enabled settings return the capability owned by the history service."""
capability = mocker.Mock()
service = mocker.Mock(step_persistence=capability)
mocker.patch(
history = mocker.patch(
"agent.capabilities.step_persistence.factory.AgentHistoryService",
return_value=service,
)

assert StepPersistenceFactory.build_capability() is capability
assert (
StepPersistenceFactory.build_capability(StepPersistenceSettings(enabled=True))
is capability
)
history.assert_called_once_with()


def test_returns_none_when_persistence_is_disabled(mocker: MockerFixture) -> None:
"""A history service without a store produces no capability."""
def test_returns_none_when_no_store_is_configured(mocker: MockerFixture) -> None:
"""Enabled settings still produce no capability when the store is a no-op."""
service = mocker.Mock(step_persistence=None)
mocker.patch(
"agent.capabilities.step_persistence.factory.AgentHistoryService",
return_value=service,
)

assert StepPersistenceFactory.build_capability() is None
assert (
StepPersistenceFactory.build_capability(StepPersistenceSettings(enabled=True))
is None
)


def test_disabled_settings_build_nothing(mocker: MockerFixture) -> None:
"""Disabled settings do not ask the history service for a capability."""
history = mocker.patch(
"agent.capabilities.step_persistence.factory.AgentHistoryService",
)

assert (
StepPersistenceFactory.build_capability(StepPersistenceSettings(enabled=False))
is None
)

history.assert_not_called()
2 changes: 1 addition & 1 deletion tests/unit/agent/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async def test_create_agent_assembles_resolved_capabilities(
factories["skills"].assert_called_once_with(settings.skills)
factories["knowledge"].assert_called_once_with(settings.knowledge_sources)
factories["compaction"].assert_called_once_with(settings.compaction)
factories["persistence"].assert_called_once_with()
factories["persistence"].assert_called_once_with(settings.step_persistence)
agent_cls.assert_called_once_with(
model=model,
instructions="Be helpful",
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/agent/types/settings/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from agent.types.settings.mcp import MCPSettings
from agent.types.settings.shield import ShieldSettings
from agent.types.settings.skill import SkillSettings
from agent.types.settings.step_persistence import StepPersistenceSettings
from constants import OKP_RAG_ID
from models.api.requests import QueryRequest
from models.config import (
Expand Down Expand Up @@ -122,6 +123,7 @@ def test_query_fields_select_the_agent(mocker: MockerFixture) -> None:
model="granite",
enabled=True,
)
assert settings.step_persistence == StepPersistenceSettings(enabled=True)
assert settings.skills == [SkillSettings(path="skills/one")]
assert settings.shields == [
ShieldSettings(id="topic", enabled=False),
Expand Down Expand Up @@ -151,6 +153,7 @@ def test_omitted_query_fields_use_configuration_defaults(
model="default-model",
enabled=True,
)
assert settings.step_persistence == StepPersistenceSettings(enabled=True)
assert settings.skills == []
assert [shield.enabled for shield in settings.shields] == [True, True]
assert [source.enabled for source in settings.knowledge_sources] == [True, True]
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/agent/types/settings/test_step_persistence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Unit tests for step persistence settings built from a query."""

from agent.types.settings.step_persistence import StepPersistenceSettings
from models.api.requests import QueryRequest


def test_query_does_not_select_step_persistence() -> None:
"""Every query keeps step persistence enabled.

The history service decides whether a store exists. The query is accepted
so every setting is built the same way.
"""
request = QueryRequest(query="What is Kubernetes?")

assert StepPersistenceSettings.from_query_request(
request
) == StepPersistenceSettings(enabled=True)
Loading