Skip to content

Commit 5fe6006

Browse files
roberttwangclaude
andcommitted
fix(web): translate nested causes of shared-task share failures, keep role names
Review of 4443992 found two gaps in the wrapped-error rules: - "共享 X 失败:<cause>" only translated the shell; causes from catalog_sharing.go (共享 LOOP 只能使用已共享的沙箱, 角色 "…" 不存在 …) stayed Chinese or went through fragment rules. - Rules ending in 不存在 / 不属于… ran first and, with a lazy (.+?) capture, swallowed "共享 sop/x 失败:角色 …" as the reference, so the fragment pass rewrote the role name "代码审查" to "Code review". References are now matched as [^\s:]+, the share-failure rule runs first and translates the known causes with role names kept as written. A matched message skips the fragment passes, including the observer's second pass after the translated text is written back to the node. Co-Authored-By: Claude Code <noreply@anthropic.com>
1 parent 4443992 commit 5fe6006

2 files changed

Lines changed: 64 additions & 15 deletions

File tree

‎web/i18n.js‎

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,41 +116,75 @@
116116
}],
117117
];
118118
// Server errors wrapping ErrSharedScheduleReferences (internal/repo/shared_schedule_assets.go)
119-
// read "共享任务引用资产不可用:<detail>", and the detail carries asset ids. No dictionary key
120-
// can match such a message whole, and translating only the prefix leaves the detail Chinese,
121-
// so each detail is translated as one sentence before the fragment pass runs.
119+
// read "共享任务引用资产不可用:<detail>", and the detail carries asset ids and names. No
120+
// dictionary key can match such a message whole, and translating only the prefix leaves the
121+
// detail Chinese, so the whole message is translated here and the fragment passes never see it
122+
// (they would rewrite a role named "代码审查" into "Code review"). An asset reference is
123+
// "type/id" or a bare type: no spaces, no full-width colon.
122124
const SHARED_REF = 'A project asset referenced by the shared task is unavailable: ';
125+
const REF = '([^\\s:]+)';
126+
// Causes returned by the sharing path (internal/repo/catalog_sharing.go) when publishing a task
127+
// shares a referenced LOOP; names in quotes are user data and stay as written.
128+
const shareCauses = [
129+
[/^共享 LOOP 只能使用已共享的沙箱$/, () => 'a shared LOOP can only use shared sandboxes'],
130+
[/^沙箱不属于当前项目且尚未共享$/, () => "its sandbox isn't in this project and isn't shared yet"],
131+
[/^角色 (".*") 不存在$/, (_, name) => `role ${name} doesn't exist`],
132+
[/^角色 (".*") 不属于当前项目且尚未共享$/, (_, name) => `role ${name} isn't in this project and isn't shared yet`],
133+
];
134+
const shareCause = cause => {
135+
for (const [re, rep] of shareCauses) if (re.test(cause)) return cause.replace(re, rep);
136+
return HAN.test(cause) ? translate(cause) : cause;
137+
};
123138
const wrapped = [
124-
[/共享任务引用资产不可用:(.+?) 不存在或尚未共享(?=\s*$)/, (_, ref) => `${SHARED_REF}${ref} doesn't exist or isn't shared yet`],
125-
[/共享任务引用资产不可用:(.+?) 不属于当前项目且尚未共享(?=\s*$)/, (_, ref) => `${SHARED_REF}${ref} isn't in this project and isn't shared yet`],
126-
[/共享任务引用资产不可用:(.+?) 不支持随 AI 定时任务共享(?=\s*$)/, (_, ref) => `${SHARED_REF}${ref} can't be shared with an AI scheduled task`],
127-
[/共享任务引用资产不可用:(.+?) 的归属已变更,请发布新版本(?=\s*$)/, (_, ref) => `${SHARED_REF}the owner of ${ref} has changed. Publish a new version`],
128-
[/共享任务引用资产不可用:(.+?) 的权限或归属已变更(?=\s*$)/, (_, ref) => `${SHARED_REF}the access or owner of ${ref} has changed`],
129-
[/共享任务引用资产不可用:(.+?) 不存在(?=\s*$)/, (_, ref) => `${SHARED_REF}${ref} doesn't exist`],
139+
// First, so a cause ending in 不存在 etc. is not taken for a reference by the rules below.
140+
[new RegExp(`共享任务引用资产不可用:共享 ${REF} 失败:(.+?)(?=\\s*$)`), (_, ref, cause) => `${SHARED_REF}failed to share ${ref}: ${shareCause(cause)}`],
141+
[new RegExp(`共享任务引用资产不可用:${REF} 不存在或尚未共享(?=\\s*$)`), (_, ref) => `${SHARED_REF}${ref} doesn't exist or isn't shared yet`],
142+
[new RegExp(`共享任务引用资产不可用:${REF} 不属于当前项目且尚未共享(?=\\s*$)`), (_, ref) => `${SHARED_REF}${ref} isn't in this project and isn't shared yet`],
143+
[new RegExp(`共享任务引用资产不可用:${REF} 不支持随 AI 定时任务共享(?=\\s*$)`), (_, ref) => `${SHARED_REF}${ref} can't be shared with an AI scheduled task`],
144+
[new RegExp(`共享任务引用资产不可用:${REF} 的归属已变更,请发布新版本(?=\\s*$)`), (_, ref) => `${SHARED_REF}the owner of ${ref} has changed. Publish a new version`],
145+
[new RegExp(`共享任务引用资产不可用:${REF} 的权限或归属已变更(?=\\s*$)`), (_, ref) => `${SHARED_REF}the access or owner of ${ref} has changed`],
146+
[new RegExp(`共享任务引用资产不可用:${REF} 不存在(?=\\s*$)`), (_, ref) => `${SHARED_REF}${ref} doesn't exist`],
130147
[/共享任务引用资产不可用:最多引用 (\d+) 个资产(?=\s*$)/, (_, n) => `${SHARED_REF}a task can reference up to ${n} assets`],
131148
[/共享任务引用资产不可用:直接引用列表与共享版本不一致(?=\s*$)/, () => `${SHARED_REF}the referenced assets don't match the shared version`],
132-
// The cause after the last colon comes from the sharing path and goes through the usual passes.
133-
[/共享任务引用资产不可用:共享 (\S+) 失败:/, (_, ref) => `${SHARED_REF}failed to share ${ref}: `],
134149
];
150+
// Text before the wrapped error (a caller's own prefix) still goes through the usual passes.
151+
function translateWrapped(text) {
152+
for (const [re, rep] of wrapped) {
153+
const m = re.exec(text);
154+
if (!m) continue;
155+
const head = translate(text.slice(0, m.index));
156+
return head + (head && !/\s$/.test(head) ? ' ' : '') + text.slice(m.index).replace(re, rep);
157+
}
158+
return null;
159+
}
135160
const cache = new Map();
136161

