Skip to content

Commit 060a037

Browse files
committed
fix(security): add 60-second wall-clock timeout to AI agent loop
Prevents resource exhaustion from expensive queries. Logs timeout events with elapsed time and iteration count. Fixes #9
1 parent a7fb36f commit 060a037

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

Model/Assistant/AgentLoop.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class AgentLoop
1111
{
1212
private const MAX_ITERATIONS = 10;
13+
private const MAX_EXECUTION_SECONDS = 60;
1314

1415
public function __construct(
1516
private readonly OpenAiClientFactory $openAiClientFactory,
@@ -41,8 +42,19 @@ public function run(string $userQuery, array $conversationHistory = []): array
4142

4243
$tools = $this->toolRegistry->getToolDefinitions();
4344
$client = $this->openAiClientFactory->create();
45+
$startTime = microtime(true);
4446

4547
for ($i = 0; $i < self::MAX_ITERATIONS; $i++) {
48+
if ((microtime(true) - $startTime) > self::MAX_EXECUTION_SECONDS) {
49+
$this->logger->warning('AI Assistant agent loop timed out', [
50+
'elapsed_seconds' => round(microtime(true) - $startTime, 1),
51+
'iterations' => $i,
52+
]);
53+
return [
54+
'answer' => 'The request took too long to process. Please try a simpler question.',
55+
'messages' => $messages,
56+
];
57+
}
4658
$response = $client->chat()->create([
4759
'model' => $this->resolveModel(),
4860
'messages' => $messages,

0 commit comments

Comments
 (0)