Skip to content

Enable Garnet Lua scripting#364

Merged
ModerRAS merged 1 commit into
masterfrom
fix/garnet-lua-scripts
Jun 1, 2026
Merged

Enable Garnet Lua scripting#364
ModerRAS merged 1 commit into
masterfrom
fix/garnet-lua-scripts

Conversation

@ModerRAS

@ModerRAS ModerRAS commented Jun 1, 2026

Copy link
Copy Markdown
Owner

Summary

  • start embedded Garnet with Lua scripting and Lua transaction mode enabled
  • expose existing Lua script constants internally so tests execute the same scripts used in production
  • add an integration test that starts Garnet and runs the coding-agent enqueue, agent-chat lock, and music semaphore Lua scripts

Tests

  • dotnet test TelegramSearchBot.Test\TelegramSearchBot.Test.csproj --configuration Release --filter "FullyQualifiedName~GarnetLuaScriptIntegrationTests"
  • dotnet test --configuration Release

Summary by CodeRabbit

Release Notes

  • Tests

    • Added integration tests to validate Redis Lua script functionality across job enqueuing, agent chat locks, and music generation semaphores.
  • Chores

    • Enhanced database infrastructure by enabling Lua scripting support and transaction mode.
    • Refactored internal code organization to improve testability and maintainability.

@coderabbitai

coderabbitai Bot commented Jun 1, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR enables Redis Lua scripting throughout the TelegramSearchBot project. Lua script constants are exposed to tests, Garnet server configuration now includes Lua support with transaction mode, and a comprehensive integration test suite validates three Lua scripts covering coding agent jobs, agent chat locks, and music generation semaphores.

Changes

Lua Script Integration and Testing

