fixes
This commit is contained in:
1 parent
3b0d692839
commit
8b540d22bc
14 files changed
+526
-82
No files matched your search
@@ -194,6 +194,110 @@ func matchesDefaultFormula(current, previous string) bool {
|
||||
return string(left) == string(right)
|
||||
}
|
||||
|
||||
// isMixedBuiltInEnglishPrompt recognizes a historical default whose three
|
||||
// roles came from different RELEASED defaults. A full role scaffold is required;
|
||||
// each role must be an EXACT prior built-in fragment. Never match arbitrary
|
||||
// user-written instructions by keywords or number of paragraphs.
|
||||
func isMixedBuiltInEnglishPrompt(value string, versions []aiprompts.CategoryFormulaDefaults) bool {
|
||||
markers := [][2]string{
|
||||
{aiprompts.SectionTitleStart, aiprompts.SectionTitleEnd},
|
||||
{aiprompts.SectionDescriptionStart, aiprompts.SectionDescriptionEnd},
|
||||
{aiprompts.SectionMetaStart, aiprompts.SectionMetaEnd},
|
||||
}
|
||||
if !aiprompts.CategoryEnhanceHasRoleSections(value) {
|
||||
return false
|
||||
}
|
||||
// No unrecognized preamble, trailing content, or hidden custom section.
|
||||
if strings.TrimSpace(strings.ReplaceAll(value, "\r\n", "\n")) != aiprompts.BuildCategoryEnhanceOverlay(aiprompts.LegacyEnhanceParts{
|
||||
TitleRules: aiprompts.ExtractEnhanceSectionBody(value, markers[0][0], markers[0][1]),
|
||||
DescriptionRules: aiprompts.ExtractEnhanceSectionBody(value, markers[1][0], markers[1][1]),
|
||||
MetaRules: aiprompts.ExtractEnhanceSectionBody(value, markers[2][0], markers[2][1]),
|
||||
WasLegacy: true,
|
||||
}) {
|
||||
return false
|
||||
}
|
||||
for _, pair := range markers {
|
||||
text := aiprompts.ExtractEnhanceSectionBody(value, pair[0], pair[1])
|
||||
if text == "" {
|
||||
return false
|
||||
}
|
||||
known := false
|
||||
for _, ver := range versions {
|
||||
if text == aiprompts.ExtractEnhanceSectionBody(ver.Overlay, pair[0], pair[1]) {
|
||||
known = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !known {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// isKnownBuiltInVariantSet permits migrations from *mixed* released platform
|
||||
// versions (for example, English from the old five-H2 default and German from
|
||||
// the SEO-v4 release). Every non-empty language must match a known, exact
|
||||
// built-in value. One company-authored field disqualifies the entire category.
|
||||
// Cache lookups are read-only; no fuzzy matches on real tenant instructions.
|
||||
func isKnownBuiltInVariantSet(prompts company.LangPromptMap, versions []aiprompts.CategoryFormulaDefaults, cache map[string]*defaultTranslation) bool {
|
||||
allowedEN := map[string]bool{}
|
||||
allowedLocalized := map[string]map[string]bool{}
|
||||
for _, version := range versions {
|
||||
if version.Overlay == "" {
|
||||
continue
|
||||
}
|
||||
allowedEN[strings.TrimSpace(version.Overlay)] = true
|
||||
if entry := cache[catalog.DefaultPromptHash(version.Overlay)]; entry != nil {
|
||||
for lang, val := range entry.Values {
|
||||
if !company.IsAllowedLanguage(lang) || strings.TrimSpace(val) == "" {
|
||||
continue
|
||||
}
|
||||
if lang == "en" {
|
||||
allowedEN[strings.TrimSpace(val)] = true
|
||||
continue
|
||||
}
|
||||
if allowedLocalized[lang] == nil {
|
||||
allowedLocalized[lang] = map[string]bool{}
|
||||
}
|
||||
allowedLocalized[lang][strings.TrimSpace(val)] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
for lang, val := range prompts {
|
||||
val = strings.TrimSpace(val)
|
||||
if val == "" {
|
||||
continue
|
||||
}
|
||||
if lang == "*" || lang == "en" {
|
||||
if !allowedEN[val] && !isMixedBuiltInEnglishPrompt(val, versions) {
|
||||
return false
|
||||
}
|
||||
} else if !company.IsAllowedLanguage(lang) || !allowedLocalized[lang][val] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Only exact former built-in JSON is eligible for an upgrade, even when the
|
||||
// prompt and formula columns happen to be from different older deployments.
|
||||
func isKnownBuiltInFormula(current string, versions []aiprompts.CategoryFormulaDefaults, title bool) bool {
|
||||
if matchesDefaultFormula(current, "") {
|
||||
return true
|
||||
}
|
||||
for _, version := range versions {
|
||||
compare := version.DescriptionTemplateJSON
|
||||
if title {
|
||||
compare = version.TitleTemplateJSON
|
||||
}
|
||||
if matchesDefaultFormula(current, compare) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// upgradeOldDefault is scoped to a single category and guarded by an exact
|
||||
// snapshot of its three current columns, so concurrent edits are not overwritten.
|
||||
func upgradeOldDefault(ctx context.Context, pool *pgxpool.Pool, row categoryRow, def aiprompts.CategoryFormulaDefaults, translations company.LangPromptMap) (bool, error) {
|
||||
@@ -222,9 +326,58 @@ func upgradeOldDefault(ctx context.Context, pool *pgxpool.Pool, row categoryRow,
|
||||
return result.RowsAffected() > 0, err
|
||||
}
|
||||
|
||||
// replaceKnownBuiltInLocales upgrades only exact released DEFAULT locale values.
|
||||
// A1 can contain its own custom Slovenian category prompt alongside an old
|
||||
// platform-generated English translation. Preserve its custom languages, name
|
||||
// and meta formulas while replacing the old built-in English/other languages.
|
||||
func replaceKnownBuiltInLocales(prompts company.LangPromptMap, versions []aiprompts.CategoryFormulaDefaults, cache map[string]*defaultTranslation) int {
|
||||
if len(versions) == 0 {
|
||||
return 0
|
||||
}
|
||||
canonical := versions[0]
|
||||
current := cache[catalog.DefaultPromptHash(canonical.Overlay)]
|
||||
if current == nil {
|
||||
return 0
|
||||
}
|
||||
updates := 0
|
||||
for lang, text := range prompts {
|
||||
val := strings.TrimSpace(text)
|
||||
if val == "" {
|
||||
continue
|
||||
}
|
||||
replacement := strings.TrimSpace(current.Values[lang])
|
||||
if lang == "*" || lang == "en" {
|
||||
replacement = canonical.Overlay
|
||||
}
|
||||
if replacement == "" || replacement == val {
|
||||
continue
|
||||
}
|
||||
for _, prior := range versions[1:] {
|
||||
if prior.Overlay == "" {
|
||||
continue
|
||||
}
|
||||
old := prior.Overlay
|
||||
if lang != "*" && lang != "en" {
|
||||
entry := cache[catalog.DefaultPromptHash(prior.Overlay)]
|
||||
if entry == nil {
|
||||
continue
|
||||
}
|
||||
old = strings.TrimSpace(entry.Values[lang])
|
||||
}
|
||||
if (val == strings.TrimSpace(old) && old != "") ||
|
||||
((lang == "*" || lang == "en") && isMixedBuiltInEnglishPrompt(val, versions)) {
|
||||
prompts[lang] = replacement
|
||||
updates++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return updates
|
||||
}
|
||||
|
||||
// normalizeA1Category differentiates repetitive legacy A1 benefit instructions.
|
||||
// It neither translates nor changes A1 naming/meta instructions.
|
||||
func normalizeA1Category(ctx context.Context, pool *pgxpool.Pool, row categoryRow, dryRun bool) (int, bool, error) {
|
||||
func normalizeA1Category(ctx context.Context, pool *pgxpool.Pool, row categoryRow, cache map[string]*defaultTranslation, dryRun bool) (int, int, bool, error) {
|
||||
next := make(company.LangPromptMap, len(row.Prompts))
|
||||
removed := 0
|
||||
for lang, value := range row.Prompts {
|
||||
@@ -232,17 +385,23 @@ func normalizeA1Category(ctx context.Context, pool *pgxpool.Pool, row categoryRo
|
||||
next[lang] = cleaned
|
||||
removed += n
|
||||
}
|
||||
def := aiprompts.DefaultCategoryFormula(row.UID, row.Name)
|
||||
versions := []aiprompts.CategoryFormulaDefaults{def,
|
||||
aiprompts.PreviousSEODefaultCategoryFormula(row.UID, row.Name),
|
||||
aiprompts.PriorDefaultCategoryFormula(row.UID, row.Name),
|
||||
aiprompts.LegacyDefaultCategoryFormula(row.UID, row.Name)}
|
||||
localesUpgraded := replaceKnownBuiltInLocales(next, versions, cache)
|
||||
desc, n, err := aiprompts.NormalizeA1DescriptionTemplate(row.Desc)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
return 0, 0, false, err
|
||||
}
|
||||
removed += n
|
||||
if removed == 0 || dryRun {
|
||||
return removed, false, nil
|
||||
if removed == 0 && localesUpgraded == 0 || dryRun {
|
||||
return removed, localesUpgraded, false, nil
|
||||
}
|
||||
encoded, err := company.EncodeLangPromptMap(next)
|
||||
if err != nil {
|
||||
return removed, false, err
|
||||
return removed, localesUpgraded, false, err
|
||||
}
|
||||
result, err := pool.Exec(ctx, `UPDATE categories SET prompt = $4::jsonb,
|
||||
description_template = NULLIF($5,'')::jsonb, updated_at = now()
|
||||
@@ -251,9 +410,9 @@ func normalizeA1Category(ctx context.Context, pool *pgxpool.Pool, row categoryRo
|
||||
AND COALESCE(description_template::text,'') = $6`,
|
||||
row.ID, row.Company, string(row.Raw), string(encoded), desc, row.Desc)
|
||||
if err != nil {
|
||||
return removed, false, err
|
||||
return removed, localesUpgraded, false, err
|
||||
}
|
||||
return removed, result.RowsAffected() == 1, nil
|
||||
return removed, localesUpgraded, result.RowsAffected() == 1, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -320,6 +479,19 @@ func main() {
|
||||
}
|
||||
missing++
|
||||
if *dryRun {
|
||||
// Simulate bundled values in memory so the preview classifies
|
||||
// existing mixed-language defaults exactly as the real run.
|
||||
preview := source
|
||||
if lang != "en" {
|
||||
var previewErr error
|
||||
preview, previewErr = aiprompts.OfflineDefaultPrompt(lang)
|
||||
if previewErr != nil {
|
||||
failures++
|
||||
log.Printf("FAIL preview language=%s: %v", lang, previewErr)
|
||||
continue
|
||||
}
|
||||
}
|
||||
entry.Values[lang] = preview
|
||||
continue
|
||||
}
|
||||
translated := source
|
||||
@@ -358,19 +530,20 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
eligible, changed, custom, upgraded, protected, a1Removed, a1Updated := 0, 0, 0, 0, 0, 0, 0
|
||||
eligible, changed, custom, upgraded, protected, a1Removed, a1Updated, a1Locales := 0, 0, 0, 0, 0, 0, 0, 0
|
||||
for _, row := range categories {
|
||||
// A1 has its own Slovenian category instructions. Never apply the
|
||||
// global platform-default backfill to this customer.
|
||||
if row.Company.String() == "604f23a8-b66e-4b21-8b45-0d72b68f4790" ||
|
||||
strings.EqualFold(strings.TrimSpace(row.CompanyName), "A1 Slovenija") {
|
||||
protected++
|
||||
removed, updated, cleanErr := normalizeA1Category(ctx, pool, row, *dryRun)
|
||||
removed, localized, updated, cleanErr := normalizeA1Category(ctx, pool, row, cache, *dryRun)
|
||||
if cleanErr != nil {
|
||||
failures++
|
||||
log.Printf("FAIL cleaning A1 category=%s: %v", row.UID, cleanErr)
|
||||
} else {
|
||||
a1Removed += removed
|
||||
a1Locales += localized
|
||||
if updated {
|
||||
a1Updated++
|
||||
}
|
||||
@@ -384,7 +557,8 @@ func main() {
|
||||
} // -limit pilot excludes some templates
|
||||
if catalog.IsUntouchedDefaultPrompt(row.Prompts, def.Overlay, entry.Values) &&
|
||||
matchesDefaultFormula(row.Title, def.TitleTemplateJSON) &&
|
||||
matchesDefaultFormula(row.Desc, def.DescriptionTemplateJSON) {
|
||||
matchesDefaultFormula(row.Desc, def.DescriptionTemplateJSON) &&
|
||||
strings.TrimSpace(row.Title) != "" && strings.TrimSpace(row.Desc) != "" {
|
||||
eligible++
|
||||
if *dryRun {
|
||||
continue
|
||||
@@ -398,30 +572,17 @@ func main() {
|
||||
}
|
||||
continue
|
||||
}
|
||||
// Upgrade ONLY exact platform-generated copies, including the prior
|
||||
// offline release. Custom company instructions are never replaced.
|
||||
foundPrevious := false
|
||||
for _, previous := range []aiprompts.CategoryFormulaDefaults{
|
||||
// Upgrade *only* built-in snapshots, including mixed versions left by
|
||||
// prior migrations. Never touch any tenant-authored language or formula.
|
||||
versions := []aiprompts.CategoryFormulaDefaults{
|
||||
def,
|
||||
aiprompts.PreviousSEODefaultCategoryFormula(row.UID, row.Name),
|
||||
aiprompts.PriorDefaultCategoryFormula(row.UID, row.Name),
|
||||
aiprompts.LegacyDefaultCategoryFormula(row.UID, row.Name),
|
||||
} {
|
||||
if previous.Overlay == "" || previous.Overlay == def.Overlay {
|
||||
continue
|
||||
}
|
||||
oldEntry := cache[catalog.DefaultPromptHash(previous.Overlay)]
|
||||
oldTranslations := company.LangPromptMap{}
|
||||
if oldEntry != nil {
|
||||
oldTranslations = oldEntry.Values
|
||||
}
|
||||
if !catalog.IsUntouchedDefaultPrompt(row.Prompts, previous.Overlay, oldTranslations) ||
|
||||
!matchesDefaultFormula(row.Title, previous.TitleTemplateJSON) ||
|
||||
!matchesDefaultFormula(row.Desc, previous.DescriptionTemplateJSON) {
|
||||
continue
|
||||
}
|
||||
foundPrevious = true
|
||||
break
|
||||
}
|
||||
if !foundPrevious {
|
||||
if !isKnownBuiltInVariantSet(row.Prompts, versions, cache) ||
|
||||
!isKnownBuiltInFormula(row.Title, versions, true) ||
|
||||
!isKnownBuiltInFormula(row.Desc, versions, false) {
|
||||
custom++
|
||||
continue
|
||||
}
|
||||
@@ -440,7 +601,7 @@ func main() {
|
||||
}
|
||||
|
||||
}
|
||||
log.Printf("offline shared default translations: languages=%d templates=%d missing=%d saved=%d eligible=%d updated=%d legacy_upgraded=%d custom_untouched=%d A1_protected=%d A1_legacy_sections_normalized=%d A1_categories_updated=%d failures=%d dry_run=%v", len(targetLangs), len(sources), missing, inserted, eligible, changed, upgraded, custom, protected, a1Removed, a1Updated, failures, *dryRun)
|
||||
log.Printf("offline shared default translations: languages=%d templates=%d missing=%d saved=%d eligible=%d updated=%d legacy_upgraded=%d custom_untouched=%d A1_protected=%d A1_legacy_sections_normalized=%d A1_default_locales_upgraded=%d A1_categories_updated=%d failures=%d dry_run=%v", len(targetLangs), len(sources), missing, inserted, eligible, changed, upgraded, custom, protected, a1Removed, a1Locales, a1Updated, failures, *dryRun)
|
||||
if *dryRun && missing > 0 {
|
||||
log.Printf("dry-run: the full run loads all 25 prelocalized language templates from disk without an LLM")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user