Files
agent-framework/python/packages/devui/tests/devui/test_discovery.py
T
Eduard van Valkenburg 1e350ea22f Python: [BREAKING] PR2 — Wire context provider pipeline, remove old types, update all consumers (#3850)
* PR2: Wire context provider pipeline and update all internal consumers

- Replace AgentThread with AgentSession across all packages
- Replace ContextProvider with BaseContextProvider across all packages
- Replace context_provider param with context_providers (Sequence)
- Replace thread= with session= in run() signatures
- Replace get_new_thread() with create_session()
- Add get_session(service_session_id) to agent interface
- DurableAgentThread -> DurableAgentSession
- Remove _notify_thread_of_new_messages from WorkflowAgent
- Wire before_run/after_run context provider pipeline in RawAgent
- Auto-inject InMemoryHistoryProvider when no providers configured

* fix: update all tests for context provider pipeline, fix lazy-loaders, remove old test files

* refactor: update all sample files for context provider pipeline (AgentThread→AgentSession, ContextProvider→BaseContextProvider)

* fix: update remaining ag-ui references (client docstring, getting_started sample)

* fix: make get_session service_session_id keyword-only to avoid confusion with session_id

* refactor: rename _RunContext.thread_messages to session_messages

* refactor: remove _threads.py, _memory.py, and old provider files; migrate devui to use plain message lists

* rename: remove _new_ prefix from test files

* refactor: rewrite SlidingWindowChatMessageStore as SlidingWindowHistoryProvider(InMemoryHistoryProvider)

* fix: read full history from session state directly instead of reaching into provider internals

* fix: update stale .pyi stubs, sample imports, and README references for new provider types

* fix: remove stale message_store, _notify_thread_of_new_messages, and session_id.key references in samples

* refactor: merge context_providers and sessions sample folders into sessions, remove aggregate_context_provider

* refactor: UserInfoMemory stores state in session.state instead of instance attributes

* feat: add Pydantic BaseModel support to session state serialization

Pydantic models stored in session.state are now automatically serialized
via model_dump() and restored via model_validate() during to_dict()/from_dict()
round-trips. Models are auto-registered on first serialization; use
register_state_type() for cold-start deserialization.

Also export register_state_type as a public API.

* fix mem0

* Update sample README links and descriptions for session terminology

- Replace 'thread' with 'session' in sample descriptions across all READMEs
- Update file links for renamed samples (mem0_sessions, redis_sessions, etc.)
- Fix Threads section → Sessions section in main samples/README.md
- Update tools, middleware, workflows, durabletask, azure_functions READMEs
- Update architecture diagrams in concepts/tools/README.md
- Update migration guides (autogen, semantic-kernel)

* Fix broken Redis README link to renamed sample

* Fix Mem0 OSS client search: pass scoping params as direct kwargs

AsyncMemory (OSS) expects user_id/agent_id/run_id as direct kwargs,
while AsyncMemoryClient (Platform) expects them in a filters dict.
Adds tests for both client types.

Port of fix from #3844 to new Mem0ContextProvider.

* Fix rebase issues: restore missing _conversation_state.py and checkpoint decode logic

- Add back _conversation_state.py (encode/decode_chat_messages) lost in rebase
- Fix on_checkpoint_restore to decode cache/conversation with decode_chat_messages
- Fix on_checkpoint_restore to use decode_checkpoint_value for pending requests
- Add tests/workflow/__init__.py for relative import support
- Fix test_agent_executor checkpoint selection (checkpoints[1] not superstep)

* Add STORES_BY_DEFAULT ClassVar to skip redundant InMemoryHistoryProvider injection

Chat clients that store history server-side by default (OpenAI Responses API,
Azure AI Agent) now declare STORES_BY_DEFAULT = True. The agent checks this
during auto-injection and skips InMemoryHistoryProvider unless the user
explicitly sets store=False.

* Fix broken markdown links in azure_ai and redis READMEs

* Fix getting-started samples to use session API instead of removed thread/ContextProvider API

* updates to workflow as agent

* fix group chat import

* Rename Thread→Session throughout, fix service_session_id propagation, remove stale AGUIThread

- Fix: Propagate conversation_id from ChatResponse back to session.service_session_id
  in both streaming and non-streaming paths in _agents.py
- Rename AgentThreadException → AgentSessionException
- Remove stale AGUIThread from ag_ui lazy-loader
- Rename use_service_thread → use_service_session in ag-ui package
- Rename test functions from *_thread_* to *_session_*
- Rename sample files from *_thread* to *_session*
- Update docstrings and comments: thread → session
- Update _mcp.py kwargs filter: add 'session' alongside 'thread'
- Fix ContinuationToken docstring example: thread=thread → session=session
- Fix _clients.py docstring: 'Agent threads' → 'Agent sessions'

* Fix broken markdown links after thread→session file renames

* fix azure ai test
2026-02-12 21:00:32 +00:00

341 lines
11 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
"""Focused tests for entity discovery functionality."""
import asyncio
import tempfile
from pathlib import Path
from agent_framework_devui._discovery import EntityDiscovery
# Note: test_entities_dir fixture is provided by conftest.py
async def test_discover_agents(test_entities_dir):
"""Test that agent discovery works and returns valid agent entities."""
discovery = EntityDiscovery(test_entities_dir)
entities = await discovery.discover_entities()
agents = [e for e in entities if e.type == "agent"]
# Test that we can discover agents (not specific count)
assert len(agents) > 0, "Should discover at least one agent"
# Test agent structure/properties
for agent in agents:
assert agent.id, "Agent should have an ID"
assert agent.name, "Agent should have a name"
assert agent.type == "agent", "Should be identified as agent type"
assert hasattr(agent, "description"), "Agent should have description attribute"
async def test_discover_workflows(test_entities_dir):
"""Test that workflow discovery works and returns valid workflow entities."""
discovery = EntityDiscovery(test_entities_dir)
entities = await discovery.discover_entities()
workflows = [e for e in entities if e.type == "workflow"]
# Test that we can discover workflows (not specific count)
assert len(workflows) > 0, "Should discover at least one workflow"
# Test workflow structure/properties
for workflow in workflows:
assert workflow.id, "Workflow should have an ID"
assert workflow.name, "Workflow should have a name"
assert workflow.type == "workflow", "Should be identified as workflow type"
assert hasattr(workflow, "description"), "Workflow should have description attribute"
async def test_empty_directory():
"""Test discovery with empty directory."""
with tempfile.TemporaryDirectory() as temp_dir:
discovery = EntityDiscovery(temp_dir)
entities = await discovery.discover_entities()
assert len(entities) == 0
async def test_discovery_accepts_agents_with_only_run():
"""Test that discovery accepts agents with only run() method.
With lazy loading, entities with only __init__.py are discovered
but marked as "unknown" type until loaded.
"""
import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Create agent with only run() method
agent_dir = temp_path / "non_streaming_agent"
agent_dir.mkdir()
init_file = agent_dir / "__init__.py"
init_file.write_text("""
from agent_framework import AgentResponse, AgentSession, Message, Role, Content
class NonStreamingAgent:
id = "non_streaming"
name = "Non-Streaming Agent"
description = "Agent with run() method"
async def run(self, messages=None, *, session=None, **kwargs):
return AgentResponse(
messages=[Message(
role="assistant",
contents=[Content.from_text(text="response")]
)],
response_id="test"
)
def create_session(self, **kwargs):
return AgentSession()
agent = NonStreamingAgent()
""")
discovery = EntityDiscovery(str(temp_path))
entities = await discovery.discover_entities()
# With lazy loading, entity is discovered but type is "unknown"
# (no agent.py or workflow.py to detect type from)
assert len(entities) == 1
entity = entities[0]
assert entity.id == "non_streaming_agent"
assert entity.type == "unknown" # Type not yet determined
assert entity.tools == [] # Sparse metadata
# Trigger lazy loading to get full metadata
agent_obj = await discovery.load_entity(entity.id)
assert agent_obj is not None
# Now check enriched metadata after loading
enriched = discovery.get_entity_info(entity.id)
assert enriched.type == "agent" # Now correctly identified
assert enriched.name == "Non-Streaming Agent"
async def test_lazy_loading():
"""Test that entities are loaded on-demand, not at discovery time."""
import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Create test workflow
workflow_dir = temp_path / "test_workflow"
workflow_dir.mkdir()
(workflow_dir / "workflow.py").write_text("""
from agent_framework import WorkflowBuilder, FunctionExecutor
# Create a simple workflow with a start executor
def test_func(input: str) -> str:
return f"Processed: {input}"
executor = FunctionExecutor(id="test_executor", func=test_func)
workflow = WorkflowBuilder(start_executor=executor).build()
""")
discovery = EntityDiscovery(str(temp_path))
# Discovery should NOT import module
entities = await discovery.discover_entities()
assert len(entities) == 1
assert entities[0].id == "test_workflow"
assert entities[0].type == "workflow" # Type detected from filename
assert entities[0].tools == [] # Sparse metadata (not loaded yet)
# Entity should NOT be in loaded_objects yet
assert discovery.get_entity_object("test_workflow") is None
# Trigger lazy load
workflow_obj = await discovery.load_entity("test_workflow")
assert workflow_obj is not None
# Now in cache
assert discovery.get_entity_object("test_workflow") is workflow_obj
# Second load is instant (from cache)
workflow_obj2 = await discovery.load_entity("test_workflow")
assert workflow_obj2 is workflow_obj # Same object
async def test_type_detection():
"""Test that entity types are detected from filenames."""
import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Create workflow with workflow.py
workflow_dir = temp_path / "my_workflow"
workflow_dir.mkdir()
(workflow_dir / "workflow.py").write_text("""
from agent_framework import WorkflowBuilder, FunctionExecutor
def test_func(input: str) -> str:
return f"Processed: {input}"
executor = FunctionExecutor(id="test_executor", func=test_func)
workflow = WorkflowBuilder(start_executor=executor).build()
""")
# Create agent with agent.py
agent_dir = temp_path / "my_agent"
agent_dir.mkdir()
(agent_dir / "agent.py").write_text("""
from agent_framework import AgentResponse, AgentSession, Message, Role, TextContent
class TestAgent:
name = "Test Agent"
async def run(self, messages=None, *, session=None, **kwargs):
return AgentResponse(
messages=[Message(role="assistant", contents=[Content.from_text(text="test")])],
response_id="test"
)
def create_session(self, **kwargs):
return AgentSession()
agent = TestAgent()
""")
# Create ambiguous entity with __init__.py only
unknown_dir = temp_path / "my_thing"
unknown_dir.mkdir()
(unknown_dir / "__init__.py").write_text("# thing")
discovery = EntityDiscovery(str(temp_path))
entities = await discovery.discover_entities()
# Check types detected correctly
by_id = {e.id: e for e in entities}
assert by_id["my_workflow"].type == "workflow"
assert by_id["my_agent"].type == "agent"
assert by_id["my_thing"].type == "unknown"
async def test_hot_reload():
"""Test that invalidate_entity() enables hot reload."""
import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Create workflow
workflow_dir = temp_path / "test_workflow"
workflow_dir.mkdir()
workflow_file = workflow_dir / "workflow.py"
workflow_file.write_text("""
from agent_framework import WorkflowBuilder, FunctionExecutor
def test_func(input: str) -> str:
return "v1"
executor = FunctionExecutor(id="test_executor", func=test_func)
workflow = WorkflowBuilder(start_executor=executor).build()
""")
discovery = EntityDiscovery(str(temp_path))
await discovery.discover_entities()
# Load entity
workflow1 = await discovery.load_entity("test_workflow")
assert workflow1 is not None
# Modify file to create a different workflow
workflow_file.write_text("""
from agent_framework import WorkflowBuilder, FunctionExecutor
def test_func(input: str) -> str:
return "v2"
def test_func2(input: str) -> str:
return "v2_extra"
executor1 = FunctionExecutor(id="test_executor", func=test_func)
executor2 = FunctionExecutor(id="test_executor2", func=test_func2)
workflow = WorkflowBuilder(start_executor=executor1).add_edge(executor1, executor2).build()
""")
# Without invalidation, gets cached version
workflow2 = await discovery.load_entity("test_workflow")
assert workflow2 is workflow1 # Same object (cached)
# Old workflow has 1 executor
assert len(workflow2.get_executors_list()) == 1
# Invalidate cache
discovery.invalidate_entity("test_workflow")
# Now reloads from disk
workflow3 = await discovery.load_entity("test_workflow")
assert workflow3 is not workflow1 # Different object
# New workflow has 2 executors
assert len(workflow3.get_executors_list()) == 2
async def test_in_memory_entities_bypass_lazy_loading():
"""Test that in-memory entities work as before (no lazy loading needed)."""
from agent_framework import FunctionExecutor, WorkflowBuilder
# Create in-memory workflow
def test_func(input: str) -> str:
return f"Processed: {input}"
executor = FunctionExecutor(id="test_executor", func=test_func)
workflow = WorkflowBuilder(start_executor=executor).build()
discovery = EntityDiscovery()
# Register in-memory entity
entity_info = await discovery.create_entity_info_from_object(workflow, entity_type="workflow", source="in_memory")
discovery.register_entity(entity_info.id, entity_info, workflow)
# Should be immediately available (no lazy loading)
loaded = discovery.get_entity_object(entity_info.id)
assert loaded is workflow
# load_entity() should return immediately from cache
loaded2 = await discovery.load_entity(entity_info.id)
assert loaded2 is workflow # Same object (cache hit)
if __name__ == "__main__":
# Simple test runner
async def run_tests():
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Create test files
agent_file = temp_path / "test_agent.py"
agent_file.write_text("""
class WeatherAgent:
name = "Weather Agent"
description = "Gets weather information"
def run(self, input_str, *, stream: bool = False, session=None, **kwargs):
return f"Weather in {input_str}"
""")
workflow_file = temp_path / "test_workflow.py"
workflow_file.write_text("""
class DataWorkflow:
name = "Data Processing Workflow"
description = "Processes data"
def run(self, data):
return f"Processed {data}"
""")
discovery = EntityDiscovery(str(temp_path))
await discovery.discover_entities()
asyncio.run(run_tests())