fix
This commit is contained in:
1 parent
8b540d22bc
commit
c92ee93482
24 files changed
+1495
-327
No files matched your search
@@ -124,21 +124,39 @@ func loadTranslationCache(ctx context.Context, pool *pgxpool.Pool) (map[string]*
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// saveDefaultLanguage is safe to resume and cannot overwrite an existing
|
||||
// translation, including an administrator's concurrent edit to the cache.
|
||||
// saveDefaultLanguage owns ONLY the shared platform-default cache. Unlike tenant
|
||||
// prompts, this is generated application data: outdated translations must be
|
||||
// refreshed on a release. Earlier versions used DO NOTHING and permanently
|
||||
// retained the English five-benefit scaffold in this cache.
|
||||
func saveDefaultLanguage(ctx context.Context, pool *pgxpool.Pool, hash, source, lang, value string) (bool, error) {
|
||||
_, err := pool.Exec(ctx, `INSERT INTO default_category_prompt_translations(source_hash, source_prompt)
|
||||
VALUES($1, $2) ON CONFLICT (source_hash) DO NOTHING`, hash, source)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
result, err := pool.Exec(ctx, `UPDATE default_category_prompt_translations
|
||||
SET translations = jsonb_set(translations, ARRAY[$3::text], to_jsonb($4::text), true), updated_at = now()
|
||||
WHERE source_hash = $1 AND source_prompt = $2 AND COALESCE(btrim(translations->>$3), '') = ''`,
|
||||
result, err := pool.Exec(ctx, `INSERT INTO default_category_prompt_translations
|
||||
(source_hash, source_prompt, translations, updated_at)
|
||||
VALUES ($1, $2, jsonb_build_object($3::text, $4::text), now())
|
||||
ON CONFLICT (source_hash) DO UPDATE SET
|
||||
translations = jsonb_set(default_category_prompt_translations.translations,
|
||||
ARRAY[$3::text], to_jsonb($4::text), true), updated_at = now()
|
||||
WHERE default_category_prompt_translations.source_prompt = EXCLUDED.source_prompt
|
||||
AND default_category_prompt_translations.translations->>$3 IS DISTINCT FROM $4`,
|
||||
hash, source, lang, value)
|
||||
return result.RowsAffected() > 0, err
|
||||
}
|
||||
|
||||
// Historical cache snapshots are read-only proof of previously shipped platform
|
||||
// translations. They may be used for exact-match migration, NEVER as replacement
|
||||
// text. This keeps custom company wording protected.
|
||||
func knownCacheValues(source string, current, historical map[string]*defaultTranslation, lang string) []string {
|
||||
values := make([]string, 0, 2)
|
||||
hash := catalog.DefaultPromptHash(source)
|
||||
for _, cache := range []map[string]*defaultTranslation{current, historical} {
|
||||
if entry := cache[hash]; entry != nil {
|
||||
if value := strings.TrimSpace(entry.Values[lang]); value != "" {
|
||||
values = append(values, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
// extendDefaultPrompt inserts only missing language variants. The SQL compares
|
||||
// the full source snapshot: edits made during a long translation run are safe.
|
||||
func extendDefaultPrompt(ctx context.Context, pool *pgxpool.Pool, row categoryRow, expectedSource string, cached company.LangPromptMap, langs []string) (bool, error) {
|
||||
@@ -240,7 +258,7 @@ func isMixedBuiltInEnglishPrompt(value string, versions []aiprompts.CategoryForm
|
||||
// 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 {
|
||||
func isKnownBuiltInVariantSet(prompts company.LangPromptMap, versions []aiprompts.CategoryFormulaDefaults, cache, historical map[string]*defaultTranslation) bool {
|
||||
allowedEN := map[string]bool{}
|
||||
allowedLocalized := map[string]map[string]bool{}
|
||||
for _, version := range versions {
|
||||
@@ -248,19 +266,21 @@ func isKnownBuiltInVariantSet(prompts company.LangPromptMap, versions []aiprompt
|
||||
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
|
||||
for _, previous := range []map[string]*defaultTranslation{cache, historical} {
|
||||
if entry := previous[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
|
||||
}
|
||||
if lang == "en" {
|
||||
allowedEN[strings.TrimSpace(val)] = true
|
||||
continue
|
||||
}
|
||||
if allowedLocalized[lang] == nil {
|
||||
allowedLocalized[lang] = map[string]bool{}
|
||||
}
|
||||
allowedLocalized[lang][strings.TrimSpace(val)] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -330,7 +350,7 @@ func upgradeOldDefault(ctx context.Context, pool *pgxpool.Pool, row categoryRow,
|
||||
// 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 {
|
||||
func replaceKnownBuiltInLocales(prompts company.LangPromptMap, versions []aiprompts.CategoryFormulaDefaults, cache, historical map[string]*defaultTranslation) int {
|
||||
if len(versions) == 0 {
|
||||
return 0
|
||||
}
|
||||
@@ -352,20 +372,23 @@ func replaceKnownBuiltInLocales(prompts company.LangPromptMap, versions []aiprom
|
||||
if replacement == "" || replacement == val {
|
||||
continue
|
||||
}
|
||||
for _, prior := range versions[1:] {
|
||||
for _, prior := range versions {
|
||||
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])
|
||||
known := []string{}
|
||||
if lang == "*" || lang == "en" {
|
||||
known = append(known, prior.Overlay)
|
||||
} else {
|
||||
known = knownCacheValues(prior.Overlay, cache, historical, lang)
|
||||
}
|
||||
if (val == strings.TrimSpace(old) && old != "") ||
|
||||
((lang == "*" || lang == "en") && isMixedBuiltInEnglishPrompt(val, versions)) {
|
||||
matched := (lang == "*" || lang == "en") && isMixedBuiltInEnglishPrompt(val, versions)
|
||||
for _, old := range known {
|
||||
if val == strings.TrimSpace(old) && old != "" {
|
||||
matched = true
|
||||
}
|
||||
}
|
||||
if matched {
|
||||
prompts[lang] = replacement
|
||||
updates++
|
||||
break
|
||||
@@ -377,26 +400,43 @@ func replaceKnownBuiltInLocales(prompts company.LangPromptMap, versions []aiprom
|
||||
|
||||
// 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, cache map[string]*defaultTranslation, dryRun bool) (int, int, bool, error) {
|
||||
func normalizeA1Category(ctx context.Context, pool *pgxpool.Pool, row categoryRow, cache, historical map[string]*defaultTranslation, dryRun bool) (int, int, bool, error) {
|
||||
next := make(company.LangPromptMap, len(row.Prompts))
|
||||
removed := 0
|
||||
metaUpdated := 0
|
||||
for lang, value := range row.Prompts {
|
||||
cleaned, n := aiprompts.NormalizeA1RepeatedBenefits(value)
|
||||
next[lang] = cleaned
|
||||
removed += n
|
||||
locale := lang
|
||||
if locale == "*" || !company.IsAllowedLanguage(locale) {
|
||||
locale = "sl"
|
||||
}
|
||||
if metaClean, changed := aiprompts.NormalizeA1MetaRole(cleaned, locale); changed {
|
||||
cleaned = metaClean
|
||||
metaUpdated++
|
||||
}
|
||||
next[lang] = cleaned
|
||||
}
|
||||
def := aiprompts.DefaultCategoryFormula(row.UID, row.Name)
|
||||
versions := []aiprompts.CategoryFormulaDefaults{def,
|
||||
aiprompts.PreviousCanonicalV6CategoryFormula(row.UID, row.Name),
|
||||
aiprompts.PreviousSEODefaultCategoryFormula(row.UID, row.Name),
|
||||
aiprompts.PriorDefaultCategoryFormula(row.UID, row.Name),
|
||||
aiprompts.LegacyDefaultCategoryFormula(row.UID, row.Name)}
|
||||
localesUpgraded := replaceKnownBuiltInLocales(next, versions, cache)
|
||||
localesUpgraded := replaceKnownBuiltInLocales(next, versions, cache, historical)
|
||||
desc, n, err := aiprompts.NormalizeA1DescriptionTemplate(row.Desc)
|
||||
if err != nil {
|
||||
return 0, 0, false, err
|
||||
}
|
||||
removed += n
|
||||
if removed == 0 && localesUpgraded == 0 || dryRun {
|
||||
metaLocale := "sl"
|
||||
if normalized, repaired, normalizeErr := aiprompts.NormalizeA1MetaTemplate(desc, metaLocale); normalizeErr != nil {
|
||||
return 0, 0, false, normalizeErr
|
||||
} else if repaired {
|
||||
desc = normalized
|
||||
metaUpdated++
|
||||
}
|
||||
if (removed == 0 && localesUpgraded == 0 && metaUpdated == 0) || dryRun {
|
||||
return removed, localesUpgraded, false, nil
|
||||
}
|
||||
encoded, err := company.EncodeLangPromptMap(next)
|
||||
@@ -456,6 +496,28 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Keep the old cache for exact-match detection when correcting stale records.
|
||||
historical := make(map[string]*defaultTranslation, len(cache))
|
||||
for key, entry := range cache {
|
||||
values := make(company.LangPromptMap, len(entry.Values))
|
||||
for lang, value := range entry.Values {
|
||||
values[lang] = value
|
||||
}
|
||||
historical[key] = &defaultTranslation{Source: entry.Source, Values: values}
|
||||
}
|
||||
// An exact frozen v6 bundle supplies migration evidence even when a past
|
||||
// deployment failed to populate its shared translation cache.
|
||||
previous := aiprompts.PreviousCanonicalV6CategoryFormula("", "")
|
||||
oldHash := catalog.DefaultPromptHash(previous.Overlay)
|
||||
if historical[oldHash] == nil {
|
||||
oldValues := make(company.LangPromptMap)
|
||||
for _, lang := range company.ContentLanguages {
|
||||
if old, loadErr := aiprompts.PreviousOfflineDefaultPromptV6(lang); loadErr == nil {
|
||||
oldValues[lang] = old
|
||||
}
|
||||
}
|
||||
historical[oldHash] = &defaultTranslation{Source: previous.Overlay, Values: oldValues}
|
||||
}
|
||||
// Always include the generic default and the complete bundled taxonomy.
|
||||
// One translation cache is shared across EVERY company, including future ones.
|
||||
sources := aiprompts.AllDefaultCategoryPrompts()
|
||||
@@ -474,39 +536,29 @@ func main() {
|
||||
log.Fatalf("shared default hash collision for %s", hash)
|
||||
}
|
||||
for _, lang := range targetLangs {
|
||||
if strings.TrimSpace(entry.Values[lang]) != "" {
|
||||
continue
|
||||
}
|
||||
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
|
||||
if lang != "en" {
|
||||
translated, err = aiprompts.OfflineDefaultPrompt(lang)
|
||||
if err != nil {
|
||||
failures++
|
||||
log.Printf("FAIL default=%d/%d lang=%s: %v", i+1, len(sources), lang, err)
|
||||
log.Printf("FAIL bundled default=%d lang=%s: %v", i+1, lang, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
saved, err := saveDefaultLanguage(ctx, pool, hash, source, lang, translated)
|
||||
if err != nil {
|
||||
if entry.Values[lang] == translated {
|
||||
continue
|
||||
}
|
||||
missing++
|
||||
if *dryRun {
|
||||
// A dry run must compare against the corrected bundle, not an
|
||||
// old value from the database cache.
|
||||
entry.Values[lang] = translated
|
||||
continue
|
||||
}
|
||||
saved, saveErr := saveDefaultLanguage(ctx, pool, hash, source, lang, translated)
|
||||
if saveErr != nil {
|
||||
failures++
|
||||
log.Printf("FAIL storing default=%d lang=%s: %v", i+1, lang, err)
|
||||
log.Printf("FAIL storing default=%d lang=%s: %v", i+1, lang, saveErr)
|
||||
continue
|
||||
}
|
||||
if saved {
|
||||
@@ -537,7 +589,7 @@ func main() {
|
||||
if row.Company.String() == "604f23a8-b66e-4b21-8b45-0d72b68f4790" ||
|
||||
strings.EqualFold(strings.TrimSpace(row.CompanyName), "A1 Slovenija") {
|
||||
protected++
|
||||
removed, localized, updated, cleanErr := normalizeA1Category(ctx, pool, row, cache, *dryRun)
|
||||
removed, localized, updated, cleanErr := normalizeA1Category(ctx, pool, row, cache, historical, *dryRun)
|
||||
if cleanErr != nil {
|
||||
failures++
|
||||
log.Printf("FAIL cleaning A1 category=%s: %v", row.UID, cleanErr)
|
||||
@@ -576,11 +628,12 @@ func main() {
|
||||
// prior migrations. Never touch any tenant-authored language or formula.
|
||||
versions := []aiprompts.CategoryFormulaDefaults{
|
||||
def,
|
||||
aiprompts.PreviousCanonicalV6CategoryFormula(row.UID, row.Name),
|
||||
aiprompts.PreviousSEODefaultCategoryFormula(row.UID, row.Name),
|
||||
aiprompts.PriorDefaultCategoryFormula(row.UID, row.Name),
|
||||
aiprompts.LegacyDefaultCategoryFormula(row.UID, row.Name),
|
||||
}
|
||||
if !isKnownBuiltInVariantSet(row.Prompts, versions, cache) ||
|
||||
if !isKnownBuiltInVariantSet(row.Prompts, versions, cache, historical) ||
|
||||
!isKnownBuiltInFormula(row.Title, versions, true) ||
|
||||
!isKnownBuiltInFormula(row.Desc, versions, false) {
|
||||
custom++
|
||||
|
||||
Reference in new issue
Block a user