feat(tenant): enforce English output for en-US template generation

When the article locale is en-US, Chinese prompt templates could leak
Chinese wording into generated titles, outlines, and article bodies.
Add high-priority language-consistency guards so the model treats the
Chinese template text as internal instructions and emits English only.

- Append per-artifact locale guards (analyze/title/outline) in template
  assist prompts and an article-body guard in the generation prompt,
  only when locale is en-US
- Reinforce locale consistency in the title/outline runtime prompts and
  drop retained images from the rewrite prompt
- Cover the new guards with unit tests

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-09 19:38:55 +08:00
parent f69edc2218
commit 2c394d436a
7 changed files with 159 additions and 7 deletions
@@ -64,6 +64,64 @@ func TestBuildOutlinePromptForConfiguredTemplateForcesOutlineObject(t *testing.T
}
}
func TestBuildAnalyzePromptEnglishLocaleAddsHardLanguageGuard(t *testing.T) {
template := "你正在做品牌分析。\n语言: {{locale}}\n品牌名: {{brand_name}}"
prompt := buildAnalyzePrompt("top_x_article", "Top X 推荐文章", AnalyzeTaskRequest{
Locale: "en-US",
BrandName: "Rankly",
}, &template)
for _, expected := range []string{
"High-priority language consistency requirement:",
"Return English JSON field values for brand_summary, keywords, competitor descriptions",
"Competitor, brand, company, and product names may remain in their official language",
} {
if !strings.Contains(prompt, expected) {
t.Fatalf("buildAnalyzePrompt() = %q, want English language guard %q", prompt, expected)
}
}
}
func TestBuildTitlePromptEnglishLocaleAddsHardLanguageGuard(t *testing.T) {
template := "你正在为文章生成标题。\n语言: {{locale}}\n优化关键词: {{primary_keyword}}"
prompt := buildTitlePrompt("top_x_article", "Top X 推荐文章", TitleTaskRequest{
Locale: "en-US",
BrandQuestion: "whole-home customization",
}, &template)
for _, expected := range []string{
"High-priority language consistency requirement:",
"Return English JSON strings only.",
"The titles must be natural, professional English titles",
} {
if !strings.Contains(prompt, expected) {
t.Fatalf("buildTitlePrompt() = %q, want English language guard %q", prompt, expected)
}
}
}
func TestBuildOutlinePromptEnglishLocaleAddsHardLanguageGuard(t *testing.T) {
template := "你正在为文章生成大纲。\n语言: {{locale}}\n标题: {{title}}\n已选段落: {{outline_sections}}"
prompt := buildOutlinePrompt("top_x_article", "Top X 推荐文章", OutlineTaskRequest{
Locale: "en-US",
Title: "How to Choose Whole-home Customization",
OutlineSections: []string{"Introduction", "Key Points"},
}, &template)
for _, expected := range []string{
"High-priority language consistency requirement:",
"Every outline and child outline value must be natural, professional English.",
"Do not output Chinese section names",
} {
if !strings.Contains(prompt, expected) {
t.Fatalf("buildOutlinePrompt() = %q, want English language guard %q", prompt, expected)
}
}
}
func TestNormalizeAnalyzeResultKeepsTenQueryKeywords(t *testing.T) {
input := AnalyzeTaskResult{
BrandSummary: " 全屋定制服务摘要 ",