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(tool-execution): sign Market calls with the run identity (#19444)

LiJianb0eb29b1af74
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/agentBuilder.test.ts b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/agentBuilder.test.ts
index e1a76968b16..431176b7ce1 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/agentBuilder.test.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/agentBuilder.test.ts
@@ -1,5 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { DiscoverService } from '@/server/services/discover';
+
import { agentBuilderRuntime } from '../agentBuilder';
const {
@@ -69,6 +71,15 @@ const createRuntime = () =>
userId: 'user-1',
});
+const createWorkspaceRuntime = () =>
+ agentBuilderRuntime.factory({
+ editingAgentId: 'agent-1',
+ serverDB: {} as never,
+ toolManifestMap: {},
+ userId: 'user-1',
+ workspaceId: 'workspace-1',
+ });
+
describe('agentBuilderRuntime', () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -295,4 +306,27 @@ describe('agentBuilderRuntime', () => {
});
});
});
+
+ // Regression guard for `searchMarketTools` returning `unauthorized`: built
+ // without an identity, DiscoverService signs no trusted-client token, so every
+ // server-executed market search failed β€” which the model reports as a plain
+ // tool failure and silently works around, leaving the built agent with no
+ // market tool.
+ describe('market identity', () => {
+ it('passes the run identity to DiscoverService', () => {
+ createRuntime();
+
+ expect(DiscoverService).toHaveBeenCalledWith({
+ userInfo: { userId: 'user-1', workspaceId: undefined },
+ });
+ });
+
+ it('scopes the market identity to the run workspace', () => {
+ createWorkspaceRuntime();
+
+ expect(DiscoverService).toHaveBeenCalledWith({
+ userInfo: { userId: 'user-1', workspaceId: 'workspace-1' },
+ });
+ });
+ });
});
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/agentManagement.test.ts b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/agentManagement.test.ts
index 315da5161d7..c4819d79fd8 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/agentManagement.test.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/agentManagement.test.ts
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { AgentModel } from '@/database/models/agent';
import { PluginModel } from '@/database/models/plugin';
+import { DiscoverService } from '@/server/services/discover';
import { agentManagementRuntime } from '../agentManagement';
@@ -423,4 +424,26 @@ describe('agentManagementRuntime', () => {
expect(mockUpdateConfig).not.toHaveBeenCalled();
});
});
+
+ // Regression guard: built without an identity, DiscoverService sends no
+ // credentials at all, so every market read from this runtime failed as
+ // `unauthorized` while the client-side path β€” which signs a trusted-client
+ // token β€” kept working.
+ describe('market identity', () => {
+ it('passes the run identity to DiscoverService', () => {
+ createRuntime();
+
+ expect(DiscoverService).toHaveBeenCalledWith({
+ userInfo: { userId: 'user-1', workspaceId: undefined },
+ });
+ });
+
+ it('scopes the market identity to the run workspace', () => {
+ createWorkspaceRuntime();
+
+ expect(DiscoverService).toHaveBeenCalledWith({
+ userInfo: { userId: 'user-1', workspaceId: 'workspace-1' },
+ });
+ });
+ });
});
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/skills.test.ts b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/skills.test.ts
index 57a478ccffe..5d44719e02c 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/skills.test.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/skills.test.ts
@@ -1009,4 +1009,42 @@ describe('skillsRuntime', () => {
canExecuteOnDevice: true,
});
});
+
+ // Regression guard for the split-sandbox bug: the sandbox session is keyed by
+ // the acting account, which is derived from the trusted-client token. Without
+ // `workspaceId` this runtime acted as the personal account while `lobe-creds`
+ // and `lobe-cloud-sandbox` (which pass it) acted as the workspace, so
+ // credentials injected for a workspace topic were invisible to every command
+ // run here.
+ it('scopes the market identity to the run workspace so sandbox calls share one session', async () => {
+ const { MarketService } = await import('@/server/services/market');
+ const { skillsRuntime } = await import('../skills');
+
+ await skillsRuntime.factory({
+ serverDB: {} as never,
+ toolManifestMap: {},
+ topicId: 'topic-1',
+ userId: 'user-1',
+ workspaceId: 'workspace-1',
+ });
+
+ expect(MarketService).toHaveBeenLastCalledWith(
+ expect.objectContaining({
+ userInfo: { userId: 'user-1', workspaceId: 'workspace-1' },
+ }),
+ );
+
+ await skillsRuntime.factory({
+ serverDB: {} as never,
+ toolManifestMap: {},
+ topicId: 'topic-1',
+ userId: 'user-1',
+ });
+
+ expect(MarketService).toHaveBeenLastCalledWith(
+ expect.objectContaining({
+ userInfo: { userId: 'user-1', workspaceId: undefined },
+ }),
+ );
+ });
});
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/agentBuilder.ts b/apps/server/src/services/toolExecution/serverRuntimes/agentBuilder.ts
index f81aaf41104..10047d386b6 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/agentBuilder.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/agentBuilder.ts
@@ -39,7 +39,17 @@ export const agentBuilderRuntime: ServerRuntimeRegistration = {
const agentModel = new AgentModel(context.serverDB, userId, context.workspaceId);
const pluginModel = new PluginModel(context.serverDB, userId, context.workspaceId);
const aiInfraRepos = new AiInfraRepos(context.serverDB, userId, {}, context.workspaceId);
- const discoverService = new DiscoverService();
+ /**
+ * Market list endpoints require an authenticated caller, and `DiscoverService`
+ * only signs a trusted-client token when it is given an identity β€” built
+ * without one it sends no credentials at all and every market read fails as
+ * `unauthorized`, which `searchMarketTools` surfaces as a plain tool failure
+ * the model silently works around. `workspaceId` additionally attributes the
+ * read to the workspace rather than the personal account.
+ */
+ const discoverService = new DiscoverService({
+ userInfo: { userId, workspaceId: context.workspaceId },
+ });
return {
getAvailableModels: async (
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/agentManagement.ts b/apps/server/src/services/toolExecution/serverRuntimes/agentManagement.ts
index b9d89938eae..8e1b10b2038 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/agentManagement.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/agentManagement.ts
@@ -41,7 +41,12 @@ export const agentManagementRuntime: ServerRuntimeRegistration = {
const agentModel = new AgentModel(context.serverDB, context.userId, context.workspaceId);
const pluginModel = new PluginModel(context.serverDB, context.userId, context.workspaceId);
- const discoverService = new DiscoverService();
+ // Same identity requirement as the Agent Builder runtime: built without an
+ // identity, DiscoverService sends no credentials and every market read fails
+ // as `unauthorized`.
+ const discoverService = new DiscoverService({
+ userInfo: { userId: context.userId, workspaceId: context.workspaceId },
+ });
return {
callAgent: async (
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/skills.ts b/apps/server/src/services/toolExecution/serverRuntimes/skills.ts
index 8f71ed78217..e84b0f27b49 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/skills.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/skills.ts
@@ -685,9 +685,16 @@ export const skillsRuntime: ServerRuntimeRegistration = {
context.userId,
context.workspaceId,
);
+ /**
+ * `workspaceId` decides which sandbox session this runtime reaches: the
+ * session is keyed by the acting account, so a token without it acts as the
+ * personal account while `lobe-creds` and `lobe-cloud-sandbox` β€” which do
+ * pass it β€” act as the workspace. Omitting it split one workspace topic
+ * across two sandboxes, leaving injected credentials invisible here.
+ */
const marketService = new MarketService({
accessToken: marketAccessToken,
- userInfo: { userId: context.userId },
+ userInfo: { userId: context.userId, workspaceId: context.workspaceId },
});
const fileService = new FileService(context.serverDB, context.userId, context.workspaceId);
const fileModel = new FileModel(context.serverDB, context.userId, context.workspaceId);