Skip to content

OpenConceptLab/ocl_issues#3088 | Send each candidate's names to the AI Assistant once, at most 50 - #143

Merged
snyaggarwal merged 3 commits into
mainfrom
issues#3088
Dec 2, 2026
Merged

snyaggarwal merged 3 commits into
mainfrom
issues#3088

Conversation

@paynejd

@paynejd paynejd commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

What this fixes

When the Mapper builds the candidates it sends to the OCL AI Assistant for a recommendation, filterAndTrimNames filtered each candidate's names by locale only. It never dropped duplicates or capped the count. In a production run on 27 Nov 2026, ICD-11 NE61 went with 814 copies of the same name, and that row's prompt was about 5 times the median.

closes OpenConceptLab/ocl_issues#3088

What changed

filterAndTrimNames (src/components/map-projects/normalizers.js):

  • Duplicates. A name repeated with the same name, locale and name type is sent once, where it first appears. If any copy is locale_preferred, the kept one is too.
  • Cap. At most MAX_NAMES_PER_CANDIDATE (50) names per candidate, counted after duplicates are dropped. Past the cap, the preferred and fully specified names are kept first, then names in the requested locales, then the rest. The names kept stay in the source's order.
  • A list within the cap and without duplicates comes back unchanged, in the same order. So rows whose candidates had neither problem keep the same candidate-pool fingerprint, and Auto Match won't analyse them again. A row that did have a duplicated or over-long candidate gets a new fingerprint, so the next Auto Match run analyses it once more.

The cap: 50, and why

The cap is a backstop for runaway lists. Dropping duplicates is the real fix for NE61. I looked at how many distinct names concepts have in production (latest concept versions, not retired):

Source Concepts Most distinct names, all locales Most distinct names, English Over 50 names
WHO ICD-11 in ICD-11-WHO-Mapper 46,545 2 2 0
LOINC 247,127 49 4 0
CIEL 55,606 189 182 39 in English, 55 across all locales
  • At 20, the cap would cut 186 CIEL concepts in English. With no locale set it would also cut the translations of about 55,000 LOINC concepts (LOINC has up to 49 names across its locales).
  • At 50, every LOINC and ICD-11 concept goes whole, even with no locale set. Only about 40 CIEL concepts are cut: drug products with long lists of brand names (Guaifenesin / pseudoephedrine has 182). Their preferred and fully specified names always go.
  • A 50-name candidate is still small next to what NE61 sent.

It's a named constant, so it's easy to tune.

NE61 before and after

The test buildRecommendableConceptEntry: an NE61-shaped candidate is sent with one name builds an entry from 814 copies of NE61's name. The same fixture run through the code on main and on this branch:

Names sent Entry size (JSON)
main 814 157,484 bytes
this branch 1 575 bytes

Other requests that carry a candidate's full names (not changed here)

Neither goes through filterAndTrimNames, so this PR leaves them alone:

  • The rerank call. buildRerankRowsForRow (MapProject.jsx) sends names: def.names straight from the concept cache: every locale and every duplicate, so a candidate like NE61 goes to $rerank/ with every copy.
  • Saving the project. serializeCandidates stores the whole concept cache, names included, in the project's saved candidates, and each row's mapped concept goes with its full names in matches.
  • The concept details panel shows a candidate's full list of names, so repeated names appear there repeatedly. That's display, not a request.

The AI payload's bridge context sends only each bridge concept's display name, not its names, so it isn't affected.

