Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(agent-runtime): Agent Runtime Redis 客户端读取 REDIS_PASSWORD 等连接配置 (#22553)

Mocha0c137f27a261
diff --git a/apps/server/src/modules/AgentRuntime/__tests__/redis.test.ts b/apps/server/src/modules/AgentRuntime/__tests__/redis.test.ts
new file mode 100644
index 00000000000..5364ecc8c9d
--- /dev/null
+++ b/apps/server/src/modules/AgentRuntime/__tests__/redis.test.ts
@@ -0,0 +1,62 @@
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+const instances: Array<{ options: Record<string, unknown>; url: string }> = [];
+
+vi.mock('ioredis', () => ({
+ default: class {
+ constructor(url: string, options: Record<string, unknown>) {
+ instances.push({ options, url });
+ }
+
+ on() {
+ return this;
+ }
+ },
+}));
+
+const redisEnv: Record<string, unknown> = {};
+
+vi.mock('@/envs/redis', () => ({ redisEnv }));
+vi.mock('@/server/modules/Redis', () => ({ isRedisDisabledByEnv: () => false }));
+
+const { createAgentRuntimeRedisClient } = await import('../redis');
+
+describe('createAgentRuntimeRedisClient', () => {
+ afterEach(() => {
+ instances.length = 0;
+ for (const key of Object.keys(redisEnv)) delete redisEnv[key];
+ });
+
+ // A deployment that keeps the password out of REDIS_URL (REDIS_PASSWORD etc.)
+ // must authenticate the same way the shared Redis provider does; otherwise
+ // every Agent Runtime command fails with NOAUTH.
+ it('authenticates with the separately configured Redis credentials', () => {
+ Object.assign(redisEnv, {
+ REDIS_DATABASE: 2,
+ REDIS_PASSWORD: 'secret',
+ REDIS_TLS: true,
+ REDIS_URL: 'redis://redis:6379',
+ REDIS_USERNAME: 'agent',
+ });
+
+ createAgentRuntimeRedisClient();
+
+ expect(instances).toHaveLength(1);
+ expect(instances[0].url).toBe('redis://redis:6379');
+ expect(instances[0].options).toMatchObject({
+ db: 2,
+ password: 'secret',
+ tls: {},
+ username: 'agent',
+ });
+ });
+
+ it('does not send the env credentials to an explicitly passed URL', () => {
+ Object.assign(redisEnv, { REDIS_PASSWORD: 'secret', REDIS_URL: 'redis://redis:6379' });
+
+ createAgentRuntimeRedisClient('redis://other:6379');
+
+ expect(instances[0].url).toBe('redis://other:6379');
+ expect(instances[0].options.password).toBeUndefined();
+ });
+});
diff --git a/apps/server/src/modules/AgentRuntime/redis.ts b/apps/server/src/modules/AgentRuntime/redis.ts
index 2f663a839ec..096b326ac31 100644
--- a/apps/server/src/modules/AgentRuntime/redis.ts
+++ b/apps/server/src/modules/AgentRuntime/redis.ts
@@ -1,5 +1,5 @@
import debug from 'debug';
-import Redis from 'ioredis';
+import Redis, { type RedisOptions } from 'ioredis';
import { redisEnv } from '@/envs/redis';
import { isRedisDisabledByEnv } from '@/server/modules/Redis';
@@ -27,7 +27,23 @@ const getRedisConnectionDescription = (url: string): string => {
};
/**
- * Create Redis client instance for Agent Runtime
+ * Connection options from the shared Redis env, applied the way the main
+ * provider (`IoRedisRedisProvider`) applies them. ioredis lets values already
+ * in the URL win, so `redis://user:pass@host/1` keeps working.
+ */
+const getRedisEnvConnectionOptions = (): RedisOptions => ({
+ db: redisEnv.REDIS_DATABASE,
+ password: redisEnv.REDIS_PASSWORD,
+ tls: redisEnv.REDIS_TLS ? {} : undefined,
+ username: redisEnv.REDIS_USERNAME,
+});
+
+/**
+ * Create Redis client instance for Agent Runtime.
+ *
+ * Without an explicit `url` the client targets the env-configured server and
+ * also uses its credentials/db/TLS. An explicit `url` is self-contained: the
+ * env credentials belong to the env server and are not sent to another host.
*/
export const createAgentRuntimeRedisClient = (url?: string): Redis | null => {
if (isRedisDisabledByEnv()) return null;
@@ -45,6 +61,7 @@ export const createAgentRuntimeRedisClient = (url?: string): Redis | null => {
timing('Redis client creating at %d', createStart);
const client = new Redis(redisUrl, {
+ ...(url ? {} : getRedisEnvConnectionOptions()),
maxRetriesPerRequest: 3,
});