Store category prompts as EXACT split legacy text, no boilerplate

The per-category prompt editor showed English machine boilerplate
("Role: description. Build JSON …", schema intro, {{var}} context lines)
because seed/sync baked the render framing into categories.prompt. Now the
stored overlay is exactly the legacy wp_product_categories.sql content,
split into the three sections a user would type themselves:

  --- Title ---        Slovenian naming formula
  --- Description ---  legacy <H2>/<p>/<ul> body structure
  --- Meta ---         legacy metaDescription instruction

Schema, role framing, and Name/Description/Category/Attrs product context
stay render-time only (system template + ensureCategoryEnhanceUserContext),
where they already existed.

- SplitLegacyCombinedEnhancePrompt: non-legacy input now returns "" —
  categories without seed/legacy content get their override CLEARED
  (company default) instead of being stuffed with the canonical template
  (e.g. parent categories like "Bela tehnika" absent from the SQL).
- CategoryEnhancePromptNeedsRepair flags stored boilerplate ("parsed as
  JSON", "Build JSON", retired Attributes section) so Sync rewrites old
  data to the clean shape; repair supports clearing (prompt = '{}').
- seed-a1 apply-category-prompts skips instead of writing template text.
- Web editor compose stores only the user's section text: empty sections
  keep bare markers, default bodies and the default preamble are never
  persisted (DEFAULT_SECTION_BODIES removed).
- Verified locally: apply rewrote 238 A1+Demo categories (216 exact
  splits, 22 cleared), zero boilerplate matches in DB, idempotent re-run
  (would_update=0).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-18 00:53:26 +02:00
co-authored by Claude Fable 5
parent 0eef202f56
commit 9a839d6d13
13 changed files with 182 additions and 157 deletions
+12 -7
View File
@@ -84,12 +84,12 @@ Category: {{category}}
Attrs: {{attrs}}`
// CategoryEnhancePromptNeedsRepair reports whether a stored categories.prompt value
// should be rewritten into role-sectioned enhance overlay form. Empty prompts are
// left alone. Canonical CategoryEnhanceUserTemplate and per-category overlays that
// already carry Title/Description/Meta markers are OK — equality with the shared
// template is not required (legacy splits keep Slovenian rules). Prompts still
// carrying a retired "--- Attributes ---" role section are rewritten so attribute
// prompting stays out of category prompts.
// should be rewritten into clean role-sectioned overlay form (or cleared). Empty
// prompts are left alone. A clean overlay carries Title/Description/Meta markers
// with ONLY the category's own instruction text — schema/role machine boilerplate
// (old seed versions stored the canonical template text, "Build JSON …" role
// instructions, and a retired "--- Attributes ---" section) marks a prompt for
// repair so the prompt editor shows exactly the per-category rules.
func CategoryEnhancePromptNeedsRepair(prompt string) bool {
p := strings.TrimSpace(prompt)
if p == "" {
@@ -101,7 +101,12 @@ func CategoryEnhancePromptNeedsRepair(prompt string) bool {
if !CategoryEnhanceHasRoleSections(p) {
return true
}
return strings.Contains(strings.ToLower(p), strings.ToLower(SectionAttributesStart))
lower := strings.ToLower(p)
if strings.Contains(lower, strings.ToLower(SectionAttributesStart)) {
return true
}
return strings.Contains(lower, "your reply is parsed as json") ||
strings.Contains(lower, `build json "`)
}
// BuiltInDefaults match the previous hardcoded system prompts, with structured user templates.
+8 -6
View File
@@ -43,17 +43,16 @@ func TestCategoryEnhancePromptNeedsRepair(t *testing.T) {
if CategoryEnhancePromptNeedsRepair("") {
t.Fatal("empty should not need repair")
}
if CategoryEnhancePromptNeedsRepair(CategoryEnhanceUserTemplate) {
t.Fatal("canonical template should be idempotent")
}
if CategoryEnhancePromptNeedsRepair(" " + CategoryEnhanceUserTemplate + "\n") {
t.Fatal("whitespace-trimmed canonical should be idempotent")
// A STORED copy of the built-in template is boilerplate pollution — repair
// clears it so the category falls back to the company default.
if !CategoryEnhancePromptNeedsRepair(CategoryEnhanceUserTemplate) {
t.Fatal("stored canonical template text should need repair (cleared to default)")
}
legacy := `Ustvari nov opis\n<H2>foo</H2>\nAttrs missing`
if !CategoryEnhancePromptNeedsRepair(legacy) {
t.Fatal("legacy HTML prompt should need repair")
}
// Per-category sectioned overlay (Slovenian rules) must not force re-repair.
// Per-category sectioned overlay (pure Slovenian rules) must not force re-repair.
split := SplitLegacyCombinedEnhancePrompt(`GPT predloga:
<name>{Napiši tip izdelka}</name>
<metaDescription>{140 znakov}</metaDescription>
@@ -61,6 +60,9 @@ func TestCategoryEnhancePromptNeedsRepair(t *testing.T) {
if CategoryEnhancePromptNeedsRepair(split) {
t.Fatal("sectioned split overlay should be idempotent")
}
if CategoryEnhancePromptNeedsRepair(" " + split + "\n") {
t.Fatal("whitespace-trimmed split overlay should be idempotent")
}
}
func TestBuiltInProductEnhanceUsesSharedUserTemplate(t *testing.T) {
+21 -43
View File
@@ -74,61 +74,39 @@ func ParseLegacyCombinedEnhancePrompt(prompt string) LegacyEnhanceParts {
}
// SplitLegacyCombinedEnhancePrompt rewrites a legacy combined name+description(+meta)
// prompt into CategoryEnhanceUserTemplate role sections, preserving Slovenian
// naming / HTML / meta intent as category-specific rules under Title / Description / Meta.
// Non-legacy input returns CategoryEnhanceUserTemplate (canonical shared overlay).
// prompt into role sections carrying ONLY the original per-category text — the
// Slovenian naming formula under Title, the HTML body structure under Description,
// the SEO instruction under Meta. No schema/role boilerplate is stored: JSON
// schema, role framing, and {{name}}/{{description}}/{{category}}/{{attrs}}
// product context are all supplied at render time (system template +
// processing.ensureCategoryEnhanceUserContext). Non-legacy input returns "" —
// callers leave the category without an override (company default applies).
func SplitLegacyCombinedEnhancePrompt(prompt string) string {
parts := ParseLegacyCombinedEnhancePrompt(prompt)
if !parts.WasLegacy {
return strings.TrimSpace(CategoryEnhanceUserTemplate)
return ""
}
return BuildCategoryEnhanceOverlay(parts)
}
// BuildCategoryEnhanceOverlay composes a role-sectioned enhance USER overlay from
// extracted legacy parts (or empty parts → canonical template text with no extra rules).
// extracted legacy parts — the stored text is exactly the split legacy content,
// matching what a user would type into the Title/Description/Meta prompt areas.
func BuildCategoryEnhanceOverlay(parts LegacyEnhanceParts) string {
var b strings.Builder
b.WriteString(`Your reply is parsed as JSON {"name":"string","description":"string","meta_title":"string","meta_description":"string","attrs":{}} only (system schema). Write all string fields in {{language}} (do not hardcode a language).`)
b.WriteString("\n\n")
b.WriteString(SectionTitleStart)
b.WriteByte('\n')
b.WriteString(TitleRoleInstruction)
b.WriteByte('\n')
if rules := strings.TrimSpace(parts.TitleRules); rules != "" {
b.WriteString("Category naming rules (preserve intent): ")
b.WriteString(rules)
section := func(start, end, body string) {
b.WriteString(start)
b.WriteByte('\n')
if body = strings.TrimSpace(body); body != "" {
b.WriteString(body)
b.WriteByte('\n')
}
b.WriteString(end)
b.WriteString("\n\n")
}
b.WriteString("Name: {{name}}\n")
b.WriteString(SectionTitleEnd)
b.WriteString("\n\n")
b.WriteString(SectionDescriptionStart)
b.WriteString("\nRole: description. Build JSON \"description\": product body HTML only (not SEO meta). When a Description formula follows, emit ONE HTML string covering each section in order (tags matching type: h1/h2/h3/h4, p, ul); otherwise prefer 1-3 factual paragraphs as ONE string with limited HTML (<h2><p><ul><li>) — do NOT emit a competing full HTML document or wrap the whole reply in <name>/<metaDescription> tags.\n")
if rules := strings.TrimSpace(parts.DescriptionRules); rules != "" {
b.WriteString("Category HTML structure (preserve intent; emit as ONE description HTML string, not tagged name/meta blocks):\n")
b.WriteString(rules)
b.WriteByte('\n')
}
b.WriteString("Description: {{description}}\n")
b.WriteString(SectionDescriptionEnd)
b.WriteString("\n\n")
b.WriteString(SectionMetaStart)
b.WriteString("\nRole: meta. Build JSON \"meta_title\" and \"meta_description\" as plain SEO text (never HTML). meta_title: 50-60 chars; meta_description: 120-155 chars; follow any SEO meta formula that follows; never copy the full description HTML into meta_description.\n")
if rules := strings.TrimSpace(parts.MetaRules); rules != "" {
b.WriteString("Category SEO rules (preserve intent; plain text only): ")
b.WriteString(rules)
b.WriteByte('\n')
}
b.WriteString(SectionMetaEnd)
b.WriteString("\n\n")
b.WriteString("Product context:\n")
b.WriteString("Category: {{category}}\n")
b.WriteString("Attrs: {{attrs}}")
section(SectionTitleStart, SectionTitleEnd, parts.TitleRules)
section(SectionDescriptionStart, SectionDescriptionEnd, parts.DescriptionRules)
section(SectionMetaStart, SectionMetaEnd, parts.MetaRules)
return strings.TrimSpace(b.String())
}
@@ -44,19 +44,25 @@ GPT predloga:
if !strings.Contains(got, "Tehnične specifikacije") && !strings.Contains(got, "tehničnih specifikacij") {
t.Fatal("description section must preserve HTML body intent")
}
if !strings.Contains(got, "{{attrs}}") || !strings.Contains(got, "{{language}}") {
t.Fatal("overlay must keep {{attrs}} and {{language}}")
// The stored overlay is EXACTLY the split legacy text — no schema/role
// boilerplate, no {{var}} placeholders (product context is render-time).
lower := strings.ToLower(got)
for _, junk := range []string{"your reply is parsed as json", "role: title", "role: description", "role: meta", "{{attrs}}", "{{name}}", "{{language}}", "product context"} {
if strings.Contains(lower, junk) {
t.Fatalf("overlay must not carry boilerplate %q:\n%s", junk, got)
}
}
if strings.Contains(got, "OPIS IZDELKA") || strings.Contains(got, "STARO IME IZDELKA") {
t.Fatal("legacy placeholders must be modernized")
}
}
func TestSplitLegacyNonLegacyFallsBackToTemplate(t *testing.T) {
func TestSplitLegacyNonLegacyReturnsEmpty(t *testing.T) {
t.Parallel()
got := SplitLegacyCombinedEnhancePrompt("short retail overlay")
if strings.TrimSpace(got) != strings.TrimSpace(CategoryEnhanceUserTemplate) {
t.Fatalf("non-legacy should fall back to canonical template")
// Non-legacy input has no per-category content to preserve — callers must
// leave the category on the company default instead of storing boilerplate.
if got := SplitLegacyCombinedEnhancePrompt("short retail overlay"); got != "" {
t.Fatalf("non-legacy should return empty, got %q", got)
}
}
+4 -2
View File
@@ -25,8 +25,10 @@ func TestCategoryEnhanceHasRoleSections(t *testing.T) {
if !CategoryEnhancePromptNeedsRepair(old) {
t.Fatal("old attributes-bearing prompt must need repair (attributes section retired)")
}
if CategoryEnhancePromptNeedsRepair(CategoryEnhanceUserTemplate) {
t.Fatal("canonical template must not need repair")
// The built-in template is render-time only; a STORED copy is boilerplate
// pollution and must be repaired (cleared to company default).
if !CategoryEnhancePromptNeedsRepair(CategoryEnhanceUserTemplate) {
t.Fatal("stored canonical template text must need repair")
}
}
@@ -86,12 +86,17 @@ func TestTitleRoleInstructionNeverBrandOnly(t *testing.T) {
if !strings.Contains(CategoryEnhanceUserTemplate, TitleRoleInstruction) {
t.Fatal("CategoryEnhanceUserTemplate must embed TitleRoleInstruction")
}
// Split overlays carry ONLY the legacy per-category text; the never-brand-only
// rule lives in the render-time role framing, not the stored prompt.
split := SplitLegacyCombinedEnhancePrompt(`GPT predloga:
<name>{Napiši tip izdelka, znamka, poln model}</name>
<metaDescription>{140 znakov}</metaDescription>
<H2>{benefit}</H2>`)
if !strings.Contains(split, "never brand-only") {
t.Fatalf("split Title section missing never brand-only: %s", split)
if strings.Contains(split, "never brand-only") {
t.Fatalf("split overlay must not embed role boilerplate: %s", split)
}
if !strings.Contains(split, "Napiši tip izdelka, znamka, poln model") {
t.Fatalf("split Title section must keep the legacy naming text: %s", split)
}
}