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(i18n): 补 URL/邮箱标点边界、fingerprint 计数、boolean schema 与英文菜单省略号

Dashe44aafaebdd1
diff --git a/apps/desktop/src/main/applicationMenuLabels.ts b/apps/desktop/src/main/applicationMenuLabels.ts
index 935db4bc6e..ef6a684818 100644
--- a/apps/desktop/src/main/applicationMenuLabels.ts
+++ b/apps/desktop/src/main/applicationMenuLabels.ts
@@ -53,8 +53,8 @@ export const APPLICATION_MENU_LABELS: Record<ApplicationMenuLocale, ApplicationM
about: 'About {{appName}}',
hide: 'Hide {{appName}}',
quit: 'Quit {{appName}}',
- settings: 'Settings...',
- checkForUpdates: 'Check for Updates...',
+ settings: 'Settings…',
+ checkForUpdates: 'Check for Updates…',
fileMenu: 'File',
newMaker: 'New Maker',
viewMenu: 'View',
diff --git a/scripts/__tests__/glossary-rules.test.mjs b/scripts/__tests__/glossary-rules.test.mjs
index 4be2b88a95..661d4e154c 100644
--- a/scripts/__tests__/glossary-rules.test.mjs
+++ b/scripts/__tests__/glossary-rules.test.mjs
@@ -21,6 +21,8 @@ import {
occursIn,
stripNonProse,
normalizeForPunctuation,
+ countOccurrences,
+ countHalfWidthPunct,
} from '../shared/glossary-rules.mjs';
import { validateAgainstSchema } from '../shared/json-schema-lite.mjs';
import { renderGlossaryDoc } from '../shared/glossary-doc.mjs';
@@ -567,4 +569,51 @@ test('validateAgainstSchema: 关键字的值形状写错必须报错而不是被
() => validateAgainstSchema({}, { type: 'object', additionalProperties: 'yes' }),
/additionalProperties 只支持 boolean 或 schema 对象/,
);
+ // items 同属「值是 schema」的位置,同样允许 boolean——不能按 object 死判,
+ // 否则合法的 `items: false` 会被误判成 schema 写错
+ assert.throws(
+ () => validateAgainstSchema({}, { type: 'array', items: 'yes' }),
+ /items 只支持 boolean 或 schema 对象/,
+ );
+});
+
+test('normalizeForPunctuation: URL / 邮箱后紧跟半角标点 + 中文时留下正文边界', () => {
+ // TOKEN_TAIL 把逗号留在了 token 外面,但 token 换成空格后逗号前就是空格,
+ // findHalfWidthPunct 认不出左边界,违规照样漏
+ assert.equal(findHalfWidthPunct(normalizeForPunctuation('请访问 https://x.test,返回对话列表')), ',');
+ assert.equal(findHalfWidthPunct(normalizeForPunctuation('联系 [email protected],重启后重试')), ',');
+ assert.equal(findHalfWidthPunct(normalizeForPunctuation('联系 [email protected];然后重启')), ';');
+ // 不能误伤 URL 自身的 query string / 端口号 / 路径冒号
+ for (const t of [
+ '见 https://a.test/x?ids=1,2&y=3 页面',
+ '详见 https://a.example/b:8080 页面',
+ '详见 config.json:12 行',
+ '打开 https://a.test/p 查看',
+ '邮件发到 [email protected] 即可',
+ ]) {
+ assert.equal(findHalfWidthPunct(normalizeForPunctuation(t)), null, t);
+ }
+});
+
+test('countOccurrences: 次数进 fingerprint 才能挡住「已冻结 key 里新增一处」', () => {
+ assert.equal(countOccurrences('这个对话和那个会话', '会话'), 1);
+ assert.equal(countOccurrences('会话结束后新建会话', '会话'), 2);
+ // ASCII 词按词边界,允许复数 s;ssh-agent 不算产品 Agent
+ assert.equal(countOccurrences('创建 Worker 后 worker 会启动', 'Worker'), 2);
+ assert.equal(countOccurrences('用 ssh-agent 转发', 'Agent'), 0);
+ assert.equal(countHalfWidthPunct('先这样,再那样,最后收尾'), 2);
+});
+
+test('validateAgainstSchema: boolean schema 的 false 必须恒失败', () => {
+ // false 被当成「无约束对象」时,items: false 变成不检查、oneOf 的 false 分支
+ // 甚至能算唯一通过的分支,把「禁止一切」翻转成「允许一切」
+ assert.deepEqual(validateAgainstSchema(['x'], { type: 'array', items: true }), []);
+ const errors = validateAgainstSchema(['x'], { type: 'array', items: false });
+ assert.equal(errors.length, 1);
+ assert.match(errors[0], /schema 为 false,此处不允许任何值/);
+ // oneOf 里的 false 分支不能算作通过
+ assert.match(
+ validateAgainstSchema('x', { oneOf: [false, false] }).join(),
+ /oneOf/,
+ );
});
diff --git a/scripts/check-i18n-glossary.mjs b/scripts/check-i18n-glossary.mjs
index c4a39b59e9..3db79ddc24 100644
--- a/scripts/check-i18n-glossary.mjs
+++ b/scripts/check-i18n-glossary.mjs
@@ -44,6 +44,8 @@ import { validateAgainstSchema } from './shared/json-schema-lite.mjs';
import {
ELLIPSIS_LOCALES,
FULL_WIDTH_PUNCT,
+ countHalfWidthPunct,
+ countOccurrences,
HALFWIDTH_PUNCT_LOCALES,
WORD_BOUNDARY,
findCaseMismatch,
@@ -186,7 +188,13 @@ function truncate(text, max) {
return flat.length > max ? `${flat.slice(0, max)}…` : flat;
}
-/** 一条违规。fingerprint 用于 baseline 比对,必须稳定。 */
+/**
+ * 一条违规。fingerprint 用于 baseline 比对,必须稳定。
+ *
+ * detail 里带**命中次数**:否则同一个 key 里 1 处「会话」与 3 处「会话」产出相同指纹,
+ * 一处被冻进 baseline 后再往同一条文案加一处,新违规会被静默掩盖、CI 照过——
+ * 那就破了 baseline「只减不增」的契约。带上次数,增加一处就是一条新指纹。
+ */
function makeViolation({ locale, key, rule, detail, severity, hint }) {
return {
locale,
@@ -234,7 +242,8 @@ for (const term of glossary.terms) {
for (const [key, value] of entries) {
if (isExempt(key)) continue;
- if (!occursIn(stripNonProse(value), bad)) continue;
+ const badCount = countOccurrences(stripNonProse(value), bad);
+ if (badCount === 0) continue;
if (sourceRe) {
// 英文源同样要先剥离非文案片段:whenEn 若只出现在 URL / 文件名 / $t() 里
// (例如英文源含 agent-config.json),会被误判为「英文命中」,把禁用规则
@@ -258,7 +267,7 @@ for (const term of glossary.terms) {
locale,
key,
rule: 'forbidden-term',
- detail: `${term.id}:${bad}`,
+ detail: `${term.id}:${bad}\u00d7${badCount}`,
severity,
hint:
`「${bad}」是 ${term.en} 条目下的禁用译法` +
@@ -286,14 +295,16 @@ for (const term of glossary.terms) {
if (!standard || standard !== term.en) continue;
for (const [key, value] of entries) {
if (isExempt(key)) continue;
- const hit = findCaseMismatch(stripNonProse(value), standard);
+ const prose = stripNonProse(value);
+ const hit = findCaseMismatch(prose, standard);
if (!hit) continue;
+ const hitCount = countOccurrences(prose, standard);
violations.push(
makeViolation({
locale,
key,
rule: 'term-case',
- detail: `${term.id}:${hit}`,
+ detail: `${term.id}:${hit}\u00d7${hitCount}`,
severity,
// 这条给出确定目标是可以的:大小写形态与语境无关,worker 在任何句子里都该写
// Worker,答案唯一。禁用译法则不同——同一个中文词对应多个英文概念,目标不唯一,
@@ -324,7 +335,7 @@ for (const locale of locales) {
locale,
key,
rule: 'punct-halfwidth',
- detail: mark,
+ detail: `${mark}\u00d7${countHalfWidthPunct(prose)}`,
severity: 'error',
hint: `中文字符后应使用全角「${FULL_WIDTH_PUNCT[mark] ?? mark}」,当前是半角「${mark}」`,
}),
@@ -337,7 +348,7 @@ for (const locale of locales) {
locale,
key,
rule: 'punct-ellipsis',
- detail: '...',
+ detail: `...×${(prose.match(/\.\.\./g) ?? []).length}`,
severity: 'error',
hint: '省略号应使用「…」而非三个半角点',
}),
diff --git a/scripts/shared/glossary-rules.mjs b/scripts/shared/glossary-rules.mjs
index 450fb26ea8..bdd7ba20a1 100644
--- a/scripts/shared/glossary-rules.mjs
+++ b/scripts/shared/glossary-rules.mjs
@@ -85,6 +85,36 @@ export function occursIn(text, term) {
return text.includes(term);
}
+/**
+ * 术语在文案里出现的次数。
+ *
+ * fingerprint 需要它:光靠「locale + key + 规则 + 词」无法区分同一个 key 里命中 1 次还是
+ * 3 次。某个 key 的一处「会话」被冻进 baseline 后,再往同一条文案里加一处「会话」会产出
+ * 完全相同的 fingerprint,新违规就被 baseline 掩盖、CI 照过——这违反 baseline「只减不增」
+ * 的契约。把次数编进 fingerprint,增加一处就是一条新指纹。
+ */
+export function countOccurrences(text, term) {
+ if (/^[\x20-\x7e]+$/.test(term)) {
+ const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+ const re = new RegExp(`(?<![${WORD_BOUNDARY}])${escaped}s?(?![${WORD_BOUNDARY}])`, 'gi');
+ return [...text.matchAll(re)].length;
+ }
+ // CJK 词用不重叠的子串计数
+ let n = 0;
+ let from = 0;
+ for (;;) {
+ const i = text.indexOf(term, from);
+ if (i < 0) return n;
+ n += 1;
+ from = i + term.length;
+ }
+}
+
+/** 半角标点在文案里出现的次数(同 countOccurrences,供标点规则的 fingerprint 用)。 */
+export function countHalfWidthPunct(text) {
+ return [...text.matchAll(new RegExp(HALF_WIDTH_AFTER_HAN.source, 'g'))].length;
+}
+
/**
* 大小写形态检查:命中术语但拼写形态与标准不符时返回**第一个不符的**实际拼写,否则 null。
*
@@ -186,6 +216,21 @@ export function normalizeForPunctuation(text) {
.replace(/\{\{[^}]*\}\}/g, PROSE_PLACEHOLDER)
.replace(/<\/?\d+>/g, PROSE_PLACEHOLDER)
.replace(/\$t\([^)]*\)/g, PROSE_PLACEHOLDER)
+ // URL / 邮箱在标点检查里也要留下正文边界。
+ //
+ // TOKEN_TAIL 已经正确地把 `https://x.test,返回` 的逗号留在了外面,但若把 token 换成
+ // 空格,就变成「 ,返回」——逗号前面是空格,findHalfWidthPunct 认不出左边界,违规照样
+ // 漏掉。对读者来说 URL 渲染出来就是正文的一部分,和插值一样该用汉字替身。
+ //
+ // 注意只有**紧跟「半角标点 + CJK」**的 token 才替换成汉字替身:其余情形仍替换为空格,
+ // 否则 `config.json:` 这类路径分隔符会被当成正文标点误报(FILENAME_TOKEN 同理,
+ // 它永远替空格)。
+ //
+ // lookahead 里必须带 CJK,不能只写 [,;:!?]:token 匹配是贪婪的,只要 lookahead 能满足
+ // 就会回溯出更短的匹配——`https://a.test/x?ids=1,2` 会被切在 query string 内部的逗号处,
+ // 于是那个逗号被误判成正文标点。带上 CJK 后与 TOKEN_TAIL 自身的截断条件一致,不会回溯。
+ .replace(new RegExp(`${URL_TOKEN.source}(?=[,;:!?][${CJK_CHAR}])`, 'gi'), PROSE_PLACEHOLDER)
+ .replace(new RegExp(`${EMAIL_TOKEN.source}(?=[,;:!?][${CJK_CHAR}])`, 'g'), PROSE_PLACEHOLDER)
.replace(URL_TOKEN, ' ')
.replace(EMAIL_TOKEN, ' ')
.replace(FILENAME_TOKEN, ' ');
diff --git a/scripts/shared/json-schema-lite.mjs b/scripts/shared/json-schema-lite.mjs
index b103651ee4..9aaebd5c25 100644
--- a/scripts/shared/json-schema-lite.mjs
+++ b/scripts/shared/json-schema-lite.mjs
@@ -59,10 +59,17 @@ const KEYWORD_SHAPE = {
oneOf: 'array',
properties: 'object',
definitions: 'object',
- items: 'object',
- // additionalProperties 可以是 boolean 或 schema 对象,单独判
};
+/**
+ * 值本身是一个 schema 的关键字。
+ *
+ * 这些位置允许 boolean schema(draft-07 里 `true` 恒通过、`false` 恒失败),
+ * 所以不能按 'object' 死判——那会把合法的 `items: false` 误判成 schema 写错。
+ * 其余关键字(type / required / …)仍按 KEYWORD_SHAPE 严格校验。
+ */
+const SCHEMA_VALUED = new Set(['items', 'additionalProperties']);
+
function shapeOf(value) {
if (Array.isArray(value)) return 'array';
if (value === null) return 'null';
@@ -82,10 +89,10 @@ function assertSupported(node, path = '#') {
'不要让它被静默忽略',
);
}
- if (key === 'additionalProperties') {
+ if (SCHEMA_VALUED.has(key)) {
if (typeof value !== 'boolean' && shapeOf(value) !== 'object') {
throw new Error(
- `schema ${path}/additionalProperties 只支持 boolean 或 schema 对象,实际是 ${shapeOf(value)}`,
+ `schema ${path}/${key} 只支持 boolean 或 schema 对象,实际是 ${shapeOf(value)}`,
);
}
continue;
@@ -107,7 +114,7 @@ function assertSupported(node, path = '#') {
if (typeof node.additionalProperties === 'object') {
assertSupported(node.additionalProperties, `${path}/additionalProperties`);
}
- if (node.items) assertSupported(node.items, `${path}/items`);
+ if (node.items !== undefined) assertSupported(node.items, `${path}/items`);
for (const [i, child] of (node.oneOf ?? []).entries()) {
assertSupported(child, `${path}/oneOf/${i}`);
}
@@ -132,6 +139,16 @@ function matchesType(value, expected) {
* 远程引用这类需要网络的能力。
*/
function validateNode(value, schema, root, path, errors) {
+ // boolean schema 是合法 draft-07:true 恒通过,false 恒失败。
+ // assertSupported 已经放行它们,这里若不处理,`schema.$ref`/`schema.type` 全是 undefined,
+ // false 就被当成「无约束对象」——`items: false` 变成不检查,`oneOf` 里的 false 分支
+ // 甚至能算作唯一通过的分支,把本意「禁止一切」的约束翻转成「允许一切」。
+ if (schema === true) return;
+ if (schema === false) {
+ errors.push(`${path}: schema 为 false,此处不允许任何值`);
+ return;
+ }
+
if (schema.$ref) {
const m = /^#\/definitions\/([^/]+)$/.exec(schema.$ref);
if (!m) throw new Error(`只支持 #/definitions/X 形式的 $ref,收到 ${schema.$ref}`);
@@ -175,7 +192,9 @@ function validateNode(value, schema, root, path, errors) {
if (schema.minItems !== undefined && value.length < schema.minItems) {
errors.push(`${path}: 至少需要 ${schema.minItems} 项`);
}
- if (schema.items) {
+ // 必须判 !== undefined:`items: false` 是合法 draft-07(禁止任何元素),
+ // 但它是 falsy,写 `if (schema.items)` 会把这条约束整个跳过。
+ if (schema.items !== undefined) {
value.forEach((item, i) => validateNode(item, schema.items, root, `${path}[${i}]`, errors));
}
}