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(group): stop orphaning group conversations when activeGroupId is transiently empty (#16149)

Arvin Xu415f1b691ab0
diff --git a/src/routes/(main)/group/features/Conversation/useGroupContext.ts b/src/routes/(main)/group/features/Conversation/useGroupContext.ts
index 4146ed605b3..de8195c250b 100644
--- a/src/routes/(main)/group/features/Conversation/useGroupContext.ts
+++ b/src/routes/(main)/group/features/Conversation/useGroupContext.ts
@@ -13,16 +13,20 @@ import { useChatStore } from '@/store/chat';
* Used for group chat pages where multiple agents participate.
*/
export function useGroupContext(): ConversationContext {
- const [topicId, threadId, groupId] = useChatStore((s) => [
+ const [topicId, threadId] = useChatStore((s) => [
s.activeTopicId ?? null,
s.activeThreadId ?? null,
- s.activeGroupId ?? null,
]);
const currentGroup = useAgentGroupStore((s) =>
s.activeGroupId ? agentGroupSelectors.getGroupById(s.activeGroupId)(s) : undefined,
);
const supervisorAgentId = currentGroup?.supervisorAgentId;
+ // Derive groupId from the resolved group — the same source as supervisorAgentId —
+ // instead of the route-synced chatStore.activeGroupId. When that global is
+ // transiently empty, sourcing groupId from it writes group messages/topics with an
+ // agentId but groupId=null, orphaning them out of the group's history view.
+ const groupId = currentGroup?.id ?? null;
// Group context uses supervisorAgentId as agentId for message storage
// When in group mode (not group_agent thread mode), the supervisor is responding
diff --git a/src/store/chat/slices/aiAgent/actions/__tests__/groupOrchestration.test.ts b/src/store/chat/slices/aiAgent/actions/__tests__/groupOrchestration.test.ts
index c0fb34ac95d..b8153c39afe 100644
--- a/src/store/chat/slices/aiAgent/actions/__tests__/groupOrchestration.test.ts
+++ b/src/store/chat/slices/aiAgent/actions/__tests__/groupOrchestration.test.ts
@@ -2,6 +2,7 @@ import { act, renderHook } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { useClientDataSWR } from '@/libs/swr';
+import { useAgentGroupStore } from '@/store/agentGroup';
import { useChatStore } from '@/store/chat/store';
// Keep zustand mock as it's needed globally
@@ -243,4 +244,88 @@ describe('groupOrchestration actions', () => {
);
});
});
+
+ describe('#resolveGroupId fallback (chat store has no active group)', () => {
+ afterEach(() => {
+ useAgentGroupStore.setState({ activeGroupId: undefined, groupMap: {} }, false);
+ });
+
+ it('resolves the group from this run’s supervisor', async () => {
+ // Chat store active group is cleared (e.g. left the group route in a popup)…
+ useChatStore.setState({ activeGroupId: undefined }, false);
+ // …but the group is still known via its supervisor.
+ useAgentGroupStore.setState(
+ {
+ activeGroupId: undefined,
+ groupMap: {
+ [TEST_IDS.GROUP_ID]: {
+ agents: [],
+ id: TEST_IDS.GROUP_ID,
+ supervisorAgentId: TEST_IDS.SUPERVISOR_AGENT_ID,
+ } as any,
+ },
+ },
+ false,
+ );
+
+ const { result } = renderHook(() => useChatStore());
+ const internal_execGroupOrchestrationSpy = vi.fn().mockResolvedValue({ status: 'done' });
+ act(() => {
+ useChatStore.setState({
+ internal_execGroupOrchestration: internal_execGroupOrchestrationSpy,
+ });
+ });
+
+ await act(async () => {
+ await result.current.triggerSpeak({
+ agentId: 'target-agent-id',
+ instruction: 'hi',
+ skipCallSupervisor: false,
+ supervisorAgentId: TEST_IDS.SUPERVISOR_AGENT_ID,
+ });
+ });
+
+ expect(internal_execGroupOrchestrationSpy).toHaveBeenCalledWith(
+ expect.objectContaining({ groupId: TEST_IDS.GROUP_ID }),
+ );
+ });
+
+ it('skips instead of running against a stale popup group from a different supervisor', async () => {
+ useChatStore.setState({ activeGroupId: undefined }, false);
+ // A stale popup group lingers in the agentGroup store, but it is NOT this
+ // run’s supervisor — the trigger must skip, not run against it.
+ useAgentGroupStore.setState(
+ {
+ activeGroupId: 'stale-popup-group',
+ groupMap: {
+ 'stale-popup-group': {
+ agents: [],
+ id: 'stale-popup-group',
+ supervisorAgentId: 'some-other-supervisor',
+ } as any,
+ },
+ },
+ false,
+ );
+
+ const { result } = renderHook(() => useChatStore());
+ const internal_execGroupOrchestrationSpy = vi.fn().mockResolvedValue({ status: 'done' });
+ act(() => {
+ useChatStore.setState({
+ internal_execGroupOrchestration: internal_execGroupOrchestrationSpy,
+ });
+ });
+
+ await act(async () => {
+ await result.current.triggerSpeak({
+ agentId: 'target-agent-id',
+ instruction: 'hi',
+ skipCallSupervisor: false,
+ supervisorAgentId: TEST_IDS.SUPERVISOR_AGENT_ID,
+ });
+ });
+
+ expect(internal_execGroupOrchestrationSpy).not.toHaveBeenCalled();
+ });
+ });
});
diff --git a/src/store/chat/slices/aiAgent/actions/groupOrchestration.ts b/src/store/chat/slices/aiAgent/actions/groupOrchestration.ts
index 842324913a8..6dec9323932 100644
--- a/src/store/chat/slices/aiAgent/actions/groupOrchestration.ts
+++ b/src/store/chat/slices/aiAgent/actions/groupOrchestration.ts
@@ -6,6 +6,8 @@ import { type SWRResponse } from 'swr';
import { useClientDataSWR } from '@/libs/swr';
import { aiAgentService } from '@/services/aiAgent';
+import { getChatGroupStoreState } from '@/store/agentGroup';
+import { agentGroupByIdSelectors } from '@/store/agentGroup/selectors';
import { createGroupOrchestrationExecutors } from '@/store/chat/agents/GroupOrchestration';
import { type ChatStore } from '@/store/chat/store';
import { type GroupOrchestrationCallbacks } from '@/store/tool/slices/builtin/types';
@@ -48,6 +50,22 @@ export class GroupOrchestrationActionImpl {
this.#get = get;
}
+ /**
+ * Resolve the groupId for an orchestration run. Prefer the chat store's
+ * route-synced activeGroupId; when it's transiently empty, resolve the group from
+ * the supervisor that owns THIS run — NOT the global agentGroup activeGroupId,
+ * which can be a stale popup group (popup agent routes only clear the chat store)
+ * and would make a non-group popup trigger work in the wrong group. Returns
+ * undefined for a non-group supervisor, so the trigger safely skips as before.
+ */
+ #resolveGroupId = (supervisorAgentId: string): string | undefined => {
+ const activeGroupId = this.#get().activeGroupId;
+ if (activeGroupId) return activeGroupId;
+ return agentGroupByIdSelectors.groupBySupervisorAgentId(supervisorAgentId)(
+ getChatGroupStoreState(),
+ )?.id;
+ };
+
getGroupOrchestrationCallbacks = (): GroupOrchestrationCallbacks => {
return {
triggerSpeak: this.#get().triggerSpeak,
@@ -70,7 +88,7 @@ export class GroupOrchestrationActionImpl {
skipCallSupervisor,
);
- const groupId = this.#get().activeGroupId;
+ const groupId = this.#resolveGroupId(supervisorAgentId);
if (!groupId) {
log('[triggerSpeak] No active group, skipping');
return;
@@ -104,7 +122,7 @@ export class GroupOrchestrationActionImpl {
toolMessageId,
);
- const groupId = this.#get().activeGroupId;
+ const groupId = this.#resolveGroupId(supervisorAgentId);
if (!groupId) {
log('[triggerBroadcast] No active group, skipping');
return;
@@ -136,7 +154,7 @@ export class GroupOrchestrationActionImpl {
reason,
);
- const groupId = this.#get().activeGroupId;
+ const groupId = this.#resolveGroupId(supervisorAgentId);
if (!groupId) {
log('[triggerDelegate] No active group, skipping');
return;
@@ -180,7 +198,7 @@ export class GroupOrchestrationActionImpl {
runInClient,
);
- const groupId = this.#get().activeGroupId;
+ const groupId = this.#resolveGroupId(supervisorAgentId);
if (!groupId) {
log('[triggerExecuteTask] No active group, skipping');
return;
@@ -213,7 +231,7 @@ export class GroupOrchestrationActionImpl {
skipCallSupervisor,
);
- const groupId = this.#get().activeGroupId;
+ const groupId = this.#resolveGroupId(supervisorAgentId);
if (!groupId) {
log('[triggerExecuteTasks] No active group, skipping');
return;