Layer / File(s) Summary
Script constant accessibility and test assembly access
TelegramSearchBot.LLM/Service/Tools/CodingAgentJobService.cs, TelegramSearchBot/Service/AI/LLM/AgentChatBatchDispatchService.cs, TelegramSearchBot/Service/Tools/MusicGenerationToolService.cs, TelegramSearchBot.LLM/TelegramSearchBot.LLM.csproj
Three Lua script constants (EnqueueJobScript, ReleaseLockScript, RenewLockScript, AcquireChannelSemaphoreScript) are changed from private to internal visibility. The TelegramSearchBot.LLM project grants InternalsVisibleTo access to TelegramSearchBot.Test assembly.
Garnet server Lua configuration
TelegramSearchBot/AppBootstrap/SchedulerBootstrap.cs
A new BuildGarnetArguments helper centralizes Garnet server argument construction, adding --lua and --lua-transaction-mode flags to the existing bind and port arguments. The Startup method now uses this helper.
Lua script integration tests
TelegramSearchBot.Test/AppBootstrap/GarnetLuaScriptIntegrationTests.cs
An xUnit test class (GarnetLuaScriptIntegrationTests) boots an embedded Garnet server, connects via a retry-capable Redis multiplexer, and validates three Lua scripts: EnqueueJobScript (job enqueueing with Redis state verification), ReleaseLockScript and RenewLockScript (lock renewal and release with ownership checks), and AcquireChannelSemaphoreScript (semaphore acquire with value normalization). Supporting helpers select an available TCP port and connect with bounded timeout and retry logic.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A rabbit hops through Lua scripts so fine,
Making constants internal—access redesigned!
Garnet server sparkles with transaction mode,
Tests validate the path where data flows and glowed.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'Enable Garnet Lua scripting' accurately summarizes the main objective: enabling Lua scripting in the embedded Garnet server and exposing Lua script constants for testing.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/garnet-lua-scripts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@TelegramSearchBot.Test/AppBootstrap/GarnetLuaScriptIntegrationTests.cs`:
- Around line 17-19: The test currently uses GetAvailablePort() which closes the
probe socket before GarnetServer binds, causing a TOCTOU race; change the setup
so the test either binds and holds the listener until GarnetServer takes it
(have GetAvailablePort return an already-bound TcpListener or endpoint) or
modify GarnetServer/SchedulerBootstrap to accept an ephemeral port (0) and then
query the actual bound port after server.Start(); update the test to pass the
held/socket-backed endpoint into GarnetServer (or use port 0 and read
server.BoundPort after Start) instead of releasing the port between probe and
bind.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b621151a-c625-442c-9613-ddcbd0b97f67

📥 Commits

Reviewing files that changed from the base of the PR and between f7b20dc and 3763436.

📒 Files selected for processing (6)
  • TelegramSearchBot.LLM/Service/Tools/CodingAgentJobService.cs
  • TelegramSearchBot.LLM/TelegramSearchBot.LLM.csproj
  • TelegramSearchBot.Test/AppBootstrap/GarnetLuaScriptIntegrationTests.cs
  • TelegramSearchBot/AppBootstrap/SchedulerBootstrap.cs
  • TelegramSearchBot/Service/AI/LLM/AgentChatBatchDispatchService.cs
  • TelegramSearchBot/Service/Tools/MusicGenerationToolService.cs

Comment on lines +17 to +19
var port = GetAvailablePort();
using var server = new GarnetServer(SchedulerBootstrap.BuildGarnetArguments(port.ToString()));
server.Start();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Avoid TOCTOU port reservation race in test server startup.

GetAvailablePort() releases the socket before GarnetServer binds, so another process can take that port and make this integration test flaky.

Suggested hardening
-            var port = GetAvailablePort();
-            using var server = new GarnetServer(SchedulerBootstrap.BuildGarnetArguments(port.ToString()));
-            server.Start();
+            GarnetServer? server = null;
+            var port = 0;
+            for (var attempt = 0; attempt < 5 && server == null; attempt++) {
+                port = GetAvailablePort();
+                try {
+                    server = new GarnetServer(SchedulerBootstrap.BuildGarnetArguments(port.ToString()));
+                    server.Start();
+                } catch {
+                    server?.Dispose();
+                    server = null;
+                }
+            }
+            Assert.NotNull(server);
+            using (server) {
+                using var redis = await ConnectWithRetryAsync(port);
+                var db = redis.GetDatabase();
 
-            using var redis = await ConnectWithRetryAsync(port);
-            var db = redis.GetDatabase();
-
-            await RunCodingAgentEnqueueScriptAsync(db);
-            await RunAgentChatLockScriptsAsync(db);
-            await RunMusicGenerationSemaphoreScriptAsync(db);
+                await RunCodingAgentEnqueueScriptAsync(db);
+                await RunAgentChatLockScriptsAsync(db);
+                await RunMusicGenerationSemaphoreScriptAsync(db);
+            }

Also applies to: 150-157

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@TelegramSearchBot.Test/AppBootstrap/GarnetLuaScriptIntegrationTests.cs`
around lines 17 - 19, The test currently uses GetAvailablePort() which closes
the probe socket before GarnetServer binds, causing a TOCTOU race; change the
setup so the test either binds and holds the listener until GarnetServer takes
it (have GetAvailablePort return an already-bound TcpListener or endpoint) or
modify GarnetServer/SchedulerBootstrap to accept an ephemeral port (0) and then
query the actual bound port after server.Start(); update the test to pass the
held/socket-backed endpoint into GarnetServer (or use port 0 and read
server.BoundPort after Start) instead of releasing the port between probe and
bind.

@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

PR Check Report

Summary

Test Results

Platform Status Details
Ubuntu Passed Tests passed, artifacts uploaded
Windows Passed Tests passed, artifacts uploaded

Code Quality

  • Code formatting check
  • Security vulnerability scan
  • Dependency analysis
  • Code coverage collection

Test Artifacts

  • Test results artifacts count: 2
  • Code coverage uploaded to Codecov

Links


This report is auto-generated by GitHub Actions

@ModerRAS ModerRAS merged commit 5f7cffb into master Jun 1, 2026
8 checks passed
@ModerRAS ModerRAS deleted the fix/garnet-lua-scripts branch June 1, 2026 12:39
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