Skip to content

The agent that grows with you

fix(desktop): Maintenance tails a second run of the same op

John Paul Solivac129d9897be8
diff --git a/apps/desktop/src/app/command-center/maintenance.test.tsx b/apps/desktop/src/app/command-center/maintenance.test.tsx
new file mode 100644
index 00000000000..adc66aab835
--- /dev/null
+++ b/apps/desktop/src/app/command-center/maintenance.test.tsx
@@ -0,0 +1,84 @@
+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import type * as HermesApi from '@/hermes'
+import { getActionStatus } from '@/hermes'
+import { $desktopActionTasks } from '@/store/activity'
+
+import { MaintenancePanel } from './maintenance'
+
+// The backend spawns each op under one fixed action name ('doctor', 'security-audit',
+// 'backup', 'curator-run'), a re-spawn replaces the record under that name, and
+// /api/actions/<name>/status reports the latest run. The fake keeps that contract.
+const runs: Record<string, number> = {}
+const running: Record<string, boolean> = {}
+let nextRunStaysRunning = false
+
+function spawn(name: string) {
+ runs[name] = (runs[name] ?? 0) + 1
+ running[name] = nextRunStaysRunning
+
+ return Promise.resolve({ name, ok: true, pid: 1000 + runs[name] })
+}
+
+vi.mock('@/hermes', async importOriginal => ({
+ ...(await importOriginal<typeof HermesApi>()),
+ getActionStatus: vi.fn(async (name: string) => ({
+ exit_code: running[name] ? null : 0,
+ lines: [`${name} run ${runs[name]} output`],
+ name,
+ pid: 1000 + runs[name],
+ running: running[name]
+ })),
+ getCuratorStatus: vi.fn(() => new Promise(() => {})),
+ getMemoryStatus: vi.fn(() => new Promise(() => {})),
+ runDoctor: vi.fn(() => spawn('doctor')),
+ runSecurityAudit: vi.fn(() => spawn('security-audit'))
+}))
+
+beforeEach(() => {
+ for (const key of Object.keys(runs)) {
+ delete runs[key]
+ delete running[key]
+ }
+
+ nextRunStaysRunning = false
+ $desktopActionTasks.set({})
+ vi.mocked(getActionStatus).mockClear()
+})
+
+afterEach(cleanup)
+
+const button = (name: string) => screen.getByRole('button', { name }) as HTMLButtonElement
+
+describe('MaintenancePanel action tail', () => {
+ it('tails a second run of the same op', async () => {
+ render(<MaintenancePanel />)
+
+ await act(async () => void fireEvent.click(button('Run doctor')))
+ await screen.findByText('doctor run 1 output')
+
+ nextRunStaysRunning = true
+ await act(async () => void fireEvent.click(button('Run doctor')))
+
+ await screen.findByText('doctor run 2 output')
+ expect(vi.mocked(getActionStatus)).toHaveBeenCalledTimes(2)
+ expect(screen.getByText('Running...')).toBeTruthy()
+ expect(button('Run doctor').disabled).toBe(true)
+ expect($desktopActionTasks.get().doctor?.status).toMatchObject({ pid: 1002, running: true })
+ })
+
+ it('tails a different op launched after the first', async () => {
+ render(<MaintenancePanel />)
+
+ await act(async () => void fireEvent.click(button('Run doctor')))
+ await screen.findByText('doctor run 1 output')
+
+ nextRunStaysRunning = true
+ await act(async () => void fireEvent.click(button('Security audit')))
+
+ await screen.findByText('security-audit run 1 output')
+ expect(vi.mocked(getActionStatus)).toHaveBeenLastCalledWith('security-audit', 200)
+ expect(button('Security audit').disabled).toBe(true)
+ })
+})
diff --git a/apps/desktop/src/app/command-center/maintenance.tsx b/apps/desktop/src/app/command-center/maintenance.tsx
index d603bfc7fdd..a7da30487f1 100644
--- a/apps/desktop/src/app/command-center/maintenance.tsx
+++ b/apps/desktop/src/app/command-center/maintenance.tsx
@@ -54,7 +54,9 @@ export function MaintenancePanel() {
const { t } = useI18n()
const mm = t.commandCenter.maintenance
- const [actionName, setActionName] = useState<null | string>(null)
+ // A fresh object per launch: the backend reuses one fixed name per op, so a bare name
+ // would leave the tail effect's deps unchanged when the same op runs again.
+ const [tailTarget, setTailTarget] = useState<null | { name: string }>(null)
const [actionStatus, setActionStatus] = useState<ActionStatusResponse | null>(null)
const [curator, setCurator] = useState<CuratorStatusResponse | null>(null)
const [curatorBusy, setCuratorBusy] = useState(false)
@@ -79,17 +81,18 @@ export function MaintenancePanel() {
// Tail the most recently launched spawn action.
useEffect(() => {
- if (!actionName) {
+ if (!tailTarget) {
return
}
+ const { name } = tailTarget
let cancelled = false
let polls = 0
let timer: null | number = null
const poll = async () => {
try {
- const status = await getActionStatus(actionName, 200)
+ const status = await getActionStatus(name, 200)
if (cancelled) {
return
@@ -116,7 +119,7 @@ export function MaintenancePanel() {
window.clearTimeout(timer)
}
}
- }, [actionName])
+ }, [tailTarget])
const launch = useCallback(
async (label: string, start: () => Promise<ActionResponse>) => {
@@ -125,7 +128,7 @@ export function MaintenancePanel() {
try {
const started = await start()
setActionStatus(null)
- setActionName(started.name)
+ setTailTarget({ name: started.name })
notify({ kind: 'success', title: mm.actionStarted(label), message: '' })
} catch (err) {
setError(err instanceof Error ? err.message : String(err))