Skip to content

fix(disambiguate): mapeo por contenido en lugar de índice de array (#78)#86

Merged
jansaldo merged 1 commit into
developfrom
fix/disambiguate-text-based-mapping
May 26, 2026
Merged

fix(disambiguate): mapeo por contenido en lugar de índice de array (#78)#86
jansaldo merged 1 commit into
developfrom
fix/disambiguate-text-based-mapping

Conversation

@jansaldo
Copy link
Copy Markdown
Contributor

@jansaldo jansaldo commented May 26, 2026

El endpoint /anonymizer/disambiguate devuelve un array de párrafos procesados. El código anterior asumía que el backend respondía en el mismo orden y con la misma cantidad de elementos que el request, mapeando por índice. Si el backend reordenaba o descartaba algún párrafo, las etiquetas quedaban asignadas al párrafo incorrecto, o el paragraphId se seteaba con el texto plano del párrafo (que tras el fix del PR #85 ya no coincide con ningún ID en el store de Redux).

Cambios:

  • Se reemplaza el flatMap((item, i) => paragraphs[i]) por una consumption queue keyed por texto: cada item del response se resuelve buscando el primer párrafo no consumido con ese texto, tolerando reordenamiento y párrafos duplicados.
  • Si el backend omite un párrafo, sus predicciones originales se preservan en lugar de descartarse silenciosamente.

Resuelve parcialmente #78.

Summary by Sourcery

Align disambiguation results with their original paragraphs based on paragraph content instead of response index, and preserve predictions for paragraphs omitted by the backend.

Bug Fixes:

  • Match disambiguation response items to paragraphs by their text content, tolerating backend reordering and duplicate paragraphs to avoid misassigned labels and invalid paragraph IDs.
  • Keep existing predictions for paragraphs that the backend omits from the disambiguation response so annotations are not silently lost.

@jansaldo jansaldo requested review from Copilot and llapenna May 26, 2026 17:33
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 26, 2026

Reviewer's Guide

Refactors the disambiguation result mapping to match response items to paragraphs by text content using a consumption queue, and preserves predictions for paragraphs that the backend omits, instead of relying on brittle array index-based mapping that could misalign labels or lose annotations.

File-Level Changes

Change Details Files
Match disambiguation response items to paragraphs by content using a per-text consumption queue instead of relying on array indices.
  • Replace index-based mapping over parsed disambiguation data with a lookup queue keyed by paragraph text to handle reordering and drops from the backend response.
  • Build a Map from paragraph text to an ordered list of Paragraphs to support duplicate texts and track consumption per text key.
  • Track consumed items per text value and skip response items that cannot be matched to any remaining paragraph.
src/renderer/src/services/aymurai/queries.ts
Ensure paragraph identifiers and predictions remain stable and preserve annotations when the backend omits paragraphs.
  • Always use the matched paragraph.id when building the origByPos lookup and assigning paragraphId on resulting PredictLabel objects, removing fallbacks to the plain text document field.
  • Capture a set of matched paragraph IDs while mapping disambiguation results.
  • After mapping, re-append any original predictions for paragraphs not returned by the backend so those annotations are not silently discarded.
src/renderer/src/services/aymurai/queries.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes incorrect paragraph-to-disambiguation-result alignment in the /anonymizer/disambiguate flow by matching backend response items back to the original paragraphs by paragraph content (text) instead of array index, and it avoids losing existing annotations when the backend drops paragraphs from the response.

Changes:

  • Replace index-based mapping (paragraphs[i]) with a per-text “consumption queue” to match response items to the first unconsumed paragraph with the same text.
  • Ensure returned PredictLabel.paragraphId is always the stable paragraph id (not the raw paragraph text from the backend).
  • Preserve original predictions for any paragraph omitted by the backend response.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 170 to +239
@@ -207,10 +225,18 @@ export const disambiguate = (file: DocFile) =>
aymurai_label_instance: l.attrs.aymurai_label_instance ?? null,
aymurai_disambiguation: l.attrs.aymurai_disambiguation ?? null,
},
paragraphId: paragraph?.id ?? item.document,
paragraphId: paragraph.id,
} satisfies PredictLabel;
});
});

// Preserve raw predictions for any paragraph the backend did not return,
// so a backend count drop never silently erases annotations.
const unmatched = predictions.filter(
(p) => !matchedParagraphIds.has(p.paragraphId),
);

return [...disambiguated, ...unmatched];
@jansaldo jansaldo merged commit 47fb1fb into develop May 26, 2026
2 checks passed
@jansaldo jansaldo deleted the fix/disambiguate-text-based-mapping branch May 26, 2026 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants