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): 空禁用词不再挂死门禁,fingerprint 带文案摘要,引号纳入标点边界

Dash3d745bd3979f
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
index 3f20fed35c..9bbdd9163b 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
@@ -192,7 +192,7 @@
"unlockSubmit": "解锁",
"generateTitle": "生成新的 SSH 密钥",
"nameLabel": "名字(可选)",
- "nameHint": "保存为 ~/.ssh/<名字>。默认 \"cindy\",有重名时自动加 -1 / -2 …",
+ "nameHint": "保存为 ~/.ssh/<名字>。默认 \"cindy\",有重名时自动加 -1 / -2 …",
"encryptLabel": "用 passphrase 加密(推荐)",
"encryptHint": "passphrase 现在输入一次,然后 ssh-add 会把解密后的密钥缓存在系统 ssh-agent 里。macOS 还会把 passphrase 存到 Keychain,重启后自动恢复。",
"passLabel": "Passphrase",
diff --git a/i18n/glossary.schema.json b/i18n/glossary.schema.json
index b07eeb5be0..c5d32bdc75 100644
--- a/i18n/glossary.schema.json
+++ b/i18n/glossary.schema.json
@@ -84,14 +84,14 @@
"type": "array",
"items": {
"oneOf": [
- { "type": "string" },
+ { "type": "string", "minLength": 1 },
{
"type": "object",
"required": ["text", "whenEn"],
"additionalProperties": false,
"properties": {
- "text": { "type": "string", "description": "禁用的译法" },
- "whenEn": { "type": "string", "description": "英文源需匹配的词(按词边界、大小写不敏感匹配)" }
+ "text": { "type": "string", "minLength": 1, "description": "禁用的译法。minLength 为 1 是必须的:空串会被计数函数当成 CJK 词,而空串的 indexOf 永远返回同一个偏移、长度为 0,循环不推进——门禁会挂死而不是报错,且 CI 里 check:i18n-glossary 排在单测之前,一个手滑的 \"\" 就能卡住整条流水线。" },
+ "whenEn": { "type": "string", "minLength": 1, "description": "英文源需匹配的词(按词边界、大小写不敏感匹配)。同样不能为空:空串会让条件禁用退化成无条件命中。" }
}
}
]
diff --git a/scripts/__tests__/glossary-rules.test.mjs b/scripts/__tests__/glossary-rules.test.mjs
index 59d37cf68e..808b5835bf 100644
--- a/scripts/__tests__/glossary-rules.test.mjs
+++ b/scripts/__tests__/glossary-rules.test.mjs
@@ -689,3 +689,35 @@ test('stripNonProse: 较长扩展名的文件名同样要剥离', () => {
}
assert.ok(stripNonProse('已用 1.5 GB').includes('1.5'));
});
+
+test('空术语不能让计数函数挂死', () => {
+ // 空串走 CJK 分支时 indexOf('', from) 永远返回 from、term.length 为 0,循环不推进
+ // ——门禁会挂死而不是报错。CI 里 check:i18n-glossary 排在单测之前,一个手滑的 ""
+ // 就能卡住整条流水线。schema 的 minLength 是第一道防线,这里是第二道。
+ assert.equal(countOccurrences('任意文案', ''), 0);
+ assert.equal(occursIn('任意文案', ''), false);
+ assert.equal(countCaseMismatches('任意文案', ''), 0);
+});
+
+test('glossary.schema.json: 禁用词不允许为空串', () => {
+ const schema = JSON.parse(fs.readFileSync(path.join(ROOT, 'i18n', 'glossary.schema.json'), 'utf8'));
+ const bad = {
+ ...glossary,
+ terms: [{ ...glossary.terms[0], forbidden: { 'zh-CN': [''] } }],
+ };
+ assert.ok(
+ validateAgainstSchema(bad, schema).length > 0,
+ '空禁用词必须在 schema 阶段就被拒,不能让它走到计数函数',
+ );
+});
+
+test('findHalfWidthPunct: 闭合的半角引号也算左边界,但要求右侧是中文', () => {
+ // 中文文案常写 `默认 "cindy",有重名时…`,闭合引号后那个逗号是正文标点
+ assert.equal(findHalfWidthPunct('默认 "cindy",有重名时加后缀'), ',');
+ assert.equal(findHalfWidthPunct("用 'plugin',再重启"), ',');
+ assert.equal(findHalfWidthPunct('见 `worker`,然后继续'), ',');
+ // 半角引号常用来包英文,右侧不是中文时不判违规
+ assert.equal(findHalfWidthPunct('英文 "note", then continue'), null);
+ // 全角括号那一支不要求右侧是中文(已对 26 处实例逐条核对)
+ assert.equal(findHalfWidthPunct('(直接替换,不留原文),或用卡片'), ',');
+});
diff --git a/scripts/check-i18n-glossary.mjs b/scripts/check-i18n-glossary.mjs
index b4494b56cf..446037b968 100644
--- a/scripts/check-i18n-glossary.mjs
+++ b/scripts/check-i18n-glossary.mjs
@@ -35,6 +35,7 @@
* (src/auth/loginMessages.ts 等 .ts 文件),本脚本只扫 locale JSON,不解析这些 TS。
* 它们由各自的编译期 Record 类型 + parity 单测保证结构,但术语一致性目前是盲区。
*/
+import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -192,11 +193,17 @@ function truncate(text, max) {
/**
* 一条违规。fingerprint 用于 baseline 比对,必须稳定。
*
- * detail 里带**命中次数**:否则同一个 key 里 1 处「会话」与 3 处「会话」产出相同指纹,
- * 一处被冻进 baseline 后再往同一条文案加一处,新违规会被静默掩盖、CI 照过——
- * 那就破了 baseline「只减不增」的契约。带上次数,增加一处就是一条新指纹。
+ * detail 里带**命中次数**,fingerprint 再带**该条文案的摘要**。两者缺一不可:
+ * - 只带词不带次数:1 处「会话」与 3 处「会话」同指纹,冻结一处后再加一处即被掩盖;
+ * - 只带次数不带摘要:同一个 key 里「修掉一处、又在别处新增一处」时次数不变
+ * (`甲,乙` → `甲,乙,丙` 仍是 `,×1`),违规照样被掩盖,仍然破「只减不增」。
+ *
+ * 摘要是**整条文案**的哈希:任何改动都会让旧 baseline 条目失效、被判为 stale,
+ * 迫使人重新确认这条文案的现状。代价是无关改动也要摘一次账,但比「悄悄放过新违规」
+ * 安全得多——baseline 的全部意义就是「已知且冻结」,文案变了就不再是同一件事。
*/
-function makeViolation({ locale, key, rule, detail, severity, hint }) {
+function makeViolation({ locale, key, rule, detail, severity, hint, text }) {
+ const digest = crypto.createHash('sha256').update(text ?? '').digest('hex').slice(0, 12);
return {
locale,
key,
@@ -204,7 +211,7 @@ function makeViolation({ locale, key, rule, detail, severity, hint }) {
detail,
severity,
hint,
- fingerprint: `${locale}\t${key}\t${rule}\t${detail}`,
+ fingerprint: `${locale}\t${key}\t${rule}\t${detail}\t${digest}`,
};
}
@@ -270,6 +277,7 @@ for (const term of glossary.terms) {
rule: 'forbidden-term',
detail: `${term.id}:${bad}\u00d7${badCount}`,
severity,
+ text: value,
hint:
`「${bad}」是 ${term.en} 条目下的禁用译法` +
(whenEn ? `(该 key 的英文源含 ${whenEn})` : '') +
@@ -310,6 +318,7 @@ for (const term of glossary.terms) {
rule: 'term-case',
detail: `${term.id}:${hit}\u00d7${hitCount}`,
severity,
+ text: value,
// 这条给出确定目标是可以的:大小写形态与语境无关,worker 在任何句子里都该写
// Worker,答案唯一。禁用译法则不同——同一个中文词对应多个英文概念,目标不唯一,
// 所以那边只报事实、不给目标(见规则 1)。标点同理,答案也唯一。
@@ -354,6 +363,7 @@ for (const locale of locales) {
rule: 'punct-halfwidth',
detail: `${mark}\u00d7${countHalfWidthPunct(prose)}`,
severity: 'error',
+ text: value,
hint: `中文字符后应使用全角「${FULL_WIDTH_PUNCT[mark] ?? mark}」,当前是半角「${mark}」`,
}),
);
@@ -367,6 +377,7 @@ for (const locale of locales) {
rule: 'punct-ellipsis',
detail: `...×${(prose.match(/\.\.\./g) ?? []).length}`,
severity: 'error',
+ text: value,
hint: '省略号应使用「…」而非三个半角点',
}),
);
diff --git a/scripts/shared/glossary-rules.mjs b/scripts/shared/glossary-rules.mjs
index f3b1d3880c..b5f2d6fcf0 100644
--- a/scripts/shared/glossary-rules.mjs
+++ b/scripts/shared/glossary-rules.mjs
@@ -89,6 +89,7 @@ export function stripNonProse(text) {
* 含 CJK 的词(代理 / 插件)没有词边界概念,用子串。
*/
export function occursIn(text, term) {
+ if (!term) return false;
if (/^[\x20-\x7e]+$/.test(term)) {
const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
return new RegExp(`(?<![${WORD_BOUNDARY}])${escaped}s?(?![${WORD_BOUNDARY}])`).test(text);
@@ -105,6 +106,11 @@ export function occursIn(text, term) {
* 的契约。把次数编进 fingerprint,增加一处就是一条新指纹。
*/
export function countOccurrences(text, term, { caseInsensitive = false } = {}) {
+ // 空串必须先挡掉:走到下面的 CJK 分支时 `indexOf('', from)` 永远返回 from、
+ // `term.length` 为 0,循环不推进——门禁会**挂死**而不是报错。CI 里
+ // check:i18n-glossary 排在单测之前,一个手滑的 "" 就能让整条流水线卡住。
+ // schema 那边也加了 minLength: 1,这里是第二道防线:任何调用路径都不该挂住。
+ if (!term) return 0;
if (/^[\x20-\x7e]+$/.test(term)) {
const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// 默认**大小写敏感**,与 occursIn 同口径。禁用译法的判定必须如此:术语表里
@@ -206,6 +212,10 @@ export const ELLIPSIS_LOCALES = new Set(['en', 'zh-CN', 'ja', 'ko']);
*
* 1. 汉字 / 右闭合符号 + 半角标点。只认汉字的话 `(直接替换,不留原文),或…` 会漏——
* 逗号前面是右括号。右括号 / 右引号 / 右书名号后面接的仍是中文正文。
+ * 直引号与反引号同样要算:中文文案里常写 `默认 "cindy",有重名时…`,
+ * 闭合的半角引号后面那个逗号一样是正文标点。但引号这一支**要求右侧是 CJK**
+ * ——半角引号常用来包英文,`英文 "note", then continue` 里的逗号是英文标点,
+ * 不该判违规。全角括号 / 书名号那一支不需要这个条件(已对 26 处实例逐条核对过)。
* 2. 拉丁字母或数字 + 半角标点 + **后面是 CJK**。`Keychain,重启` 这类漏了整整一类:
* 中文句子里夹的英文产品名、技术词后面同样该用全角。这一支必须要求右边是 CJK,
* 否则 `a=1,b=2`、`GPT-4,Claude` 这种纯 ASCII 片段会被误判。
@@ -215,7 +225,7 @@ export const ELLIPSIS_LOCALES = new Set(['en', 'zh-CN', 'ja', 'ko']);
* ——`a=1, b=2` 后面仍不是 CJK。
*/
const HALF_WIDTH_AFTER_HAN = new RegExp(
- `[一-鿿))」』】》〉”’][,:;!?]|[A-Za-z0-9][,:;!?](?=\\s*[${CJK_CHAR}])`,
+ `[一-鿿))」』】》〉”’][,:;!?]|["'\`][,:;!?](?=\\s*[${CJK_CHAR}])|[A-Za-z0-9][,:;!?](?=\\s*[${CJK_CHAR}])`,
);
const ASCII_ELLIPSIS = /\.\.\./;