diff --git a/src/client/FileTree.tsx b/src/client/FileTree.tsx index 0fb753a4..5f9f28f1 100644 --- a/src/client/FileTree.tsx +++ b/src/client/FileTree.tsx @@ -49,7 +49,8 @@ import { } from '@deepseek-ai/dsh-client-ui-primitives' import { SiCursor, SiZedindustries } from 'react-icons/si' import { VscFolderOpened, VscLinkExternal, VscPin, VscPinned } from 'react-icons/vsc' -import { api, archiveBuild, archiveDownloadUrl, archiveStatus, downloadUrl, type FsEntry } from './api.ts' +import { api, archiveBuild, archiveDownloadUrl, archiveStatus, downloadUrl, type FsEntry, type FsLevel } from './api.ts' +import { FS_TREES_MAX_PATHS } from '../fs-batch.ts' import { builtinFileIcon, builtinFolderIcon } from './file-icons.tsx' import { IconUploadOutline16, IconVscode16 } from './icons.tsx' import { isImeComposition } from './ime-guard.ts' @@ -675,33 +676,56 @@ export function FileTree(props: { // fresh one arrives: no blank frame, and a failed refresh degrades to the // previous listing plus a hint instead of an empty tree. if (!force) for (const path of wanted) storeLevel(path, {}) + // The host rows ONE `fs.trees` request at FS_TREES_MAX_PATHS and refuses a + // larger one OUTRIGHT (`too many paths`), which used to blank the WHOLE + // tree for a session whose persisted expansion set reached the cap. Split + // the visible set into cap-sized batches and keep the per-level error + // identity the batch route is built around: a batch that fails marks only + // the levels it carried. + const batches: string[][] = [] + for (let index = 0; index < wanted.length; index += FS_TREES_MAX_PATHS) { + batches.push(wanted.slice(index, index + FS_TREES_MAX_PATHS)) + } // The route takes no signal: the GENERATION counter is the staleness guard // (a refresh tick or an unmount bumps it, so a late answer is dropped). - api.fsTrees({ sessionId, cwd }, wanted).then((result) => { + type BatchOutcome = + | { batch: string[]; ok: true; levels: FsLevel[] } + | { batch: string[]; ok: false; message: string } + void Promise.all(batches.map(async (batch): Promise => { + try { + return { batch, ok: true, levels: (await api.fsTrees({ sessionId, cwd }, batch)).levels } + } catch (error: unknown) { + return { batch, ok: false, message: error instanceof Error ? error.message : String(error) } + } + })).then((settled) => { if (generation !== generationRef.current) return setLoadError(null) - for (const level of result.levels) { - storeLevel(level.path, { - entries: level.entries, - truncated: level.truncated, - ...(level.error !== undefined ? { error: level.error } : {}), - }) - } - }).catch((error: unknown) => { - if (generation !== generationRef.current) return - const message = error instanceof Error ? error.message : String(error) + let failure = '' let keptListing = false - for (const path of wanted) { - const level = dataRef.current[path] - if (level?.entries !== undefined) { - keptListing = true + for (const outcome of settled) { + if (outcome.ok) { + for (const level of outcome.levels) { + storeLevel(level.path, { + entries: level.entries, + truncated: level.truncated, + ...(level.error !== undefined ? { error: level.error } : {}), + }) + } continue } - storeLevel(path, { error: message }) + if (failure === '') failure = outcome.message + for (const path of outcome.batch) { + const level = dataRef.current[path] + if (level?.entries !== undefined) { + keptListing = true + continue + } + storeLevel(path, { error: outcome.message }) + } } // Levels that had a listing keep it; the hint explains why they are not // fresher. With nothing to keep, the per-level rows already say it. - setLoadError(keptListing ? message : null) + if (failure !== '') setLoadError(keptListing ? failure : null) }) }, [sessionId, cwd, storeLevel]) diff --git a/src/fs-batch.ts b/src/fs-batch.ts new file mode 100644 index 00000000..8992f3c8 --- /dev/null +++ b/src/fs-batch.ts @@ -0,0 +1,10 @@ +/** + * Row bound of ONE `fs.trees` batch request. + * + * Shared by both halves so they cannot drift: the host refuses a larger batch + * outright (`too many paths (max N)`), and the client splits the visible set on + * the same number — a session whose persisted expansion set reaches the cap + * used to blank its WHOLE file tree on every mount/refresh. Kept free of + * runtime dependencies so the browser bundle pulls nothing in with it. + */ +export const FS_TREES_MAX_PATHS = 64 diff --git a/src/index.ts b/src/index.ts index ab5bbf00..bdd7d83d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,6 +51,9 @@ import { buildSidechatApi } from './sidechat-routes.ts' import { createAssistantLiveBuffer, type AssistantLiveBuffer } from './assistant-live.ts' import { readJsonBody, requireString, SidebarError, writeError, writeJson, writeOk } from './wire.ts' import { readPersistedSession } from './session-store.ts' +// Shared with the client's batch splitter: the two halves must agree on the +// row bound, so it lives in a dependency-free module both can import. +import { FS_TREES_MAX_PATHS } from './fs-batch.ts' export { Config } export type { SidebarConfig, ResolvedSidebarConfig } @@ -76,8 +79,9 @@ export { archiveNameOf, collectZipEntries, contentDispositionOf, disambiguateArc /** Plugin identity for cordis.yml rows. */ export const name = 'dsh-better-sidebar' -/** Row bound of one `fs.trees` batch request (a mount/refresh sends what it shows). */ -export const FS_TREES_MAX_PATHS = 64 +/** Row bound of one `fs.trees` batch request (a mount/refresh sends what it + * shows, split into cap-sized batches by the client — see `fs-batch.ts`). */ +export { FS_TREES_MAX_PATHS } /** One level of an `fs.trees` batch: either a listing or that level's failure. */ export interface SidebarFsLevel { diff --git a/tests/file-tree-batch-load.spec.tsx b/tests/file-tree-batch-load.spec.tsx index c9e11c44..d204fe5e 100644 --- a/tests/file-tree-batch-load.spec.tsx +++ b/tests/file-tree-batch-load.spec.tsx @@ -19,6 +19,7 @@ import { createElement } from 'react' import { createRoot, type Root } from 'react-dom/client' import { act } from 'react-dom/test-utils' import { FileTree } from '../src/client/FileTree.tsx' +import { FS_TREES_MAX_PATHS } from '../src/fs-batch.ts' import { setupReactAct } from './test-utils.ts' setupReactAct() @@ -167,6 +168,50 @@ describe('FileTree level loading is batched', () => { expect(harness.container.textContent).toContain('d8-file.ts') }) + it('splits a visible set larger than the host cap into cap-sized batches', async () => { + // The expansion set persists per session, so a heavy session can cross the + // host's row bound. ONE oversized request used to be refused outright + // (`too many paths (max 64)`) and every level fell back to an error row; + // the tree now splits on the same number the host enforces. + const many = Array.from({ length: FS_TREES_MAX_PATHS + 6 }, (_, index) => `/tmp/d${index + 1}`) + harness = mountTree(many) + await flush() + + expect(fsTrees).toHaveBeenCalledTimes(2) + const sizes = fsTrees.mock.calls.map(call => (call[1] as string[]).length).sort((a, b) => a - b) + expect(sizes).toEqual([6 + 1, FS_TREES_MAX_PATHS]) + // Every visible path is asked for exactly once across the batches. + expect(fsTrees.mock.calls.flatMap(call => call[1] as string[]).sort()).toEqual(['/tmp', ...many].sort()) + // …and the tree renders instead of collapsing into error rows. + expect(harness.container.textContent).toContain('d1-file.ts') + expect(harness.container.textContent).not.toContain('too many paths') + }) + + it('keeps one failed batch from blanking the levels another batch carried', async () => { + // Batching must not cost the route's per-level error identity: a batch that + // fails marks only ITS paths, never the whole visible set. + const many = Array.from({ length: FS_TREES_MAX_PATHS + 6 }, (_, index) => `/tmp/d${index + 1}`) + fsTrees.mockImplementation(async (_scope: unknown, paths: readonly string[]) => { + // The trailing (small) batch fails; the leading one keeps working. + if (paths.length < 32) throw new Error('too many paths (max 64)') + return { + levels: paths.map(path => path === '/tmp' + ? { + path, + entries: many.map(dir => ({ name: dir.slice('/tmp/'.length), path: dir, isDir: true })), + truncated: false, + } + : levelOf(path)), + } + }) + harness = mountTree(many) + await flush() + // The levels the leading batch carried are NOT marked as failed… + expect(harness.container.textContent).toContain('d1-file.ts') + // …while the failed batch's own levels say why they are missing. + expect(harness.container.textContent).toContain('too many paths') + }) + it('lists only the newly expanded directory (batch of 1)', async () => { harness = mountTree(DIRS) await flush()