|
3 | 3 | import pytest |
4 | 4 |
|
5 | 5 | from strands import Agent, tool |
| 6 | +from strands.experimental.steering.context_providers.ledger_provider import LedgerProvider |
6 | 7 | from strands.experimental.steering.core.action import Guide, Interrupt, Proceed |
| 8 | +from strands.experimental.steering.core.handler import SteeringHandler |
7 | 9 | from strands.experimental.steering.handlers.llm.llm_handler import LLMSteeringHandler |
8 | 10 |
|
9 | 11 |
|
@@ -98,3 +100,45 @@ def test_agent_with_tool_steering_e2e(): |
98 | 100 | notification_metrics = tool_metrics["send_notification"] |
99 | 101 | assert notification_metrics.call_count >= 1, "send_notification should have been called" |
100 | 102 | assert notification_metrics.success_count >= 1, "send_notification should have succeeded" |
| 103 | + |
| 104 | + |
| 105 | +def test_ledger_captures_tool_calls(): |
| 106 | + """Test that ledger correctly captures tool call information.""" |
| 107 | + |
| 108 | + class LedgerCheckingHandler(SteeringHandler): |
| 109 | + def __init__(self): |
| 110 | + super().__init__(context_providers=[LedgerProvider()]) |
| 111 | + |
| 112 | + async def steer_before_tool(self, *, agent, tool_use, **kwargs): |
| 113 | + ledger = self.steering_context.data.get("ledger") |
| 114 | + assert ledger is not None, "Ledger should exist" |
| 115 | + assert "tool_calls" in ledger, "Ledger should have tool_calls" |
| 116 | + |
| 117 | + # Find the current tool call in the ledger |
| 118 | + tool_calls = ledger["tool_calls"] |
| 119 | + current_call = next((tc for tc in tool_calls if tc["tool_name"] == tool_use["name"]), None) |
| 120 | + assert current_call is not None, f"{tool_use['name']} should be in ledger" |
| 121 | + assert current_call["tool_args"] == tool_use["input"], "tool_args should match input" |
| 122 | + assert current_call["status"] == "pending", "Status should be pending before execution" |
| 123 | + |
| 124 | + return Proceed(reason="Ledger verified") |
| 125 | + |
| 126 | + handler = LedgerCheckingHandler() |
| 127 | + agent = Agent(tools=[send_notification], hooks=[handler]) |
| 128 | + |
| 129 | + agent("Send a notification to alice saying test message") |
| 130 | + |
| 131 | + # Verify the ledger has the completed tool call |
| 132 | + ledger = handler.steering_context.data.get("ledger") |
| 133 | + assert ledger is not None |
| 134 | + assert len(ledger["tool_calls"]) >= 1, "At least one tool call should be recorded" |
| 135 | + |
| 136 | + # Check the tool call details |
| 137 | + tool_call = ledger["tool_calls"][-1] |
| 138 | + assert tool_call["tool_name"] == "send_notification" |
| 139 | + assert "tool_args" in tool_call |
| 140 | + assert tool_call["tool_args"]["recipient"] == "alice" |
| 141 | + assert tool_call["tool_args"]["message"] == "test message" |
| 142 | + assert tool_call["status"] == "success" |
| 143 | + assert "completion_timestamp" in tool_call |
| 144 | + assert tool_call["error"] is None |
0 commit comments