Skip to content

Align built-in pretranslation with the pretranslation upload API #4526

Closed

Description

@flodolo

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.

See thread

Activity

  1. added
    P3
    on Sep 14, 2026
  2. moved this from 🆕 Needs triage to 🔖 Ready in Pontoon Roadmapon Sep 14, 2026
  3. added theissue type on Sep 14, 2026
  4. flodolo commented on Sep 15, 2026

    @flodolo
    CollaboratorAuthor

    Copying from the previous issue on failing checks.

    pretranslate.py runs run_checks() on each pretranslation

    failed_checks = run_checks(

    On failure it retries once with preserve_placeables=True, but if the retry also fails, the result is stored anyway — nothing checks it again.

    bulk_run_checks(Translation.objects.for_checks().filter(pk__in=translation_pks))

    #4499 makes these warnings visible and keeps the translation out of sync, but the pretranslation is still created.

  5. baha-bouali commented on Sep 21, 2026

    @baha-bouali
    Contributor

    I'd love to take this!

  6. flodolo commented on Sep 22, 2026

    @flodolo
    CollaboratorAuthor

    I'd love to take this!

    Assigned. Note that this is quite a bit more complicated than the previous patches you have worked on.

  7. flodolo commented on Oct 5, 2026

    @flodolo
    CollaboratorAuthor

    @baha-bouali
    Any update on this?

  8. baha-bouali commented on Oct 5, 2026

    @baha-bouali
    Contributor

    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.

  9. baha-bouali commented on Oct 6, 2026

    @baha-bouali
    Contributor

    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):

    My suggestion is to refuse

    3. Who is recorded as rejecting?

    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:

    1. 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.
    2. It still needs the conflict check. Otherwise a suggestion approved during the MT calls gets deactivated, and the unreviewed pretranslation becomes the active one.
    3. 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.

  10. baha-bouali commented on Oct 6, 2026

    @baha-bouali
    Contributor

    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.

  11. flodolo commented on Oct 6, 2026

    @flodolo
    CollaboratorAuthor

    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.

  12. flodolo commented on Oct 6, 2026

    @flodolo
    CollaboratorAuthor

    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.

  13. baha-bouali commented on Oct 7, 2026

    @baha-bouali
    Contributor

    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.

  14. baha-bouali commented on Oct 7, 2026

    @baha-bouali
    Contributor
    • 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?
  15. flodolo commented on Oct 7, 2026

    @flodolo
    CollaboratorAuthor

    That looks right (but the devil is in the (implementation) details) ;-)

  16. mathjazz commented on Oct 14, 2026

    @mathjazz
    Collaborator

    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.

  17. baha-bouali commented on Oct 14, 2026

    @baha-bouali
    Contributor

    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?

  18. mathjazz commented on Oct 14, 2026

    @mathjazz
    Collaborator

    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?

    See the Strings with unreviewed suggestions section: "a matching suggestion becomes the active pretranslation, keeping its author".

  19. moved this from 🔖 Ready to ✅ Done in Pontoon Roadmapon Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3hours

Type

Fields

Priority

None yet

Effort

None yet

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions