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(cli): keep persisted workspace scope out of server-dispatched runs (#19182)

Arvin Xu3a6e1c7a6a19
diff --git a/apps/cli/src/api/workspace.test.ts b/apps/cli/src/api/workspace.test.ts
index 774fdefe1ae..983394cf86a 100644
--- a/apps/cli/src/api/workspace.test.ts
+++ b/apps/cli/src/api/workspace.test.ts
@@ -68,6 +68,23 @@ describe('api/workspace scope resolution', () => {
expect(resolveWorkspaceScope()).toEqual({ source: 'env', workspaceId: 'ws_env' });
});
+ // A goal/task dispatch spawns `hetero exec` with an operation-scoped
+ // LOBEHUB_JWT and states its scope solely through LOBEHUB_WORKSPACE_ID. The
+ // machine's `workspace use` scope must not leak into that run: it FORBIDDENs
+ // every ingest call of a personal-scope topic.
+ it('ignores the persisted workspace when running under an injected LOBEHUB_JWT', () => {
+ process.env.LOBEHUB_JWT = 'jwt-from-dispatch';
+ mockLoadActiveWorkspace.mockReturnValue(stored());
+ try {
+ expect(resolveWorkspaceScope()).toEqual({ source: 'personal' });
+
+ process.env.LOBEHUB_WORKSPACE_ID = 'ws_env';
+ expect(resolveWorkspaceScope()).toEqual({ source: 'env', workspaceId: 'ws_env' });
+ } finally {
+ delete process.env.LOBEHUB_JWT;
+ }
+ });
+
it('prefers an explicit argument over everything else', () => {
process.env.LOBEHUB_WORKSPACE_ID = 'ws_env';
mockLoadActiveWorkspace.mockReturnValue(stored());
diff --git a/apps/cli/src/api/workspace.ts b/apps/cli/src/api/workspace.ts
index 1f8d290b5f5..fbf41431afa 100644
--- a/apps/cli/src/api/workspace.ts
+++ b/apps/cli/src/api/workspace.ts
@@ -58,6 +58,15 @@ export function resolveWorkspaceScope(explicit?: string): WorkspaceScope {
const fromEnv = process.env.LOBEHUB_WORKSPACE_ID;
if (fromEnv && fromEnv.length > 0) return { source: 'env', workspaceId: fromEnv };
+ // A server-dispatched execution (operation-scoped `LOBEHUB_JWT`) carries its
+ // complete scope in env: `LOBEHUB_WORKSPACE_ID` when the run is
+ // workspace-scoped, nothing when personal. The persisted `workspace use`
+ // scope belongs to whoever sits at this machine's terminal; letting it leak
+ // into a dispatched run stamps every heteroIngest/heteroFinish call with a
+ // workspace the run's topic is not in, and the server rejects the entire
+ // event stream ("Topic is outside the caller scope").
+ if (process.env.LOBEHUB_JWT) return { source: 'personal' };
+
return resolvePersistedScope() ?? { source: 'personal' };
}