Skip to content

feat(metadata-editor): apply custom logic for multiple confidence scores and target locations#4502

Merged
mergify[bot] merged 2 commits intobox:masterfrom
dlasecki-box:deduplicate-confidence-scores
Apr 14, 2026
Merged

feat(metadata-editor): apply custom logic for multiple confidence scores and target locations#4502
mergify[bot] merged 2 commits intobox:masterfrom
dlasecki-box:deduplicate-confidence-scores

Conversation

@dlasecki-box
Copy link
Copy Markdown
Contributor

@dlasecki-box dlasecki-box commented Apr 13, 2026

For multiSelect metadata fields, the Intelligence API returns per-value confidence scores, confidence levels, and bounding boxes - rather than a single set per field. This doesn't align with the current review/approval (HITL) process or meta-metadata schema, which expect one confidence score and one flat list of bounding boxes per field.
This PR applies the agreed-upon workaround:

  • Confidence scores/levels: when multiple scores are returned for a field, we take the lowest score (and its level) so the user sees the most conservative assessment in the UI
  • Bounding boxes: when nested arrays of references are returned, we flatten them so all bounding boxes are visible and clickable in Preview
  • Reference entries without bounding boxes: filtered out to prevent rendering errors; fields where all references lack bounding boxes correctly trigger deselection
    Some per-value granularity is lost (one score/level stored per field instead of per option), but this is acceptable for the HITL process.

Summary by CodeRabbit

  • Bug Fixes

    • Selects lowest confidence when per-field scores are arrays; flattens nested reference locations; deselects metadata fields when location entries lack bounding boxes.
  • Tests

    • Added tests covering metadata field selection with missing bounding boxes and AI suggestion handling for arrayed confidence scores and nested references.

@dlasecki-box dlasecki-box requested review from a team as code owners April 13, 2026 13:34
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d049aeae-e83d-4881-aa46-e0241b6e1f70

📥 Commits

Reviewing files that changed from the base of the PR and between f06ee4e and 1b19f2b.

📒 Files selected for processing (1)
  • src/elements/content-sidebar/hooks/useSidebarMetadataFetcher.ts

Walkthrough

Filter out metadata targetLocation entries that lack bounding boxes when computing highlights; select the lowest confidence score from arrays for AI suggestions and flatten nested reference arrays into a single targetLocation. Tests were added/updated to cover these behaviors.

Changes

Cohort / File(s) Summary
useMetadataFieldSelection
src/elements/content-sidebar/hooks/useMetadataFieldSelection.ts, src/elements/content-sidebar/__tests__/useMetadataFieldSelection.test.ts
Added a type guard to narrow targetLocation entries to those with boundingBox; convertTargetLocationToBoundingBox now ignores entries without boundingBox. Selection now deselects when resulting boundingBoxes are undefined/empty. Tests cover partial and all-missing boundingBox scenarios.
useSidebarMetadataFetcher
src/elements/content-sidebar/hooks/useSidebarMetadataFetcher.ts, src/elements/content-sidebar/__tests__/useSidebarMetadataFetcher.test.tsx
When per-field confidenceScores is an array, pick the element with the lowest score for AI suggestion fields. When references may be nested arrays, flatten into a single targetLocation array. Tests added for array confidence handling (including equal minima) and flattened vs already-flat references.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • #4494: Overlaps with bounding-box handling and updates to useMetadataFieldSelection.
  • #4481: Related changes in content-sidebar metadata processing and targetLocation handling.
  • #4499: Related updates to AI confidence-score parsing and wiring into metadata fetcher.

Suggested reviewers

  • Lindar90
  • jfox-box
  • reneshen0328

Poem

🐰 I hop through code with gentle paws,
Bounding boxes checked for all the laws,
I pick the smallest confidence score,
Flatten references till they're no more,
A tiny rabbit clap—now tests adore! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main changes: applying custom logic for handling multiple confidence scores and target locations in metadata fields.
Description check ✅ Passed The description provides comprehensive context and explains the workaround implemented, including handling of confidence scores, bounding boxes, and filtering logic.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/elements/content-sidebar/__tests__/useSidebarMetadataFetcher.test.tsx (1)

