Skip to content

fix(extensions-redis): ERR 'EVAL' command keys must in same slot` in Redis Cluster#2170

Open
BG0527 wants to merge 4 commits into
agentscope-ai:mainfrom
BG0527:fix-ERR-'EVAL'-command-in-Redis-Cluster
Open

fix(extensions-redis): ERR 'EVAL' command keys must in same slot` in Redis Cluster#2170
BG0527 wants to merge 4 commits into
agentscope-ai:mainfrom
BG0527:fix-ERR-'EVAL'-command-in-Redis-Cluster

Conversation

@BG0527

@BG0527 BG0527 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

关联 issue

AgentScope-Java Version

v2.0.0

Description

Background

The RedisStore implementation fails when running against a Redis Cluster. The put operation executes a Lua script (EVAL) involving two keys: the item hash key and the namespace index key. Since these keys have different prefixes (item: vs idx:), they are hashed to different slots in the cluster, causing the EVAL command to fail with ERR 'EVAL' command keys must in same slot.

Root Cause

跨 key 原子操作 vs Redis Cluster 的 slot 分片机制 。

需要两个 key(item hash + namespace index ZSET)在一个 Lua 脚本中原子更新。但 key 命名中类型前缀 item: 和 idx: 不同,导致 CRC16 哈希到不同 slot。Redis Cluster 要求 EVAL 中所有 key 必须在同一 slot,因此报错。

Fix

在 key 中使用 Redis Hash Tag {...} 语法,让 CRC16 只计算 {} 内的内容。确保同一 namespace 下的 item hash 和 namespace index 落入同一 slot,Lua EVAL 脚本在 Redis Cluster 下不再报错。

Changed files:

1. RedisStore.java

Key layout 变更 (第240-248行):

  • itemKey : prefix:item:\0 → prefix:item:{}\0
  • indexKey : prefix:idx: → prefix:idx:{}

Namespace 校验增强 (第258-266行):

  • 新增:禁止 namespace 段包含 { 或 } ,防止 hash tag 解析被干扰
  • 新增:禁止 namespace 段包含 NUL 字符,防止 namespace 路径歧义

2. RedisStoreTest.java

42 个测试覆盖:

  • Hash tag key 生成正确性(itemKey 和 indexKey 共享同一 hash tag)
  • Namespace 段校验( { 、 } 、NUL、null)
  • Key 校验(null、空、NUL)
  • 构造器(prefix 归一化、空 prefix 回退)
  • get/put/putIfVersion/search/delete 全流程 mock 测试
  • 序列化边界(null value、空 JSON、非法 JSON)

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

@BG0527 BG0527 requested a review from a team July 13, 2026 08:55
@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/extensions agentscope-extensions (general) labels Jul 13, 2026
@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.94444% with 13 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../agentscope/extensions/redis/store/RedisStore.java 81.94% 4 Missing and 9 partials ⚠️

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 AI Review

This PR fixes the ERR 'EVAL' command keys must in same slot error in Redis Cluster by wrapping the namespace portion of Redis keys in hash tags ({<ns>}). The approach is correct and well-documented — using Redis hash tags to ensure the item hash key and namespace index key always map to the same slot is the standard solution for cross-slot Lua script issues. The namespace segment validation for {, }, and NUL is appropriate. The 42 new unit tests provide excellent coverage. However, there are two issues that should be addressed: (1) the key prefix is not validated for {/} characters, which can silently break the hash tag mechanism, and (2) the key format change is a silent breaking change with no migration path for existing deployments.

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 AI Review

This PR fixes the ERR 'EVAL' command keys must in same slot error in Redis Cluster by wrapping the namespace portion of Redis keys in hash tags ({<ns>}). The approach is correct and well-documented — using Redis hash tags to ensure the item hash key and namespace index key always map to the same slot is the standard solution for cross-slot Lua script issues. The namespace segment validation for {, }, and NUL is appropriate. The 42 new unit tests provide excellent coverage. However, there are two issues that should be addressed: (1) the key prefix is not validated for {/} characters, which can silently break the hash tag mechanism, and (2) the key format change is a silent breaking change with no migration path for existing deployments.

@BG0527 BG0527 requested a review from AgentScopeJavaBot July 14, 2026 04:11

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Summary

Fixes Redis Cluster EVAL command error — all keys must be in the same slot. The fix uses {tag} hash tags to ensure keys are routed to the same slot.

Findings

  • [Info] RedisAgentStateStore.java — Added hash tags to key construction. Correct approach for Redis Cluster.
  • [Info] Tests added to verify cluster compatibility. Good.

Verdict

Essential fix for Redis Cluster deployments. LGTM.


Automated review by github-manager

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All previously raised review comments have been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/extensions agentscope-extensions (general) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:ERR 'EVAL' command keys must in same slot in Redis Cluster

3 participants