Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

chore: Namespace all Redis keys and pub/sub channels under tfg: (#671)

Srajan Asthanaa36ffaf8df28
diff --git a/.changeset/redis-tfg-key-prefix.md b/.changeset/redis-tfg-key-prefix.md
new file mode 100644
index 000000000..17b92c705
--- /dev/null
+++ b/.changeset/redis-tfg-key-prefix.md
@@ -0,0 +1,6 @@
+---
+"@truefoundry/trueforge-core": patch
+"@truefoundry/trueforge": patch
+---
+
+Namespace all Redis keys and pub/sub channels under `tfg:` so TrueForge can share a Redis instance without colliding with other apps.
diff --git a/packages/trueforge-core/src/core/index.ts b/packages/trueforge-core/src/core/index.ts
index 58394b7a4..76cafcf45 100644
--- a/packages/trueforge-core/src/core/index.ts
+++ b/packages/trueforge-core/src/core/index.ts
@@ -133,6 +133,7 @@ export type {
// Errors / utils
export { AgentHarnessError, McpConnectionError, McpDcrConfigurationError } from './errors';
+export { REDIS_KEY_NAMESPACE, redisKey } from './redisKeys';
export { describeUnknownError, extractErrorLogFields } from './util/errorLogFields';
export { PromiseTimeoutError, withTimeout } from './util/promiseUtils';
diff --git a/packages/trueforge-core/src/core/redisKeys.ts b/packages/trueforge-core/src/core/redisKeys.ts
new file mode 100644
index 000000000..27925c847
--- /dev/null
+++ b/packages/trueforge-core/src/core/redisKeys.ts
@@ -0,0 +1,9 @@
+/**
+ * Shared Redis key/channel namespace for TrueForge.
+ * Every Redis key and pub/sub channel MUST start with this segment.
+ */
+export const REDIS_KEY_NAMESPACE = 'tfg' as const;
+
+export function redisKey(...parts: readonly string[]): string {
+ return [REDIS_KEY_NAMESPACE, ...parts].join(':');
+}
diff --git a/packages/trueforge-core/src/request-reply/executor.ts b/packages/trueforge-core/src/request-reply/executor.ts
index dbda98ee0..6b2e774d5 100644
--- a/packages/trueforge-core/src/request-reply/executor.ts
+++ b/packages/trueforge-core/src/request-reply/executor.ts
@@ -42,7 +42,7 @@ export type RequestReplyErrorHandler = (err: Error, context: { executorId: strin
*/
export class RequestReplyExecutor {
readonly executorId: string;
- /** `rr:req:<executorId>` — the channel this executor subscribes to. */
+ /** `tfg:rr:req:<executorId>` — the channel this executor subscribes to. */
readonly channel: string;
private readonly redis: RedisClientType;
private readonly subscriberClient: RedisClientType;
diff --git a/packages/trueforge-core/src/request-reply/utils.ts b/packages/trueforge-core/src/request-reply/utils.ts
index 12403ae57..0d6dbe67e 100644
--- a/packages/trueforge-core/src/request-reply/utils.ts
+++ b/packages/trueforge-core/src/request-reply/utils.ts
@@ -1,20 +1,20 @@
+import { redisKey } from '../core/redisKeys';
+
export function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
-export const KEY_PREFIX = 'rr';
-
/** Liveness key an executor refreshes; callers check it before publishing. */
export function heartbeatKey(executorId: string): string {
- return `${KEY_PREFIX}:hb:${executorId}`;
+ return redisKey('rr', 'hb', executorId);
}
/** Pub/sub channel: executors subscribe; callers publish the JSON request (see publishedRequestSchema). */
export function requestChannel(executorId: string): string {
- return `${KEY_PREFIX}:req:${executorId}`;
+ return redisKey('rr', 'req', executorId);
}
/** Per-request key the executor SETs the JSONReply to; the caller polls it with GETDEL. */
export function replyKey(requestId: string): string {
- return `${KEY_PREFIX}:reply:${requestId}`;
+ return redisKey('rr', 'reply', requestId);
}
diff --git a/packages/trueforge-core/tests/core/redisKeys.test.ts b/packages/trueforge-core/tests/core/redisKeys.test.ts
new file mode 100644
index 000000000..174df3a43
--- /dev/null
+++ b/packages/trueforge-core/tests/core/redisKeys.test.ts
@@ -0,0 +1,15 @@
+import { REDIS_KEY_NAMESPACE, redisKey } from '../../src/core/redisKeys';
+import { heartbeatKey, replyKey, requestChannel } from '../../src/request-reply/utils';
+
+describe('redisKeys', () => {
+ it('namespaces every key under tfg', () => {
+ expect(REDIS_KEY_NAMESPACE).toBe('tfg');
+ expect(redisKey('agent', 'turn', 'ten', 'sess', 'turn1', 'stream')).toBe('tfg:agent:turn:ten:sess:turn1:stream');
+ });
+
+ it('prefixes request-reply keys and channels', () => {
+ expect(heartbeatKey('exec-1')).toBe('tfg:rr:hb:exec-1');
+ expect(requestChannel('exec-1')).toBe('tfg:rr:req:exec-1');
+ expect(replyKey('req-1')).toBe('tfg:rr:reply:req-1');
+ });
+});
diff --git a/packages/trueforge/src/apis/turns.ts b/packages/trueforge/src/apis/turns.ts
index 2968d8b58..3123f8320 100644
--- a/packages/trueforge/src/apis/turns.ts
+++ b/packages/trueforge/src/apis/turns.ts
@@ -22,6 +22,7 @@ import {
isFileContentPart,
McpConnectionError,
rawSandboxId,
+ redisKey,
SandboxError,
VercelAILLM,
} from '@truefoundry/trueforge-core/core';
@@ -304,7 +305,7 @@ export function streamTTLSecondsFor(event: TurnStreamingEvent): number | undefin
/** Redis/in-memory key for one turn's resumable event stream. */
export function turnStreamId(tenantId: string, sessionId: string, turnId: string): string {
- return `agent:turn:${tenantId}:${sessionId}:${turnId}:stream`;
+ return redisKey('agent', 'turn', tenantId, sessionId, turnId, 'stream');
}
/**
diff --git a/packages/trueforge/tests/unit/apis/turns.test.ts b/packages/trueforge/tests/unit/apis/turns.test.ts
index 354126efd..426fbece4 100644
--- a/packages/trueforge/tests/unit/apis/turns.test.ts
+++ b/packages/trueforge/tests/unit/apis/turns.test.ts
@@ -33,6 +33,10 @@ function mcpServerStoreWithAuth(db: Kysely<Database>, tokenStore: SqliteOAuthTok
}
describe('turns', () => {
+ it('namespaces turn stream ids under tfg', () => {
+ expect(turnStreamId('ten', 'sess', 'turn1')).toBe('tfg:agent:turn:ten:sess:turn1:stream');
+ });
+
describe('turn ownership', () => {
it('returns 403 for all turn routes when the caller is not the session creator', async () => {
const db = createSqliteDb(':memory:');