Repository navigation
Handle multiple messages for one taskId #712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Assistant\Tests\Unit\Listener; | ||
|
|
||
| use OCA\Assistant\Db\ChattyLLM\Message; | ||
| use OCA\Assistant\Db\ChattyLLM\MessageMapper; | ||
| use OCA\Assistant\Db\ChattyLLM\Session; | ||
| use OCA\Assistant\Db\ChattyLLM\SessionMapper; | ||
| use OCA\Assistant\Listener\ChattyLLMTaskListener; | ||
| use OCA\Assistant\Service\NotificationService; | ||
| use OCA\Assistant\Service\TaskProcessingService; | ||
| use OCP\AppFramework\Db\DoesNotExistException; | ||
| use OCP\TaskProcessing\Events\TaskSuccessfulEvent; | ||
| use OCP\TaskProcessing\Task; | ||
| use OCP\TaskProcessing\TaskTypes\TextToTextChat; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use Psr\Log\LoggerInterface; | ||
| use Test\TestCase; | ||
|
|
||
| /** | ||
| * Concurrent task processing executors can run the same task twice, | ||
| * which must not store the chat message twice. | ||
| */ | ||
| class ChattyLLMTaskListenerTest extends TestCase { | ||
|
|
||
| private const SESSION_ID = 23; | ||
| private const TASK_ID = 91; | ||
|
|
||
| private MessageMapper&MockObject $messageMapper; | ||
| private SessionMapper&MockObject $sessionMapper; | ||
| private ChattyLLMTaskListener $listener; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| $this->messageMapper = $this->createMock(MessageMapper::class); | ||
| $this->sessionMapper = $this->createMock(SessionMapper::class); | ||
|
|
||
| $session = new Session(); | ||
| $session->setAssignmentId(null); | ||
| $this->sessionMapper->method('getUserSession')->willReturn($session); | ||
|
|
||
| $this->listener = new ChattyLLMTaskListener( | ||
| $this->messageMapper, | ||
| $this->sessionMapper, | ||
| $this->createMock(TaskProcessingService::class), | ||
| $this->createMock(LoggerInterface::class), | ||
| $this->createMock(NotificationService::class), | ||
| ); | ||
| } | ||
|
|
||
| private function makeTask(): Task { | ||
| $task = new Task( | ||
| TextToTextChat::ID, | ||
| [ | ||
| 'input' => 'What is a cat?', | ||
| 'system_prompt' => '', | ||
| 'history' => [], | ||
| ], | ||
| 'assistant:chatty-llm', | ||
| 'admin', | ||
| 'chatty-llm:' . self::SESSION_ID, | ||
| ); | ||
| $task->setId(self::TASK_ID); | ||
| $task->setOutput(['output' => 'A cat is an animal.']); | ||
| return $task; | ||
| } | ||
|
|
||
| private function handleTask(): void { | ||
| $event = new TaskSuccessfulEvent($this->makeTask()); | ||
| $this->listener->handle($event); | ||
| } | ||
|
|
||
| public function testMessageIsStored(): void { | ||
| $this->messageMapper->method('getMessageByTaskId') | ||
| ->with(self::SESSION_ID, self::TASK_ID) | ||
| ->willThrowException(new DoesNotExistException('Message not found')); | ||
| $storedMessage = null; | ||
| $this->messageMapper->expects($this->once()) | ||
| ->method('insert') | ||
| ->willReturnCallback(static function (Message $message) use (&$storedMessage): Message { | ||
| $storedMessage = $message; | ||
| $message->setId(3); | ||
| return $message; | ||
| }); | ||
| $this->sessionMapper->expects($this->once())->method('update'); | ||
|
|
||
| $this->handleTask(); | ||
|
|
||
| $this->assertNotNull($storedMessage); | ||
| $this->assertSame(self::SESSION_ID, $storedMessage->getSessionId()); | ||
| $this->assertSame(self::TASK_ID, $storedMessage->getOcpTaskId()); | ||
| $this->assertSame(Message::ROLE_ASSISTANT, $storedMessage->getRole()); | ||
| $this->assertSame('A cat is an animal.', $storedMessage->getContent()); | ||
| } | ||
|
|
||
| public function testMessageIsStoredOnlyOnce(): void { | ||
| $storedMessage = new Message(); | ||
| $storedMessage->setSessionId(self::SESSION_ID); | ||
| $storedMessage->setOcpTaskId(self::TASK_ID); | ||
| $this->messageMapper->method('getMessageByTaskId') | ||
| ->with(self::SESSION_ID, self::TASK_ID) | ||
| ->willReturn($storedMessage); | ||
| $this->messageMapper->expects($this->never())->method('insert'); | ||
| $this->sessionMapper->expects($this->never())->method('update'); | ||
|
|
||
| $this->handleTask(); | ||
| } | ||
|
|
||
| public function testTitleTasksAreUntouched(): void { | ||
| $task = new Task( | ||
| TextToTextChat::ID, | ||
| [ | ||
| 'input' => 'Generate a title', | ||
| 'system_prompt' => '', | ||
| 'history' => [], | ||
| ], | ||
| 'assistant:chatty-llm', | ||
| 'admin', | ||
| 'chatty-title:' . self::SESSION_ID, | ||
| ); | ||
| $task->setId(self::TASK_ID); | ||
| $task->setOutput(['output' => 'Cats']); | ||
|
|
||
| $event = new TaskSuccessfulEvent($task); | ||
| $this->messageMapper->expects($this->never())->method('getMessageByTaskId'); | ||
| $this->messageMapper->expects($this->never())->method('insert'); | ||
| $this->sessionMapper->expects($this->once()) | ||
| ->method('updateSessionTitle') | ||
| ->with('admin', self::SESSION_ID, 'Cats'); | ||
|
|
||
| $this->listener->handle($event); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Make the duplicate check atomic with insertion.
When two executors handle the same task concurrently, both can receive
DoesNotExistExceptionbefore either reachesinsert()at Line 158. Both then insert a message. The new check does not prevent the duplicate rows described in the PR. Use a database-enforced unique claim for the session and task, or a distributed lock that covers both the lookup and insertion. Account for existing duplicate rows before adding a unique constraint.