From 71d6ad5ee05a2f36781d210cef69fc71536bea7f Mon Sep 17 00:00:00 2001 From: salman Date: Thu, 3 Sep 2026 23:37:54 +0200 Subject: [PATCH] Fix web series portable run resolution --- muSRFitlib.js | 20 ++++- muSRFitweb.js | 84 +++++++++++++++--- tests/electron-bridge.test.js | 2 +- tests/electron-portable-naming.test.js | 2 +- tests/run-naming.test.js | 46 ++++++---- tests/web-runner.test.js | 115 +++++++++++++++++++++++++ 6 files changed, 240 insertions(+), 29 deletions(-) diff --git a/muSRFitlib.js b/muSRFitlib.js index 9466972..0040ecb 100644 --- a/muSRFitlib.js +++ b/muSRFitlib.js @@ -9183,9 +9183,18 @@ function portableRunFileName(run, year, instrName, dataRoot) { for (let candidate of candidates) { for (let dataPath of dataPaths) { - if (resolveMusrfitRunFile(candidate.subPath, dataPath)) { + // The shared generator is synchronous. Accept only an explicit boolean + // confirmation so a Promise returned by a web adapter cannot be mistaken + // for a successful filesystem lookup. + if (resolveMusrfitRunFile(candidate.subPath, dataPath) === true) { return { subPath: candidate.subPath, + // Extension-less name used for RUN/ADDRUN directives: musrfit + // infers the encoding from the trailing format field, so the + // directive conventionally omits the file extension. Keeping the + // extension off also lets runSuffixFromFile() extract the run + // suffix for msr2data run-qualified global parameters. + file: candidate.file, dataPath: dataPath, beamLine: instPars[instrName] ? instPars[instrName].beamLine : "PSI", format: candidate.format @@ -9241,7 +9250,7 @@ function runFileNameAuto(run, year, instrName, dataRoot) { if (portableRunNamingEnabled) { let portable = portableRunFileName(run, year, instrName, dataRoot); if (portable) { - runLine = ["RUN", portable.subPath, portable.beamLine, "PSI", portable.format].join(" "); + runLine = ["RUN", portable.file, portable.beamLine, "PSI", portable.format].join(" "); return runLine; } } @@ -9700,6 +9709,13 @@ async function prepareMSRRunFiles(actionLabel, prepareOptions = {}) { return({ ok: 0 }); } + if (webOrApp && typeof resolveWebPortableRunSelection === "function") { + // Prime the web resolver before createMSR() enters the synchronous shared + // naming path. Unresolved runs retain the explicit-path fallback and are + // subsequently reported through the normal missing-data handling. + await resolveWebPortableRunSelection(runSelection, All); + } + let fileNameSuffix = fitFileSuffix(All, webOrApp); let workPath = All.workPath && All.workPath !== "" ? All.workPath : defaultWorkPath; diff --git a/muSRFitweb.js b/muSRFitweb.js index 0d1dd30..02d72f8 100644 --- a/muSRFitweb.js +++ b/muSRFitweb.js @@ -7,6 +7,14 @@ if (typeof window.MusrfitWebRequestActive === "undefined") { window.MusrfitWebRequestActive = false; } +// MSR generation is synchronous, while the browser must ask the CGI whether a +// portable data file exists. Resolve selected runs before generation and keep +// only server-confirmed path pairs in this short-lived per-action cache. +var webPortableRunResolution = { + confirmed: new Set(), + activeDataPath: "" +}; + // Web-only shell and I/O adapters for the shared parameter-plot renderer. // Plot construction, panel state, Plotly traces, error bars, and controls stay // in muSRFitParameterPlot.js; this file owns only browser integration. @@ -853,6 +861,63 @@ function webSeriesInventoryFailure(config, execution, reason, message) { }; } +async function resolveWebPortableRunSelection(runSelection, all) { + let result = { ok: true, resolvedRuns: [], unresolvedRuns: [] }; + if (!portableRunNamingEnabled || !portableRunNamingStartup + || typeof portableMusrfitRunCandidates !== "function") { + return result; + } + + let physicalRuns = Array.from(new Set((runSelection || []).reduce(function(runs, dataSet) { + return runs.concat(seriesDataSetPhysicalRuns(dataSet)); + }, []))).sort(function(a, b) { return a - b; }); + let configuredPaths = Array.isArray(portableRunNamingStartup.dataPaths) + ? portableRunNamingStartup.dataPaths.slice() : []; + + // pc13280 exposes both unavailable PSI paths and its mounted NEMU path. Try + // the CGI-selected MUSRFULLDATAPATH first, while retaining every configured + // root as a fallback for installations with several accessible data trees. + let activePath = webPortableRunResolution.activeDataPath; + if (activePath && configuredPaths.includes(activePath)) { + configuredPaths = [activePath].concat(configuredPaths.filter(function(dataPath) { + return dataPath !== activePath; + })); + } + + await Promise.all(physicalRuns.map(async function(run) { + let candidates = portableMusrfitRunCandidates( + run, all.selYear, all.instrName, portableRunNamingStartup + ); + for (let candidate of candidates) { + for (let dataPath of configuredPaths) { + let cacheKey = dataPath + "\n" + candidate.subPath; + if (webPortableRunResolution.confirmed.has(cacheKey)) { + result.resolvedRuns.push(run); + return; + } + + let request = await postToMusrfitCGI({ + action: "resolve_run_name", + sub_path: candidate.subPath, + data_path: dataPath + }); + if (request.ok && request.payload && request.payload.ok === true + && request.payload.sub_path === candidate.subPath + && request.payload.data_path === dataPath) { + webPortableRunResolution.confirmed.add(cacheKey); + result.resolvedRuns.push(run); + return; + } + } + } + result.ok = false; + result.unresolvedRuns.push(run); + })); + result.resolvedRuns.sort(function(a, b) { return a - b; }); + result.unresolvedRuns.sort(function(a, b) { return a - b; }); + return result; +} + // Ask the server for one immutable read-only snapshot, then run the normal // transport-independent classifier against in-memory maps. No browser HEAD // request is made for server filesystem paths, and a malformed/incomplete CGI @@ -899,6 +964,7 @@ async function inspectSeriesFitPlanWeb(config = {}) { let physicalRuns = Array.from(new Set(runSelection.reduce(function(runs, dataSet) { return runs.concat(seriesDataSetPhysicalRuns(dataSet)); }, []))).sort(function(a, b) { return a - b; }); + await resolveWebPortableRunSelection(runSelection, all); let dataRequests = []; try { dataRequests = physicalRuns.map(function(run) { @@ -1027,24 +1093,22 @@ async function loadWebDataRoot() { // under an admin-configured data path, so it is scoped and not a general // server file-existence API. let startup = request.payload.environment.startup_xml; + webPortableRunResolution.confirmed.clear(); + webPortableRunResolution.activeDataPath = + request.payload.environment.musr_full_data_path || ""; if (startup && startup.ok && startup.templates && typeof setPortableRunNaming === "function") { setPortableRunNaming( true, { dataPaths: startup.data_paths || [], templates: startup.templates }, function resolveRunFile(subPath, dataPath) { - return new Promise(function(resolve) { - if (typeof postToMusrfitCGI !== "function") { resolve(false); return; } - postToMusrfitCGI({ - action: "resolve_run_name", - sub_path: subPath, - data_path: dataPath - }).then(function(r) { - resolve(r.ok && r.payload && r.payload.ok); - }).catch(function() { resolve(false); }); - }); + return webPortableRunResolution.confirmed.has( + dataPath + "\n" + subPath + ); } ); + } else if (typeof setPortableRunNaming === "function") { + setPortableRunNaming(false, null, null); } return 1; } diff --git a/tests/electron-bridge.test.js b/tests/electron-bridge.test.js index 50fc444..892e5eb 100644 --- a/tests/electron-bridge.test.js +++ b/tests/electron-bridge.test.js @@ -170,7 +170,7 @@ function testBridgeDrivesPortableNaming() { assert.strictEqual(lib.loadLocalPortableRunNaming(), startupXmlPath); assert.strictEqual( lib.runFileNameAuto(7, "2026", "LEM", "/psi.ch/data/"), - "RUN d2026/tdc/lem26_his_0007.root MUE4 PSI MUSR-ROOT" + "RUN d2026/tdc/lem26_his_0007 MUE4 PSI MUSR-ROOT" ); } diff --git a/tests/electron-portable-naming.test.js b/tests/electron-portable-naming.test.js index 86274a2..12f9c63 100644 --- a/tests/electron-portable-naming.test.js +++ b/tests/electron-portable-naming.test.js @@ -84,7 +84,7 @@ function testPortableEnabledViaConfiguredXml() { assert.strictEqual(found, startupXmlPath); assert.strictEqual( lib.runFileNameAuto(514, "2026", "LEM", "/psi.ch/data/"), - "RUN d2026/tdc/lem26_his_0514.root MUE4 PSI MUSR-ROOT" + "RUN d2026/tdc/lem26_his_0514 MUE4 PSI MUSR-ROOT" ); // The indicator is null-safe in a document without the status element. assert.strictEqual(lib.updateStartupNamingSourceIndicator(), undefined); diff --git a/tests/run-naming.test.js b/tests/run-naming.test.js index 0149c44..52e4a50 100644 --- a/tests/run-naming.test.js +++ b/tests/run-naming.test.js @@ -190,13 +190,13 @@ function testPortableNamingIntegration() { lib.setPortableRunNaming(true, startup, resolveTrue); assert.strictEqual( lib.runFileNameAuto(514, "2026", "LEM", "/psi.ch/data/"), - "RUN d2026/tdc/lem26_his_0514.root MUE4 PSI MUSR-ROOT" + "RUN d2026/tdc/lem26_his_0514 MUE4 PSI MUSR-ROOT" ); // HAL maps to hifi templates. assert.strictEqual( lib.runFileNameAuto(514, "2026", "HAL", "/psi.ch/data/"), - "RUN d2026/tdc/root/deltat_tdc_hifi_2026_0514.mdu PIE3 PSI PSI-MDU" + "RUN d2026/tdc/root/deltat_tdc_hifi_2026_0514 PIE3 PSI PSI-MDU" ); // When no template resolves, fall back to the absolute path. @@ -222,6 +222,21 @@ function testPortableNamingIntegration() { lib.setPortableRunNaming(false, null, null); } +function testPromiseResolverIsNotAcceptedAsAFileMatch() { + const lib = loadLibrary(); + const startup = lib.parseMusrfitStartupXML(SAMPLE_XML); + + // Browser fetch() returns a Promise. The synchronous generator must not + // interpret that object as proof that the first candidate exists. + lib.setPortableRunNaming(true, startup, function() { + return Promise.resolve(true); + }); + assert.strictEqual( + lib.runFileNameAuto(514, "2026", "LEM", "/psi.ch/data/"), + "RUN /psi.ch/data/lem/his/2026/lem26_his_0514 MUE4 PSI MUSR-ROOT" + ); +} + // Historical era transitions and format selection: when multiple templates // exist for one instrument across years, only the era whose file actually // resolves is accepted, and its format (BIN vs ROOT vs MDU) is carried through. @@ -245,8 +260,8 @@ function testEraAndFormatSelection() { return Object.prototype.hasOwnProperty.call(gpsRoot, subPath); }); assert.strictEqual( - lib.runFileNameAuto(7, "2024", "GPS", "/psi.ch/data/"), - "RUN d2024/tdc/root/deltatbc_tdc_gps_2024_0007.root PIM3 PSI MUSR-ROOT" +lib.runFileNameAuto(7, "2024", "GPS", "/psi.ch/data/"), + "RUN d2024/tdc/root/deltatbc_tdc_gps_2024_0007 PIM3 PSI MUSR-ROOT" ); // Year 2010 (2008-2022): only the tdc BIN-era template exists. @@ -256,7 +271,7 @@ function testEraAndFormatSelection() { }); assert.strictEqual( lib.runFileNameAuto(7, "2010", "GPS", "/psi.ch/data/"), - "RUN d2010/tdc/deltat_tdc_gps_0007.bin PIM3 PSI PSI-BIN" + "RUN d2010/tdc/deltat_tdc_gps_0007 PIM3 PSI PSI-BIN" ); // HAL maps to hifi and prefers the pre-2023 MDU-era template (5-digit run). @@ -266,7 +281,7 @@ function testEraAndFormatSelection() { }); assert.strictEqual( lib.runFileNameAuto(7, "2015", "HAL", "/psi.ch/data/"), - "RUN d2015/tdc_hifi_2015_00007.bin PIE3 PSI PSI-MDU" + "RUN d2015/tdc_hifi_2015_00007 PIE3 PSI PSI-MDU" ); lib.setPortableRunNaming(false, null, null); @@ -315,7 +330,7 @@ function testPlatformEquivalence() { assert.strictEqual(electronLine, webLine); assert.strictEqual( electronLine, - "RUN d2026/tdc/lem26_his_0514.root MUE4 PSI MUSR-ROOT" + "RUN d2026/tdc/lem26_his_0514 MUE4 PSI MUSR-ROOT" ); } @@ -328,21 +343,21 @@ function testAllInstrumentsRunLines() { const cases = [ // [instrument, run, year, accepted sub-path, expected RUN line] ["LEM", 7, "2026", "d2026/tdc/lem26_his_0007.root", - "RUN d2026/tdc/lem26_his_0007.root MUE4 PSI MUSR-ROOT"], + "RUN d2026/tdc/lem26_his_0007 MUE4 PSI MUSR-ROOT"], ["GPS", 7, "2024", "d2024/tdc/root/deltat_tdc_gps_2024_0007.root", - "RUN d2024/tdc/root/deltat_tdc_gps_2024_0007.root PIM3 PSI MUSR-ROOT"], + "RUN d2024/tdc/root/deltat_tdc_gps_2024_0007 PIM3 PSI MUSR-ROOT"], ["LTF", 7, "2026", "d2026/tdc/deltat_tdc_ltf_0007.bin", - "RUN d2026/tdc/deltat_tdc_ltf_0007.bin PIM3 PSI PSI-BIN"], + "RUN d2026/tdc/deltat_tdc_ltf_0007 PIM3 PSI PSI-BIN"], ["Dolly", 7, "2026", "d2026/tdc/root/deltat_tdc_dolly_2026_0007.root", - "RUN d2026/tdc/root/deltat_tdc_dolly_2026_0007.root PIE1 PSI MUSR-ROOT"], + "RUN d2026/tdc/root/deltat_tdc_dolly_2026_0007 PIE1 PSI MUSR-ROOT"], ["VMS", 7, "2026", "d2026/tdc/root/deltat_tdc_vms_2026_0007.root", - "RUN d2026/tdc/root/deltat_tdc_vms_2026_0007.root PIE1 PSI MUSR-ROOT"], + "RUN d2026/tdc/root/deltat_tdc_vms_2026_0007 PIE1 PSI MUSR-ROOT"], ["GPD", 7, "2010", "d2010/tdc/deltat_tdc_gpd_0007.bin", - "RUN d2010/tdc/deltat_tdc_gpd_0007.bin PIE1 PSI PSI-BIN"], + "RUN d2010/tdc/deltat_tdc_gpd_0007 PIE1 PSI PSI-BIN"], ["FLAME", 7, "2026", "d2026/tdc/root/deltat_tdc_flame_2026_0007.root", - "RUN d2026/tdc/root/deltat_tdc_flame_2026_0007.root PIM3 PSI MUSR-ROOT"], + "RUN d2026/tdc/root/deltat_tdc_flame_2026_0007 PIM3 PSI MUSR-ROOT"], ["HAL", 7, "2026", "d2026/tdc/root/deltat_tdc_hifi_2026_0007.root", - "RUN d2026/tdc/root/deltat_tdc_hifi_2026_0007.root PIE3 PSI MUSR-ROOT"] + "RUN d2026/tdc/root/deltat_tdc_hifi_2026_0007 PIE3 PSI MUSR-ROOT"] ]; cases.forEach(function(c) { @@ -428,6 +443,7 @@ testInstrumentNameMapping(); testCandidateGeneration(); testAbsolutePathUnchangedWhenDisabled(); testPortableNamingIntegration(); +testPromiseResolverIsNotAcceptedAsAFileMatch(); testEraAndFormatSelection(); testPlatformEquivalence(); testAllInstrumentsRunLines(); diff --git a/tests/web-runner.test.js b/tests/web-runner.test.js index cbe7a67..955bd04 100644 --- a/tests/web-runner.test.js +++ b/tests/web-runner.test.js @@ -456,6 +456,120 @@ async function testWebSeriesInventoryFeedsSharedClassifier() { assert.strictEqual(request.data_files.length, 4); } +async function testWebPortableSeriesUsesConfirmedMountedPath() { + const calls = []; + const missingRoot = "/psi.ch/group/lmu/public/data/lem/his"; + const mountedRoot = "/mnt/data/nemu/his"; + const lib = loadWebLibrary(async function(url, options) { + const body = new URLSearchParams(options.body); + const action = body.get("action"); + calls.push({ action: action, body: body }); + if (action === "health") { + return createResponse(JSON.stringify({ + ok: true, + environment: { + data_root: "/psi.ch/group/lmu/public/data", + musr_full_data_path: mountedRoot, + startup_xml: { + ok: true, + data_paths: [missingRoot, mountedRoot], + templates: { + lem: ["%yyyy%/lem%yy%_his_%rrrr%.root"] + } + } + } + })); + } + if (action === "resolve_run_name") { + const subPath = body.get("sub_path"); + const dataPath = body.get("data_path"); + const found = dataPath === mountedRoot; + return createResponse(JSON.stringify(found ? { + ok: true, + code: 0, + sub_path: subPath, + data_path: dataPath, + run_name: subPath + } : { + ok: false, + code: 404, + message: "Portable RUN file not found" + })); + } + if (action === "series_inventory") { + const inventory = JSON.parse(body.get("inventory")); + return createResponse(JSON.stringify({ + ok: true, + code: 0, + reason: "series-inventory-ready", + job_exists: false, + msr_files: inventory.msr_files.map(function(item) { + return { + run: item.run, + name: item.name, + path: "/tmp/job/" + item.name, + exists: false, + readable: false, + size: 0, + text: "" + }; + }), + data_files: inventory.data_files.map(function(item) { + return { run: item.run, path: item.path, exists: true }; + }) + })); + } + throw new Error("Unexpected CGI action: " + action); + }); + lib.setConfiguredDataRoot = function() { + }; + + assert.strictEqual(await lib.loadWebDataRoot(), 1); + // Before the asynchronous preflight completes, no Promise is accepted as a + // portable match and the normal explicit-path fallback remains intact. + assert.match( + lib.runFileNameAuto(514, "2026", "LEM", "/psi.ch/group/lmu/public/data"), + /^RUN\s+\/psi\.ch\// + ); + + const plan = await lib.inspectSeriesFitPlanWeb({ + all: { + runNumbers: "514,515", + fitAsyType: "AsymmetryGLB", + workPath: "/tmp/job", + dataRoot: "/psi.ch/group/lmu/public/data", + instrName: "LEM", + selYear: "2026" + }, + workPath: "/tmp/job", + fileNameSuffix: "_asy", + sharedFlags: {} + }); + + assert.strictEqual(plan.ok, true); + assert.deepStrictEqual(Array.from(plan.availableDataRuns), [514, 515]); + assert.deepStrictEqual(Array.from(plan.entries.map(function(entry) { + return entry.action; + })), ["fit", "fit"]); + assert.deepStrictEqual(Array.from(plan.entries.map(function(entry) { + return entry.dataPaths[0]; + })), [ + mountedRoot + "/2026/lem26_his_0514.root", + mountedRoot + "/2026/lem26_his_0515.root" + ]); + assert.strictEqual( + lib.runFileNameAuto(514, "2026", "LEM", "/psi.ch/group/lmu/public/data"), + "RUN 2026/lem26_his_0514 MUE4 PSI MUSR-ROOT" + ); + const resolutionCalls = calls.filter(function(call) { + return call.action === "resolve_run_name"; + }); + assert.strictEqual(resolutionCalls.length, 2); + assert.strictEqual(resolutionCalls.every(function(call) { + return call.body.get("data_path") === mountedRoot; + }), true, "the active mounted data path must be checked before unavailable XML roots"); +} + async function testIncompleteWebSeriesInventoryFailsClosed() { const lib = loadWebLibrary(async function() { return createResponse(JSON.stringify({ @@ -670,6 +784,7 @@ async function runTests() { await testWebSeriesCollectionRegistersArtifactsAndPlot(); await testWebParameterCollectionSurvivesClientMergeFailure(); await testWebSeriesInventoryFeedsSharedClassifier(); + await testWebPortableSeriesUsesConfirmedMountedPath(); await testIncompleteWebSeriesInventoryFailsClosed(); await testWebRunLoadsFittedMSRAndArtifacts(); await testWebRunReportsMissingDataSeparately();