mirror of
https://github.com/stablyai/orca.git
synced 2026-10-23 16:03:49 +00:00
* batch remote watcher events and defer File Explorer refreshes Remote filesystem watcher events now batch with the shared 150ms trailing and 500ms max-wait window, coalescing per-path like local events. File Explorer tree and directory refreshes are scheduled with debounce and transport-aware concurrency caps (16 local, 8 runtime, 4 SSH). Stale directory cache tracking prevents trusting collapsed listings skipped by full refresh; they are re-read on re-expansion. Relay implements a 15-minute idle-only grace cap for zero-PTY relays via PTY pool lifecycle tracking, independent of explicitly configured grace time. * fix(watch/relay): bound remote watcher fan-out and read the live relay grace Three P1 fixes from the SSH/remote freeze audit: - Remote watchers now debounce on the same 150/500 window as local ones (finding D), and every teardown path drops the trailing flush timer instead of letting it fire into a dead watch. The deferred send is wrapped so a frame disposed mid-window can't escape as a fatal main-process exception. - File Explorer refreshes are scheduled and concurrency-capped rather than fanned out unbounded over expanded dirs (finding C). Local transports use a zero window, since main already coalesced the burst. - relay.startGrace reads ptyHandler.configuredGraceTimeMs instead of the launch-time argv closure, so a grace raised after launch is honored. The branch selection moves to relay-grace-branch.ts because relay.ts has no exports and calls main() at import, making it untestable. Consequence: a host-sleep relay holding zero PTYs now exits after the idle cap. Pinned by test and documented in docs/reference/relay-grace-time-reconfiguration.md. Also drops the duplicated 150/500/5000 constants in the runtime-RPC batcher in favor of the shared window module. * docs(relay): correct grace-reconfiguration line numbers after the relay.ts edit Co-authored-by: Orca <help@stably.ai> * refactor(file-explorer): use useMemo for paths; remove relay reference Replace manual ref-based caching with proper React hooks for content-stable path memoization. Remove outdated relay grace-time reference documentation from code review cycle. * rm design doc * fix(remote-watcher): prevent stranded timer after close An in-flight provider receive can land after the batch is torn down. Without a guard, pushing events to a closed batch would re-arm a timer that would never be cleared, stranding the task indefinitely. Track the closed state and skip pushes after close(). Relay.ts comment clarifies why pool watches remain registered during grace-period shutdown deferral — the socket server stays listening so a reconnecting client can cancel the grace and resume. --------- Co-authored-by: Orca <help@stably.ai>
177 lines
6.2 KiB
TypeScript
177 lines
6.2 KiB
TypeScript
import { join, resolve } from 'node:path'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const { handleMock } = vi.hoisted(() => ({
|
|
handleMock: vi.fn()
|
|
}))
|
|
|
|
vi.mock('electron', () => ({
|
|
ipcMain: {
|
|
handle: handleMock
|
|
}
|
|
}))
|
|
|
|
vi.mock('fs/promises', () => ({
|
|
stat: vi.fn()
|
|
}))
|
|
|
|
vi.mock('@parcel/watcher', () => ({
|
|
subscribe: vi.fn()
|
|
}))
|
|
|
|
vi.mock('./filesystem-watcher-wsl', () => ({
|
|
createWslWatcher: vi.fn()
|
|
}))
|
|
|
|
vi.mock('../providers/ssh-filesystem-dispatch', () => ({
|
|
getSshFilesystemProvider: vi.fn(),
|
|
onSshFilesystemProviderRegistered: () => () => {}
|
|
}))
|
|
|
|
import { closeAllWatchers, registerFilesystemWatcherHandlers } from './filesystem-watcher'
|
|
import { stat } from 'node:fs/promises'
|
|
import { subscribe as subscribeParcelWatcher } from '@parcel/watcher'
|
|
import type { Event as WatcherEvent } from '@parcel/watcher'
|
|
import type { FsChangedPayload } from '../../shared/types'
|
|
import {
|
|
WATCH_BATCH_MAX_WAIT_MS,
|
|
WATCH_BATCH_TRAILING_MS
|
|
} from '../../shared/filesystem-watch-batch-window'
|
|
|
|
type HandlerMap = Record<string, (_event: unknown, args: unknown) => Promise<unknown> | unknown>
|
|
|
|
describe('local filesystem watcher large batches', () => {
|
|
const handlers: HandlerMap = {}
|
|
|
|
beforeEach(async () => {
|
|
vi.useRealTimers()
|
|
handleMock.mockReset()
|
|
vi.mocked(stat).mockReset()
|
|
vi.mocked(subscribeParcelWatcher).mockReset()
|
|
for (const key of Object.keys(handlers)) {
|
|
delete handlers[key]
|
|
}
|
|
handleMock.mockImplementation((channel, handler) => {
|
|
handlers[channel] = handler
|
|
})
|
|
registerFilesystemWatcherHandlers()
|
|
await closeAllWatchers()
|
|
})
|
|
|
|
it('accepts a large local watcher event batch without overflowing V8 arguments', async () => {
|
|
vi.useFakeTimers()
|
|
vi.mocked(stat).mockResolvedValue({ isDirectory: () => true } as never)
|
|
const worktreePath = resolve('/tmp/repo')
|
|
let watcherCallback: ((err: Error | null, events: WatcherEvent[]) => void) | undefined
|
|
vi.mocked(subscribeParcelWatcher).mockImplementation(async (_root, callback) => {
|
|
watcherCallback = callback as typeof watcherCallback
|
|
return { unsubscribe: vi.fn() } as never
|
|
})
|
|
|
|
await handlers['fs:watchWorktree'](
|
|
{ sender: { isDestroyed: () => false, send: vi.fn(), once: vi.fn(), id: 1 } },
|
|
{ worktreePath }
|
|
)
|
|
|
|
const events = Array.from(
|
|
{ length: 200_000 },
|
|
(_, index): WatcherEvent => ({ type: 'delete', path: join(worktreePath, `file-${index}`) })
|
|
)
|
|
|
|
expect(() => watcherCallback?.(null, events)).not.toThrow()
|
|
await closeAllWatchers()
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
it('emits one overflow event for oversized native watcher batches', async () => {
|
|
vi.useFakeTimers()
|
|
vi.mocked(stat).mockResolvedValue({ isDirectory: () => true } as never)
|
|
let watcherCallback: ((err: Error | null, events: WatcherEvent[]) => void) | undefined
|
|
vi.mocked(subscribeParcelWatcher).mockImplementation(async (_root, callback) => {
|
|
watcherCallback = callback as typeof watcherCallback
|
|
return { unsubscribe: vi.fn() } as never
|
|
})
|
|
const worktreePath = resolve('/tmp/repo')
|
|
const sender = { isDestroyed: () => false, send: vi.fn(), once: vi.fn(), id: 1 }
|
|
|
|
await handlers['fs:watchWorktree']({ sender }, { worktreePath })
|
|
watcherCallback?.(
|
|
null,
|
|
Array.from(
|
|
{ length: 5_001 },
|
|
(_, index): WatcherEvent => ({
|
|
type: 'update',
|
|
path: join(worktreePath, `file-${index}.txt`)
|
|
})
|
|
)
|
|
)
|
|
|
|
await vi.advanceTimersByTimeAsync(150)
|
|
|
|
expect(stat).toHaveBeenCalledTimes(1)
|
|
expect(sender.send).toHaveBeenCalledWith('fs:changed', {
|
|
worktreePath,
|
|
events: [{ kind: 'overflow', absolutePath: worktreePath }]
|
|
})
|
|
await closeAllWatchers()
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
it('still coalesces a local burst on the shared trailing window', async () => {
|
|
vi.useFakeTimers()
|
|
vi.mocked(stat).mockResolvedValue({ isDirectory: () => true } as never)
|
|
let watcherCallback: ((err: Error | null, events: WatcherEvent[]) => void) | undefined
|
|
vi.mocked(subscribeParcelWatcher).mockImplementation(async (_root, callback) => {
|
|
watcherCallback = callback as typeof watcherCallback
|
|
return { unsubscribe: vi.fn() } as never
|
|
})
|
|
const worktreePath = resolve('/tmp/repo')
|
|
const filePath = join(worktreePath, 'a.ts')
|
|
const sender = { isDestroyed: () => false, send: vi.fn(), once: vi.fn(), id: 1 }
|
|
|
|
await handlers['fs:watchWorktree']({ sender }, { worktreePath })
|
|
watcherCallback?.(null, [{ type: 'update', path: filePath }])
|
|
watcherCallback?.(null, [{ type: 'update', path: filePath }])
|
|
|
|
await vi.advanceTimersByTimeAsync(WATCH_BATCH_TRAILING_MS - 1)
|
|
expect(sender.send).not.toHaveBeenCalled()
|
|
|
|
await vi.advanceTimersByTimeAsync(1)
|
|
expect(sender.send).toHaveBeenCalledTimes(1)
|
|
expect(sender.send).toHaveBeenCalledWith('fs:changed', {
|
|
worktreePath,
|
|
events: [{ kind: 'update', absolutePath: filePath, isDirectory: true }]
|
|
})
|
|
await closeAllWatchers()
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
it('still flushes a sustained local stream at the shared max wait', async () => {
|
|
vi.useFakeTimers()
|
|
vi.mocked(stat).mockResolvedValue({ isDirectory: () => true } as never)
|
|
let watcherCallback: ((err: Error | null, events: WatcherEvent[]) => void) | undefined
|
|
vi.mocked(subscribeParcelWatcher).mockImplementation(async (_root, callback) => {
|
|
watcherCallback = callback as typeof watcherCallback
|
|
return { unsubscribe: vi.fn() } as never
|
|
})
|
|
const worktreePath = resolve('/tmp/repo')
|
|
const sender = { isDestroyed: () => false, send: vi.fn(), once: vi.fn(), id: 1 }
|
|
|
|
await handlers['fs:watchWorktree']({ sender }, { worktreePath })
|
|
// Step under the trailing window so only the max wait can force a flush.
|
|
const step = 100
|
|
for (let elapsed = 0; elapsed <= WATCH_BATCH_MAX_WAIT_MS; elapsed += step) {
|
|
watcherCallback?.(null, [{ type: 'update', path: join(worktreePath, `f-${elapsed}.ts`) }])
|
|
if (elapsed < WATCH_BATCH_MAX_WAIT_MS) {
|
|
expect(sender.send).not.toHaveBeenCalled()
|
|
}
|
|
await vi.advanceTimersByTimeAsync(step)
|
|
}
|
|
|
|
expect(sender.send).toHaveBeenCalledTimes(1)
|
|
expect((sender.send.mock.calls[0][1] as FsChangedPayload).events).toHaveLength(6)
|
|
await closeAllWatchers()
|
|
vi.useRealTimers()
|
|
})
|
|
})
|