Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(file-browser): sort numbered entries naturally and update SSH daemon

DavidShen581d588dd8ed
diff --git a/apps/desktop/src/main/file-browser/__tests__/remoteFileBrowser.test.ts b/apps/desktop/src/main/file-browser/__tests__/remoteFileBrowser.test.ts
index ac0cbd79bb..9db827859d 100644
--- a/apps/desktop/src/main/file-browser/__tests__/remoteFileBrowser.test.ts
+++ b/apps/desktop/src/main/file-browser/__tests__/remoteFileBrowser.test.ts
@@ -191,18 +191,25 @@ describe('RemoteFileBrowserManager', () => {
await mgr.disposeAll();
});
- it('stale bundle (schema compatible) → reinstalls then serves', async () => {
- // schema 相等但 bundle 落后:daemon 行为修复只 bump bundle 版本,必须触发重推。
+ it('upgrades the pre-natural-sort daemon and returns naturally ordered entries', async () => {
+ // #7064, unit: the previous generic stale-version fixture did not catch a
+ // missing bundle bump. 0.2.6 is the released daemon with lexical sorting.
+ await fsWriteFile(path.join(workdir, '3.md'), 'three');
+ await fsWriteFile(path.join(workdir, '10.md'), 'ten');
const { deps, calls } = makeDeps({
probeResults: [
- { ...READY_PROBE, bundleVersion: '0.0.0-stale' },
+ { ...READY_PROBE, bundleVersion: '0.2.6' },
READY_PROBE,
],
});
const mgr = new RemoteFileBrowserManager(deps);
- await mgr.request('h1', 'stat', { workdir, relPath: 'hello.txt' });
- expect(calls.install).toBe(1);
- await mgr.disposeAll();
+ try {
+ const { entries } = await mgr.request('h1', 'listDir', { workdir });
+ expect(calls.install).toBe(1);
+ expect(entries.map(entry => entry.name)).toEqual(['3.md', '10.md', 'hello.txt']);
+ } finally {
+ await mgr.disposeAll();
+ }
});
it('node missing → NODE_MISSING error, nothing spawned', async () => {
diff --git a/packages/file-browser-core/src/__tests__/completeDirectory.test.ts b/packages/file-browser-core/src/__tests__/completeDirectory.test.ts
index 574929b72a..f09ea0660b 100644
--- a/packages/file-browser-core/src/__tests__/completeDirectory.test.ts
+++ b/packages/file-browser-core/src/__tests__/completeDirectory.test.ts
@@ -1,5 +1,6 @@
-import { afterEach, expect, it } from 'vitest';
+import { afterEach, expect, it, vi } from 'vitest';
import fs from 'node:fs/promises';
+import type { Dirent } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { listDir } from '../scanner';
@@ -35,3 +36,41 @@ it('retains realpath boundaries when listing without presentation filters', asyn
expect(await listDir(root, '')).toEqual([]);
await expect(listDir(root, 'link')).rejects.toThrow('escapes');
});
+
+// #7064: exercise listDir's returned order, not a copied comparator. Existing
+// fixtures only cover alphabetic names; 3.md / 10.md is the minimal failure.
+// Unit tier: small real directories, no subprocess or external service.
+it('orders numeric filename segments naturally with directories first', async () => {
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'complete-dir-'));
+ roots.push(root);
+ for (const name of ['Chapter10', 'chapter2']) await fs.mkdir(path.join(root, name));
+ for (const name of ['01.md', '02.md', '3.md', '10.md', '29.md', '30.md', 'part2-10.md', 'Part2-3.md']) {
+ await fs.writeFile(path.join(root, name), 'fixture');
+ }
+ expect((await listDir(root, '')).map(entry => entry.name)).toEqual([
+ 'chapter2', 'Chapter10', '01.md', '02.md', '3.md', '10.md', '29.md', '30.md',
+ 'Part2-3.md', 'part2-10.md',
+ ]);
+});
+
+// #7064 compatibility: numeric ties and nonnumeric names must retain legacy
+// locale ordering. Reverse enumeration exposes lost distinctions (1 / 01 and
+// a / full-width A) that ordinary readdir order could accidentally conceal.
+it.each([
+ ['equal numeric values', ['1.md', '001.md', '01.md']],
+ ['text, accents and character width', ['README.md', 'apple.md', 'a.md', 'A.md', 'á.md', 'zebra.md']],
+])('preserves legacy ordering for %s', async (_label, names) => {
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'complete-dir-'));
+ roots.push(root);
+ for (const name of names) await fs.writeFile(path.join(root, name), 'fixture');
+ const expected = [...names].sort((a, b) => a.toLocaleLowerCase().localeCompare(b.toLocaleLowerCase()));
+ const entries = await fs.readdir(root, { withFileTypes: true });
+ entries.sort((a, b) => expected.indexOf(b.name) - expected.indexOf(a.name));
+ const reader: { readdir(dir: string, options: { withFileTypes: true }): Promise<Dirent[]> } = fs;
+ const readDir = vi.spyOn(reader, 'readdir').mockResolvedValueOnce(entries);
+ try {
+ expect((await listDir(root, '')).map(entry => entry.name)).toEqual(expected);
+ } finally {
+ readDir.mockRestore();
+ }
+});
diff --git a/packages/file-browser-core/src/scanner.ts b/packages/file-browser-core/src/scanner.ts
index a8b077bca1..3bd1af9a9b 100644
--- a/packages/file-browser-core/src/scanner.ts
+++ b/packages/file-browser-core/src/scanner.ts
@@ -21,6 +21,9 @@ import { loadIgnoreMatcher, type Matcher } from './ignore.js';
const log = scopedLogger('file-browser/scanner');
+// Reuse the locale-aware comparator across lazy directory expansions.
+const entryNameCollator = new Intl.Collator(undefined, { numeric: true });
+
/** Hard cap on file size we'll send to the renderer. Anything larger gets
* truncated; renderer shows a "file truncated, open in OS to see more"
* banner. 2 MiB is enough for any realistic markdown/code file. */
@@ -282,8 +285,7 @@ async function hasDocDescendant(
* List entries of one directory (single-layer, no recursion). Filtered
* through an optional presentation matcher. Without a matcher all ordinary
* files are listed, including hidden files and build outputs. Sort: directories before files,
- * each group case-insensitive lexicographic — same convention as VSCode
- * Explorer / Obsidian.
+ * each group case-insensitive natural order (chapter2 before chapter10).
*/
export async function listDir(
workdir: string,
@@ -354,10 +356,13 @@ export async function listDir(
);
const out: DirEntry[] = candidates.filter((e): e is DirEntry => e !== null);
- // dirs first (case-insensitive a-z), then files (same)
+ // Directories first, then natural name order within each group.
out.sort((a, b) => {
if (a.type !== b.type) return a.type === 'directory' ? -1 : 1;
- return a.name.toLocaleLowerCase().localeCompare(b.name.toLocaleLowerCase());
+ const aName = a.name.toLocaleLowerCase();
+ const bName = b.name.toLocaleLowerCase();
+ // Keep the legacy text order for numeric ties such as 001, 01 and 1.
+ return entryNameCollator.compare(aName, bName) || aName.localeCompare(bName);
});
return out;
diff --git a/packages/remote-file-service/src/protocol.ts b/packages/remote-file-service/src/protocol.ts
index 71a85c0ddf..17d60dddcc 100644
--- a/packages/remote-file-service/src/protocol.ts
+++ b/packages/remote-file-service/src/protocol.ts
@@ -10,8 +10,8 @@
* - `FILE_SERVICE_SCHEMA_VERSION` 是协议兼容性判据。client handshake 时带上
* 自己的版本,不相等即视为不兼容 → 上层重推 bundle(bundle ~几百 KB,重推
* 是秒级操作,所以不做向后兼容协商,严格相等最简单)。
- * - `FILE_SERVICE_BUNDLE_VERSION` 是人读的 bundle 版本,手动 bump,probe /
- * 日志用,不参与兼容性判断。
+ * - `FILE_SERVICE_BUNDLE_VERSION` 标识实现版本,行为变化时手动 bump。
+ * Desktop 会在 probe 的 bundle 版本不匹配时重推,即使 schema 兼容。
*
* 帧类型三种,全部单行 JSON:
* - Request (client → daemon): { id, method, params }
@@ -35,8 +35,8 @@ import type {
/** 协议兼容版本:client 与 daemon 严格相等才可用。改动任何请求/响应形状时 +1。 */
export const FILE_SERVICE_SCHEMA_VERSION = 3;
-/** 人读 bundle 版本(probe / 日志用),行为变化时手动 bump。 */
-export const FILE_SERVICE_BUNDLE_VERSION = '0.2.6';
+/** 实现版本;含共享扫描器在内的行为变化需 bump,使已安装的远端自动更新。 */
+export const FILE_SERVICE_BUNDLE_VERSION = '0.2.7';
/* ============================== 帧 ============================== */