137162
function translateExact(text) {
138163
const trimmed = text && text.trim();
139164
return trimmed && Object.prototype.hasOwnProperty.call(dict, trimmed) ? text.replace(trimmed, dict[trimmed]) : text;
140165
}
141166

167+
// Output of translateWrapped can still hold user names in Chinese. Writing it back to a text
168+
// node fires the observer again, and that second pass must not run the fragment rules over it.
169+
const wrappedDone = new Set();
170+
142171
function translate(text) {
143-
if (!text || !HAN.test(text)) return text;
172+
if (!text || !HAN.test(text) || wrappedDone.has(text)) return text;
144173
const hit = cache.get(text);
145174
if (hit !== undefined) return hit;
146175
const trimmed = text.trim();
147176
let out;
177+
const whole = Object.prototype.hasOwnProperty.call(dict, trimmed) ? null : translateWrapped(text);
178+
if (whole !== null) {
179+
if (cache.size > 20000) { cache.clear(); wrappedDone.clear(); }
180+
cache.set(text, whole);
181+
if (HAN.test(whole)) wrappedDone.add(whole);
182+
return whole;
183+
}
148184
if (Object.prototype.hasOwnProperty.call(dict, trimmed)) {
149185
out = text.replace(trimmed, dict[trimmed]);
150186
} else {
151-
out = text;
152-
for (const [re, rep] of wrapped) out = out.replace(re, rep);
153-
if (fragmentRE) out = out.replace(fragmentRE, m => dict[m]);
187+
out = fragmentRE ? text.replace(fragmentRE, m => dict[m]) : text;
154188
if (sentenceRE && HAN.test(out)) out = out.replace(sentenceRE, m => dict[m]);
155189
for (const [re, rep] of patterns) out = out.replace(re, rep);
156190
}

‎webtest/tests/i18n-schedules.test.mjs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,19 @@ test('English shared-task reference errors are translated whole, never half Engl
290290
['共享任务引用资产不可用:agent/a1 的归属已变更,请发布新版本', 'the owner of agent/a1 has changed. Publish a new version'],
291291
['共享任务引用资产不可用:sop/flow-1 的权限或归属已变更', 'the access or owner of sop/flow-1 has changed'],
292292
['共享任务引用资产不可用:共享 agent/a1 失败:database is locked', 'failed to share agent/a1: database is locked'],
293+
// Publishing shares a referenced LOOP; causes from internal/repo/catalog_sharing.go.
294+
['共享任务引用资产不可用:共享 sop/cr1506-flow 失败:共享 LOOP 只能使用已共享的沙箱', 'failed to share sop/cr1506-flow: a shared LOOP can only use shared sandboxes'],
295+
['共享任务引用资产不可用:共享 sop/wf 失败:沙箱不属于当前项目且尚未共享', "failed to share sop/wf: its sandbox isn't in this project and isn't shared yet"],
296+
// A cause ending in 不存在 / 不属于… must not be read as a reference, and the role name is user data.
297+
['共享任务引用资产不可用:共享 sop/wf-nested 失败:角色 "ghost-role" 不存在', 'failed to share sop/wf-nested: role "ghost-role" doesn\'t exist'],
298+
['共享任务引用资产不可用:共享 sop/cr1506-flow3 失败:角色 "代码审查" 不属于当前项目且尚未共享', 'failed to share sop/cr1506-flow3: role "代码审查" isn\'t in this project and isn\'t shared yet'],
293299
];
294300
for (const [zh, english] of cases) assert.equal(en.lfTranslate(zh), prefix + english, zh);
295301
// Wrapped once more by a caller, the reference error is still one English sentence.
296302
assert.equal(en.lfTranslate('执行失败:共享任务引用资产不可用:agent/ghost 不存在'), `Failed: ${prefix}agent/ghost doesn't exist`);
303+
assert.equal(en.lfTranslate('执行失败:共享任务引用资产不可用:共享 sop/x 失败:角色 "代码审查" 不存在'), `Failed: ${prefix}failed to share sop/x: role "代码审查" doesn't exist`);
304+
// Surrounding whitespace of a text node is kept.
305+
assert.equal(en.lfTranslate(' 共享任务引用资产不可用:agent/ghost 不存在\n'), ` ${prefix}agent/ghost doesn't exist\n`);
297306
// Sentences that only share the wording keep translating as before.
298307
assert.equal(en.lfTranslate('沙箱不属于当前项目且尚未共享'), '沙箱不属于当前项目且尚未共享');
299308
assert.equal(en.lfTranslate('共享任务使用的角色不属于当前项目且尚未共享'), '共享任务使用的角色不属于当前项目且尚未共享');
@@ -305,6 +314,12 @@ test('English shared-task reference errors are translated whole, never half Engl
305314
en.document.body.append(node);
306315
await new Promise((resolve) => en.setTimeout(resolve, 0));
307316
assert.equal(node.textContent, `${prefix}agent/ghost-agent doesn't exist`);
317+
// The translated text still holds the role name in Chinese; writing it back to the node must
318+
// not run the fragment rules over it ("代码审查" is also a dictionary key: Code review).
319+
node.textContent = '共享任务引用资产不可用:共享 sop/cr1506-flow3 失败:角色 "代码审查" 不属于当前项目且尚未共享';
320+
await new Promise((resolve) => en.setTimeout(resolve, 0));
321+
await new Promise((resolve) => en.setTimeout(resolve, 0));
322+
assert.equal(node.textContent, `${prefix}failed to share sop/cr1506-flow3: role "代码审查" isn't in this project and isn't shared yet`);
308323
});
309324

310325
test('English publish toast names the Shared task market without mixing languages or rewriting the task name', async (t) => {

0 commit comments

Comments
 (0)