π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(task): keep the task page's detail after a task opened beside it closes (#23100)
Rdmclin270d170ea6132
diff --git a/src/features/AgentTasks/AgentTaskDetail/useActiveTaskDetail.test.ts b/src/features/AgentTasks/AgentTaskDetail/useActiveTaskDetail.test.tsindex cebbf374e59..d3d3a57c752 100644--- a/src/features/AgentTasks/AgentTaskDetail/useActiveTaskDetail.test.ts+++ b/src/features/AgentTasks/AgentTaskDetail/useActiveTaskDetail.test.ts@@ -38,7 +38,7 @@ const buildTaskState = (const { agentId = 'agt_assignee', detail = true, taskError } = overrides;return {activeTaskId: undefined,- setActiveTaskId: vi.fn(),+ claimActiveTask: vi.fn(() => () => {}),taskDetailMap: detail ? { 'T-194': { agentId } } : {},// `useFetchTaskDetail` is read off the store and called with the id.useFetchTaskDetail: () => ({ error: taskError, mutate: vi.fn() }),diff --git a/src/features/AgentTasks/AgentTaskDetail/useActiveTaskDetail.ts b/src/features/AgentTasks/AgentTaskDetail/useActiveTaskDetail.tsindex cd15a2e81f7..f2329340030 100644--- a/src/features/AgentTasks/AgentTaskDetail/useActiveTaskDetail.ts+++ b/src/features/AgentTasks/AgentTaskDetail/useActiveTaskDetail.ts@@ -33,7 +33,7 @@ interface ActiveTaskDetailState {*/export const useActiveTaskDetail = (taskId?: string): ActiveTaskDetailState => {const isLogin = useUserStore(authSelectors.isLogin);- const setActiveTaskId = useTaskStore((s) => s.setActiveTaskId);+ const claimActiveTask = useTaskStore((s) => s.claimActiveTask);const useFetchTaskDetail = useTaskStore((s) => s.useFetchTaskDetail);const useHydrateAgentConfig = useAgentStore((s) => s.useHydrateAgentConfig);@@ -49,11 +49,14 @@ export const useActiveTaskDetail = (taskId?: string): ActiveTaskDetailState => {);const assigneeInMap = useAgentStore((s) => !!(assigneeAgentId && s.agentMap[assigneeAgentId]));+ // A claim, not a plain set: a task opened beside this surface (the Portal)+ // takes over the active task, and closing it must hand it back here rather+ // than clear it β this effect does not re-run, so a cleared id would leave+ // the page rendering an empty task under the right breadcrumb.useEffect(() => {if (!taskId) return;- setActiveTaskId(taskId);- return () => setActiveTaskId(undefined);- }, [taskId, setActiveTaskId]);+ return claimActiveTask(taskId);+ }, [taskId, claimActiveTask]);// `fetchTaskDetail` throws on a missing task, so `error` is the definitive// "settled and absent" signal β using it (instead of `!isLoading`) avoids thediff --git a/src/features/AgentTasks/AgentTaskDetail/useActiveTaskResult.ts b/src/features/AgentTasks/AgentTaskDetail/useActiveTaskResult.tsindex b4e57aaf6aa..58e53f74b9b 100644--- a/src/features/AgentTasks/AgentTaskDetail/useActiveTaskResult.ts+++ b/src/features/AgentTasks/AgentTaskDetail/useActiveTaskResult.ts@@ -12,17 +12,18 @@ interface ActiveTaskResultState {/** Fetches only the Task snapshot needed by the result review surface. */export const useActiveTaskResult = (taskId?: string): ActiveTaskResultState => {- const setActiveTaskId = useTaskStore((state) => state.setActiveTaskId);+ const claimActiveTask = useTaskStore((state) => state.claimActiveTask);const useFetchTaskDetail = useTaskStore((state) => state.useFetchTaskDetail);const hasTaskDetail = useTaskStore((state) =>taskId ? Boolean(state.taskDetailMap[state.taskRefIndex?.[taskId] ?? taskId]) : false,);+ // Claimed like the detail surfaces (see `useActiveTaskDetail`), so closing+ // the result hands the active task back to the page underneath.useEffect(() => {if (!taskId) return;- setActiveTaskId(taskId);- return () => setActiveTaskId(undefined);- }, [setActiveTaskId, taskId]);+ return claimActiveTask(taskId);+ }, [claimActiveTask, taskId]);const { error: taskError, mutate } = useFetchTaskDetail(taskId);diff --git a/src/store/task/slices/detail/action.activeClaim.test.ts b/src/store/task/slices/detail/action.activeClaim.test.tsnew file mode 100644index 00000000000..2c11c76e97a--- /dev/null+++ b/src/store/task/slices/detail/action.activeClaim.test.ts@@ -0,0 +1,58 @@+import { afterEach, describe, expect, it } from 'vitest';++import { useTaskStore } from '../../store';++describe('TaskDetailSlice', () => {+ afterEach(() => {+ useTaskStore.setState({ activeTaskId: undefined, activeTaskRef: undefined });+ });++ describe('claimActiveTask', () => {+ it('hands the active task back to the page when a task opened beside it closes', () => {+ // The `/task/T-2` page is open; a task link in its run drawer opens T-1+ // in the Portal, and back / close unmounts that Portal.+ const releasePage = useTaskStore.getState().claimActiveTask('T-2');+ const releasePortal = useTaskStore.getState().claimActiveTask('T-1');+ expect(useTaskStore.getState().activeTaskRef).toBe('T-1');++ releasePortal();+ expect(useTaskStore.getState().activeTaskRef).toBe('T-2');++ releasePage();+ expect(useTaskStore.getState().activeTaskRef).toBeUndefined();+ });++ it('leaves the newest claim active when an older one is released first', () => {+ const releasePage = useTaskStore.getState().claimActiveTask('T-2');+ const releasePortal = useTaskStore.getState().claimActiveTask('T-1');++ releasePage();+ expect(useTaskStore.getState().activeTaskRef).toBe('T-1');++ releasePortal();+ expect(useTaskStore.getState().activeTaskRef).toBeUndefined();+ });++ it('keeps the task active while a second surface on the same task closes', () => {+ const releasePage = useTaskStore.getState().claimActiveTask('T-2');+ const releasePortal = useTaskStore.getState().claimActiveTask('T-2');++ releasePortal();+ expect(useTaskStore.getState().activeTaskRef).toBe('T-2');++ releasePage();+ expect(useTaskStore.getState().activeTaskRef).toBeUndefined();+ });++ it('ignores a release called twice', () => {+ const releasePage = useTaskStore.getState().claimActiveTask('T-2');+ const releasePortal = useTaskStore.getState().claimActiveTask('T-1');++ releasePortal();+ releasePortal();+ expect(useTaskStore.getState().activeTaskRef).toBe('T-2');++ releasePage();+ });+ });+});diff --git a/src/store/task/slices/detail/action.ts b/src/store/task/slices/detail/action.tsindex 3ec32eb3770..42abb616cea 100644--- a/src/store/task/slices/detail/action.ts+++ b/src/store/task/slices/detail/action.ts@@ -108,6 +108,8 @@ export class TaskDetailSliceActionImpl {readonly #detail;readonly #reconciliations = new WeakMap<TaskDetailData, Promise<void>>();readonly #previous = new WeakMap<TaskDetailData, TaskDetailData | undefined>();+ /** Mounted task-detail surfaces, oldest first β see `claimActiveTask`. */+ readonly #activeTaskClaims: { ref: string }[] = [];constructor(set: Setter, get: () => TaskStore, _api?: unknown) {void _api;@@ -399,6 +401,32 @@ export class TaskDetailSliceActionImpl {await this.internal_refreshTaskDetail(taskId);};+ /**+ * Make `taskRef` the active task for as long as the caller stays mounted.+ *+ * Several surfaces can show a task detail at once β the `/task` page and a+ * task opened beside it in the Portal β and every detail section reads the+ * one global `activeTaskId`. Claims stack: the newest wins, and releasing the+ * top claim hands the task back to the claim below instead of clearing it,+ * so closing the Portal leaves the page under it on its own task. Releasing+ * a claim that is not on top changes nothing.+ *+ * Returns the release function; call it on unmount.+ */+ claimActiveTask = (taskRef: string): (() => void) => {+ const claim = { ref: taskRef };+ this.#activeTaskClaims.push(claim);+ this.setActiveTaskId(taskRef);++ return () => {+ const index = this.#activeTaskClaims.indexOf(claim);+ if (index === -1) return;+ const wasTop = index === this.#activeTaskClaims.length - 1;+ this.#activeTaskClaims.splice(index, 1);+ if (wasTop) this.setActiveTaskId(this.#activeTaskClaims.at(-1)?.ref);+ };+ };+setActiveTaskId = (taskId?: string): void => {if (this.#get().activeTaskRef === taskId) return;this.#set(