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(trusted-client): skip Market trusted token for synthetic eval userIds (#15959)

Arvin Xu63b52522d07b
diff --git a/src/libs/trusted-client/index.test.ts b/src/libs/trusted-client/index.test.ts
new file mode 100644
index 00000000000..1bf6106c552
--- /dev/null
+++ b/src/libs/trusted-client/index.test.ts
@@ -0,0 +1,54 @@
+import { buildTrustedClientPayload, createTrustedClientToken } from '@lobehub/market-sdk';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { generateTrustedClientToken, isSyntheticTrustedClientUserId } from './index';
+
+vi.mock('@/envs/app', () => ({
+ appEnv: {
+ MARKET_TRUSTED_CLIENT_ID: 'client-id',
+ MARKET_TRUSTED_CLIENT_SECRET: 'client-secret',
+ },
+}));
+
+vi.mock('@lobehub/market-sdk', () => ({
+ buildTrustedClientPayload: vi.fn((params) => ({ ...params, nonce: 'n', timestamp: 0 })),
+ createTrustedClientToken: vi.fn(() => 'signed-token'),
+}));
+
+beforeEach(() => {
+ vi.clearAllMocks();
+});
+
+describe('isSyntheticTrustedClientUserId', () => {
+ it('flags eval_ and qstash_smoke_ prefixed ids', () => {
+ expect(isSyntheticTrustedClientUserId('eval_123')).toBe(true);
+ expect(isSyntheticTrustedClientUserId('qstash_smoke_abc')).toBe(true);
+ });
+
+ it('does not flag real user ids or near-misses', () => {
+ expect(isSyntheticTrustedClientUserId('user_2abc')).toBe(false);
+ // "evaluator_" starts with "eval" but not the "eval_" prefix
+ expect(isSyntheticTrustedClientUserId('evaluator_1')).toBe(false);
+ // prefix must be at the start
+ expect(isSyntheticTrustedClientUserId('abc-eval_1')).toBe(false);
+ });
+});
+
+describe('generateTrustedClientToken', () => {
+ it('returns undefined for synthetic eval/smoke userIds without signing', () => {
+ expect(generateTrustedClientToken({ userId: 'eval_xyz' })).toBeUndefined();
+ expect(generateTrustedClientToken({ userId: 'qstash_smoke_1' })).toBeUndefined();
+ expect(buildTrustedClientPayload).not.toHaveBeenCalled();
+ expect(createTrustedClientToken).not.toHaveBeenCalled();
+ });
+
+ it('signs a token for a real userId', () => {
+ const token = generateTrustedClientToken({ email: '[email protected]', userId: 'user_real' });
+
+ expect(token).toBe('signed-token');
+ expect(buildTrustedClientPayload).toHaveBeenCalledWith(
+ expect.objectContaining({ clientId: 'client-id', userId: 'user_real' }),
+ );
+ expect(createTrustedClientToken).toHaveBeenCalledWith(expect.anything(), 'client-secret');
+ });
+});
diff --git a/src/libs/trusted-client/index.ts b/src/libs/trusted-client/index.ts
index 9260fa9102a..9c2fdb9f3b6 100644
--- a/src/libs/trusted-client/index.ts
+++ b/src/libs/trusted-client/index.ts
@@ -17,6 +17,19 @@ export interface TrustedClientUserInfo {
export { getSessionUser } from './getSessionUser';
+/**
+ * Synthetic user ids used by local agent-evals / smoke scripts (e.g. `eval_*`,
+ * `qstash_smoke_*`). These are never real platform accounts β€” no real userId
+ * carries these prefixes β€” so Market rejects any trusted-client token built
+ * from them with `invalid_trust_token / Invalid userId format`. We skip token
+ * generation for them to avoid the doomed round-trip and the noisy prep drag it
+ * causes during evals.
+ */
+const SYNTHETIC_USER_ID_PATTERN = /^(?:eval|qstash_smoke)_/;
+
+export const isSyntheticTrustedClientUserId = (userId: string): boolean =>
+ SYNTHETIC_USER_ID_PATTERN.test(userId);
+
/**
* Check if trusted client authentication is enabled
*/
@@ -38,6 +51,12 @@ export const generateTrustedClientToken = (userInfo: TrustedClientUserInfo): str
return undefined;
}
+ // Synthetic eval/smoke userIds can never be valid Market accounts; skip
+ // signing a token Market is guaranteed to reject.
+ if (isSyntheticTrustedClientUserId(userInfo.userId)) {
+ return undefined;
+ }
+
try {
const payload = buildTrustedClientPayload({
clientId: MARKET_TRUSTED_CLIENT_ID,