The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: serialize MCP log polling
Xipongd93988233c10
diff --git a/apps/desktop/src/app/skills/mcp-tab.tsx b/apps/desktop/src/app/skills/mcp-tab.tsxindex 4f984a4d4f5..263770a5193 100644--- a/apps/desktop/src/app/skills/mcp-tab.tsx+++ b/apps/desktop/src/app/skills/mcp-tab.tsx@@ -31,6 +31,7 @@ import {testMcpServer} from '@/hermes'import { type Translations, useI18n } from '@/i18n'+import { createSerialTask, startCompletionPoll } from '@/lib/completion-poll'import { compactNumber } from '@/lib/format'import { brandFor } from '@/lib/mcp-brands'import { estimateServerTokens, serverUsageCount } from '@/lib/mcp-cost'@@ -1728,34 +1729,28 @@ function McpLogs({// the active profile tears down the old poll (its `cancelled` flag blocks a// late setLines) so profile A's logs never flash in B.const activeProfile = useStore($activeGatewayProfile)+ const readLogs = useMemo(() => createSerialTask(getLogs), [])useEffect(() => {- let cancelled = false+ setLines(null)- const poll = async () => {- try {+ return startCompletionPoll({+ delayMs: LOG_POLL_MS,+ poll: async signal => {const response =source === 'stdio'- ? await getLogs({ file: 'mcp', lines: 500 })- : await getLogs({ file: 'agent', lines: 300, search: server ?? 'mcp' })+ ? await readLogs({ file: 'mcp', lines: 500 }, signal)+ : await readLogs({ file: 'agent', lines: 300, search: server ?? 'mcp' }, signal)- if (!cancelled) {- setLines(source === 'stdio' && server ? filterStdioSections(response.lines, server) : response.lines)+ if (signal.aborted) {+ throw new DOMException('Polling stopped', 'AbortError')}- } catch {- // Backend momentarily unavailable — keep the last tail.- }- }- setLines(null)- void poll()- const timer = window.setInterval(() => void poll(), LOG_POLL_MS)-- return () => {- cancelled = true- window.clearInterval(timer)- }- }, [server, source, activeProfile])+ return source === 'stdio' && server ? filterStdioSections(response.lines, server) : response.lines+ },+ publish: setLines+ })+ }, [server, source, activeProfile, readLogs])return <LogTail emptyLabel={emptyLabel} lines={lines} />}diff --git a/apps/desktop/src/lib/completion-poll.test.ts b/apps/desktop/src/lib/completion-poll.test.tsnew file mode 100644index 00000000000..62d5877e532--- /dev/null+++ b/apps/desktop/src/lib/completion-poll.test.ts@@ -0,0 +1,79 @@+import { afterEach, describe, expect, it, vi } from 'vitest'++import { createSerialTask, startCompletionPoll } from './completion-poll'++function deferred<T>() {+ let resolve!: (value: T) => void++ const promise = new Promise<T>(done => {+ resolve = done+ })++ return { promise, resolve }+}++describe('startCompletionPoll', () => {+ afterEach(() => {+ vi.useRealTimers()+ })++ it('keeps a slow producer single-flight and resumes cadence after completion', async () => {+ vi.useFakeTimers()+ const first = deferred<string>()+ const poll = vi.fn(() => first.promise)+ const publish = vi.fn()++ const stop = startCompletionPoll({ delayMs: 2_000, poll, publish })+ await vi.advanceTimersByTimeAsync(20_000)+ expect(poll).toHaveBeenCalledTimes(1)++ first.resolve('tail')+ await Promise.resolve()+ expect(publish).toHaveBeenCalledWith('tail')+ await vi.advanceTimersByTimeAsync(1_999)+ expect(poll).toHaveBeenCalledTimes(1)+ await vi.advanceTimersByTimeAsync(1)+ expect(poll).toHaveBeenCalledTimes(2)+ stop()+ })++ it('cancels stale publication and serializes a replacement lifecycle behind the active request', async () => {+ vi.useFakeTimers()+ const first = deferred<string>()+ const second = deferred<string>()+ const producer = vi.fn((profile: string) => (profile === 'A' ? first.promise : second.promise))+ const serial = createSerialTask(producer)+ const publishA = vi.fn()+ const publishB = vi.fn()++ const stopA = startCompletionPoll({+ delayMs: 2_000,+ poll: signal => serial('A', signal),+ publish: publishA+ })++ await Promise.resolve()+ stopA()++ const stopB = startCompletionPoll({+ delayMs: 2_000,+ poll: signal => serial('B', signal),+ publish: publishB+ })++ await Promise.resolve()+ expect(producer).toHaveBeenCalledTimes(1)++ first.resolve('stale')+ await vi.advanceTimersByTimeAsync(0)+ expect(producer).toHaveBeenCalledTimes(2)+ expect(publishA).not.toHaveBeenCalled()++ second.resolve('fresh')+ await vi.advanceTimersByTimeAsync(0)+ expect(publishB).toHaveBeenCalledWith('fresh')++ stopB()+ expect(vi.getTimerCount()).toBe(0)+ })+})diff --git a/apps/desktop/src/lib/completion-poll.ts b/apps/desktop/src/lib/completion-poll.tsnew file mode 100644index 00000000000..1822cb04ae5--- /dev/null+++ b/apps/desktop/src/lib/completion-poll.ts@@ -0,0 +1,61 @@+interface CompletionPollOptions<T> {+ delayMs: number+ poll: (signal: AbortSignal) => Promise<T>+ publish: (value: T) => void+}++/** Serialize a producer across effect lifecycles without publishing stale work. */+export function createSerialTask<Args, Result>(+ task: (args: Args) => Promise<Result>+): (args: Args, signal: AbortSignal) => Promise<Result> {+ let tail = Promise.resolve()++ return (args, signal) => {+ const result = tail.then(() => {+ if (signal.aborted) {+ throw new DOMException('Task stopped', 'AbortError')+ }++ return task(args)+ })++ tail = result.then(+ () => undefined,+ () => undefined+ )++ return result+ }+}++/** Poll immediately, then wait one full cadence after each request settles. */+export function startCompletionPoll<T>({ delayMs, poll, publish }: CompletionPollOptions<T>): () => void {+ const controller = new AbortController()+ let timer: number | undefined++ const run = async () => {+ try {+ const value = await poll(controller.signal)++ if (!controller.signal.aborted) {+ publish(value)+ }+ } catch {+ // A transient producer failure keeps the previous value visible.+ }++ if (!controller.signal.aborted) {+ timer = window.setTimeout(() => void run(), delayMs)+ }+ }++ void run()++ return () => {+ controller.abort()++ if (timer !== undefined) {+ window.clearTimeout(timer)+ }+ }+}