From e51bd6d8441588ef83ecb54b053f84d8d40329fd Mon Sep 17 00:00:00 2001 From: Jonathan Payne Date: Mon, 23 Nov 2026 16:48:03 -0500 Subject: [PATCH 1/6] closes OpenConceptLab/ocl_issues#3064 | Read each version's mapped-sources list once, shared by the bridge probe and the target The bridge probe re-ran on every project change and the target version read the same list again. Both now read through one reader that keeps each page's request for the open project, so a version's list is read once, and a page that fails is read again on the next ask. The probe runs again only when the bridge algorithm, its repository or version, whether it's selected, or the namespace changes, and a selected bridge algorithm still pins its resolved version. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AaZ4qVXohXhqMLGigP4RHV --- src/components/map-projects/MapProject.jsx | 85 +++---- .../__tests__/mappedSources.test.js | 219 ++++++++++++++++++ src/components/map-projects/mappedSources.js | 84 +++++++ 3 files changed, 329 insertions(+), 59 deletions(-) create mode 100644 src/components/map-projects/__tests__/mappedSources.test.js create mode 100644 src/components/map-projects/mappedSources.js diff --git a/src/components/map-projects/MapProject.jsx b/src/components/map-projects/MapProject.jsx index 4ff62b0..4dc21b3 100644 --- a/src/components/map-projects/MapProject.jsx +++ b/src/components/map-projects/MapProject.jsx @@ -125,6 +125,7 @@ import { normalizeLookupConfig } from './lookupConfigFields' import { getDeclaredLocales, getDefaultTargetRepoVersion, getProjectTargetRepoVersion, getSavedTargetRepoVersion, getTargetRepoVersionFromUrl, getTargetRepoVersionId } from './projectTargetRepo' import { buildBridgeTargetDownloadEntries, buildQualityRowViews, conceptBelongsToTargetRepo, conceptForMapping, findTargetRowView, formatBridgeTargetDownloadEntry, resolveAICandidateID, getScoreDetails, getAIAnalysisCandidateIDs, getAIIndicatorTooltip, withCachedConcept, withCandidateNames, buildMatchEntries, restoredNote, withOwnNames, withSavedMatch } from './viewBuilders.js' import { formatClockTime, getCapacityWaitLabel, mergeCapacityWaits } from './rowProgress.js' +import { createMappedSourcesReader, MAPPED_SOURCES_PAGE_SIZE, probeBridgeMappedSources } from './mappedSources' import './MapProject.scss' import '../common/ResizablePanel.scss' @@ -153,6 +154,10 @@ import '../common/ResizablePanel.scss' const NEW_PROJECT_BLOCK_ERROR_CODES = {access: 'mapper_access_denied', projects_not_entitled: 'mapper_projects_not_entitled', projects: 'mapper_projects_limit_reached'} const MAPPER_QUOTA_SURFACE = {match_operations: 'auto_match', ai_assistant_calls: 'auto_match', projects: 'new_project', rows: 'import'} +const fetchMappedSourcesPage = (url, page) => APIService.new().overrideURL(url).appendToUrl('mapped-sources/').get(null, null, { + excludeSelf: false, brief: true, limit: MAPPED_SOURCES_PAGE_SIZE, offset: (page - 1) * MAPPED_SOURCES_PAGE_SIZE, page +}) + const MapProject = () => { const { t } = useTranslation(); const { toggles, setAlert: baseSetAlert, setProjectRowSearch } = React.useContext(OperationsContext); @@ -493,6 +498,9 @@ const MapProject = () => { const [showTargetRepoVersionError, setShowTargetRepoVersionError] = React.useState(false) const [mappedSources, setMappedSources] = React.useState([]) const [bridgeMappedSources, setBridgeMappedSources] = React.useState({}) + // One read of each version's list, shared by the target and the bridge probe. + const [readMappedSources] = React.useState(() => createMappedSourcesReader(fetchMappedSourcesPage)) + const stopTargetMappedSourcesRef = React.useRef(() => {}) const [locales, setLocales] = React.useState([]) const [versions, setVersions] = React.useState([]) const [conceptCache, setConceptCache] = React.useState({}) @@ -870,38 +878,22 @@ const MapProject = () => { } }, [repoVersion, project]) - React.useEffect(() => { - if(!bridgeProbeAlgo?.id) - return - - const bridgeUrl = bridgeProbeAlgo?.target_repo_url - if(!bridgeUrl) - return - - if(bridgeProbeAlgo.target_repo_version) { - fetchMappedSources(bridgeUrl + bridgeProbeAlgo.target_repo_version + '/', sources => - setBridgeMappedSources(prev => ({...prev, [bridgeUrl]: sources}))) - return - } - - const resolveNamespace = namespace || get(project, 'owner_url') || owner - APIService.new() + // Runs again only when an input it uses changes, not on every project change. + const bridgeNamespace = namespace || get(project, 'owner_url') || owner + React.useEffect(() => probeBridgeMappedSources({ + algo: bridgeProbeAlgo, + selected: bridgeEnabled, + namespace: bridgeNamespace, + resolveVersion: (bridgeUrl, resolveNamespace) => APIService.new() .overrideURL('/$resolveReference/') .post([{url: bridgeUrl}], currentUserToken(), null, resolveNamespace ? {namespace: resolveNamespace} : undefined) - .then(response => { - const resolvedVersion = get(response, 'data.0.result.version', '') - if(!resolvedVersion) - return - - if(bridgeEnabled) - setAlgosSelected(prev => prev.map(algo => - algo?.id === bridgeProbeAlgo.id ? {...algo, target_repo_version: resolvedVersion} : algo - )) - fetchMappedSources(bridgeUrl + resolvedVersion + '/', sources => - setBridgeMappedSources(prev => ({...prev, [bridgeUrl]: sources}))) - }) - .catch(() => {}) - }, [bridgeProbeAlgo?.id, bridgeProbeAlgo?.target_repo_url, bridgeProbeAlgo?.target_repo_version, bridgeEnabled, namespace, project, owner]) + .then(response => get(response, 'data.0.result.version', '')), + readMappedSources, + pinVersion: (algoId, version) => setAlgosSelected(prev => prev.map(algo => + algo?.id === algoId ? {...algo, target_repo_version: version} : algo + )), + onSources: (bridgeUrl, sources) => setBridgeMappedSources(prev => ({...prev, [bridgeUrl]: sources})) + }), [bridgeProbeAlgo?.id, bridgeProbeAlgo?.target_repo_url, bridgeProbeAlgo?.target_repo_version, bridgeEnabled, bridgeNamespace]) const createProjectFromTemplate = () => { @@ -2170,40 +2162,15 @@ const MapProject = () => { const fetchRepo = (url, _repo) => APIService.new().overrideURL(url).get().then(response => setRepo(response.data?.id ? response.data : _repo)) - const fetchMappedSources = (url, setter) => { - let limit = 25 - - const recurse = (offset, page, fetchedSoFar, accumulated) => { - _fetchMappedSources(url, limit, offset, page, response => { - const data = response?.data || [] - const updated = [...accumulated, ...data] - - setter(updated) - - const fetched = fetchedSoFar + data.length - const numFound = parseInt(response?.headers?.num_found || 0) - - if (fetched < numFound) { - recurse(offset + limit, page + 1, fetched, updated) - } - }) - } - - recurse(0, 1, 0, []) - } - - const _fetchMappedSources = ( - url, limit, offset, page, callback - ) => { - APIService.new().overrideURL(url).appendToUrl('mapped-sources/').get(null, null, {excludeSelf: false, brief: true, limit: limit || 25, offset: offset || 0, page: page || 1}).then(callback) - } - const onRepoVersionChange = version => { setRepoVersion(version || false) setShowTargetRepoVersionError(false) + // A slower read of an earlier version doesn't overwrite this one's list. + stopTargetMappedSourcesRef.current() + stopTargetMappedSourcesRef.current = () => {} if(version?.version_url) { setLocales(getDeclaredLocales(version)) - fetchMappedSources(version.version_url, setMappedSources) + stopTargetMappedSourcesRef.current = readMappedSources(version.version_url, setMappedSources) updateAlgosByRepoVersion(version) } else { setMappedSources([]) diff --git a/src/components/map-projects/__tests__/mappedSources.test.js b/src/components/map-projects/__tests__/mappedSources.test.js new file mode 100644 index 0000000..a0b5510 --- /dev/null +++ b/src/components/map-projects/__tests__/mappedSources.test.js @@ -0,0 +1,219 @@ +/** + * The Mapper reads a repository version's mapped-sources list once and shares + * it between the bridge probe and the target version + * (OpenConceptLab/ocl_issues#3064). + */ + +import test from 'node:test' +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import babel from '@babel/core' + +import { createMappedSourcesReader, probeBridgeMappedSources } from '../mappedSources.js' + +const CIEL = '/orgs/CIEL/sources/CIEL/' +const CIEL_RELEASE = '/orgs/CIEL/sources/CIEL/v2026-09-24/' + +const sourcesPage = (page, count, numFound) => ({ + data: Array.from({length: count}, (_, i) => ({url: `/orgs/X/sources/S${(page - 1) * 25 + i}/`})), + headers: {num_found: String(numFound)} +}) + +// A fake API that answers each page after `settle()`, and counts the reads. +const fakeAPI = (numFound = 50, {fail = () => false} = {}) => { + const reads = [] + const pending = [] + const fetchPage = (url, page) => { + reads.push(`${url}#${page}`) + return new Promise(resolve => pending.push(() => resolve( + fail(url, page) ? 'Service Unavailable' : sourcesPage(page, Math.min(25, numFound - (page - 1) * 25), numFound) + ))) + } + const settle = async () => { + for(;;) { + await new Promise(resolve => setTimeout(resolve, 0)) + if(!pending.length) + return + pending.splice(0).forEach(resolve => resolve()) + } + } + return {fetchPage, reads, settle} +} + +test('the bridge probe and the target share one read of each page of the same version', async () => { + const api = fakeAPI(50) + const read = createMappedSourcesReader(api.fetchPage) + const lists = {bridge: [], target: []} + + read(CIEL_RELEASE, list => lists.bridge.push(list.length)) + read(CIEL_RELEASE, list => lists.target.push(list.length)) + await api.settle() + + assert.deepEqual(api.reads, [`${CIEL_RELEASE}#1`, `${CIEL_RELEASE}#2`]) + // Each still gets the list page by page, as before. + assert.deepEqual(lists.bridge, [25, 50]) + assert.deepEqual(lists.target, [25, 50]) +}) + +test('asking again for a version already read reads nothing more', async () => { + const api = fakeAPI(50) + const read = createMappedSourcesReader(api.fetchPage) + read(CIEL_RELEASE, () => {}) + await api.settle() + + let last + read(CIEL_RELEASE, list => { last = list }) + read(CIEL_RELEASE.replace(/\/$/, ''), () => {}) + await api.settle() + + assert.equal(api.reads.length, 2) + assert.equal(last.length, 50) +}) + +test('another version is read on its own', async () => { + const api = fakeAPI(10) + const read = createMappedSourcesReader(api.fetchPage) + read(CIEL_RELEASE, () => {}) + read(`${CIEL}HEAD/`, () => {}) + await api.settle() + + assert.deepEqual(api.reads, [`${CIEL_RELEASE}#1`, `${CIEL}HEAD/#1`]) +}) + +test('a failed page gives what was read so far and is read again on the next ask', async () => { + let failing = true + const api = fakeAPI(50, {fail: (_, page) => failing && page === 2}) + const read = createMappedSourcesReader(api.fetchPage) + const lists = [] + read(CIEL_RELEASE, list => lists.push(list.length)) + await api.settle() + assert.deepEqual(lists, [25, 25]) + + failing = false + const retried = [] + read(CIEL_RELEASE, list => retried.push(list.length)) + await api.settle() + + // Page 1 came back, so only page 2 is asked for again. + assert.deepEqual(api.reads, [`${CIEL_RELEASE}#1`, `${CIEL_RELEASE}#2`, `${CIEL_RELEASE}#2`]) + assert.deepEqual(retried, [25, 50]) +}) + +test('a stopped read gives nothing more', async () => { + const api = fakeAPI(50) + const read = createMappedSourcesReader(api.fetchPage) + const lists = [] + const stop = read(CIEL_RELEASE, list => lists.push(list.length)) + stop() + await api.settle() + + assert.deepEqual(lists, []) +}) + +// The bridge probe, with a fake $resolveReference. +const probeWith = ({algo, selected = false, namespace = '/users/jontest1/', version = 'v2026-09-24'}) => { + const api = fakeAPI(50) + const resolves = [] + const pins = [] + const sources = {} + let answer + const stop = probeBridgeMappedSources({ + algo, + selected, + namespace, + resolveVersion: (url, ns) => { + resolves.push([url, ns]) + return new Promise(resolve => { answer = () => resolve(version) }) + }, + readMappedSources: createMappedSourcesReader(api.fetchPage), + pinVersion: (id, v) => pins.push([id, v]), + onSources: (url, list) => { sources[url] = list.length } + }) + return {api, resolves, pins, sources, stop, resolve: async () => { answer?.(); await api.settle() }} +} + +test('a bridge algorithm with a version reads that version without resolving', async () => { + const probe = probeWith({algo: {id: 'b', target_repo_url: CIEL, target_repo_version: 'v2026-09-24'}}) + await probe.resolve() + + assert.deepEqual(probe.resolves, []) + assert.deepEqual(probe.api.reads, [`${CIEL_RELEASE}#1`, `${CIEL_RELEASE}#2`]) + assert.deepEqual(probe.sources, {[CIEL]: 50}) +}) + +test('a bridge algorithm with no version reads the version it resolves to, in the namespace given', async () => { + const probe = probeWith({algo: {id: 'b', target_repo_url: CIEL}}) + await probe.resolve() + + assert.deepEqual(probe.resolves, [[CIEL, '/users/jontest1/']]) + assert.deepEqual(probe.api.reads, [`${CIEL_RELEASE}#1`, `${CIEL_RELEASE}#2`]) + // The API's offer isn't selected: its version isn't pinned. + assert.deepEqual(probe.pins, []) +}) + +test('selecting a bridge algorithm pins its resolved version', async () => { + const probe = probeWith({algo: {id: 'b', target_repo_url: CIEL}, selected: true}) + await probe.resolve() + + assert.deepEqual(probe.pins, [['b', 'v2026-09-24']]) +}) + +test('a probe stopped before its version resolves pins and reads nothing', async () => { + const probe = probeWith({algo: {id: 'b', target_repo_url: CIEL}, selected: true}) + probe.stop() + await probe.resolve() + + assert.deepEqual(probe.pins, []) + assert.deepEqual(probe.api.reads, []) +}) + +test('no version, or no bridge repository, reads nothing', async () => { + for(const algo of [undefined, {id: 'b'}, {id: 'b', target_repo_url: CIEL}]) { + const probe = probeWith({algo, version: ''}) + await probe.resolve() + assert.deepEqual(probe.api.reads, []) + } +}) + +// MapProject can't be mounted here (these tests run without a JSX transform), +// so this reads its source. +const source = readFileSync(new URL('../MapProject.jsx', import.meta.url), 'utf8') +const ast = babel.parseSync(source, {babelrc: false, configFile: false, parserOpts: {plugins: ['jsx']}}) +const code = node => source.slice(node.start, node.end) + +test('the bridge probe runs again only when an input it uses changes, not on every project change', () => { + const deps = [] + babel.traverse(ast, { + CallExpression({node}) { + const [effect, dependencies] = node.arguments + if(code(node.callee) === 'React.useEffect' && effect && code(effect).includes('probeBridgeMappedSources(')) + deps.push(dependencies.elements.map(code)) + } + }) + assert.deepEqual(deps, [[ + 'bridgeProbeAlgo?.id', + 'bridgeProbeAlgo?.target_repo_url', + 'bridgeProbeAlgo?.target_repo_version', + 'bridgeEnabled', + 'bridgeNamespace' + ]]) + assert.match(source, /const bridgeNamespace = namespace \|\| get\(project, 'owner_url'\) \|\| owner\n/) +}) + +test('the target version and the bridge probe read through the one shared reader', () => { + const readers = [] + const reads = [] + babel.traverse(ast, { + CallExpression({node}) { + if(node.callee.name === 'createMappedSourcesReader') + readers.push(code(node)) + if(node.callee.name === 'readMappedSources') + reads.push(node.arguments.map(code)) + } + }) + assert.deepEqual(readers, ['createMappedSourcesReader(fetchMappedSourcesPage)']) + assert.deepEqual(reads, [['version.version_url', 'setMappedSources']]) + assert.match(source, /readMappedSources,\n/) + // Nothing reads the list around the reader. + assert.equal(source.match(/appendToUrl\('mapped-sources\/'\)/g).length, 1) +}) diff --git a/src/components/map-projects/mappedSources.js b/src/components/map-projects/mappedSources.js new file mode 100644 index 0000000..a7fb8ab --- /dev/null +++ b/src/components/map-projects/mappedSources.js @@ -0,0 +1,84 @@ +// A repository version's mapped-sources list, read once per page and shared by +// everyone who asks for it while the project is open: the bridge probe and the +// target version often ask for the same version (OpenConceptLab/ocl_issues#3064). + +export const MAPPED_SOURCES_PAGE_SIZE = 25 + +const versionURL = url => `${String(url || '').replace(/\/+$/, '')}/` + +// fetchPage(url, page) answers as APIService does: a response with the page in +// `data` and the total in the `num_found` header, or the error itself. A page +// that fails isn't kept, so the next ask reads it again. +// +// The reader returned, read(url, onList), gives onList the list so far after +// each page, and returns a function that stops it. +export const createMappedSourcesReader = fetchPage => { + const pages = new Map() + + const readPage = (url, page) => { + const key = `${url}#${page}` + if(!pages.has(key)) { + const forget = () => pages.delete(key) + pages.set(key, Promise.resolve(fetchPage(url, page)).then( + response => { + if(!Array.isArray(response?.data)) + forget() + return response + }, + () => { + forget() + return null + } + )) + } + return pages.get(key) + } + + return (url, onList) => { + const versionUrl = versionURL(url) + let stopped = false + const read = (page, soFar) => readPage(versionUrl, page).then(response => { + if(stopped) + return + const data = Array.isArray(response?.data) ? response.data : [] + const list = [...soFar, ...data] + onList(list) + if(data.length && list.length < parseInt(response?.headers?.num_found || 0)) + read(page + 1, list) + }) + read(1, []) + return () => { stopped = true } + } +} + +// The Bridge Match component mounts once it has the bridge repository's mapped +// sources. A bridge algorithm with no version reads the version its repository +// resolves to in the namespace, and a selected one keeps that version. +// Returns a function that stops the probe. +export const probeBridgeMappedSources = ({algo, selected, namespace, resolveVersion, readMappedSources, pinVersion, onSources}) => { + const bridgeUrl = algo?.target_repo_url + if(!algo?.id || !bridgeUrl) + return () => {} + + let stopped = false + let stopReading = () => {} + const read = version => { + stopReading = readMappedSources(bridgeUrl + version + '/', sources => onSources(bridgeUrl, sources)) + } + + if(algo.target_repo_version) + read(algo.target_repo_version) + else + resolveVersion(bridgeUrl, namespace).then(version => { + if(stopped || !version) + return + if(selected) + pinVersion(algo.id, version) + read(version) + }).catch(() => {}) + + return () => { + stopped = true + stopReading() + } +} From c6df4d7a6958a0f4c4698cdc6e6e7dd71b0898e9 Mon Sep 17 00:00:00 2001 From: Jonathan Payne Date: Mon, 23 Nov 2026 16:55:56 -0500 Subject: [PATCH 2/6] OpenConceptLab/ocl_issues#3064 | Settle throttled reads, share HEAD's list, and retry transient failures Read the list pages and resolve the bridge version in APIService's raw mode, so a throttled read settles and isn't kept. A repository's HEAD is one version at its own URL or at HEAD/. A network error or a 5xx on a page or on $resolveReference is tried again twice, so a passing failure no longer leaves the bridge off until the page reloads. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AaZ4qVXohXhqMLGigP4RHV --- src/components/map-projects/MapProject.jsx | 6 +- .../__tests__/mappedSources.test.js | 145 +++++++++++++----- src/components/map-projects/mappedSources.js | 62 +++++--- 3 files changed, 151 insertions(+), 62 deletions(-) diff --git a/src/components/map-projects/MapProject.jsx b/src/components/map-projects/MapProject.jsx index 4dc21b3..7150d14 100644 --- a/src/components/map-projects/MapProject.jsx +++ b/src/components/map-projects/MapProject.jsx @@ -154,9 +154,10 @@ import '../common/ResizablePanel.scss' const NEW_PROJECT_BLOCK_ERROR_CODES = {access: 'mapper_access_denied', projects_not_entitled: 'mapper_projects_not_entitled', projects: 'mapper_projects_limit_reached'} const MAPPER_QUOTA_SURFACE = {match_operations: 'auto_match', ai_assistant_calls: 'auto_match', projects: 'new_project', rows: 'import'} +// Raw, so a failed or throttled read settles and isn't kept. const fetchMappedSourcesPage = (url, page) => APIService.new().overrideURL(url).appendToUrl('mapped-sources/').get(null, null, { excludeSelf: false, brief: true, limit: MAPPED_SOURCES_PAGE_SIZE, offset: (page - 1) * MAPPED_SOURCES_PAGE_SIZE, page -}) +}, true) const MapProject = () => { const { t } = useTranslation(); @@ -886,8 +887,7 @@ const MapProject = () => { namespace: bridgeNamespace, resolveVersion: (bridgeUrl, resolveNamespace) => APIService.new() .overrideURL('/$resolveReference/') - .post([{url: bridgeUrl}], currentUserToken(), null, resolveNamespace ? {namespace: resolveNamespace} : undefined) - .then(response => get(response, 'data.0.result.version', '')), + .post([{url: bridgeUrl}], currentUserToken(), null, resolveNamespace ? {namespace: resolveNamespace} : undefined, true), readMappedSources, pinVersion: (algoId, version) => setAlgosSelected(prev => prev.map(algo => algo?.id === algoId ? {...algo, target_repo_version: version} : algo diff --git a/src/components/map-projects/__tests__/mappedSources.test.js b/src/components/map-projects/__tests__/mappedSources.test.js index a0b5510..bf10f42 100644 --- a/src/components/map-projects/__tests__/mappedSources.test.js +++ b/src/components/map-projects/__tests__/mappedSources.test.js @@ -13,36 +13,46 @@ import { createMappedSourcesReader, probeBridgeMappedSources } from '../mappedSo const CIEL = '/orgs/CIEL/sources/CIEL/' const CIEL_RELEASE = '/orgs/CIEL/sources/CIEL/v2026-09-24/' +const NO_WAIT = {maxRetries: 2, baseDelayMs: 0} -const sourcesPage = (page, count, numFound) => ({ - data: Array.from({length: count}, (_, i) => ({url: `/orgs/X/sources/S${(page - 1) * 25 + i}/`})), +// Failed answers as APIService gives them in raw mode. +const httpError = status => ({isAxiosError: true, response: {status}}) +const networkError = {isAxiosError: true, code: 'ERR_NETWORK'} + +const sourcesPage = (page, numFound) => ({ + data: Array.from({length: Math.min(25, numFound - (page - 1) * 25)}, (_, i) => ({url: `/orgs/X/sources/S${(page - 1) * 25 + i}/`})), headers: {num_found: String(numFound)} }) -// A fake API that answers each page after `settle()`, and counts the reads. -const fakeAPI = (numFound = 50, {fail = () => false} = {}) => { +// A fake API that answers each page once `settle()` runs, and counts the +// reads. `answer(url, page, n)` can replace the n-th read's answer. +const fakeAPI = (numFound = 50, answer = () => undefined) => { const reads = [] const pending = [] const fetchPage = (url, page) => { reads.push(`${url}#${page}`) - return new Promise(resolve => pending.push(() => resolve( - fail(url, page) ? 'Service Unavailable' : sourcesPage(page, Math.min(25, numFound - (page - 1) * 25), numFound) - ))) + const n = reads.length + return new Promise(resolve => pending.push(() => resolve(answer(url, page, n) || sourcesPage(page, numFound)))) } const settle = async () => { - for(;;) { + for(let idle = 0; idle < 5;) { await new Promise(resolve => setTimeout(resolve, 0)) - if(!pending.length) - return - pending.splice(0).forEach(resolve => resolve()) + if(pending.length) { + idle = 0 + pending.splice(0).forEach(resolve => resolve()) + } else { + idle++ + } } } return {fetchPage, reads, settle} } +const readerFor = api => createMappedSourcesReader(api.fetchPage, NO_WAIT) + test('the bridge probe and the target share one read of each page of the same version', async () => { const api = fakeAPI(50) - const read = createMappedSourcesReader(api.fetchPage) + const read = readerFor(api) const lists = {bridge: [], target: []} read(CIEL_RELEASE, list => lists.bridge.push(list.length)) @@ -57,7 +67,7 @@ test('the bridge probe and the target share one read of each page of the same ve test('asking again for a version already read reads nothing more', async () => { const api = fakeAPI(50) - const read = createMappedSourcesReader(api.fetchPage) + const read = readerFor(api) read(CIEL_RELEASE, () => {}) await api.settle() @@ -72,21 +82,36 @@ test('asking again for a version already read reads nothing more', async () => { test('another version is read on its own', async () => { const api = fakeAPI(10) - const read = createMappedSourcesReader(api.fetchPage) + const read = readerFor(api) read(CIEL_RELEASE, () => {}) - read(`${CIEL}HEAD/`, () => {}) + read(`${CIEL}v2026-10-23/`, () => {}) + await api.settle() + + assert.deepEqual(api.reads, [`${CIEL_RELEASE}#1`, `${CIEL}v2026-10-23/#1`]) +}) + +test('a repository\'s HEAD is one version, at its own URL or at HEAD/', async () => { + // The versions list gives HEAD the repository's URL; a bridge resolved to + // HEAD asks for /HEAD/. + const api = fakeAPI(10) + const read = readerFor(api) + const lists = [] + read(CIEL, list => lists.push(list.length)) + read(`${CIEL}HEAD/`, list => lists.push(list.length)) await api.settle() - assert.deepEqual(api.reads, [`${CIEL_RELEASE}#1`, `${CIEL}HEAD/#1`]) + assert.deepEqual(api.reads, [`${CIEL}#1`]) + assert.deepEqual(lists, [10, 10]) }) test('a failed page gives what was read so far and is read again on the next ask', async () => { let failing = true - const api = fakeAPI(50, {fail: (_, page) => failing && page === 2}) - const read = createMappedSourcesReader(api.fetchPage) + const api = fakeAPI(50, (_, page) => failing && page === 2 ? httpError(404) : undefined) + const read = readerFor(api) const lists = [] read(CIEL_RELEASE, list => lists.push(list.length)) await api.settle() + // A 404 isn't tried again. assert.deepEqual(lists, [25, 25]) failing = false @@ -99,9 +124,40 @@ test('a failed page gives what was read so far and is read again on the next ask assert.deepEqual(retried, [25, 50]) }) +test('a throttled page is read again when the version is chosen again', async () => { + const api = fakeAPI(10, (_, __, n) => n === 1 ? httpError(429) : undefined) + const read = readerFor(api) + const lists = [] + read(CIEL_RELEASE, list => lists.push(list.length)) + await api.settle() + assert.deepEqual(lists, [0]) + + read(CIEL_RELEASE, list => lists.push(list.length)) + await api.settle() + + assert.equal(api.reads.length, 2) + assert.deepEqual(lists, [0, 10]) +}) + +test('a network error or a 5xx is tried again, at most twice', async () => { + const api = fakeAPI(10, (_, __, n) => [networkError, httpError(503)][n - 1]) + const lists = [] + readerFor(api)(CIEL_RELEASE, list => lists.push(list.length)) + await api.settle() + assert.equal(api.reads.length, 3) + assert.deepEqual(lists, [10]) + + const failing = fakeAPI(10, () => httpError(503)) + const failed = [] + readerFor(failing)(CIEL_RELEASE, list => failed.push(list.length)) + await failing.settle() + assert.equal(failing.reads.length, 3) + assert.deepEqual(failed, [0]) +}) + test('a stopped read gives nothing more', async () => { const api = fakeAPI(50) - const read = createMappedSourcesReader(api.fetchPage) + const read = readerFor(api) const lists = [] const stop = read(CIEL_RELEASE, list => lists.push(list.length)) stop() @@ -110,31 +166,33 @@ test('a stopped read gives nothing more', async () => { assert.deepEqual(lists, []) }) -// The bridge probe, with a fake $resolveReference. -const probeWith = ({algo, selected = false, namespace = '/users/jontest1/', version = 'v2026-09-24'}) => { +// The bridge probe, with a fake $resolveReference that answers as APIService +// does in raw mode. +const resolved = version => ({data: [{result: {version}}]}) +const probeWith = ({algo, selected = false, namespace = '/users/jontest1/', answers = [resolved('v2026-09-24')]}) => { const api = fakeAPI(50) const resolves = [] const pins = [] const sources = {} - let answer const stop = probeBridgeMappedSources({ algo, selected, namespace, resolveVersion: (url, ns) => { resolves.push([url, ns]) - return new Promise(resolve => { answer = () => resolve(version) }) + return Promise.resolve(answers[Math.min(resolves.length, answers.length) - 1]) }, - readMappedSources: createMappedSourcesReader(api.fetchPage), + readMappedSources: readerFor(api), pinVersion: (id, v) => pins.push([id, v]), - onSources: (url, list) => { sources[url] = list.length } + onSources: (url, list) => { sources[url] = list.length }, + retry: NO_WAIT }) - return {api, resolves, pins, sources, stop, resolve: async () => { answer?.(); await api.settle() }} + return {api, resolves, pins, sources, stop} } test('a bridge algorithm with a version reads that version without resolving', async () => { const probe = probeWith({algo: {id: 'b', target_repo_url: CIEL, target_repo_version: 'v2026-09-24'}}) - await probe.resolve() + await probe.api.settle() assert.deepEqual(probe.resolves, []) assert.deepEqual(probe.api.reads, [`${CIEL_RELEASE}#1`, `${CIEL_RELEASE}#2`]) @@ -143,7 +201,7 @@ test('a bridge algorithm with a version reads that version without resolving', a test('a bridge algorithm with no version reads the version it resolves to, in the namespace given', async () => { const probe = probeWith({algo: {id: 'b', target_repo_url: CIEL}}) - await probe.resolve() + await probe.api.settle() assert.deepEqual(probe.resolves, [[CIEL, '/users/jontest1/']]) assert.deepEqual(probe.api.reads, [`${CIEL_RELEASE}#1`, `${CIEL_RELEASE}#2`]) @@ -153,24 +211,37 @@ test('a bridge algorithm with no version reads the version it resolves to, in th test('selecting a bridge algorithm pins its resolved version', async () => { const probe = probeWith({algo: {id: 'b', target_repo_url: CIEL}, selected: true}) - await probe.resolve() + await probe.api.settle() assert.deepEqual(probe.pins, [['b', 'v2026-09-24']]) }) -test('a probe stopped before its version resolves pins and reads nothing', async () => { +test('resolving the bridge version is tried again after a network error or a 5xx', async () => { + const probe = probeWith({algo: {id: 'b', target_repo_url: CIEL}, selected: true, answers: [httpError(503), networkError, resolved('v2026-09-24')]}) + await probe.api.settle() + + assert.equal(probe.resolves.length, 3) + assert.deepEqual(probe.pins, [['b', 'v2026-09-24']]) + assert.deepEqual(probe.sources, {[CIEL]: 50}) +}) + +test('a probe stopped before its version resolves pins, reads and retries nothing', async () => { const probe = probeWith({algo: {id: 'b', target_repo_url: CIEL}, selected: true}) probe.stop() - await probe.resolve() - + await probe.api.settle() assert.deepEqual(probe.pins, []) assert.deepEqual(probe.api.reads, []) + + const failing = probeWith({algo: {id: 'b', target_repo_url: CIEL}, answers: [httpError(503)]}) + failing.stop() + await failing.api.settle() + assert.equal(failing.resolves.length, 1) }) test('no version, or no bridge repository, reads nothing', async () => { for(const algo of [undefined, {id: 'b'}, {id: 'b', target_repo_url: CIEL}]) { - const probe = probeWith({algo, version: ''}) - await probe.resolve() + const probe = probeWith({algo, answers: [resolved('')]}) + await probe.api.settle() assert.deepEqual(probe.api.reads, []) } }) @@ -200,7 +271,7 @@ test('the bridge probe runs again only when an input it uses changes, not on eve assert.match(source, /const bridgeNamespace = namespace \|\| get\(project, 'owner_url'\) \|\| owner\n/) }) -test('the target version and the bridge probe read through the one shared reader', () => { +test('the target version and the bridge probe read through the one shared reader, in raw mode', () => { const readers = [] const reads = [] babel.traverse(ast, { @@ -214,6 +285,8 @@ test('the target version and the bridge probe read through the one shared reader assert.deepEqual(readers, ['createMappedSourcesReader(fetchMappedSourcesPage)']) assert.deepEqual(reads, [['version.version_url', 'setMappedSources']]) assert.match(source, /readMappedSources,\n/) - // Nothing reads the list around the reader. + // Nothing reads the list around the reader, and a throttled read settles. assert.equal(source.match(/appendToUrl\('mapped-sources\/'\)/g).length, 1) + assert.match(source, /appendToUrl\('mapped-sources\/'\)\.get\(null, null, \{[^}]*\}, true\)/) + assert.match(source, /resolveNamespace \? \{namespace: resolveNamespace\} : undefined, true\)/) }) diff --git a/src/components/map-projects/mappedSources.js b/src/components/map-projects/mappedSources.js index a7fb8ab..764a47b 100644 --- a/src/components/map-projects/mappedSources.js +++ b/src/components/map-projects/mappedSources.js @@ -2,35 +2,49 @@ // everyone who asks for it while the project is open: the bridge probe and the // target version often ask for the same version (OpenConceptLab/ocl_issues#3064). +import { retryWithBackoff } from '../../services/retry.js' + export const MAPPED_SOURCES_PAGE_SIZE = 25 +// A network error or a 5xx is tried again twice, after about 3 s and 12 s. +const RETRY = {maxRetries: 2, baseDelayMs: 3000} + +// APIService in raw mode answers a failed request with the error itself. +const isTransientFailure = response => Boolean(response?.isAxiosError) && + (!response.response || response.response.status >= 500) + +// Settles with the answer, after trying again while the failure is transient. +const withRetry = (request, {isCancelled, ...retry}) => retryWithBackoff( + () => Promise.resolve(request()).then(response => { + if(isTransientFailure(response)) + throw response + return response + }), + {...retry, isCancelled, isRetryable: isTransientFailure} +).catch(() => null) + const versionURL = url => `${String(url || '').replace(/\/+$/, '')}/` -// fetchPage(url, page) answers as APIService does: a response with the page in -// `data` and the total in the `num_found` header, or the error itself. A page -// that fails isn't kept, so the next ask reads it again. +// A repository's HEAD is listed at its own URL and at /HEAD/. +const versionKey = url => versionURL(url).replace(/^(\/(?:orgs|users)\/[^/]+\/(?:sources|collections)\/[^/]+\/)HEAD\/$/, '$1') + +// fetchPage(url, page) answers as APIService does in raw mode: a response with +// the page in `data` and the total in the `num_found` header, or the error. A +// page that fails isn't kept, so the next ask reads it again. // // The reader returned, read(url, onList), gives onList the list so far after // each page, and returns a function that stops it. -export const createMappedSourcesReader = fetchPage => { +export const createMappedSourcesReader = (fetchPage, retry = RETRY) => { const pages = new Map() const readPage = (url, page) => { - const key = `${url}#${page}` - if(!pages.has(key)) { - const forget = () => pages.delete(key) - pages.set(key, Promise.resolve(fetchPage(url, page)).then( - response => { - if(!Array.isArray(response?.data)) - forget() - return response - }, - () => { - forget() - return null - } - )) - } + const key = `${versionKey(url)}#${page}` + if(!pages.has(key)) + pages.set(key, withRetry(() => fetchPage(url, page), retry).then(response => { + if(!Array.isArray(response?.data)) + pages.delete(key) + return response + })) return pages.get(key) } @@ -53,9 +67,10 @@ export const createMappedSourcesReader = fetchPage => { // The Bridge Match component mounts once it has the bridge repository's mapped // sources. A bridge algorithm with no version reads the version its repository -// resolves to in the namespace, and a selected one keeps that version. +// resolves to in the namespace (resolveVersion answers as APIService does in +// raw mode), and a selected one keeps that version. // Returns a function that stops the probe. -export const probeBridgeMappedSources = ({algo, selected, namespace, resolveVersion, readMappedSources, pinVersion, onSources}) => { +export const probeBridgeMappedSources = ({algo, selected, namespace, resolveVersion, readMappedSources, pinVersion, onSources, retry = RETRY}) => { const bridgeUrl = algo?.target_repo_url if(!algo?.id || !bridgeUrl) return () => {} @@ -69,13 +84,14 @@ export const probeBridgeMappedSources = ({algo, selected, namespace, resolveVers if(algo.target_repo_version) read(algo.target_repo_version) else - resolveVersion(bridgeUrl, namespace).then(version => { + withRetry(() => resolveVersion(bridgeUrl, namespace), {...retry, isCancelled: () => stopped}).then(response => { + const version = response?.data?.[0]?.result?.version if(stopped || !version) return if(selected) pinVersion(algo.id, version) read(version) - }).catch(() => {}) + }) return () => { stopped = true From 43cdd28af21bba4329a25fa345f7d5509ce269be Mon Sep 17 00:00:00 2001 From: Jonathan Payne Date: Mon, 23 Nov 2026 17:02:36 -0500 Subject: [PATCH 3/6] OpenConceptLab/ocl_issues#3064 | Key encoded version ids as the bare id, and retry a page only while a read waits for it Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AaZ4qVXohXhqMLGigP4RHV --- src/components/map-projects/MapProject.jsx | 1 + .../__tests__/mappedSources.test.js | 24 ++++++++ src/components/map-projects/mappedSources.js | 61 +++++++++++++------ 3 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/components/map-projects/MapProject.jsx b/src/components/map-projects/MapProject.jsx index 7150d14..3a5ecb1 100644 --- a/src/components/map-projects/MapProject.jsx +++ b/src/components/map-projects/MapProject.jsx @@ -502,6 +502,7 @@ const MapProject = () => { // One read of each version's list, shared by the target and the bridge probe. const [readMappedSources] = React.useState(() => createMappedSourcesReader(fetchMappedSourcesPage)) const stopTargetMappedSourcesRef = React.useRef(() => {}) + React.useEffect(() => () => stopTargetMappedSourcesRef.current(), []) const [locales, setLocales] = React.useState([]) const [versions, setVersions] = React.useState([]) const [conceptCache, setConceptCache] = React.useState({}) diff --git a/src/components/map-projects/__tests__/mappedSources.test.js b/src/components/map-projects/__tests__/mappedSources.test.js index bf10f42..33f60f6 100644 --- a/src/components/map-projects/__tests__/mappedSources.test.js +++ b/src/components/map-projects/__tests__/mappedSources.test.js @@ -104,6 +104,16 @@ test('a repository\'s HEAD is one version, at its own URL or at HEAD/', async () assert.deepEqual(lists, [10, 10]) }) +test('a version id the API encodes in its URL is the same version as the bare id', async () => { + const api = fakeAPI(10) + const read = readerFor(api) + read(`${CIEL}v1%40build/`, () => {}) + read(`${CIEL}v1@build/`, () => {}) + await api.settle() + + assert.deepEqual(api.reads, [`${CIEL}v1%40build/#1`]) +}) + test('a failed page gives what was read so far and is read again on the next ask', async () => { let failing = true const api = fakeAPI(50, (_, page) => failing && page === 2 ? httpError(404) : undefined) @@ -155,6 +165,18 @@ test('a network error or a 5xx is tried again, at most twice', async () => { assert.deepEqual(failed, [0]) }) +test('a page isn\'t tried again once no read waits for it', async () => { + const api = fakeAPI(10, () => httpError(503)) + const read = readerFor(api) + const stopBridge = read(CIEL_RELEASE, () => {}) + const stopTarget = read(CIEL_RELEASE, () => {}) + stopBridge() + stopTarget() + await api.settle() + + assert.equal(api.reads.length, 1) +}) + test('a stopped read gives nothing more', async () => { const api = fakeAPI(50) const read = readerFor(api) @@ -285,6 +307,8 @@ test('the target version and the bridge probe read through the one shared reader assert.deepEqual(readers, ['createMappedSourcesReader(fetchMappedSourcesPage)']) assert.deepEqual(reads, [['version.version_url', 'setMappedSources']]) assert.match(source, /readMappedSources,\n/) + // Leaving the project stops the target's read. + assert.match(source, /React\.useEffect\(\(\) => \(\) => stopTargetMappedSourcesRef\.current\(\), \[\]\)/) // Nothing reads the list around the reader, and a throttled read settles. assert.equal(source.match(/appendToUrl\('mapped-sources\/'\)/g).length, 1) assert.match(source, /appendToUrl\('mapped-sources\/'\)\.get\(null, null, \{[^}]*\}, true\)/) diff --git a/src/components/map-projects/mappedSources.js b/src/components/map-projects/mappedSources.js index 764a47b..e5eecbb 100644 --- a/src/components/map-projects/mappedSources.js +++ b/src/components/map-projects/mappedSources.js @@ -25,43 +25,68 @@ const withRetry = (request, {isCancelled, ...retry}) => retryWithBackoff( const versionURL = url => `${String(url || '').replace(/\/+$/, '')}/` -// A repository's HEAD is listed at its own URL and at /HEAD/. -const versionKey = url => versionURL(url).replace(/^(\/(?:orgs|users)\/[^/]+\/(?:sources|collections)\/[^/]+\/)HEAD\/$/, '$1') +const decoded = url => { + try { + return decodeURIComponent(url) + } catch { + return url + } +} + +// The API encodes a version's id in its URL, and the bridge's URL is built +// from the bare id. A repository's HEAD is listed at its own URL and at +// /HEAD/. +const versionKey = url => decoded(versionURL(url)).replace(/^(\/(?:orgs|users)\/[^/]+\/(?:sources|collections)\/[^/]+\/)HEAD\/$/, '$1') // fetchPage(url, page) answers as APIService does in raw mode: a response with // the page in `data` and the total in the `num_found` header, or the error. A -// page that fails isn't kept, so the next ask reads it again. +// page that fails isn't kept, so the next ask reads it again, and a page is +// tried again only while a read still waits for it. // // The reader returned, read(url, onList), gives onList the list so far after // each page, and returns a function that stops it. export const createMappedSourcesReader = (fetchPage, retry = RETRY) => { const pages = new Map() - const readPage = (url, page) => { + const readPage = (url, page, waiter) => { const key = `${versionKey(url)}#${page}` - if(!pages.has(key)) - pages.set(key, withRetry(() => fetchPage(url, page), retry).then(response => { + if(!pages.has(key)) { + const waiting = new Set() + const request = withRetry(() => fetchPage(url, page), {...retry, isCancelled: () => !waiting.size}).then(response => { if(!Array.isArray(response?.data)) pages.delete(key) return response - })) - return pages.get(key) + }) + pages.set(key, {request, waiting}) + } + const {request, waiting} = pages.get(key) + waiting.add(waiter) + return {request: request.finally(() => waiting.delete(waiter)), stop: () => waiting.delete(waiter)} } return (url, onList) => { const versionUrl = versionURL(url) + const waiter = {} let stopped = false - const read = (page, soFar) => readPage(versionUrl, page).then(response => { - if(stopped) - return - const data = Array.isArray(response?.data) ? response.data : [] - const list = [...soFar, ...data] - onList(list) - if(data.length && list.length < parseInt(response?.headers?.num_found || 0)) - read(page + 1, list) - }) + let stopWaiting = () => {} + const read = (page, soFar) => { + const {request, stop} = readPage(versionUrl, page, waiter) + stopWaiting = stop + request.then(response => { + if(stopped) + return + const data = Array.isArray(response?.data) ? response.data : [] + const list = [...soFar, ...data] + onList(list) + if(data.length && list.length < parseInt(response?.headers?.num_found || 0)) + read(page + 1, list) + }) + } read(1, []) - return () => { stopped = true } + return () => { + stopped = true + stopWaiting() + } } } From d39cd6407c7c9201c1b3f39ef40f772fe1129968 Mon Sep 17 00:00:00 2001 From: Jonathan Payne Date: Mon, 23 Nov 2026 17:07:12 -0500 Subject: [PATCH 4/6] OpenConceptLab/ocl_issues#3064 | Key twice-encoded version ids as the bare id, and start no read from a versions answer after leaving Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AaZ4qVXohXhqMLGigP4RHV --- src/components/map-projects/MapProject.jsx | 2 +- .../__tests__/mappedSources.test.js | 6 +++++- src/components/map-projects/mappedSources.js | 17 +++++++++++++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/components/map-projects/MapProject.jsx b/src/components/map-projects/MapProject.jsx index 3a5ecb1..115e736 100644 --- a/src/components/map-projects/MapProject.jsx +++ b/src/components/map-projects/MapProject.jsx @@ -3031,7 +3031,7 @@ const MapProject = () => { const fetchVersions = (url, savedVersion) => { const request = ++versionsRequestRef.current APIService.new().overrideURL(dropVersion(url)).appendToUrl('versions/?verbose=true').get().then(response => { - if(request !== versionsRequestRef.current) + if(request !== versionsRequestRef.current || !isMountedRef.current) return let _versions = response.data setVersions(_versions) diff --git a/src/components/map-projects/__tests__/mappedSources.test.js b/src/components/map-projects/__tests__/mappedSources.test.js index 33f60f6..f0cc5f0 100644 --- a/src/components/map-projects/__tests__/mappedSources.test.js +++ b/src/components/map-projects/__tests__/mappedSources.test.js @@ -109,6 +109,8 @@ test('a version id the API encodes in its URL is the same version as the bare id const read = readerFor(api) read(`${CIEL}v1%40build/`, () => {}) read(`${CIEL}v1@build/`, () => {}) + // oclapi2 can encode it twice: once itself, then again in Django's reverse(). + read(`${CIEL}v1%2540build/`, () => {}) await api.settle() assert.deepEqual(api.reads, [`${CIEL}v1%40build/#1`]) @@ -307,7 +309,9 @@ test('the target version and the bridge probe read through the one shared reader assert.deepEqual(readers, ['createMappedSourcesReader(fetchMappedSourcesPage)']) assert.deepEqual(reads, [['version.version_url', 'setMappedSources']]) assert.match(source, /readMappedSources,\n/) - // Leaving the project stops the target's read. + // Leaving the project stops the target's read, and a versions answer that + // lands after it starts none. + assert.match(source, /if\(request !== versionsRequestRef\.current \|\| !isMountedRef\.current\)\n\s*return\n\s*let _versions/) assert.match(source, /React\.useEffect\(\(\) => \(\) => stopTargetMappedSourcesRef\.current\(\), \[\]\)/) // Nothing reads the list around the reader, and a throttled read settles. assert.equal(source.match(/appendToUrl\('mapped-sources\/'\)/g).length, 1) diff --git a/src/components/map-projects/mappedSources.js b/src/components/map-projects/mappedSources.js index e5eecbb..b93d727 100644 --- a/src/components/map-projects/mappedSources.js +++ b/src/components/map-projects/mappedSources.js @@ -25,12 +25,21 @@ const withRetry = (request, {isCancelled, ...retry}) => retryWithBackoff( const versionURL = url => `${String(url || '').replace(/\/+$/, '')}/` +// The API's version URLs can encode a version's id twice. const decoded = url => { - try { - return decodeURIComponent(url) - } catch { - return url + let value = url + for(let i = 0; i < 3; i++) { + let next + try { + next = decodeURIComponent(value) + } catch { + break + } + if(next === value) + break + value = next } + return value } // The API encodes a version's id in its URL, and the bridge's URL is built From 812147c891c264363ab0a1152125a011fe796cf3 Mon Sep 17 00:00:00 2001 From: Jonathan Payne Date: Mon, 23 Nov 2026 17:08:28 -0500 Subject: [PATCH 5/6] OpenConceptLab/ocl_issues#3064 | Simplify the retry options and the version-id decoding Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AaZ4qVXohXhqMLGigP4RHV --- src/components/map-projects/mappedSources.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/components/map-projects/mappedSources.js b/src/components/map-projects/mappedSources.js index b93d727..b53a0ac 100644 --- a/src/components/map-projects/mappedSources.js +++ b/src/components/map-projects/mappedSources.js @@ -14,13 +14,13 @@ const isTransientFailure = response => Boolean(response?.isAxiosError) && (!response.response || response.response.status >= 500) // Settles with the answer, after trying again while the failure is transient. -const withRetry = (request, {isCancelled, ...retry}) => retryWithBackoff( +const withRetry = (request, options) => retryWithBackoff( () => Promise.resolve(request()).then(response => { if(isTransientFailure(response)) throw response return response }), - {...retry, isCancelled, isRetryable: isTransientFailure} + {...options, isRetryable: isTransientFailure} ).catch(() => null) const versionURL = url => `${String(url || '').replace(/\/+$/, '')}/` @@ -28,16 +28,11 @@ const versionURL = url => `${String(url || '').replace(/\/+$/, '')}/` // The API's version URLs can encode a version's id twice. const decoded = url => { let value = url - for(let i = 0; i < 3; i++) { - let next - try { - next = decodeURIComponent(value) - } catch { - break - } - if(next === value) - break - value = next + try { + for(let i = 0; i < 2; i++) + value = decodeURIComponent(value) + } catch { + // keeps the last value that decoded } return value } From 1117b0d0aaab1c636f1615215d94371ccfcaea45 Mon Sep 17 00:00:00 2001 From: Jonathan Payne Date: Mon, 23 Nov 2026 17:12:24 -0500 Subject: [PATCH 6/6] OpenConceptLab/ocl_issues#3064 | Compare version URLs as given, so an encoded id never shares a read with the bare id Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AaZ4qVXohXhqMLGigP4RHV --- .../__tests__/mappedSources.test.js | 12 +++++------ src/components/map-projects/mappedSources.js | 21 +++++-------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/components/map-projects/__tests__/mappedSources.test.js b/src/components/map-projects/__tests__/mappedSources.test.js index f0cc5f0..6f99c69 100644 --- a/src/components/map-projects/__tests__/mappedSources.test.js +++ b/src/components/map-projects/__tests__/mappedSources.test.js @@ -104,16 +104,16 @@ test('a repository\'s HEAD is one version, at its own URL or at HEAD/', async () assert.deepEqual(lists, [10, 10]) }) -test('a version id the API encodes in its URL is the same version as the bare id', async () => { +test('a version id the API encodes in its URL is read apart from the bare id', async () => { + // The server can read a bare `+` differently from the API's encoded form, + // so sharing them could give one caller the other's failed read. const api = fakeAPI(10) const read = readerFor(api) - read(`${CIEL}v1%40build/`, () => {}) - read(`${CIEL}v1@build/`, () => {}) - // oclapi2 can encode it twice: once itself, then again in Django's reverse(). - read(`${CIEL}v1%2540build/`, () => {}) + read(`${CIEL}1.2.3+build.1/`, () => {}) + read(`${CIEL}1.2.3%252Bbuild.1/`, () => {}) await api.settle() - assert.deepEqual(api.reads, [`${CIEL}v1%40build/#1`]) + assert.deepEqual(api.reads, [`${CIEL}1.2.3+build.1/#1`, `${CIEL}1.2.3%252Bbuild.1/#1`]) }) test('a failed page gives what was read so far and is read again on the next ask', async () => { diff --git a/src/components/map-projects/mappedSources.js b/src/components/map-projects/mappedSources.js index b53a0ac..6e41065 100644 --- a/src/components/map-projects/mappedSources.js +++ b/src/components/map-projects/mappedSources.js @@ -25,22 +25,11 @@ const withRetry = (request, options) => retryWithBackoff( const versionURL = url => `${String(url || '').replace(/\/+$/, '')}/` -// The API's version URLs can encode a version's id twice. -const decoded = url => { - let value = url - try { - for(let i = 0; i < 2; i++) - value = decodeURIComponent(value) - } catch { - // keeps the last value that decoded - } - return value -} - -// The API encodes a version's id in its URL, and the bridge's URL is built -// from the bare id. A repository's HEAD is listed at its own URL and at -// /HEAD/. -const versionKey = url => decoded(versionURL(url)).replace(/^(\/(?:orgs|users)\/[^/]+\/(?:sources|collections)\/[^/]+\/)HEAD\/$/, '$1') +// A repository's HEAD is listed at its own URL and at /HEAD/. +// Version URLs are otherwise compared as given: the API encodes a version's id +// in its URL and the bridge's URL holds the bare id, and the server can read +// the two differently, so they aren't taken to be one version. +const versionKey = url => versionURL(url).replace(/^(\/(?:orgs|users)\/[^/]+\/(?:sources|collections)\/[^/]+\/)HEAD\/$/, '$1') // fetchPage(url, page) answers as APIService does in raw mode: a response with // the page in `data` and the total in the `num_found` header, or the error. A