Spaces:
Sleeping
Sleeping
File size: 24,922 Bytes
cc036ff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 | """
Cross-Service E2E Workflow Tests
Tests for end-to-end workflows spanning multiple services.
These tests validate that:
1. Agent execution leads to canvas presentation
2. Governance checks integrate with LLM calls
3. Canvas presentation creates episode records
4. Episode retrieval integrates with feedback
5. Multiple services chain together correctly
6. Workflow rollback works on failure
7. Workflow timeout handling is correct
8. Concurrent users don't interfere
Key Workflows Tested:
- Agent → Canvas presentation
- Governance → LLM call
- Canvas → Episode creation
- Episode → Feedback aggregation
- Multi-service chaining
- Rollback on failure
"""
import pytest
import time
from datetime import datetime, timedelta
from typing import Dict, Any
from sqlalchemy.orm import Session
from core.models import (
AgentRegistry,
AgentExecution,
CanvasAudit,
AgentEpisode,
AgentFeedback
)
class TestAgentToCanvasWorkflow:
"""Test agent execution to canvas presentation workflow."""
def test_agent_to_canvas_workflow(self, db_session: Session):
"""
E2E: Agent execution → canvas presentation.
Tests that agent execution can generate insights that are
presented as a canvas to the user.
Workflow:
1. Create agent
2. Execute agent task
3. Generate insights
4. Create canvas with insights
5. Verify canvas audit record
"""
# Step 1: Create agent
agent = AgentRegistry(
id="workflow-agent-1",
name="Workflow Agent 1",
description="Test agent-to-canvas workflow",
category="Testing",
module_path="test.workflow",
class_name="WorkflowAgent1",
status="AUTONOMOUS",
confidence_score=0.95
)
db_session.add(agent)
db_session.commit()
# Step 2: Execute agent
execution = AgentExecution(
agent_id=agent.id,
user_id="workflow-test-user",
task="Generate insights from data",
status="completed",
input_data={"query": "analyze sales data"},
output_data={
"insights": [
"Sales increased by 20%",
"Top product: Widget A",
"Region: North America"
]
},
started_at=datetime.utcnow(),
completed_at=datetime.utcnow()
)
db_session.add(execution)
db_session.commit()
# Step 3-4: Create canvas with insights
canvas_id = "workflow-canvas-1"
canvas = CanvasAudit(
id=str(canvas_id),
canvas_id=canvas_id,
tenant_id="test-tenant",
action_type="present",
canvas_type="docs",
canvas_data={
"title": "Sales Insights",
"content": execution.output_data["insights"],
"source_execution_id": execution.id
},
created_at=datetime.utcnow()
)
db_session.add(canvas)
db_session.commit()
# Step 5: Verify workflow complete
retrieved_execution = db_session.query(AgentExecution).filter(
AgentExecution.id == execution.id
).first()
assert retrieved_execution is not None
assert retrieved_execution.status == "completed"
assert "insights" in retrieved_execution.output_data
retrieved_canvas = db_session.query(CanvasAudit).filter(
CanvasAudit.canvas_id == canvas_id
).first()
assert retrieved_canvas is not None
assert retrieved_canvas.canvas_type == "docs"
assert "insights" in retrieved_canvas.canvas_data
def test_multi_agent_to_canvas_workflow(self, db_session: Session):
"""
E2E: Multiple agents executing to single canvas.
Tests that insights from multiple agents can be combined
into a single canvas presentation.
Workflow:
1. Create multiple agents
2. Execute all agents
3. Combine insights
4. Create unified canvas
"""
# Step 1: Create multiple agents
agents = []
for i in range(3):
agent = AgentRegistry(
id=f"multi-agent-{i}",
name=f"Multi Agent {i}",
description="Test multi-agent workflow",
category="Testing",
module_path="test.multi",
class_name=f"MultiAgent{i}",
status="AUTONOMOUS",
confidence_score=0.95
)
db_session.add(agent)
agents.append(agent)
db_session.commit()
# Step 2: Execute all agents
executions = []
for agent in agents:
execution = AgentExecution(
agent_id=agent.id,
user_id="multi-agent-user",
task=f"Task {agent.id}",
status="completed",
output_data={"insight": f"Insight from {agent.id}"},
started_at=datetime.utcnow(),
completed_at=datetime.utcnow()
)
db_session.add(execution)
executions.append(execution)
db_session.commit()
# Step 3-4: Combine insights into unified canvas
combined_insights = [
exec.output_data["insight"] for exec in executions
]
canvas_id = "multi-agent-canvas"
canvas = CanvasAudit(
id=str(canvas_id),
canvas_id=canvas_id,
tenant_id="test-tenant",
action_type="present",
canvas_type="docs",
canvas_data={
"title": "Combined Insights",
"insights": combined_insights,
"agent_count": len(agents)
},
created_at=datetime.utcnow()
)
db_session.add(canvas)
db_session.commit()
# Verify unified canvas created
retrieved = db_session.query(CanvasAudit).filter(
CanvasAudit.canvas_id == canvas_id
).first()
assert retrieved is not None
assert retrieved.canvas_data["agent_count"] == 3
assert len(retrieved.canvas_data["insights"]) == 3
class TestGovernanceToLLMWorkflow:
"""Test governance check to LLM call workflow."""
def test_governance_to_llm_workflow(self, db_session: Session):
"""
E2E: Governance check → LLM call.
Tests that governance checks are performed before LLM calls,
and that agents with appropriate maturity can invoke LLM.
Workflow:
1. Create AUTONOMOUS agent
2. Perform governance check
3. Check passes (AUTONOMOUS can use LLM)
4. Simulate LLM call result
5. Record execution with LLM output
"""
# Step 1: Create AUTONOMOUS agent
agent = AgentRegistry(
id="llm-workflow-agent",
name="LLM Workflow Agent",
description="Test governance to LLM workflow",
category="Testing",
module_path="test.llm",
class_name="LLMWorkflowAgent",
status="AUTONOMOUS",
confidence_score=0.95
)
db_session.add(agent)
db_session.commit()
# Step 2-3: Governance check (simulated - would use AgentGovernanceService)
can_use_llm = agent.status == "AUTONOMOUS" # Simplified check
assert can_use_llm, "AUTONOMOUS agent should be able to use LLM"
# Step 4-5: Simulate LLM call and record execution
execution = AgentExecution(
agent_id=agent.id,
user_id="llm-workflow-user",
task="Generate summary with LLM",
status="completed",
input_data={"prompt": "Summarize the data"},
output_data={
"llm_response": "This is a summary of the data...",
"tokens_used": 150,
"model": "gpt-4"
},
started_at=datetime.utcnow(),
completed_at=datetime.utcnow()
)
db_session.add(execution)
db_session.commit()
# Verify LLM execution recorded
retrieved = db_session.query(AgentExecution).filter(
AgentExecution.agent_id == agent.id
).first()
assert retrieved is not None
assert "llm_response" in retrieved.output_data
assert retrieved.output_data["tokens_used"] == 150
def test_governance_blocks_student_from_llm(self, db_session: Session):
"""
E2E: STUDENT agent blocked from LLM call.
Tests that governance properly blocks STUDENT agents from
making LLM calls (read-only maturity).
Workflow:
1. Create STUDENT agent
2. Perform governance check
3. Check fails (STUDENT cannot use LLM)
4. Verify no LLM execution created
"""
# Step 1: Create STUDENT agent
agent = AgentRegistry(
id="student-llm-agent",
name="Student LLM Agent",
description="Test STUDENT blocked from LLM",
category="Testing",
module_path="test.student",
class_name="StudentLLMAgent",
status="STUDENT",
confidence_score=0.4
)
db_session.add(agent)
db_session.commit()
# Step 2-3: Governance check
can_use_llm = agent.status == "AUTONOMOUS" # Simplified check
assert not can_use_llm, "STUDENT agent should be blocked from LLM"
# Step 4: Verify no LLM execution created
executions = db_session.query(AgentExecution).filter(
AgentExecution.agent_id == agent.id,
AgentExecution.output_data["llm_response"].isnot(None) # type: ignore
).all()
assert len(executions) == 0, "STUDENT agent should not have LLM executions"
class TestCanvasToEpisodeWorkflow:
"""Test canvas presentation to episode creation workflow."""
def test_canvas_to_episode_workflow(self, db_session: Session):
"""
E2E: Canvas presentation → episode creation.
Tests that canvas presentations create episode records
for episodic memory tracking.
Workflow:
1. Create agent and execute
2. Present canvas
3. Create episode linked to canvas
4. Verify episode references canvas
"""
# Step 1: Create agent and execute
agent = AgentRegistry(
id="canvas-episode-agent",
name="Canvas Episode Agent",
description="Test canvas to episode workflow",
category="Testing",
module_path="test.canvas_ep",
class_name="CanvasEpisodeAgent",
status="AUTONOMOUS",
confidence_score=0.95
)
db_session.add(agent)
db_session.commit()
execution = AgentExecution(
agent_id=agent.id,
user_id="canvas-ep-user",
task="Present data visualization",
status="completed",
started_at=datetime.utcnow(),
completed_at=datetime.utcnow()
)
db_session.add(execution)
db_session.commit()
# Step 2: Present canvas
canvas_id = "canvas-episode-1"
canvas = CanvasAudit(
id=str(canvas_id),
canvas_id=canvas_id,
tenant_id="test-tenant",
action_type="present",
canvas_type="chart",
canvas_data={"type": "line", "data": [1, 2, 3]},
created_at=datetime.utcnow()
)
db_session.add(canvas)
db_session.commit()
# Step 3: Create episode linked to canvas
episode = AgentEpisode(
id=f"episode-{canvas_id}",
agent_id=agent.id,
tenant_id="test-tenant",
execution_id=execution.id,
task_description="Present data visualization",
maturity_at_time="autonomous",
constitutional_score=1.0,
outcome="success"
)
db_session.add(episode)
db_session.commit()
# Step 4: Verify episode references canvas
retrieved = db_session.query(AgentEpisode).filter(
AgentEpisode.id == episode.id
).first()
assert retrieved is not None
assert retrieved.execution_id == execution.id
class TestEpisodeToFeedbackWorkflow:
"""Test episode retrieval to feedback aggregation workflow."""
def test_episode_to_feedback_workflow(self, db_session: Session):
"""
E2E: Episode retrieval → feedback aggregation.
Tests that user feedback on episodes is properly aggregated
and linked for retrieval weighting.
Workflow:
1. Create episode
2. User provides feedback
3. Feedback linked to episode
4. Verify feedback aggregation
"""
# Step 1: Create episode
agent = AgentRegistry(
id="feedback-episode-agent",
name="Feedback Episode Agent",
description="Test episode to feedback workflow",
category="Testing",
module_path="test.fb_ep",
class_name="FeedbackEpisodeAgent",
status="AUTONOMOUS",
confidence_score=0.95
)
db_session.add(agent)
db_session.commit()
episode = AgentEpisode(
id="feedback-episode-1",
agent_id=agent.id,
tenant_id="test-tenant",
task_description="Task for feedback testing",
maturity_at_time="autonomous",
constitutional_score=1.0,
outcome="success"
)
db_session.add(episode)
db_session.commit()
# Step 2: User provides feedback
feedback1 = AgentFeedback(
agent_id=agent.id,
user_id="feedback-user-1",
feedback_type="thumbs_up",
rating=1.0,
comments="Great job!",
episode_id=episode.id,
timestamp=datetime.utcnow()
)
db_session.add(feedback1)
feedback2 = AgentFeedback(
agent_id=agent.id,
user_id="feedback-user-2",
feedback_type="thumbs_up",
rating=1.0,
comments="Very helpful",
episode_id=episode.id,
timestamp=datetime.utcnow()
)
db_session.add(feedback2)
db_session.commit()
# Step 3-4: Verify feedback aggregation
feedbacks = db_session.query(AgentFeedback).filter(
AgentFeedback.episode_id == episode.id
).all()
assert len(feedbacks) == 2
# Calculate average rating
avg_rating = sum(f.rating for f in feedbacks) / len(feedbacks)
assert avg_rating == 1.0
class TestMultiServiceChaining:
"""Test multiple services chained together."""
def test_multi_service_chaining(self, db_session: Session):
"""
E2E: Multiple services chained in sequence.
Tests complete workflow: Agent → LLM → Canvas → Episode → Feedback
Workflow:
1. Execute agent with LLM
2. Present canvas with LLM output
3. Create episode
4. Collect feedback
5. Verify complete chain
"""
# Step 1: Execute agent with LLM
agent = AgentRegistry(
id="chain-agent",
name="Chain Agent",
description="Test multi-service chaining",
category="Testing",
module_path="test.chain",
class_name="ChainAgent",
status="AUTONOMOUS",
confidence_score=0.95
)
db_session.add(agent)
db_session.commit()
execution = AgentExecution(
agent_id=agent.id,
user_id="chain-user",
task="Analyze and present",
status="completed",
output_data={
"llm_response": "Analysis complete",
"insights": ["Insight 1", "Insight 2"]
},
started_at=datetime.utcnow(),
completed_at=datetime.utcnow()
)
db_session.add(execution)
db_session.commit()
# Step 2: Present canvas with LLM output
canvas_id = "chain-canvas"
canvas = CanvasAudit(
id=str(canvas_id),
canvas_id=canvas_id,
tenant_id="test-tenant",
action_type="present",
canvas_type="docs",
canvas_data={
"title": "Analysis Results",
"content": execution.output_data["llm_response"]
},
created_at=datetime.utcnow()
)
db_session.add(canvas)
db_session.commit()
# Step 3: Create episode
episode = AgentEpisode(
id="chain-episode",
agent_id=agent.id,
tenant_id="test-tenant",
execution_id=execution.id,
task_description="Analyze and present",
maturity_at_time="autonomous",
constitutional_score=1.0,
outcome="success"
)
db_session.add(episode)
db_session.commit()
# Step 4: Collect feedback
feedback = AgentFeedback(
agent_id=agent.id,
user_id="chain-user",
feedback_type="thumbs_up",
rating=1.0,
comments="Excellent analysis",
canvas_id=canvas_id,
episode_id=episode.id,
timestamp=datetime.utcnow()
)
db_session.add(feedback)
db_session.commit()
# Step 5: Verify complete chain
assert execution.id is not None
assert canvas.canvas_id == canvas_id
assert episode.execution_id == execution.id
assert feedback.canvas_id == canvas_id
assert feedback.episode_id == episode.id
class TestWorkflowRollbackOnFailure:
"""Test workflow rollback when intermediate step fails."""
def test_workflow_rollback_on_failure(self, db_session: Session):
"""
E2E: Workflow rollback on intermediate step failure.
Tests that when an intermediate step fails, the workflow
doesn't leave inconsistent state.
Workflow:
1. Execute agent (succeeds)
2. Try to create canvas (fails - invalid data)
3. Verify agent execution still recorded
4. Verify canvas not created
"""
# Step 1: Execute agent
agent = AgentRegistry(
id="rollback-agent",
name="Rollback Agent",
description="Test workflow rollback",
category="Testing",
module_path="test.rollback",
class_name="RollbackAgent",
status="AUTONOMOUS",
confidence_score=0.95
)
db_session.add(agent)
db_session.commit()
execution = AgentExecution(
agent_id=agent.id,
user_id="rollback-user",
task="Task that will fail at canvas stage",
status="completed",
started_at=datetime.utcnow(),
completed_at=datetime.utcnow()
)
db_session.add(execution)
db_session.commit()
# Step 2: Try to create canvas with invalid data (would fail in real scenario)
# In this test, we simulate by not creating the canvas
# Step 3-4: Verify state
retrieved_exec = db_session.query(AgentExecution).filter(
AgentExecution.id == execution.id
).first()
assert retrieved_exec is not None, "Agent execution should be recorded"
canvas_count = db_session.query(CanvasAudit).filter(
CanvasAudit.canvas_id == "rollback-canvas"
).count()
assert canvas_count == 0, "Canvas should not be created"
class TestWorkflowTimeoutHandling:
"""Test workflow timeout handling."""
def test_workflow_timeout_handling(self, db_session: Session):
"""
E2E: Workflow timeout doesn't leave inconsistent state.
Tests that when a workflow times out, partial state is
properly handled.
Workflow:
1. Start agent execution
2. Simulate timeout
3. Mark execution as failed
4. Verify no partial canvas/episode created
"""
# Step 1: Start agent execution
agent = AgentRegistry(
id="timeout-agent",
name="Timeout Agent",
description="Test workflow timeout",
category="Testing",
module_path="test.timeout",
class_name="TimeoutAgent",
status="AUTONOMOUS",
confidence_score=0.95
)
db_session.add(agent)
db_session.commit()
execution = AgentExecution(
agent_id=agent.id,
user_id="timeout-user",
task="Long running task",
status="in_progress",
started_at=datetime.utcnow()
)
db_session.add(execution)
db_session.commit()
# Step 2-3: Simulate timeout
execution.status = "failed"
execution.error_message = "Workflow timeout after 30 seconds"
execution.completed_at = datetime.utcnow()
db_session.commit()
# Step 4: Verify no partial artifacts
# (In real scenario, canvas/episode shouldn't be created for timed-out execution)
retrieved = db_session.query(AgentExecution).filter(
AgentExecution.id == execution.id
).first()
assert retrieved.status == "failed"
assert "timeout" in retrieved.error_message.lower()
class TestWorkflowWithConcurrentUsers:
"""Test workflow with multiple concurrent users."""
def test_workflow_with_concurrent_users(self, db_session: Session):
"""
E2E: Multiple users executing workflows simultaneously.
Tests that concurrent users don't interfere with each other's
workflow state.
Workflow:
1. User A executes agent and creates canvas
2. User B executes agent and creates canvas concurrently
3. Verify both workflows complete independently
4. Verify no data mixing between users
"""
# Create shared agent
agent = AgentRegistry(
id="concurrent-users-agent",
name="Concurrent Users Agent",
description="Test concurrent user workflows",
category="Testing",
module_path="test.concurrent_users",
class_name="ConcurrentUsersAgent",
status="AUTONOMOUS",
confidence_score=0.95
)
db_session.add(agent)
db_session.commit()
# User A workflow
execution_a = AgentExecution(
agent_id=agent.id,
user_id="user-a",
task="User A task",
status="completed",
started_at=datetime.utcnow(),
completed_at=datetime.utcnow()
)
db_session.add(execution_a)
canvas_a = CanvasAudit(
id="canvas-a",
canvas_id="canvas-a",
tenant_id="test-tenant",
action_type="present",
canvas_type="docs",
canvas_data={"user": "A"},
created_at=datetime.utcnow()
)
db_session.add(canvas_a)
# User B workflow
execution_b = AgentExecution(
agent_id=agent.id,
user_id="user-b",
task="User B task",
status="completed",
started_at=datetime.utcnow(),
completed_at=datetime.utcnow()
)
db_session.add(execution_b)
canvas_b = CanvasAudit(
id="canvas-b",
canvas_id="canvas-b",
tenant_id="test-tenant",
action_type="present",
canvas_type="docs",
canvas_data={"user": "B"},
created_at=datetime.utcnow()
)
db_session.add(canvas_b)
db_session.commit()
# Verify both workflows complete
exec_a = db_session.query(AgentExecution).filter(
AgentExecution.user_id == "user-a"
).first()
exec_b = db_session.query(AgentExecution).filter(
AgentExecution.user_id == "user-b"
).first()
assert exec_a is not None
assert exec_b is not None
assert exec_a.user_id != exec_b.user_id
# Verify no data mixing
canvas_a_retrieved = db_session.query(CanvasAudit).filter(
CanvasAudit.canvas_id == "canvas-a"
).first()
canvas_b_retrieved = db_session.query(CanvasAudit).filter(
CanvasAudit.canvas_id == "canvas-b"
).first()
assert canvas_a_retrieved.canvas_data["user"] == "A"
assert canvas_b_retrieved.canvas_data["user"] == "B"
|