Not in this PR

  • The content. NE61 in ICD-11-WHO-Mapper holds 453 copies of one name at its latest version. The count has grown with each of its versions, so the import looks like it adds the names again every time. That's for the source's maintainers.
  • 814 against 453. The run sent 814 copies, while the API serves 453 for the latest version today. The Mapper copies names from the API response as is (a lookup replaces them, it doesn't add to them), so the extra copies came from the server's response in that run. I haven't traced why.

Tests

  • New unit tests in src/components/map-projects/__tests__/normalizers.test.js: duplicates matched on all three fields, the kept copy's position and locale_preferred, a list within the cap left unchanged, what's kept past the cap, the cap counting names after duplicates are dropped, and the NE61-shaped entry.
  • npm test: 831 pass. npm run test:integration: 20 pass. ESLint on src: clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AaZ4qTTqXZgsTrFbWhkGHs

paynejd and others added 2 commits December 1, 2026 19:54
…I Assistant once, at most 50

filterAndTrimNames now drops a name repeated with the same name, locale
and name type, keeping the first copy, and caps each candidate at
MAX_NAMES_PER_CANDIDATE (50) names. Past the cap the preferred and fully
specified names are kept first, then names in the requested locales; the
names kept stay in the source's order, and a list within the cap comes back
unchanged.

closes OpenConceptLab/ocl_issues#3088

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qTTqXZgsTrFbWhkGHs
… only when its locale's primary subtag is requested

A name with a malformed tag such as "-US" passes the locale filter, but it
no longer takes a requested-locale slot past the cap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qTTqXZgsTrFbWhkGHs
@paynejd

paynejd commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Codex review, pass 1 (codex-cli 0.169.0, commit c31e1bc)

Verdict: findings: 3 findings, all P2.

  • F1.1 (P2) src/components/map-projects/normalizers.js:689: The deduplication key treats an omitted locale and an explicit null locale as identical, although they produce different JSON entries.
  • F1.2 (P2) src/components/map-projects/normalizers.js:701: The cap gives a name requested-locale priority whenever its locale is truthy, even when the locale has no usable primary subtag.
  • F1.3 (P2) src/components/map-projects/MapProject.jsx:1819: The PR documents the saved candidate cache but omits the separate per-row matches field and the Concept Details name list, which also consume the untrimmed names. PR description
findings.json
{
  "reviewer": "codex-cli 0.169.0",
  "perspective": "defects",
  "mode": "full",
  "commit": "c31e1bcdd1b925e285242123318d61dc690da5ea",
  "verdict": "findings",
  "prior": [],
  "findings": [
    {
      "id": "F1.1",
      "severity": "P2",
      "tag": null,
      "file": "src/components/map-projects/normalizers.js",
      "line": 689,
      "claim": "The deduplication key treats an omitted locale and an explicit null locale as identical, although they produce different JSON entries.",
      "scenario": "A candidate has two entries with the same name and type. One omits locale and the other sets it to null. The old code sent both entries. The new code drops one even though the list is under the cap, changing the fingerprint and causing Auto Match to analyse the row again.",
      "action": "Build the key from an object containing the three trimmed fields so omitted and null values remain distinct. Add a regression test for the resulting payload.",
      "lines_delta": null,
      "confidence": "high"
    },
    {
      "id": "F1.2",
      "severity": "P2",
      "tag": null,
      "file": "src/components/map-projects/normalizers.js",
      "line": 701,
      "claim": "The cap gives a name requested-locale priority whenever its locale is truthy, even when the locale has no usable primary subtag.",
      "scenario": "With English requested, a candidate has an ordinary name tagged `-US` followed by 50 ordinary `en-US` names. The malformed tag survives the existing locale filter because its primary subtag is empty. It then takes a slot from the last English name.",
      "action": "Use `set.has(primarySubtag(n.locale))` to assign requested-locale priority and cover this case in the cap test.",
      "lines_delta": null,
      "confidence": "high"
    },
    {
      "id": "F1.3",
      "severity": "P2",
      "tag": null,
      "file": "src/components/map-projects/MapProject.jsx",
      "line": 1819,
      "claim": "The PR documents the saved candidate cache but omits the separate per-row `matches` field and the Concept Details name list, which also consume the untrimmed names. [PR description](https://github.com/OpenConceptLab/oclmap/pull/143)",
      "scenario": "When a user maps the same duplicate-heavy candidate on several rows, the save puts the full list in each row's `matches[].concept.names` as well as in the candidate cache. Opening that candidate's details also passes the full list to Names and Synonyms, where duplicate entries in the displayed locale can appear repeatedly.",
      "action": "Add both consumers to the PR's list of paths that carry the untrimmed names.",
      "lines_delta": null,
      "confidence": "high"
    }
  ]
}

@paynejd

paynejd commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Replies to Codex pass 1

  • F1.1 (P2), accepted, not changed. A name with no locale and the same name with locale: null both have no locale, so they are duplicates and sending one is what's wanted. The worst case is one extra Auto Match analysis of a row that had such a pair.
  • F1.2 (P2), fixed in 1382b5f. A name now ranks as in a requested locale only when its locale's primary subtag is requested, so a malformed tag like -US ranks with the names that have no locale. The cap test now includes one.
  • F1.3 (P2), done in the PR description. Saving a project also sends each row's mapped concept with its full names (matches), and the concept details panel shows the full list. Both are now listed under the paths this PR doesn't change.

… picks the names kept

Also shortens the constant's comment and drops a test assertion another
test already covers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaZ4qTTqXZgsTrFbWhkGHs
@paynejd

paynejd commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Simplification review (claude subagent, commit 1382b5f)

Verdict: findings: 4 cuts proposed; 3 taken in 50567d0, 1 kept.

  • S1 (dup-test) src/components/map-projects/__tests__/normalizers.test.js:1667: The buildRecommendableConceptEntry NE61 test repeats the 814-copies test through a wrapper. Its byte-size assertions (over 100000 and under 1000) pin an implementation detail rather than behaviour. Only the names.length === 1 check adds anything, and only if the wrapper passes names straight through. Reply: kept. The ticket's hand-back asks for NE61's size before and after shown in a test, and this one runs the entry the AI Assistant actually receives.
  • S2 (dup-test) src/components/map-projects/__tests__/normalizers.test.js:1600: The second assertion in the 814-copies test (filterAndTrimNames(ne61Names, []).length === 1) is covered by the multi-case 'matched on name, locale and name type' test and by the cap-counts-after-dedupe test, both of which run with empty or non-matching locales. Reply: cut in 50567d0.
  • S3 (shrink) src/components/map-projects/normalizers.js:640: The doc comment on MAX_NAMES_PER_CANDIDATE restates corpus statistics (LOINC 49, ICD-11 2, about 40 CIEL drugs) that will go stale and are already on the ticket. The rest of the comment repeats the filterAndTrimNames docblock. Reply: cut in 50567d0: the comment now gives the reason without the counts, which are on the PR description.
  • S4 (shrink) src/components/map-projects/normalizers.js:702: The keep-set of indices can be replaced by a stable sort of the entries followed by a re-sort back to source order. This drops the index-tracking objects and the Set. Reply: cut in 50567d0, in a shorter form: a stable sort by rank, a Set of the first 50, then a filter of the list by that Set, so source order needs no index.

Tests after the cuts: npm test 831 pass, npm run test:integration 20 pass, ESLint clean.

findings.json
{
  "reviewer": "claude subagent",
  "perspective": "simplify",
  "mode": "full",
  "commit": "1382b5fde4e5785b2f48bfeadd7089f56a4a5710",
  "verdict": "findings",
  "prior": [],
  "findings": [
    {
      "id": "S1",
      "severity": null,
      "tag": "dup-test",
      "file": "src/components/map-projects/__tests__/normalizers.test.js",
      "line": 1667,
      "claim": "The buildRecommendableConceptEntry NE61 test repeats the 814-copies test through a wrapper. Its byte-size assertions (over 100000 and under 1000) pin an implementation detail rather than behaviour. Only the names.length === 1 check adds anything, and only if the wrapper passes names straight through.",
      "action": "Delete the test, or cut it to the single names.length === 1 assertion and drop the two JSON.stringify size checks.",
      "lines_delta": -4,
      "confidence": "medium",
      "scenario": null
    },
    {
      "id": "S2",
      "severity": null,
      "tag": "dup-test",
      "file": "src/components/map-projects/__tests__/normalizers.test.js",
      "line": 1600,
      "claim": "The second assertion in the 814-copies test (filterAndTrimNames(ne61Names, []).length === 1) is covered by the multi-case 'matched on name, locale and name type' test and by the cap-counts-after-dedupe test, both of which run with empty or non-matching locales.",
      "action": "Remove that one assertion.",
      "lines_delta": -1,
      "confidence": "low",
      "scenario": null
    },
    {
      "id": "S3",
      "severity": null,
      "tag": "shrink",
      "file": "src/components/map-projects/normalizers.js",
      "line": 640,
      "claim": "The doc comment on MAX_NAMES_PER_CANDIDATE restates corpus statistics (LOINC 49, ICD-11 2, about 40 CIEL drugs) that will go stale and are already on the ticket. The rest of the comment repeats the filterAndTrimNames docblock.",
      "action": "Shorten to two lines: the most names one candidate sends to the AI Assistant, counted after duplicates are dropped (OpenConceptLab/ocl_issues#3088). Keep the stats on the ticket.",
      "lines_delta": -5,
      "confidence": "medium",
      "scenario": null
    },
    {
      "id": "S4",
      "severity": null,
      "tag": "shrink",
      "file": "src/components/map-projects/normalizers.js",
      "line": 702,
      "claim": "The keep-set of indices can be replaced by a stable sort of the entries followed by a re-sort back to source order. This drops the index-tracking objects and the Set.",
      "action": "const idx = new Map(out.map((n, i) => [n, i])); return out.slice().sort((a, b) => rank(a) - rank(b) || idx.get(a) - idx.get(b)).slice(0, MAX_NAMES_PER_CANDIDATE).sort((a, b) => idx.get(a) - idx.get(b)). A sort that is stable on rank alone, with the first sort keyed by rank only, makes the idx tie-break unnecessary, which saves roughly 2 more lines.",
      "lines_delta": -2,
      "confidence": "low",
      "scenario": null
    }
  ]
}

@paynejd

paynejd commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Codex check of commits c31e1bc..50567d0 (codex-cli 0.169.0, commit 50567d0)

Covers the F1.2 fix (1382b5f) and the simplification cuts (50567d0), asking only whether they broke anything.

Verdict: clean, no findings.

findings.json
{
  "reviewer": "codex-cli 0.169.0",
  "perspective": "defects",
  "mode": "final",
  "commit": "50567d04b8b37b11e532cb250ac32d4b267a55eb",
  "verdict": "clean",
  "prior": [],
  "findings": []
}

@paynejd
paynejd marked this pull request as ready for review December 2, 2026 01:05
@paynejd

paynejd commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Hand-back report (ready for review; Sunny reviews and merges)

What's done. filterAndTrimNames now sends each name once (same name, locale and name type), and at most MAX_NAMES_PER_CANDIDATE = 50 names per candidate. Past the cap, the preferred and fully specified names go first, then names in the requested locales. A list within the cap and without duplicates comes back unchanged, so those rows keep their candidate-pool fingerprint. The description above gives the details and the data behind the cap.

NE61 before and after. The test buildRecommendableConceptEntry: an NE61-shaped candidate is sent with one name uses 814 copies of NE61's name. Run through main and through this branch:

Names sent Entry size (JSON)
main 814 157,484 bytes
this branch 1 575 bytes

Cap: 50. It leaves every LOINC concept whole (49 names at most across all locales) and every ICD-11 concept whole (2 distinct names at most). It cuts only about 40 CIEL drug products with long brand-name lists. A cap of 20 would have cut 186 CIEL concepts, and the translations of about 55,000 LOINC concepts in projects with no locale set.

Review loop.

  • Codex pass 1 on c31e1bc: 3 P2s, no P0 or P1.
    • F1.1 (a missing and a null locale counted as the same): accepted, since both mean no locale.
    • F1.2 (a malformed tag like -US ranked as a requested locale past the cap): fixed in 1382b5f, with a test.
    • F1.3 (the project save's matches and the details panel also carry the full names): added to the description.
  • Simplification review: 3 of 4 cuts taken in 50567d0. The NE61 entry test was kept, since it shows the size.
  • Codex check of c31e1bc..50567d0: clean.
  • npm test 831 pass, npm run test:integration 20 pass, ESLint clean, CI green.

Still sending the full list (not changed here, since they don't go through filterAndTrimNames):

  • the rerank call (buildRerankRowsForRow sends def.names as is);
  • the project save (candidates and matches);
  • the concept details panel (display only).

Possible next steps.

  1. Drop duplicate names in the rerank request too, or once when a concept enters the concept cache, which would cover rerank, save and display together. Doing it in the cache also changes the fingerprint of affected rows once.
  2. Content: NE61 in ICD-11-WHO-Mapper holds 453 copies of one name, and the count has grown with each version. 4 concepts in that source have over 20 names, all duplicates. The source's maintainers need to know: that's the ticket's third acceptance criterion, and this PR doesn't do it.
  3. The run sent 814 copies while the API serves 453 today. Not traced.

Left in place: the branch and my worktree, until the PR merges, for review replies.

@snyaggarwal
snyaggarwal merged commit 3bb7edc into main Dec 2, 2026
2 checks passed
@snyaggarwal
snyaggarwal deleted the issues#3088 branch December 2, 2026 09:00
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.

Mapper: drop duplicate names before sending candidates to the AI Assistant

2 participants