Skip to content

Conversation

@cxyfer
Copy link
Owner

@cxyfer cxyfer commented Jul 6, 2025

Summary

  • Implement request tracking to prevent duplicate LLM API calls
  • Prevent users from clicking translation/inspiration buttons multiple times
  • Add proper cleanup with try/finally blocks

Test plan

  • Test rapid clicking on translation button
  • Test rapid clicking on inspiration button
  • Verify cached responses still work correctly
  • Check error handling doesn't break cleanup

🤖 Generated with Claude Code

Implement request tracking to prevent duplicate LLM API calls when users
click translation/inspiration buttons multiple times. Uses tuple-based
tracking with (user_id, problem_id, request_type) to ensure proper
request isolation.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@cxyfer cxyfer requested a review from Copilot July 6, 2025 22:17
Copilot

This comment was marked as outdated.

cxyfer and others added 2 commits July 7, 2025 07:13
- Add asyncio.Lock for thread-safe request tracking
- Replace remove() with discard() to prevent KeyError
- Extract duplicate logic into reusable helper methods
- Add comprehensive unit tests for concurrency scenarios

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Add comprehensive documentation for the new test_interaction_handler.py file,
including detailed descriptions of all 8 test cases and instructions for
running the tests.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@cxyfer cxyfer requested a review from Copilot July 6, 2025 23:46
Copilot

This comment was marked as outdated.

Replace direct discard() calls with _cleanup_request() method to prevent
race conditions. All modifications to ongoing_llm_requests set now go
through lock-protected methods.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@cxyfer cxyfer requested a review from Copilot July 7, 2025 00:38
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a mechanism to track and prevent duplicate LLM requests in the Discord bot and adds comprehensive tests and documentation for this feature.

  • Add ongoing_llm_requests set with an asyncio.Lock to serialize check-and-add in handlers
  • Augment translation and inspiration branches with duplicate-request checks and proper cleanup in finally blocks
  • New async tests (test_interaction_handler.py) and updated tests/README.md for running interaction handler tests

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
tests/test_interaction_handler.py New async test suite covering duplicate prevention, cleanup, and concurrency
tests/README.md Document new tests and commands for running interaction handler tests
cogs/interaction_handler_cog.py Implement duplicate-request lock, prevention logic, and cleanup
Comments suppressed due to low confidence (2)

tests/README.md:104

  • [nitpick] The command uv run pytest seems incorrect; consider updating it to a standard pytest tests/test_interaction_handler.py -v invocation for clarity.
uv run pytest tests/test_interaction_handler.py -v

cogs/interaction_handler_cog.py:152

  • Consider adding a unit test that verifies _cleanup_request is called when serving cached translations, ensuring that the request is removed from tracking after a cached response.
                        await self._cleanup_request(request_key)

Comment on lines 150 to 153
await interaction.followup.send(translation, ephemeral=True)
# Remove from ongoing requests when returning cached result
await self._cleanup_request(request_key)
return
Copy link

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This inline cleanup call is redundant since the final finally block performs cleanup as well; consider removing branch-specific cleanup to simplify the flow.

Suggested change
await interaction.followup.send(translation, ephemeral=True)
# Remove from ongoing requests when returning cached result
await self._cleanup_request(request_key)
return
await interaction.followup.send(translation, ephemeral=True)
return

Copilot uses AI. Check for mistakes.
# tests/test_interaction_handler.py
import pytest
import asyncio
from unittest.mock import MagicMock, AsyncMock, patch
Copy link

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch import is not used in this file; consider removing it to clean up unused imports.

Suggested change
from unittest.mock import MagicMock, AsyncMock, patch
from unittest.mock import MagicMock, AsyncMock

Copilot uses AI. Check for mistakes.
Comment on lines 2 to 7
import discord
from discord.ext import commands
import time
import asyncio
from leetcode import html_to_text # 確保這個 import 存在
# from utils.logger import get_logger # 使用 bot.logger
Copy link

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Imports could be reorganized to follow PEP8 ordering (e.g., place import asyncio before import time) for consistency.

Suggested change
import discord
from discord.ext import commands
import time
import asyncio
from leetcode import html_to_text # 確保這個 import 存在
# from utils.logger import get_logger # 使用 bot.logger
import asyncio
import time
import discord
from discord.ext import commands
from leetcode import html_to_text # 確保這個 import 存在
# from utils.logger import get_logger # 使用 bot.logger

Copilot uses AI. Check for mistakes.
The finally block in Python always executes even after return statements,
so explicit cleanup calls before returns are redundant. Keep only the
cleanup in finally blocks to avoid duplicate lock acquisitions.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@cxyfer cxyfer merged commit f96bf37 into main Jul 7, 2025
@cxyfer cxyfer deleted the dev branch July 7, 2025 01:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant