Skip to content

The agent that grows with you

fix(desktop): kanban query keys read the connection scope reactively

kshitijk4poora6251894104f
diff --git a/apps/desktop/src/plugins/kanban/api.ts b/apps/desktop/src/plugins/kanban/api.ts
index 78560261210..8135c0fde3c 100644
--- a/apps/desktop/src/plugins/kanban/api.ts
+++ b/apps/desktop/src/plugins/kanban/api.ts
@@ -22,7 +22,8 @@ import {
type PluginRestOptions,
type PluginStorage,
type PluginTranslate,
- queryClient
+ queryClient,
+ useValue
} from '@hermes/plugin-sdk'
// Native completion notification.
@@ -68,9 +69,16 @@ const LANES_KEY = 'lanesByProfile'
const COLLAPSED_KEY = 'collapsedLanes'
/** Cache-scope id for the active connection — the segment every query key
- * embeds. `'local'` (never '') for the local pool so the key is never empty. */
+ * embeds. `'local'` covers the pre-descriptor null; the SDK atom already
+ * reports 'local' for the local pool. For NON-rendering code (mutations,
+ * socket frames); rendering components use `useKanbanScope` so the keys they
+ * build during render recompute when the connection changes. */
export function kanbanConnectionScope(): string {
- return String(host.state.connectionId?.get?.() || 'local')
+ return host.state.connectionId.get() ?? 'local'
+}
+
+export function useKanbanScope(): string {
+ return useValue(host.state.connectionId) ?? 'local'
}
/** One live `task_events` frame → precise cache invalidation: the board, plus
@@ -83,12 +91,13 @@ function onEventsFrame(slug: string, data: unknown): void {
return
}
- void queryClient.invalidateQueries({ queryKey: ['kanban', 'board'] })
+ const scope = kanbanConnectionScope()
+ void queryClient.invalidateQueries({ queryKey: boardKeyPrefix(scope) })
// Any event can change a board's card count — keep the switcher badge honest.
- void queryClient.invalidateQueries({ queryKey: BOARDS_KEY })
+ void queryClient.invalidateQueries({ queryKey: boardsKey(scope) })
for (const taskId of new Set(events.map(event => event.task_id).filter(Boolean))) {
- void queryClient.invalidateQueries({ queryKey: taskKey(slug, taskId!) })
+ void queryClient.invalidateQueries({ queryKey: taskKey(scope, slug, taskId!) })
}
// Completion notification (after invalidation so notify failure
@@ -200,16 +209,17 @@ function withBoard(path: string, params: Record<string, string> = {}): string {
// cache miss — one gateway's boards/tasks must never paint under another
// connection's route) ─────────────────────────────────────────────────────────
-const scopeKey = (): readonly [string] => [kanbanConnectionScope()]
-
-export const boardKey = (slug: string, archived: boolean) =>
- ['kanban', 'board', ...scopeKey(), slug, archived] as const
-export const taskKey = (slug: string, id: string) => ['kanban', 'task', ...scopeKey(), slug, id] as const
-export const logKey = (slug: string, id: string) => ['kanban', 'log', ...scopeKey(), slug, id] as const
-export const BOARDS_KEY = ['kanban', 'boards', ...scopeKey()] as const
-export const PROFILES_KEY = ['kanban', 'profiles', ...scopeKey()] as const
-export const PROJECTS_KEY = ['kanban', 'projects', ...scopeKey()] as const
-export const ORCHESTRATION_KEY = ['kanban', 'orchestration', ...scopeKey()] as const
+/** Prefix matching every board query on one connection (all slugs, both
+ * archived views) — the mutation-settled invalidation target. */
+export const boardKeyPrefix = (scope: string) => ['kanban', 'board', scope] as const
+export const boardKey = (scope: string, slug: string, archived: boolean) =>
+ [...boardKeyPrefix(scope), slug, archived] as const
+export const taskKey = (scope: string, slug: string, id: string) => ['kanban', 'task', scope, slug, id] as const
+export const logKey = (scope: string, slug: string, id: string) => ['kanban', 'log', scope, slug, id] as const
+export const boardsKey = (scope: string) => ['kanban', 'boards', scope] as const
+export const profilesKey = (scope: string) => ['kanban', 'profiles', scope] as const
+export const projectsKey = (scope: string) => ['kanban', 'projects', scope] as const
+export const orchestrationKey = (scope: string) => ['kanban', 'orchestration', scope] as const
// ── reads ─────────────────────────────────────────────────────────────────────
diff --git a/apps/desktop/src/plugins/kanban/board-switcher.tsx b/apps/desktop/src/plugins/kanban/board-switcher.tsx
index ea01003d13e..b08f937b885 100644
--- a/apps/desktop/src/plugins/kanban/board-switcher.tsx
+++ b/apps/desktop/src/plugins/kanban/board-switcher.tsx
@@ -36,14 +36,15 @@ import { type ReactNode, useEffect, useState } from 'react'
import {
$boardSlug,
- BOARDS_KEY,
+ boardsKey,
createBoard,
deleteBoard,
fetchBoards,
fetchProjects,
pluginOs,
- PROJECTS_KEY,
- updateBoard
+ projectsKey,
+ updateBoard,
+ useKanbanScope
} from './api'
import { runExportBoardFlow, runImportBoardFlow } from './transfer'
import type { BoardMeta } from './types'
@@ -58,7 +59,8 @@ const DEFAULT_BOARD = 'default'
* deterministic branch. "No project" falls back to scratch sandboxes. */
function ProjectPicker({ onChange, value }: { onChange: (id: string) => void; value: string }) {
const k = useKanban()
- const { data } = useQuery({ queryKey: PROJECTS_KEY, queryFn: fetchProjects, staleTime: 30_000 })
+ const scope = useKanbanScope()
+ const { data } = useQuery({ queryKey: projectsKey(scope), queryFn: fetchProjects, staleTime: 30_000 })
const projects = data?.projects ?? []
return (
@@ -89,12 +91,13 @@ function ProjectPicker({ onChange, value }: { onChange: (id: string) => void; va
* the caller finish, or surface the error and leave the dialog open. */
function useBoardWrite<T>(mutationFn: () => Promise<T>, onDone: (result: T) => void) {
const qc = useQueryClient()
+ const scope = useKanbanScope()
return useMutation({
mutationFn,
onError: err => host.notify({ kind: 'error', message: errText(err) }),
onSuccess: result => {
- void qc.invalidateQueries({ queryKey: BOARDS_KEY })
+ void qc.invalidateQueries({ queryKey: boardsKey(scope) })
onDone(result)
}
})
@@ -283,8 +286,9 @@ export function BoardSwitcher() {
// Delete reuses the app-wide label, the way sessions and profiles do.
const { t } = useI18n()
const qc = useQueryClient()
+ const scope = useKanbanScope()
const slug = useValue($boardSlug)
- const { data: boards } = useQuery({ queryFn: fetchBoards, queryKey: BOARDS_KEY, staleTime: 30_000 })
+ const { data: boards } = useQuery({ queryFn: fetchBoards, queryKey: boardsKey(scope), staleTime: 30_000 })
const [adding, setAdding] = useState(false)
const [settingsFor, setSettingsFor] = useState<BoardMeta | null>(null)
const [renameFor, setRenameFor] = useState<BoardMeta | null>(null)
@@ -296,7 +300,7 @@ export function BoardSwitcher() {
const { result } = await deleteBoard(target.slug)
$boardSlug.set('')
- void qc.invalidateQueries({ queryKey: BOARDS_KEY })
+ void qc.invalidateQueries({ queryKey: boardsKey(scope) })
host.notify({ kind: 'success', message: k.boardArchived(result.new_path) })
}
@@ -321,7 +325,7 @@ export function BoardSwitcher() {
if (imported) {
$boardSlug.set(imported)
- void qc.invalidateQueries({ queryKey: BOARDS_KEY })
+ void qc.invalidateQueries({ queryKey: boardsKey(scope) })
}
}
diff --git a/apps/desktop/src/plugins/kanban/board.tsx b/apps/desktop/src/plugins/kanban/board.tsx
index 89087ca59f1..e1df48744ff 100644
--- a/apps/desktop/src/plugins/kanban/board.tsx
+++ b/apps/desktop/src/plugins/kanban/board.tsx
@@ -67,7 +67,8 @@ import {
$introDismissed,
$lanesByProfile,
boardKey,
- BOARDS_KEY,
+ boardKeyPrefix,
+ boardsKey,
bulkTasks,
createTask,
deleteTask,
@@ -76,8 +77,9 @@ import {
fetchBoards,
fetchProfiles,
patchTask,
- PROFILES_KEY,
- taskKey
+ profilesKey,
+ taskKey,
+ useKanbanScope
} from './api'
import { BoardSwitcher } from './board-switcher'
import { TaskDrawer } from './drawer'
@@ -547,7 +549,8 @@ function NewTaskDialog({
}) {
const k = useKanban()
const qc = useQueryClient()
- const { data: roster } = useQuery({ queryKey: PROFILES_KEY, queryFn: fetchProfiles, staleTime: 60_000 })
+ const scope = useKanbanScope()
+ const { data: roster } = useQuery({ queryKey: profilesKey(scope), queryFn: fetchProfiles, staleTime: 60_000 })
// Title-only creates must RUN: "auto" resolves to the orchestration default
// (ultimately the active profile), applied at create time. Never silently
// unassigned — parking a card is the explicit choice, not the default.
@@ -558,7 +561,7 @@ function NewTaskDialog({
// dir) unless the operator overrides it below. Set the board default in the
// board switcher's "Board settings…".
const selectedSlug = useValue($boardSlug)
- const { data: boards } = useQuery({ queryKey: BOARDS_KEY, queryFn: fetchBoards, staleTime: 30_000 })
+ const { data: boards } = useQuery({ queryKey: boardsKey(scope), queryFn: fetchBoards, staleTime: 30_000 })
const currentBoard = boards?.boards.find(b => b.slug === (selectedSlug || boards.current))
const boardDefaultKind = currentBoard?.default_workspace_kind || 'scratch'
const boardDefaultDir = currentBoard?.default_workdir || ''
@@ -658,7 +661,7 @@ function NewTaskDialog({
host.notify({ kind: 'warning', message: warning })
}
- await qc.invalidateQueries({ queryKey: ['kanban', 'board'] })
+ await qc.invalidateQueries({ queryKey: boardKeyPrefix(scope) })
onClose()
} catch (err) {
setError(errText(err))
@@ -966,10 +969,11 @@ function SelectionBar({
}) {
const k = useKanban()
const qc = useQueryClient()
- const { data: roster } = useQuery({ queryKey: PROFILES_KEY, queryFn: fetchProfiles, staleTime: 60_000 })
+ const scope = useKanbanScope()
+ const { data: roster } = useQuery({ queryKey: profilesKey(scope), queryFn: fetchProfiles, staleTime: 60_000 })
const finish = (failed: Array<{ error?: string; id: string }>) => {
- void qc.invalidateQueries({ queryKey: ['kanban', 'board'] })
+ void qc.invalidateQueries({ queryKey: boardKeyPrefix(scope) })
if (failed.length > 0) {
host.notify({
@@ -1084,6 +1088,7 @@ function SelectionBar({
export function KanbanBoardPage() {
const k = useKanban()
const qc = useQueryClient()
+ const scope = useKanbanScope()
const slug = useValue($boardSlug)
const [archived, setArchived] = useState(false)
@@ -1091,7 +1096,7 @@ export function KanbanBoardPage() {
// slow heartbeat for socketless paths (OAuth remotes, dropped connections).
const { data: board, error } = useQuery({
queryFn: () => fetchBoard(archived),
- queryKey: boardKey(slug, archived),
+ queryKey: boardKey(scope, slug, archived),
refetchInterval: 60_000
})
@@ -1190,48 +1195,48 @@ export function KanbanBoardPage() {
const moveMut = useMutation({
mutationFn: ({ id, status }: { id: string; status: string }) => patchTask(id, { status }),
onMutate: async ({ id, status }) => {
- await qc.cancelQueries({ queryKey: boardKey(slug, archived) })
- const previous = qc.getQueryData<KanbanBoard>(boardKey(slug, archived))
+ await qc.cancelQueries({ queryKey: boardKey(scope, slug, archived) })
+ const previous = qc.getQueryData<KanbanBoard>(boardKey(scope, slug, archived))
if (previous) {
- qc.setQueryData(boardKey(slug, archived), moveCard(previous, id, status))
+ qc.setQueryData(boardKey(scope, slug, archived), moveCard(previous, id, status))
}
return { previous }
},
onError: (err, _vars, context) => {
if (context?.previous) {
- qc.setQueryData(boardKey(slug, archived), context.previous)
+ qc.setQueryData(boardKey(scope, slug, archived), context.previous)
}
host.notify({ kind: 'error', message: errText(err) })
},
onSettled: (_data, _err, vars) => {
- void qc.invalidateQueries({ queryKey: ['kanban', 'board'] })
- void qc.invalidateQueries({ queryKey: taskKey(slug, vars.id) })
+ void qc.invalidateQueries({ queryKey: boardKeyPrefix(scope) })
+ void qc.invalidateQueries({ queryKey: taskKey(scope, slug, vars.id) })
}
})
const deleteMut = useMutation({
mutationFn: (id: string) => deleteTask(id),
onMutate: async id => {
- await qc.cancelQueries({ queryKey: boardKey(slug, archived) })
- const previous = qc.getQueryData<KanbanBoard>(boardKey(slug, archived))
+ await qc.cancelQueries({ queryKey: boardKey(scope, slug, archived) })
+ const previous = qc.getQueryData<KanbanBoard>(boardKey(scope, slug, archived))
if (previous) {
- qc.setQueryData(boardKey(slug, archived), removeCard(previous, id))
+ qc.setQueryData(boardKey(scope, slug, archived), removeCard(previous, id))
}
return { previous }
},
onError: (err, _id, context) => {
if (context?.previous) {
- qc.setQueryData(boardKey(slug, archived), context.previous)
+ qc.setQueryData(boardKey(scope, slug, archived), context.previous)
}
host.notify({ kind: 'error', message: errText(err) })
},
- onSettled: () => void qc.invalidateQueries({ queryKey: ['kanban', 'board'] })
+ onSettled: () => void qc.invalidateQueries({ queryKey: boardKeyPrefix(scope) })
})
const onMove = (id: string, status: string) => {
diff --git a/apps/desktop/src/plugins/kanban/drawer.test.tsx b/apps/desktop/src/plugins/kanban/drawer.test.tsx
index 4e9f06afd52..bb7d5d5aa54 100644
--- a/apps/desktop/src/plugins/kanban/drawer.test.tsx
+++ b/apps/desktop/src/plugins/kanban/drawer.test.tsx
@@ -7,7 +7,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
// eslint-disable-next-line no-restricted-imports
import { registerPluginLocales } from '@/i18n/plugin-i18n'
-import { bindApi, taskKey } from './api'
+import { bindApi } from './api'
import { TaskDrawer } from './drawer'
import { en, KANBAN_LOCALES } from './i18n'
import type { KanbanTaskDetail } from './types'
@@ -94,7 +94,7 @@ describe('task attachment compatibility', () => {
// A later backend response restores the capability without remounting.
detail = { ...legacyDetail, attachments: [] }
- await act(() => client.invalidateQueries({ queryKey: taskKey('', legacyDetail.task.id) }))
+ await act(() => client.invalidateQueries({ queryKey: ['kanban', 'task'] }))
expect(await screen.findByRole('button', { name: en.uploadAttachment })).toBeTruthy()
expect(screen.getByText(en.noAttachments)).toBeTruthy()
}
diff --git a/apps/desktop/src/plugins/kanban/drawer.tsx b/apps/desktop/src/plugins/kanban/drawer.tsx
index bdca9b43ffe..807e471a8e8 100644
--- a/apps/desktop/src/plugins/kanban/drawer.tsx
+++ b/apps/desktop/src/plugins/kanban/drawer.tsx
@@ -33,6 +33,7 @@ import { type ReactNode, useEffect, useRef, useState } from 'react'
import {
$boardSlug,
addComment,
+ boardKeyPrefix,
deleteTask,
estimateTask,
fetchLog,
@@ -40,11 +41,12 @@ import {
fetchTask,
logKey,
patchTask,
- PROFILES_KEY,
+ profilesKey,
reassignTask,
reclaimTask,
taskKey,
- uploadAttachment
+ uploadAttachment,
+ useKanbanScope
} from './api'
import { ModelOverrideField, overridePatch } from './model-override'
import {
@@ -242,7 +244,8 @@ function AssigneeMenu({
onReassign: (p: string) => void
}) {
const k = useKanban()
- const { data: roster } = useQuery({ queryKey: PROFILES_KEY, queryFn: fetchProfiles, staleTime: 60_000 })
+ const scope = useKanbanScope()
+ const { data: roster } = useQuery({ queryKey: profilesKey(scope), queryFn: fetchProfiles, staleTime: 60_000 })
return (
<DropdownMenu>
@@ -551,13 +554,14 @@ export function TaskDrawer({
}) {
const k = useKanban()
const qc = useQueryClient()
+ const scope = useKanbanScope()
const slug = useValue($boardSlug)
// Socket-invalidated (bindApi); the interval is only the socketless heartbeat.
const { data: detail, error } = useQuery({
enabled: !!id,
queryFn: () => fetchTask(id!),
- queryKey: taskKey(slug, id ?? ''),
+ queryKey: taskKey(scope, slug, id ?? ''),
refetchInterval: 30_000
})
@@ -568,7 +572,7 @@ export function TaskDrawer({
const { data: log } = useQuery({
enabled: !!id,
queryFn: () => fetchLog(id!),
- queryKey: logKey(slug, id ?? ''),
+ queryKey: logKey(scope, slug, id ?? ''),
refetchInterval: running ? 3_000 : 15_000
})
@@ -585,8 +589,8 @@ export function TaskDrawer({
}, [id, onClose])
const invalidate = () => {
- void qc.invalidateQueries({ queryKey: taskKey(slug, id!) })
- void qc.invalidateQueries({ queryKey: ['kanban', 'board'] })
+ void qc.invalidateQueries({ queryKey: taskKey(scope, slug, id!) })
+ void qc.invalidateQueries({ queryKey: boardKeyPrefix(scope) })
}
// Optimistic status change against the task cache; rolls back + toasts on a
@@ -594,18 +598,18 @@ export function TaskDrawer({
const moveMut = useMutation({
mutationFn: (status: string) => patchTask(id!, { status }),
onMutate: async status => {
- await qc.cancelQueries({ queryKey: taskKey(slug, id!) })
- const previous = qc.getQueryData<KanbanTaskDetail>(taskKey(slug, id!))
+ await qc.cancelQueries({ queryKey: taskKey(scope, slug, id!) })
+ const previous = qc.getQueryData<KanbanTaskDetail>(taskKey(scope, slug, id!))
if (previous) {
- qc.setQueryData(taskKey(slug, id!), { ...previous, task: { ...previous.task, status } })
+ qc.setQueryData(taskKey(scope, slug, id!), { ...previous, task: { ...previous.task, status } })
}
return { previous }
},
onError: (err, _status, context) => {
if (context?.previous) {
- qc.setQueryData(taskKey(slug, id!), context.previous)
+ qc.setQueryData(taskKey(scope, slug, id!), context.previous)
}
host.notify({ kind: 'error', message: errText(err) })
diff --git a/apps/desktop/src/plugins/kanban/orchestration.tsx b/apps/desktop/src/plugins/kanban/orchestration.tsx
index 623c7b166c9..255ba6e7500 100644
--- a/apps/desktop/src/plugins/kanban/orchestration.tsx
+++ b/apps/desktop/src/plugins/kanban/orchestration.tsx
@@ -25,10 +25,11 @@ import {
autoDescribeProfile,
fetchOrchestration,
fetchProfiles,
- ORCHESTRATION_KEY,
- PROFILES_KEY,
+ orchestrationKey,
+ profilesKey,
saveOrchestration,
- saveProfileDescription
+ saveProfileDescription,
+ useKanbanScope
} from './api'
import type { KanbanProfile } from './types'
import { errText, FIELD_LABEL, useKanban } from './ui'
@@ -71,8 +72,9 @@ function ProfilePicker({
function ProfileDescriptionRow({ profile }: { profile: KanbanProfile }) {
const k = useKanban()
const qc = useQueryClient()
+ const scope = useKanbanScope()
const [draft, setDraft] = useState(profile.description)
- const invalidate = () => void qc.invalidateQueries({ queryKey: PROFILES_KEY })
+ const invalidate = () => void qc.invalidateQueries({ queryKey: profilesKey(scope) })
const save = useMutation({
mutationFn: () => saveProfileDescription(profile.name, draft.trim()),
@@ -132,13 +134,14 @@ function ProfileDescriptionRow({ profile }: { profile: KanbanProfile }) {
export function OrchestrationPanel() {
const k = useKanban()
const qc = useQueryClient()
- const { data: settings } = useQuery({ queryKey: ORCHESTRATION_KEY, queryFn: fetchOrchestration })
- const { data: roster } = useQuery({ queryKey: PROFILES_KEY, queryFn: fetchProfiles, staleTime: 60_000 })
+ const scope = useKanbanScope()
+ const { data: settings } = useQuery({ queryKey: orchestrationKey(scope), queryFn: fetchOrchestration })
+ const { data: roster } = useQuery({ queryKey: profilesKey(scope), queryFn: fetchProfiles, staleTime: 60_000 })
const save = useMutation({
mutationFn: (patch: Record<string, unknown>) => saveOrchestration(patch),
onError: err => host.notify({ kind: 'error', message: errText(err) }),
- onSuccess: () => void qc.invalidateQueries({ queryKey: ORCHESTRATION_KEY })
+ onSuccess: () => void qc.invalidateQueries({ queryKey: orchestrationKey(scope) })
})
if (!settings || !roster) {
diff --git a/apps/desktop/src/plugins/kanban/plugin.tsx b/apps/desktop/src/plugins/kanban/plugin.tsx
index bab22025df4..e819eaa5d67 100644
--- a/apps/desktop/src/plugins/kanban/plugin.tsx
+++ b/apps/desktop/src/plugins/kanban/plugin.tsx
@@ -30,7 +30,7 @@ import {
useValue
} from '@hermes/plugin-sdk'
-import { $boardSlug, bindApi, boardKey, fetchBoard } from './api'
+import { $boardSlug, bindApi, boardKey, fetchBoard, useKanbanScope } from './api'
import { KanbanBoardPage } from './board'
import { KANBAN_LOCALES } from './i18n'
import { $newTaskLane, useKanban } from './ui'
@@ -40,12 +40,13 @@ import { $newTaskLane, useKanban } from './ui'
// the page); hidden when nothing is in flight (or unloaded).
function KanbanCount() {
const k = useKanban()
+ const scope = useKanbanScope()
const slug = useValue($boardSlug)
// Socket-invalidated like the page (same cache); slow socketless heartbeat.
const { data: board } = useQuery({
queryFn: () => fetchBoard(false),
- queryKey: boardKey(slug, false),
+ queryKey: boardKey(scope, slug, false),
refetchInterval: 60_000
})
diff --git a/apps/desktop/src/plugins/kanban/ui.tsx b/apps/desktop/src/plugins/kanban/ui.tsx
index 626ee9f17f6..37642e2e25e 100644
--- a/apps/desktop/src/plugins/kanban/ui.tsx
+++ b/apps/desktop/src/plugins/kanban/ui.tsx
@@ -17,7 +17,7 @@ import {
} from '@hermes/plugin-sdk'
import { type ReactNode, useEffect, useState } from 'react'
-import { fetchOrchestration, ORCHESTRATION_KEY } from './api'
+import { fetchOrchestration, orchestrationKey, useKanbanScope } from './api'
import { columnLabel, useKanban } from './i18n'
import { columnMeta, type KanbanTask } from './types'
@@ -34,7 +34,9 @@ export const $newTaskLane = atom<null | string>(null)
/** Orchestration knobs (cached app-wide; the settings panel invalidates). */
export function useOrchestration() {
- return useQuery({ queryKey: ORCHESTRATION_KEY, queryFn: fetchOrchestration, staleTime: 60_000 }).data
+ const scope = useKanbanScope()
+
+ return useQuery({ queryKey: orchestrationKey(scope), queryFn: fetchOrchestration, staleTime: 60_000 }).data
}
/** The dispatcher's configured fallback for unassigned ready cards