You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Align built-in pretranslation with the pretranslation upload API #4526
POST /api/v2/upload/pretranslations/ (#4511) makes different choices from the built-in pretranslation (pontoon/pretranslation/tasks.py). We agreed to bring the two in line rather than let them drift.
Strings with unreviewed suggestions
The built-in pretranslation skips any entity with a non-rejected translation. The API pretranslates it: a matching suggestion becomes the active pretranslation, keeping its author; other suggestions stay unreviewed.
Fuzzy translations already in Pontoon.
Skipped by the same filter above; the API rejects and replaces them.
Failing checks
Both run run_checks(use_tt_checks=False), so TT checks are already disabled in both and should stay disabled. But the built-in retries once with preserve_placeables=True and stores the result unchecked (tasks.py:126-135), then flags it via bulk_run_checks() and skips it during sync; the API drops it, stores nothing, and reports it in failed_checks. → Decide whether a failing pretranslation is stored and flagged, or refused.
Hey @flodolo!
Thanks for checking in! Over the past few days, I have been carefully reading through pontoon/pretranslation/tasks.py, pretranslate.py, import_uploaded_pretranslation in upload.py, and the related tests, to understand how the two paths depend on each other.
I ve been drafting a proposal for the implementation approach, along with suggestions for the 2 open questions(failing checks & rejected bot pretranslations). I'll be dropping it later today. The challenging part was analyzing the different approaches for reusing API logic in the built in path.
Hi! Sorry this took two weeks. Once I dug in, the change turned out to touch more than expected (skip rules, checks, concurrent writes, insights, etc.... ), so I worked through every case before proposing anything. Here's what I found
I really appreciate your input on first.
Not final. I'd like to agree on these points before writing code.
My Proposal
The key point
Today's skip (tasks.py#L83-L113) leaves out any string that has a non-rejected translation, or any translation written by a pretranslation bot, even a rejected one. So every string that gets pretranslated has only rejected, human-written translations.
I propose extracting the decision logic of import_uploaded_pretranslations into a function both paths call, decide_pretranslation() (see Implementation). Given the strings above, though, it would always answer "create a new pretranslation": there's nothing to match, reject or deactivate. So sharing it changes nothing by itself. The behavior change is in which strings get skipped, and the rest of the design depends on that.
Open questions
1. Which strings should the built-in path pretranslate?
String has…
Built-in today
API
My suggestion
Unreviewed suggestion
skipped
pretranslated, suggestion deactivated
align
Fuzzy translation
skipped
rejected and replaced
align
Non-rejected pretranslation
skipped
replaced if different
keep
Rejected bot pretranslation
skipped
pretranslated again
keep
Align the first two. That's what this issue asks for, and it's where decide_pretranslation() actually does something. Cost: MT calls for strings that are skipped today.
Keep the skip for existing pretranslations. It runs before the MT call. Without it, every run would call MT again for every pretranslated string (admin-triggered runs cover all resources). Since MT output varies, pretranslations could be rejected and recreated on each run.
Keep the skip for rejected bot pretranslations. The same cost reason applies, and the rule is deliberate and tested (test_which_strings_to_pretranslate). The API case is different: a human chose to upload that text, possibly from a better source.
Rejecting a translation clears its pretranslated flag, so the rule recognizes bot pretranslations by their author (the TM and Google Translate system users). As a result, a rejected pretranslation that a human uploaded through the API doesn't block the built-in path. I think that's acceptable, since the rule is about the bot not re-proposing its own rejected text, but it's worth knowing.
decide_pretranslation() only sees non-rejected rows (include_rejected=False), so it can't see rejected bot rows. This check has to stay in the skip before the MT call, not move into the function.
2. Failing checks: refuse, or store and flag?
When checks fail, the built-in path already retries with preserve_placeables=True (tasks.py#L126-L140). Today the retried text isn't checked again before it's stored. The question is what happens when it fails too (Again):
Refuse (the API way): the pretranslation isn't stored at all, and the string keeps its current translations. This is consistent with the API, nothing broken is shown to translators, and it's in line with Built-in pretranslation can store translations that still fail checks #4510. The API refuses on warnings too. Cost: refused strings are retried, with new MT calls, on later runs. Sync runs only cover changed paths; admin runs cover everything.
Every rejection stores who did it (rejected_user and the TRANSLATION_REJECTED action log). The built-in path never rejects today, so it has never needed one. If fuzzy translations get replaced, it will reject them, with no human behind the Run.
My Suggestion: do what sync already does when a repository change replaces a translation translations_from_repo.py:L425-L443: log the rejection as an implicit action performed by the Sync user, and leave rejected_user empty. The cost: runs started from the admin page also show "Sync" in the history.
Implementation
Shared decision function
Extract the decision part of import_uploaded_pretranslations (upload.py#L554-L588) into decide_pretranslation(). It takes one string's non-rejected rows and the new value, and returns skipped, unchanged, or a PendingChange. It only decides: updating a matched row stays after the checks, as today (L601-L606).
Import
Importing pontoon.sync.upload at the top of tasks.py creates a cycle: sync.upload imports sync.core.stats, loading that runs sync/core/__init__.py, and that file imports pretranslate from the half-loaded tasks.py. So import it inside the function, as api/views.py does. Moving the function elsewhere doesn't help, because it needs translations_equal from pontoon.sync.core.
In tasks.py
The skip from question 1 stays before get_pretranslation. That includes the rejected-bot check (by author), which decide_pretranslation() can't apply because it never sees rejected rows.
If the retried text still fails checks, it's refused, as MT errors are today. This needs a second run_checks().
Existing rows are loaded once per locale, only for strings that passed the skip.
decide_pretranslation() runs after parsing (L144-L151), with the value passed as message_from_json(value).
Writing
This is only needed if question 1 aligns some of the skips.
Plan: move the write order out of write_changes into a shared function (reject → deactivate → update → create, plus action logs and rejection fields). Who is recorded becomes a parameter:
API: the uploader for everything, as today. It keeps lock_read_translations, storing check results and update_stats(project).
Built-in path: rejections are logged as implicit actions by the Sync user, with rejected_user left empty (question 3). New rows are logged under their bot author. It keeps per-resource stats and latest_translation, and adds a conflict check in a per-locale transaction around the write only. Strings whose rows changed during the MT calls are dropped and retried on the next run.
Why not reuse write_changes as is: it assumes one resource and one user per call (project-wide stats, latest_translation for a single resource), and it raises on conflict, which a background task can't report to anyone.
Why not add the rejections before the built-in bulk_create:
It copies the write order (deactivate before insert, because of the entity_locale_active constraint) and the six rejection fields. The two copies would drift apart.
It still needs the conflict check. Otherwise a suggestion approved during the MT calls gets deactivated, and the unreviewed pretranslation becomes the active one.
It still needs a transaction. If a concurrent upload adds an active row, bulk_create fails, and the rejections written before it stay.
The plan above needs the conflict check and the transaction too (points 2 and 3). The difference is point 1: with a shared write function, there's only one copy of the write code.
I think that the proposal is a bit long so here is a summary.
TLDR
Sharing the API's logic alone changes nothing. The built-in path only pretranslates strings with no translations, or only rejected human-written ones, so the API's decision logic would always just "create". The real change is which strings get skipped.
Which strings: pretranslate strings with unreviewed suggestions or fuzzy translations, like the API does. Keep skipping strings with an existing or rejected bot pretranslation, to avoid repeated MT calls.
Failing checks: if the retried text still fails, don't store it, like the API does.
Who rejects: a background run has no user, so log its rejections under the Sync user, as sync already does.
Implementation: extract the decision logic and the write order from the upload code and share them. The built-in path keeps its own stats and adds a conflict check and a transaction around the write.
These are the points that need your input before I start. The implementation follows from them.
Let's also wait for @mathjazz to chime in, as I won't be able to review in the coming ~10 days.
It keeps per-resource stats and latest_translation, and adds a conflict check in a per-locale transaction around the write only. Strings whose rows changed during the MT calls are dropped and retried on the next run.
lock_read_translations only re-locks the rows that existed when they were first read. In the API, that read happens in the same transaction, right after lock_import_target(), and only the checks run in between, so the window is short. The built-in path reads before the MT calls, so any translation added during those minutes would go unnoticed. It should instead re-read all unrejected rows of the targeted strings inside the write transaction, compare them with the first read, and drop strings that differ.
Plan: move the write order out of write_changes into a shared function (reject → deactivate → update → create, plus action logs and rejection fields). Who is recorded becomes a parameter:
I'd keep a separate write for the built-in path rather than unifying it with write_changes. Who gets recorded is only one of the differences. The built-in path also has several bot authors per batch (MT, GT), stats per resource, a latest_translation per resource, no check results to store, and drops conflicting strings instead of raising. A shared function would need a switch for most of these. Sharing decide_pretranslation() is what keeps the two paths aligned.
▎ Since nothing in a background task catches it, the built-in write should also catch IntegrityError for each locale, log it, and continue.
Calling out that there's a pre-existing race condition with the editor, for both pretranslation and API. That is complicated to fix, since the editor is the busy path, and out of scope here.
Thanks for your feedback. Here is the summary of what I caught from your feedback (to make sure I got you right)
decide_pretranslation() is shared between both paths, so the decisions made (skip, convert, reject, deactivate, create) dont drift.
Whereas, the built-in path keeps its own write: bot authors per row, per-resource stats and latest_translation, and conflicting strings are dropped rather than raising.
Conflict check:
Inside a per-locale transaction around the write, re-read all unrejected rows of the targeted strings, compare them with the first read, and drop the strings that differ. Those strigns will be retried in the next run.
IntegrityError is raised per locale, logged and the run continues.
The race condition with the editor stays is out of scope for now.
Regarding the write, instead fo moving the whole write (reject, deactivate, update, create) into one shared function like suggested in my proposal, we'll keep two separate writes. With two separate writes, one piece of code ends up written twice, which is the same thing Eemeli raised in Add API to upload translated files as pretranslation #4511. Plus, one change in one place, means the two paths will reject differently.
--> That's why I suggest a function like reject_translations (...) used by both, while everything else stays separate as @flodolo said.
In the built-in path, who should be recorded as rejected_user and in the action log when an old pretranslation is rejected: the bot authoring the new pretranslation, or the system user?
The only thing in your proposal I would change is the rejected_user. The actor really is the MT or GT bot that produced the replacement. Recording Sync is misleading, because sync did not even run if the API or batch actions were used, and you lose the ability to tell from the action log which engine displaced what.
Thanks @mathjazz, that makes sense. I'll record the GT/TM bot as the actor.
One side effect tho,
the Insights pretranslation quality chart counts rejections of
bot-authored translations, excluding only those performed by Sync. (pontoon/insights/utils.py: L630)
With the bot as the actor, GT replacing its own older pretranslation would count as a rejection and lower
the approval rate, even though no reviewer was involved. Should I still exclude bot-performed rejections there?
(Could be either here or in a follow-up)
Based on the above: when an existing suggestion is converted, I'd record the bot that produced the
matching pretranslation as the actor. Wdyt?
With the bot as the actor, GT replacing its own older pretranslation would count as a rejection and lower
the approval rate, even though no reviewer was involved. Should I still exclude bot-performed rejections there?
My understanding of your proposal above is that we'd be "skipping strings with an existing or rejected bot pretranslation", so that could only happen when pretranslating via API, i.e. not very often. And it's still a rejection of a pretranslation, so I'd keep the behaviour in the Insights chart.
Based on the above: when an existing suggestion is converted, I'd record the bot that produced the
matching pretranslation as the actor. Wdyt?
Activity
flodolo commentedon Sep 15, 2026
Copying from the previous issue on failing checks.
pretranslate.pyrunsrun_checks()on each pretranslationpontoon/pontoon/pretranslation/tasks.py
Line 126 in 17a20b0
On failure it retries once with
preserve_placeables=True, but if the retry also fails, the result is stored anyway — nothing checks it again.pontoon/pontoon/pretranslation/tasks.py
Line 201 in 17a20b0
#4499 makes these warnings visible and keeps the translation out of sync, but the pretranslation is still created.
baha-bouali commentedon Sep 21, 2026
I'd love to take this!
flodolo commentedon Sep 22, 2026
Assigned. Note that this is quite a bit more complicated than the previous patches you have worked on.
flodolo commentedon Oct 5, 2026
@baha-bouali
Any update on this?
baha-bouali commentedon Oct 5, 2026
Hey @flodolo!
Thanks for checking in! Over the past few days, I have been carefully reading through
pontoon/pretranslation/tasks.py,pretranslate.py,import_uploaded_pretranslationinupload.py, and the related tests, to understand how the two paths depend on each other.I ve been drafting a proposal for the implementation approach, along with suggestions for the 2 open questions(failing checks & rejected bot pretranslations). I'll be dropping it later today. The challenging part was analyzing the different approaches for reusing API logic in the built in path.
baha-bouali commentedon Oct 6, 2026
Hi! Sorry this took two weeks. Once I dug in, the change turned out to touch more than expected (skip rules, checks, concurrent writes, insights, etc.... ), so I worked through every case before proposing anything. Here's what I found
I really appreciate your input on first.
Not final. I'd like to agree on these points before writing code.
My Proposal
The key point
Today's skip (tasks.py#L83-L113) leaves out any string that has a non-rejected translation, or any translation written by a pretranslation bot, even a rejected one. So every string that gets pretranslated has only rejected, human-written translations.
I propose extracting the decision logic of
import_uploaded_pretranslationsinto a function both paths call,decide_pretranslation()(see Implementation). Given the strings above, though, it would always answer "create a new pretranslation": there's nothing to match, reject or deactivate. So sharing it changes nothing by itself. The behavior change is in which strings get skipped, and the rest of the design depends on that.Open questions
1. Which strings should the built-in path pretranslate?
decide_pretranslation()actually does something. Cost: MT calls for strings that are skipped today.test_which_strings_to_pretranslate). The API case is different: a human chose to upload that text, possibly from a better source.pretranslatedflag, so the rule recognizes bot pretranslations by their author (the TM and Google Translate system users). As a result, a rejected pretranslation that a human uploaded through the API doesn't block the built-in path. I think that's acceptable, since the rule is about the bot not re-proposing its own rejected text, but it's worth knowing.decide_pretranslation()only sees non-rejected rows (include_rejected=False), so it can't see rejected bot rows. This check has to stay in the skip before the MT call, not move into the function.2. Failing checks: refuse, or store and flag?
When checks fail, the built-in path already retries with
preserve_placeables=True(tasks.py#L126-L140). Today the retried text isn't checked again before it's stored. The question is what happens when it fails too (Again):My suggestion is to refuse
3. Who is recorded as rejecting?
Every rejection stores who did it (
rejected_userand theTRANSLATION_REJECTEDaction log). The built-in path never rejects today, so it has never needed one. If fuzzy translations get replaced, it will reject them, with no human behind the Run.My Suggestion: do what sync already does when a repository change replaces a translation
translations_from_repo.py:L425-L443: log the rejection as an implicit action performed by the Sync user, and leaverejected_userempty. The cost: runs started from the admin page also show "Sync" in the history.Implementation
Shared decision function
Extract the decision part of
import_uploaded_pretranslations(upload.py#L554-L588) intodecide_pretranslation(). It takes one string's non-rejected rows and the new value, and returns skipped, unchanged, or aPendingChange. It only decides: updating a matched row stays after the checks, as today (L601-L606).Import
Importing
pontoon.sync.uploadat the top oftasks.pycreates a cycle:sync.uploadimportssync.core.stats, loading that runssync/core/__init__.py, and that file importspretranslatefrom the half-loadedtasks.py. So import it inside the function, asapi/views.pydoes. Moving the function elsewhere doesn't help, because it needstranslations_equalfrompontoon.sync.core.In
tasks.pyget_pretranslation. That includes the rejected-bot check (by author), whichdecide_pretranslation()can't apply because it never sees rejected rows.run_checks().decide_pretranslation()runs after parsing (L144-L151), with the value passed asmessage_from_json(value).Writing
This is only needed if question 1 aligns some of the skips.
Plan: move the write order out of
write_changesinto a shared function (reject → deactivate → update → create, plus action logs and rejection fields). Who is recorded becomes a parameter:lock_read_translations, storing check results andupdate_stats(project).rejected_userleft empty (question 3). New rows are logged under their bot author. It keeps per-resource stats andlatest_translation, and adds a conflict check in a per-locale transaction around the write only. Strings whose rows changed during the MT calls are dropped and retried on the next run.Why not reuse
write_changesas is: it assumes one resource and one user per call (project-wide stats,latest_translationfor a single resource), and it raises on conflict, which a background task can't report to anyone.Why not add the rejections before the built-in
bulk_create:entity_locale_activeconstraint) and the six rejection fields. The two copies would drift apart.bulk_createfails, and the rejections written before it stay.The plan above needs the conflict check and the transaction too (points 2 and 3). The difference is point 1: with a shared write function, there's only one copy of the write code.
baha-bouali commentedon Oct 6, 2026
I think that the proposal is a bit long so here is a summary.
TLDR
These are the points that need your input before I start. The implementation follows from them.
flodolo commentedon Oct 6, 2026
Let's also wait for @mathjazz to chime in, as I won't be able to review in the coming ~10 days.
lock_read_translationsonly re-locks the rows that existed when they were first read. In the API, that read happens in the same transaction, right afterlock_import_target(), and only the checks run in between, so the window is short. The built-in path reads before the MT calls, so any translation added during those minutes would go unnoticed. It should instead re-read all unrejected rows of the targeted strings inside the write transaction, compare them with the first read, and drop strings that differ.I'd keep a separate write for the built-in path rather than unifying it with
write_changes. Who gets recorded is only one of the differences. The built-in path also has several bot authors per batch (MT, GT), stats per resource, alatest_translationper resource, no check results to store, and drops conflicting strings instead of raising. A shared function would need a switch for most of these. Sharingdecide_pretranslation()is what keeps the two paths aligned.▎ Since nothing in a background task catches it, the built-in write should also catch IntegrityError for each locale, log it, and continue.
flodolo commentedon Oct 6, 2026
Calling out that there's a pre-existing race condition with the editor, for both pretranslation and API. That is complicated to fix, since the editor is the busy path, and out of scope here.
baha-bouali commentedon Oct 7, 2026
Thanks for your feedback. Here is the summary of what I caught from your feedback (to make sure I got you right)
decide_pretranslation()is shared between both paths, so the decisions made (skip, convert, reject, deactivate, create) dont drift.Whereas, the built-in path keeps its own write: bot authors per row, per-resource stats and
latest_translation, and conflicting strings are dropped rather than raising.Conflict check:
Inside a per-locale transaction around the write, re-read all unrejected rows of the targeted strings, compare them with the first read, and drop the strings that differ. Those strigns will be retried in the next run.
IntegrityErroris raised per locale, logged and the run continues.The race condition with the editor stays is out of scope for now.
baha-bouali commentedon Oct 7, 2026
--> That's why I suggest a function like
reject_translations (...)used by both, while everything else stays separate as @flodolo said.rejected_userand in the action log when an old pretranslation is rejected: the bot authoring the new pretranslation, or the system user?flodolo commentedon Oct 7, 2026
That looks right (but the devil is in the (implementation) details) ;-)
mathjazz commentedon Oct 14, 2026
Great work, @baha-bouali!
The only thing in your proposal I would change is the
rejected_user. The actor really is the MT or GT bot that produced the replacement. Recording Sync is misleading, because sync did not even run if the API or batch actions were used, and you lose the ability to tell from the action log which engine displaced what.baha-bouali commentedon Oct 14, 2026
Thanks @mathjazz, that makes sense. I'll record the GT/TM bot as the actor.
One side effect tho,
the Insights pretranslation quality chart counts rejections of
bot-authored translations, excluding only those performed by Sync. (
pontoon/insights/utils.py: L630)With the bot as the actor, GT replacing its own older pretranslation would count as a rejection and lower
the approval rate, even though no reviewer was involved. Should I still exclude bot-performed rejections there?
(Could be either here or in a follow-up)
Based on the above: when an existing suggestion is converted, I'd record the bot that produced the
matching pretranslation as the actor. Wdyt?
mathjazz commentedon Oct 14, 2026
My understanding of your proposal above is that we'd be "skipping strings with an existing or rejected bot pretranslation", so that could only happen when pretranslating via API, i.e. not very often. And it's still a rejection of a pretranslation, so I'd keep the behaviour in the Insights chart.
See the Strings with unreviewed suggestions section: "a matching suggestion becomes the active pretranslation, keeping its author".