|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Cognesy\Agents\Tests\Unit\Agent; |
| 4 | + |
| 5 | +use Cognesy\Agents\Builder\AgentBuilder; |
| 6 | +use Cognesy\Agents\Capability\Core\UseDriver; |
| 7 | +use Cognesy\Agents\Collections\Tools; |
| 8 | +use Cognesy\Agents\Continuation\AgentStopException; |
| 9 | +use Cognesy\Agents\Continuation\StopReason; |
| 10 | +use Cognesy\Agents\Continuation\StopSignal; |
| 11 | +use Cognesy\Agents\Data\AgentState; |
| 12 | +use Cognesy\Agents\Data\AgentStep; |
| 13 | +use Cognesy\Agents\Drivers\CanUseTools; |
| 14 | +use Cognesy\Agents\Drivers\Testing\FakeAgentDriver; |
| 15 | +use Cognesy\Agents\Drivers\Testing\ScenarioStep; |
| 16 | +use Cognesy\Agents\Enums\ExecutionStatus; |
| 17 | +use Cognesy\Agents\Events\AgentExecutionCompleted; |
| 18 | +use Cognesy\Agents\Events\AgentExecutionFailed; |
| 19 | +use Cognesy\Agents\Events\AgentExecutionStopped; |
| 20 | +use Cognesy\Agents\Events\ContinuationEvaluated; |
| 21 | +use Cognesy\Agents\Tool\Contracts\CanExecuteToolCalls; |
| 22 | +use Cognesy\Messages\Messages; |
| 23 | + |
| 24 | +describe('Agent event payload regression', function () { |
| 25 | + it('emits sequential continuation payloads and completed execution status', function () { |
| 26 | + $driver = FakeAgentDriver::fromSteps( |
| 27 | + ScenarioStep::toolCall('noop', executeTools: false), |
| 28 | + ScenarioStep::final('step two'), |
| 29 | + ); |
| 30 | + |
| 31 | + $events = []; |
| 32 | + $agent = AgentBuilder::base() |
| 33 | + ->withCapability(new UseDriver($driver)) |
| 34 | + ->build() |
| 35 | + ->wiretap(static function (object $event) use (&$events): void { |
| 36 | + $events[] = $event; |
| 37 | + }); |
| 38 | + |
| 39 | + $final = $agent->execute( |
| 40 | + AgentState::empty()->withMessages(Messages::fromString('ping')) |
| 41 | + ); |
| 42 | + |
| 43 | + $continuations = array_values(array_filter( |
| 44 | + $events, |
| 45 | + static fn(object $event): bool => $event instanceof ContinuationEvaluated, |
| 46 | + )); |
| 47 | + $completed = array_values(array_filter( |
| 48 | + $events, |
| 49 | + static fn(object $event): bool => $event instanceof AgentExecutionCompleted, |
| 50 | + )); |
| 51 | + |
| 52 | + expect($continuations)->toHaveCount(2); |
| 53 | + expect(array_map(static fn(ContinuationEvaluated $e): int => $e->stepNumber, $continuations)) |
| 54 | + ->toBe([1, 2]); |
| 55 | + expect($continuations[1]->shouldStop())->toBeTrue(); |
| 56 | + expect($completed)->toHaveCount(1); |
| 57 | + expect($completed[0]->status)->toBe(ExecutionStatus::Completed); |
| 58 | + expect($final->status())->toBe(ExecutionStatus::Completed); |
| 59 | + }); |
| 60 | + |
| 61 | + it('emits stop payload with stop signal reason/source/message', function () { |
| 62 | + $signal = new StopSignal( |
| 63 | + reason: StopReason::Completed, |
| 64 | + message: 'stop now', |
| 65 | + source: 'RegressionStop', |
| 66 | + ); |
| 67 | + |
| 68 | + $driver = new class($signal) implements CanUseTools { |
| 69 | + public function __construct(private StopSignal $signal) {} |
| 70 | + |
| 71 | + public function useTools(AgentState $state, Tools $tools, CanExecuteToolCalls $executor): AgentState { |
| 72 | + $step = new AgentStep(inputMessages: $state->messages()); |
| 73 | + throw new AgentStopException($this->signal, $step, source: 'RegressionStop'); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + $events = []; |
| 78 | + $agent = AgentBuilder::base() |
| 79 | + ->withCapability(new UseDriver($driver)) |
| 80 | + ->build() |
| 81 | + ->wiretap(static function (object $event) use (&$events): void { |
| 82 | + $events[] = $event; |
| 83 | + }); |
| 84 | + |
| 85 | + $final = $agent->execute( |
| 86 | + AgentState::empty()->withMessages(Messages::fromString('ping')) |
| 87 | + ); |
| 88 | + |
| 89 | + $continuation = current(array_values(array_filter( |
| 90 | + $events, |
| 91 | + static fn(object $event): bool => $event instanceof ContinuationEvaluated, |
| 92 | + ))); |
| 93 | + $stopped = current(array_values(array_filter( |
| 94 | + $events, |
| 95 | + static fn(object $event): bool => $event instanceof AgentExecutionStopped, |
| 96 | + ))); |
| 97 | + $completed = current(array_values(array_filter( |
| 98 | + $events, |
| 99 | + static fn(object $event): bool => $event instanceof AgentExecutionCompleted, |
| 100 | + ))); |
| 101 | + |
| 102 | + expect($continuation)->toBeInstanceOf(ContinuationEvaluated::class); |
| 103 | + expect($continuation->stepNumber)->toBe(0); |
| 104 | + expect($continuation->stopSignal())->not->toBeNull(); |
| 105 | + expect($continuation->stopSignal()?->reason)->toBe(StopReason::StopRequested); |
| 106 | + expect($continuation->stopSignal()?->source)->toBe('RegressionStop'); |
| 107 | + expect($continuation->stopSignal()?->message)->toBe('stop now'); |
| 108 | + expect($stopped)->toBeInstanceOf(AgentExecutionStopped::class); |
| 109 | + expect($stopped->stopReason)->toBe(StopReason::StopRequested); |
| 110 | + expect($stopped->stopMessage)->toBe('stop now'); |
| 111 | + expect($stopped->source)->toBe('RegressionStop'); |
| 112 | + expect($completed)->toBeInstanceOf(AgentExecutionCompleted::class); |
| 113 | + expect($completed->status)->toBe(ExecutionStatus::Stopped); |
| 114 | + expect($final->status())->toBe(ExecutionStatus::Stopped); |
| 115 | + }); |
| 116 | + |
| 117 | + it('emits executionFailed with exception payload on hard error', function () { |
| 118 | + $driver = new class implements CanUseTools { |
| 119 | + public function useTools(AgentState $state, Tools $tools, CanExecuteToolCalls $executor): AgentState { |
| 120 | + throw new \RuntimeException('boom regression'); |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + $events = []; |
| 125 | + $agent = AgentBuilder::base() |
| 126 | + ->withCapability(new UseDriver($driver)) |
| 127 | + ->build() |
| 128 | + ->wiretap(static function (object $event) use (&$events): void { |
| 129 | + $events[] = $event; |
| 130 | + }); |
| 131 | + |
| 132 | + $final = $agent->execute( |
| 133 | + AgentState::empty()->withMessages(Messages::fromString('ping')) |
| 134 | + ); |
| 135 | + |
| 136 | + $failed = current(array_values(array_filter( |
| 137 | + $events, |
| 138 | + static fn(object $event): bool => $event instanceof AgentExecutionFailed, |
| 139 | + ))); |
| 140 | + |
| 141 | + expect($failed)->toBeInstanceOf(AgentExecutionFailed::class); |
| 142 | + expect($failed->exception->getMessage())->toBe('boom regression'); |
| 143 | + expect($failed->exception)->toBeInstanceOf(\RuntimeException::class); |
| 144 | + expect($failed->status)->toBe(ExecutionStatus::Failed); |
| 145 | + expect($final->status())->toBe(ExecutionStatus::Failed); |
| 146 | + }); |
| 147 | +}); |
0 commit comments