Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

fix(server): memory files keep one order whatever locale the server runs under (#608)

Yaowei Zheng8276f296ef7d
diff --git a/changelog/unreleased/2026-09-04-memory-file-order-locale.md b/changelog/unreleased/2026-09-04-memory-file-order-locale.md
new file mode 100644
index 00000000..0c88b8c1
--- /dev/null
+++ b/changelog/unreleased/2026-09-04-memory-file-order-locale.md
@@ -0,0 +1,10 @@
+# Memory files keep one order whatever locale the server runs under
+
+- **Date:** 2026-09-04
+- **Type:** fix
+- **Scope:** `server`
+- **PR:** [#608](https://github.com/Prism-Shadow/penguin-harness/pull/608)
+
+[中文版](2026-09-04-memory-file-order-locale.zh.md)
+
+The Memory service sorted a scope's topic files with `String.localeCompare` called without a locale argument, so the order followed the locale of the server process: a scope holding `testing-conventions.md` and `项目背景.md` listed them in that order on an `en_US.UTF-8` host and reversed on a `zh_CN.UTF-8` one. That order is what the Memory file list shows and what the `files` array of an exported scope document carries, so the same scope exported to two different hosts produced two different documents. The comparator now folds case and breaks ties on code units — total, identical on every host, and reproducing the order `en_US.UTF-8` already produced, so existing exports do not reorder.
diff --git a/changelog/unreleased/2026-09-04-memory-file-order-locale.zh.md b/changelog/unreleased/2026-09-04-memory-file-order-locale.zh.md
new file mode 100644
index 00000000..3ed3e8e0
--- /dev/null
+++ b/changelog/unreleased/2026-09-04-memory-file-order-locale.zh.md
@@ -0,0 +1,10 @@
+# 记忆文件的顺序不再取决于服务端所在的 locale
+
+- **Date:** 2026-09-04
+- **Type:** fix
+- **Scope:** `server`
+- **PR:** [#608](https://github.com/Prism-Shadow/penguin-harness/pull/608)
+
+[English](2026-09-04-memory-file-order-locale.md)
+
+Memory 服务此前用不带 locale 参数的 `String.localeCompare` 排序一个作用域的主题文件,顺序因而随服务端进程的 locale 而变:同时存有 `testing-conventions.md` 与 `项目背景.md` 的作用域,在 `en_US.UTF-8` 主机上按此顺序列出,在 `zh_CN.UTF-8` 主机上则相反。这个顺序既是 Memory 文件列表所呈现的,也是导出文档 `files` 数组所携带的,同一个作用域在两台主机上导出会得到两份不同的文档。现在的比较器先折叠大小写、再以码元顺序断同,全序、在任何主机上一致,且复现了 `en_US.UTF-8` 原本的顺序,已有的导出不会重排。
diff --git a/packages/server/src/services/memory-service.ts b/packages/server/src/services/memory-service.ts
index 34ac7494..e8dc9cef 100644
--- a/packages/server/src/services/memory-service.ts
+++ b/packages/server/src/services/memory-service.ts
@@ -115,6 +115,22 @@ export function isTopicFileName(name: string): boolean {
);
}
+/**
+ * Orders topic file names identically on every host. `localeCompare` is deliberately not used
+ * here: with no locale argument it follows the server's own locale, so the same scope listed its
+ * files one way under `en_US.UTF-8` and another under `zh_CN.UTF-8` — and that order is what an
+ * export document carries. Case-folded first, so `Alpha.md` still sorts between `a.md` and
+ * `beta.md` rather than jumping ahead of both as raw code units would; ties then fall back to
+ * code units so the order is total. `toLowerCase`, never `toLocaleLowerCase` — the locale-aware
+ * one would put the dependency straight back.
+ */
+function compareTopicFileNames(a: string, b: string): number {
+ const foldedA = a.toLowerCase();
+ const foldedB = b.toLowerCase();
+ if (foldedA !== foldedB) return foldedA < foldedB ? -1 : 1;
+ return a < b ? -1 : a > b ? 1 : 0;
+}
+
/**
* Validates a transfer document on its way to the server's disk.
*
@@ -301,7 +317,7 @@ export class MemoryService {
return (await fs.readdir(dir, { withFileTypes: true }))
.filter((e) => e.isFile() && isTopicFileName(e.name))
.map((e) => e.name)
- .sort((a, b) => a.localeCompare(b));
+ .sort(compareTopicFileNames);
} catch {
return [];
}