818-875: Add a regression test for empty confidence arrays.

Please add a case where confidence_score.field1 is [] to ensure extraction does not throw and simply omits aiSuggestionConfidenceScore.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/elements/content-sidebar/__tests__/useSidebarMetadataFetcher.test.tsx`
around lines 818 - 875, Add a regression test that ensures empty confidence
arrays are handled gracefully: in the test file use
mockAPI.extractStructured.mockResolvedValue to return answer: { field1: 'value1'
} and confidence_score: { field1: [] } (and created_at/completion_reason as in
other tests), call setupHook('123', true) and await result.current.status ===
STATUS.SUCCESS, then call result.current.extractSuggestions('templateKey',
'global') and assert that the returned suggestion for field1 contains
aiSuggestion: 'value1' but does NOT include aiSuggestionConfidenceScore (or that
aiSuggestionConfidenceScore is undefined), referencing the existing tests like
"should pick the lowest confidence score when confidence_score is an array" and
the extractSuggestions flow to place the new test alongside them.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/elements/content-sidebar/hooks/useSidebarMetadataFetcher.ts`:
- Around line 287-293: The current computation of lowestConfidence in
useSidebarMetadataFetcher.ts uses Array.prototype.reduce without guarding
against an empty array, which throws when fieldConfidence === []; fix by
checking Array.isArray(fieldConfidence) && fieldConfidence.length > 0 before
reducing (or use reduce with an initial value such as fieldConfidence[0]) and
provide a safe fallback when the array is empty or fieldConfidence is
null/undefined so that result.aiSuggestionConfidenceScore is only assigned when
a valid lowestConfidence with .score and .level exists (or assign a safe
default/null). Update the logic around the symbols fieldConfidence,
lowestConfidence, and result.aiSuggestionConfidenceScore to handle empty arrays
and avoid TypeError.

---

Nitpick comments:
In `@src/elements/content-sidebar/__tests__/useSidebarMetadataFetcher.test.tsx`:
- Around line 818-875: Add a regression test that ensures empty confidence
arrays are handled gracefully: in the test file use
mockAPI.extractStructured.mockResolvedValue to return answer: { field1: 'value1'
} and confidence_score: { field1: [] } (and created_at/completion_reason as in
other tests), call setupHook('123', true) and await result.current.status ===
STATUS.SUCCESS, then call result.current.extractSuggestions('templateKey',
'global') and assert that the returned suggestion for field1 contains
aiSuggestion: 'value1' but does NOT include aiSuggestionConfidenceScore (or that
aiSuggestionConfidenceScore is undefined), referencing the existing tests like
"should pick the lowest confidence score when confidence_score is an array" and
the extractSuggestions flow to place the new test alongside them.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5e18a21d-3f8e-4863-b9cd-700933d38bfe

📥 Commits

Reviewing files that changed from the base of the PR and between b455cdf and f06ee4e.

📒 Files selected for processing (4)
  • src/elements/content-sidebar/__tests__/useMetadataFieldSelection.test.ts
  • src/elements/content-sidebar/__tests__/useSidebarMetadataFetcher.test.tsx
  • src/elements/content-sidebar/hooks/useMetadataFieldSelection.ts
  • src/elements/content-sidebar/hooks/useSidebarMetadataFetcher.ts

Comment thread src/elements/content-sidebar/hooks/useSidebarMetadataFetcher.ts
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Apr 14, 2026

Merge Queue Status

  • Entered queue2026-04-14 16:33 UTC · Rule: Automatic strict merge
  • Checks passed · in-place
  • Merged2026-04-14 16:44 UTC · at 1b19f2b37c08e4675356139d309ba0a7967da731

This pull request spent 10 minutes 25 seconds in the queue, including 10 minutes 3 seconds running CI.

Required conditions to merge

@mergify mergify bot merged commit 3c3dba9 into box:master Apr 14, 2026
9 of 10 checks passed
@mergify mergify bot removed the queued label Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants