Skip to content

The agent that grows with you

fix(gateway): broadcast projects.changed so CLI-created projects surface in the desktop UI

Austin Pickett71225cd83208
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
index 35208c0db24..c1ea8bf7b26 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
@@ -4,7 +4,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { type ChatMessage, toChatMessages } from '@/lib/chat-messages'
import { createClientSessionState } from '@/lib/chat-runtime'
import { sessionMessagesSignature } from '@/lib/session-signatures'
-import { $changeEventsAvailable, notifySessionsChanged, resetLiveSync } from '@/store/live-sync'
+import { $changeEventsAvailable, notifyProjectsChanged, notifySessionsChanged, resetLiveSync } from '@/store/live-sync'
import {
$activeSessionId,
$selectedStoredSessionId,
@@ -45,11 +45,12 @@ vi.mock('@/hermes', async importOriginal => ({
vi.mock('@/store/projects', async importOriginal => ({
...(await importOriginal()),
- refreshProjectTree: vi.fn(async () => undefined)
+ refreshProjectTree: vi.fn(async () => undefined),
+ refreshProjects: vi.fn(async () => undefined)
}))
const { getLatestSessionMessages, getOlderSessionMessages } = await import('@/hermes')
-const { refreshProjectTree } = await import('@/store/projects')
+const { refreshProjectTree, refreshProjects } = await import('@/store/projects')
const ACTIVE_RUNTIME_ID = 'runtime-active'
const ACTIVE_STORED_ID = 'stored-active'
@@ -663,6 +664,17 @@ describe('active transcript refresh', () => {
await waitFor(() => expect(refreshProjectTree).toHaveBeenCalledTimes(1))
})
+
+ it('refreshes the project list + tree on a projects.changed tick — CLI-created projects surface without a manual refresh (#56757)', async () => {
+ $changeEventsAvailable.set(true)
+
+ renderSync(vi.fn(async () => undefined))
+
+ act(() => notifyProjectsChanged())
+
+ await waitFor(() => expect(refreshProjects).toHaveBeenCalledTimes(1))
+ await waitFor(() => expect(refreshProjectTree).toHaveBeenCalledTimes(1))
+ })
})
describe('reconcileActiveTranscript', () => {
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
index 8062288e938..4ffb678471c 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
@@ -14,10 +14,10 @@ import { sessionMessagesSignature } from '@/lib/session-signatures'
import { latestSessionTodos } from '@/lib/todos'
import { pendingSessionReplay } from '@/store/gateway'
import { $sidebarShowArchived } from '@/store/layout'
-import { $changeEventsAvailable, $cronChangeTick, $sessionsChangeTick } from '@/store/live-sync'
+import { $changeEventsAvailable, $cronChangeTick, $projectsChangeTick, $sessionsChangeTick } from '@/store/live-sync'
import { $onBattery, batteryPollInterval } from '@/store/power'
import { refreshActiveProfile } from '@/store/profile'
-import { refreshProjectTree } from '@/store/projects'
+import { refreshProjects, refreshProjectTree } from '@/store/projects'
import {
$activeSessionId,
$busy,
@@ -863,6 +863,7 @@ export function useBackgroundSync({
}: BackgroundSyncParams): void {
const changeEventsAvailable = useStore($changeEventsAvailable)
const cronChangeTick = useStore($cronChangeTick)
+ const projectsChangeTick = useStore($projectsChangeTick)
const activeTranscriptRefreshPendingRef = useRef<string | null>(null)
const activeTranscriptReadRef = useRef<{ sessionKey: string; preservePending: boolean } | null>(null)
// Tile reconcile state (#93942 slice 1): shared sequence guard + per-tile
@@ -1224,6 +1225,20 @@ export function useBackgroundSync({
)
}, [changeEventsAvailable, cronChangeTick, gatewayState, refreshCronJobs])
+ // Projects created or switched by CLI / agent tooling write projects.db without any
+ // state.db movement, so sessions.changed never fires and the Projects sidebar used to
+ // go stale until a manual refresh (#56757). The gateway's change watcher now
+ // broadcasts projects.changed when projects.db moves; this effect refetches the
+ // project list + tree on that tick.
+ useEffect(() => {
+ if (gatewayState !== 'open' || !changeEventsAvailable || projectsChangeTick === 0) {
+ return
+ }
+
+ void refreshProjects()
+ void refreshProjectTree()
+ }, [changeEventsAvailable, gatewayState, projectsChangeTick, refreshProjects, refreshProjectTree])
+
// Preserve the pre-existing messaging behavior: refresh once when a
// messaging transcript opens, then keep its visibility backstop. Desktop
// sessions never enter this effect and therefore gain no periodic timer.
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/lifecycle.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/lifecycle.ts
index 1c2ff50b63d..62511926a94 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/lifecycle.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/lifecycle.ts
@@ -6,6 +6,7 @@ import {
notifyPairingChanged,
notifyPetChanged,
notifyPlatformsChanged,
+ notifyProjectsChanged,
notifySessionsChanged,
notifySetupReady,
type PetChangeMeta,
@@ -63,6 +64,7 @@ export function handleLifecycleEvent(ctx: GatewayEventContext): boolean {
event.type === 'pet.changed' ||
event.type === 'cron.changed' ||
event.type === 'sessions.changed' ||
+ event.type === 'projects.changed' ||
event.type === 'platforms.changed' ||
event.type === 'pairing.changed'
) {
@@ -76,6 +78,8 @@ export function handleLifecycleEvent(ctx: GatewayEventContext): boolean {
notifyPetChanged(payload as PetChangeMeta | undefined)
} else if (event.type === 'cron.changed') {
notifyCronChanged()
+ } else if (event.type === 'projects.changed') {
+ notifyProjectsChanged()
} else if (event.type === 'platforms.changed') {
notifyPlatformsChanged()
} else if (event.type === 'pairing.changed') {
diff --git a/apps/desktop/src/store/live-sync.ts b/apps/desktop/src/store/live-sync.ts
index d06bc35c0e4..b2bdb509307 100644
--- a/apps/desktop/src/store/live-sync.ts
+++ b/apps/desktop/src/store/live-sync.ts
@@ -19,6 +19,7 @@ export const $changeEventsAvailable = atom(false)
export const $cronChangeTick = atom(0)
export const $sessionsChangeTick = atom(0)
export const $platformsChangeTick = atom(0)
+export const $projectsChangeTick = atom(0)
export const $pairingChangeTick = atom(0)
/** `pet.info.meta`-shaped payload carried on `pet.changed` — lets the pet skip
@@ -59,6 +60,10 @@ export function notifyPlatformsChanged(): void {
$platformsChangeTick.set($platformsChangeTick.get() + 1)
}
+export function notifyProjectsChanged(): void {
+ $projectsChangeTick.set($projectsChangeTick.get() + 1)
+}
+
export function notifyPairingChanged(): void {
$pairingChangeTick.set($pairingChangeTick.get() + 1)
}
diff --git a/apps/shared/src/gateway-contract.generated.ts b/apps/shared/src/gateway-contract.generated.ts
index 6fe6ef149df..a8faf4b7366 100644
--- a/apps/shared/src/gateway-contract.generated.ts
+++ b/apps/shared/src/gateway-contract.generated.ts
@@ -5513,6 +5513,8 @@ export interface BackendGatewayEventMap {
'preview.restart.complete': SideAgentCompletePayload
/** Progress line from the preview-restart agent. */
'preview.restart.progress': PreviewRestartProgressPayload
+ /** projects.db moved; refetch the Projects sidebar. */
+ 'projects.changed': ChangeSignalPayload
/** Affection reaction detected in the user's message (hearts etc.). */
reaction: ReactionPayload
/** A completed reasoning block (non-streaming providers). */
@@ -5622,6 +5624,7 @@ export const GATEWAY_EVENT_TYPES = [
'preview.open',
'preview.restart.complete',
'preview.restart.progress',
+ 'projects.changed',
'reaction',
'reasoning.available',
'reasoning.delta',
diff --git a/apps/shared/src/gateway-contract.openrpc.json b/apps/shared/src/gateway-contract.openrpc.json
index 96816140d38..6f2e1493901 100644
--- a/apps/shared/src/gateway-contract.openrpc.json
+++ b/apps/shared/src/gateway-contract.openrpc.json
@@ -38148,6 +38148,18 @@
}
]
},
+ {
+ "name": "projects.changed",
+ "summary": "projects.db moved; refetch the Projects sidebar.",
+ "params": [
+ {
+ "name": "payload",
+ "schema": {
+ "$ref": "#/components/schemas/ChangeSignalPayload"
+ }
+ }
+ ]
+ },
{
"name": "reaction",
"summary": "Affection reaction detected in the user's message (hearts etc.).",
diff --git a/tui_gateway/change_watcher.py b/tui_gateway/change_watcher.py
index 071ee77c207..9fd4636fa3b 100644
--- a/tui_gateway/change_watcher.py
+++ b/tui_gateway/change_watcher.py
@@ -198,6 +198,10 @@ _CHANGE_WATCHES: dict[str, tuple[float, Any, Any]] = {
"pet.changed": (2.0, _pet_sig, _pet_changed_payload),
"cron.changed": (1.0, lambda: _home_mtime_ns("cron", "jobs.json"), lambda: {}),
"sessions.changed": (0.5, _sessions_sig, lambda: {}),
+ # Projects created/switched by CLI or agent tooling write projects.db without any
+ # state.db movement, so sessions.changed never fires and the desktop Projects
+ # sidebar goes stale until a manual refresh (#56757).
+ "projects.changed": (1.0, lambda: _home_mtime_ns("projects.db"), lambda: {}),
"platforms.changed": (2.0, lambda: _home_mtime_ns("gateway_state.json"), lambda: {}),
"pairing.changed": (2.0, _pairing_sig, lambda: {}),
# 1s so a queued DM envelope reaches the Desktop's push-triggered drain fast.
diff --git a/tui_gateway/contracts/events.py b/tui_gateway/contracts/events.py
index 827e108fe1d..ead598759dc 100644
--- a/tui_gateway/contracts/events.py
+++ b/tui_gateway/contracts/events.py
@@ -705,6 +705,7 @@ class ChangeSignalPayload(OpenPayload):
event("cron.changed", ChangeSignalPayload, doc="cron/jobs.json moved; refetch the cron list.")
event("sessions.changed", ChangeSignalPayload, doc="state.db moved; refetch the session list.")
+event("projects.changed", ChangeSignalPayload, doc="projects.db moved; refetch the Projects sidebar.")
event("platforms.changed", ChangeSignalPayload, doc="gateway_state.json moved; refetch platform status.")
event("pairing.changed", ChangeSignalPayload, doc="Pairing state moved; refetch pairing.")
event("bot_relay.outbox.pending", ChangeSignalPayload, doc="A bot-relay outbox envelope is queued; drain it.")