diff --git a/TODO.md b/TODO.md index bd989a0..cd6cb90 100644 --- a/TODO.md +++ b/TODO.md @@ -366,7 +366,26 @@ executables on the server; computational and text-transformation logic belongs i files or command output that the browser never sees in web mode, so they must remain server-side. -## 5. Near-term execution order +## 5. Post-1.0 security hardening + +These defense-in-depth tasks are intentionally deferred until after the stable +`1.0.0` workflow release. Pre-1.0 changes should remain focused on reliable fit, +plot, editor, synchronization, and result-collection behavior. + +- [ ] Validate shared-configuration links against a strict schema before they + mutate GUI state. Restrict control and parameter identifiers, value types, + model names, constraint sizes, and detector-map definitions. +- [ ] Replace parameter-table and custom-parameter `innerHTML` construction with + DOM element creation and property assignment, so restored values are always + treated as text rather than markup. +- [ ] Bound compressed and uncompressed shared-configuration payload sizes and + reject oversized or excessively nested objects before decompression/restore. +- [ ] Narrow the Electron preload bridge from general filesystem and child-process + primitives to the specific file and musrfit operations required by the GUI. +- [ ] Add an Electron/web Content Security Policy and explicit navigation/window + controls, with regression tests for shared-link and imported-MSR content. + +## 6. Near-term execution order 1. Real-command regression baseline, independent-variable detection, the built-in TF scan template, and startup-XML portable naming are established diff --git a/cgi/single_musrfit.cgi b/cgi/single_musrfit.cgi index ff5b861..305a4f1 100755 --- a/cgi/single_musrfit.cgi +++ b/cgi/single_musrfit.cgi @@ -1321,13 +1321,21 @@ sub perform_series_artifact_collection { push(@warnings, "musrview did not produce a ROOT plot") if !@root_urls; } - # Bundle all generated artifacts (per-run MSRs, combined MSR, parameter - # table, log, ROOT files) into one archive for the Results "All files" link. + # Keep the archive in this job's directory. A first-run-based name in the + # shared TMPROOT allowed concurrent browser sessions to overwrite each + # other's download even though their working directories were independent. my $package_name = $base_run . "+series" . $suffix . ".tgz"; - my $package_path = "$work_path/../$package_name"; - if (!-l $package_path && system("cd " . shell_quote($work_path) + my $package_path = "$work_path/$package_name"; + if (-e $package_path || -l $package_path) { + if (-l $package_path || !unlink($package_path)) { + push(@warnings, "Could not replace the series results archive"); + $package_path = ""; + } + } + if ($package_path ne "" && system("cd " . shell_quote($work_path) . " && tar -czf " . shell_quote($package_path) . " * >/dev/null 2>&1")) { push(@warnings, "Could not create the series results archive"); + unlink($package_path) if -f $package_path && !-l $package_path; $package_path = ""; } @@ -1350,7 +1358,7 @@ sub perform_series_artifact_collection { # Results "Template MSR" entry; the full set is inside the archive. template_url => "$TMP_URL/$job_name/$run_numbers[0]$suffix.msr", package_url => $package_path ne "" - ? "$TMP_URL/$package_name" : "", + ? "$TMP_URL/$job_name/$package_name" : "", root_url => @root_urls ? $root_urls[0] : "", root_urls => \@root_urls, root_items => \@root_items, diff --git a/muSRFitelec.js b/muSRFitelec.js index 93eb422..79906ef 100644 --- a/muSRFitelec.js +++ b/muSRFitelec.js @@ -1094,7 +1094,8 @@ async function collectIndependentSeriesArtifactsLocal(spec) { } // The first fitted run's own MSR doubles as the per-run template shown by - // the Results "Template MSR" entry; the full set lands in the archive below. + // the Results "Template MSR" entry. All per-run files remain directly + // available in the local working directory. result.templatePath = prepared.msrPaths[0]; // Independent-series artifacts use their own namespace. A genuine global @@ -1213,45 +1214,6 @@ async function collectIndependentSeriesArtifactsLocal(spec) { result.plotStarted = true; } - // Bundle all generated artifacts (per-run MSRs, combined MSR, parameter - // table, log, ROOT plots) into one archive for the Results "All files" - // link, mirroring the web server's series package. - if (typeof nodeChildProcess.spawnSync === "function") { - let packagePath = nodePath.join(workPath, - combinedName.replace(/\.msr$/i, ".tgz")); - let archiveItems = prepared.msrPaths.map(function(p) { - return nodePath.basename(p); - }); - if (result.combinedMSRPath) archiveItems.push(nodePath.basename(result.combinedMSRPath)); - if (result.parameterDataPath) archiveItems.push(nodePath.basename(result.parameterDataPath)); - if (result.logPath) archiveItems.push(nodePath.basename(result.logPath)); - try { - for (let name of nodeFs.readdirSync(workPath)) { - if (/\.root$/i.test(name)) archiveItems.push(name); - } - } catch (error) { - // readdir failures only reduce the archive to the known files. - } - archiveItems = archiveItems.filter(function(name, index) { - return typeof name === "string" && name !== "" - && archiveItems.indexOf(name) === index - && nodeFs.existsSync(nodePath.join(workPath, name)); - }); - if (nodeFs.existsSync(packagePath)) nodeFs.unlinkSync(packagePath); - let tarResult = nodeChildProcess.spawnSync("tar", - ["-czf", packagePath, "-C", workPath].concat(archiveItems), { - encoding: "utf8", shell: false - }); - if ((!tarResult || tarResult.error || tarResult.status !== 0) - || !nodeFs.existsSync(packagePath)) { - result.warnings.push("Could not create the series results archive."); - } else { - result.packagePath = packagePath; - } - } else { - result.warnings.push("Series results archive is unavailable in this environment."); - } - return normalizeIndependentSeriesCollectionResult(result); } catch (error) { result.reason = "series-artifact-collection-failed"; @@ -1468,16 +1430,29 @@ async function runMSRFileLocal(fullPath, options = {}) { } } + let fitSucceeded = fitResult.ok === 1; let viewId = window.MusrfitRunner.nextViewId++; - runLocalProcess("musrview #" + viewId, musrview, musrviewArgs, fitDir, logFile).then(function(viewResult) { - if (viewResult.ok && typeof setFitStatus === "function") { + // A diagnostic viewer must not replace the failed musrfit result with its + // own successful exit status. It still writes its command and output to the + // run log, but the failed fit remains the outcome shown to the user. + runLocalProcess("musrview #" + viewId, musrview, musrviewArgs, fitDir, logFile, { + reportStatus: fitSucceeded + }).then(function(viewResult) { + if (fitSucceeded && viewResult.ok && typeof setFitStatus === "function") { setFitStatus("Plot #" + viewId + " completed.", { level: "success", details: "Local musrview #" + viewId + " completed." }); } + if (!fitSucceeded && !viewResult.ok && typeof setFitStatus === "function") { + setFitStatus("Fit failed; the diagnostic plot could not be opened.", { + level: "error", + details: "musrfit failed and diagnostic musrview #" + viewId + + " could not be opened. See the run log for both commands." + }); + } }); - return 1; + return fitSucceeded ? 1 : 0; } // Portable RUN naming for Electron: resolve candidate files on this machine and diff --git a/muSRFitlib.js b/muSRFitlib.js index abe9b91..cd04ad9 100644 --- a/muSRFitlib.js +++ b/muSRFitlib.js @@ -1220,23 +1220,20 @@ async function inspectSeriesFitPlan(config = {}, fileAccess = {}) { return !availableDataSet.has(run); }); - // A completed result remains reusable without reopening its raw data. - // Every action that would run musrfit, however, is blocked until all main - // and ADDRUN data files are positively available. - if (missingDataRuns.length > 0 && classification.action != "skip") { - classification.state = "missing-data"; - classification.action = "blocked"; - classification.reusable = false; - classification.reason = "required-data-not-found"; - } - // Per-run series action chips can override the natural classification. - // "refit" regenerates this run's MSR from the live GUI parameters and - // fits it; "skip" leaves the run completely untouched. + // "reuse" accepts an existing, identity-checked converged result even + // when it is too recent for automatic reuse. "refit" regenerates the MSR + // from the live GUI, while "skip" leaves the run completely untouched. let useGuiTemplate = false; if (Object.prototype.hasOwnProperty.call(actionOverrides, dataSet.main)) { let override = actionOverrides[dataSet.main]; - if (override == "refit") { + if (override == "reuse" && msrExists + && classification.statistic.converged === true) { + classification.state = "converged-complete"; + classification.action = "skip"; + classification.reusable = true; + classification.reason = "user-requested-reuse"; + } else if (override == "refit") { // "refit" means rebuild this run from the live GUI parameters and // replace its on-disk MSR. Replacing nothing is contradictory, so // when the MSR no longer exists (e.g. it was deleted) treat it as a @@ -1260,6 +1257,16 @@ async function inspectSeriesFitPlan(config = {}, fileAccess = {}) { } } + // A completed result remains reusable without reopening its raw data. + // Every action that would run musrfit, including a requested refit, is + // blocked until all main and ADDRUN data files are positively available. + if (missingDataRuns.length > 0 && classification.action != "skip") { + classification.state = "missing-data"; + classification.action = "blocked"; + classification.reusable = false; + classification.reason = "required-data-not-found"; + } + return { dataSet: normalizeSeriesDataSet(dataSet), run: dataSet.main, @@ -7694,10 +7701,9 @@ function renderSeriesActionChips(dataSets) { } } -// Read the series action overrides back out of the chips, returning a map of -// run number → "refit" | "skip". Runs not represented used their natural -// classification. "refit" forces a candidate to be regenerated from the live -// GUI parameters rather than from the series reference; "skip" excludes it. +// Read every visible chip back as an explicit user choice. Keeping "reuse" in +// the map matters for recent converged runs: automatic classification would +// conservatively refit them, while the user's choice should accept the result. function collectSeriesActionOverrides() { let overrides = {}; if (typeof document === "undefined") return overrides; @@ -7706,7 +7712,7 @@ function collectSeriesActionOverrides() { for (let chip of container.querySelectorAll(".series-action-chip")) { let run = Number(chip.dataset.run || 0); let action = chip.dataset.action; - if (run > 0 && (action == "refit" || action == "skip")) { + if (run > 0 && (action == "reuse" || action == "refit" || action == "skip")) { overrides[run] = action; } } @@ -10141,6 +10147,12 @@ function buildIndependentSeriesArtifactSpec(plan, config = {}) { } for (let entry of plan.entries) { + // A deliberate skip means "leave this run out", not "reuse its current + // file". Reusable skips are converged fits and remain valid inputs to + // msr2data; user-requested skips must not leak into the aggregate table. + if (entry && entry.action == "skip" && entry.reusable !== true) { + continue; + } if (!entry || entry.action == "blocked" || !entry.dataSet || !Number.isInteger(entry.dataSet.main) || entry.dataSet.main <= 0 || typeof entry.msrPath !== "string" || entry.msrPath === "") { @@ -10150,6 +10162,10 @@ function buildIndependentSeriesArtifactSpec(plan, config = {}) { result.runs.push(entry.dataSet.main.toString()); result.msrPaths.push(entry.msrPath); } + if (result.runs.length < 2) { + result.reason = "insufficient-series-artifact-runs"; + return result; + } if (new Set(result.runs).size !== result.runs.length) { result.reason = "duplicate-series-artifact-run"; return result; @@ -10561,18 +10577,22 @@ async function runIndependentSeriesFit(execution, webMode) { return 0; } - let reused = plan.summary.skip; + let reused = plan.entries.filter(function(entry) { + return entry.action == "skip" && entry.reusable === true; + }).length; + let skipped = plan.entries.filter(function(entry) { + return entry.action == "skip" && entry.reusable !== true; + }).length; + let initiallyBlocked = plan.entries.filter(function(entry) { + return entry.action == "blocked"; + }); let pendingTotal = plan.summary.fit + plan.summary.retry + plan.summary.refit; - let bootstrapSequence = null; - if (pendingTotal === 0) { - // Every selected run is skipped (all chips set to skip, or all existing - // MSRs are already converged). There is nothing left to fit or reuse as a - // reference, so report this clearly before requiring a runner or entering - // the bootstrap path. - let message = reused > 0 - ? "All selected runs are already fitted and none were set for a refit; " - + reused + " reused." - : "No run is scheduled to be fitted; set at least one run to refit or reuse."; + if (pendingTotal === 0 && reused === 0 && initiallyBlocked.length === 0) { + // Explicitly skipped runs are intentionally absent from both fitting and + // collection. Do not manufacture an aggregate from files the user chose + // to leave out. + let message = "No run is scheduled: 0 fitted, 0 reused, " + skipped + + " skipped, 0 missing, 0 failed. Set at least one run to reuse or refit."; updateJobStatus({ state: "Complete", message: message, @@ -10585,94 +10605,109 @@ async function runIndependentSeriesFit(execution, webMode) { return 1; } - let referenceEntry = plan.entries.find(function(entry) { - return entry.reusable === true && entry.action == "skip" && entry.msrExists === true; - }); - let adapter = window.MusrfitSeriesTransactionAdapter; - let runner = webMode - ? window.runSeriesMSRCandidateWeb : window.runSeriesMSRCandidateLocal; - if (!adapter || typeof runner !== "function") { - let message = seriesFitFailureMessage("series-runner-unavailable"); - finishJobStatus(0, 1, message); - return 0; - } - setMusrfitActionState("fit", "running"); - if (!referenceEntry) { - if (!validateRRFSettings("Fit", config.all)) { - finishJobStatus(0, 1, "Check the RRF settings before fitting the first run."); - return 0; - } - let t0BgValidation = validateT0BgOverrides( - config.all, [plan.entries[0].run] - ); - if (!t0BgValidation.ok) { - finishJobStatus(0, 1, t0BgValidation.message); + let sequence; + if (pendingTotal === 0) { + // Reusing completed fits does not require the musrfit runner. Continue to + // the common post-processing path so msr2data refreshes the aggregate. + sequence = { + ok: initiallyBlocked.length === 0, + reason: initiallyBlocked.length > 0 + ? "series-no-runnable-candidates" : "series-already-complete", + completed: [], + excluded: plan.entries.slice(), + failed: null + }; + } else { + let bootstrapSequence = null; + let referenceEntry = plan.entries.find(function(entry) { + return entry.reusable === true && entry.action == "skip" && entry.msrExists === true; + }); + let adapter = window.MusrfitSeriesTransactionAdapter; + let runner = webMode + ? window.runSeriesMSRCandidateWeb : window.runSeriesMSRCandidateLocal; + if (!adapter || typeof runner !== "function") { + let message = seriesFitFailureMessage("series-runner-unavailable"); + finishJobStatus(0, 1, message); return 0; } - updateJobStatus({ - state: "Preparing first run", - message: "Preparing the first-run template.", - messageDetail: "No reusable reference exists; generating the first selected run from the GUI.", - noticeLevel: "info" - }); - appendJobLog("No reusable reference found; generating run " - + plan.entries[0].run + " from the GUI."); - let bootstrap = prepareSeriesBootstrapCandidate(plan, config); - if (!bootstrap.ok) { - let message = seriesFitFailureMessage(bootstrap.reason, bootstrap); - finishJobStatus(0, 1, message + "\nReason: " + bootstrap.reason); + if (!referenceEntry) { + if (!validateRRFSettings("Fit", config.all)) { + finishJobStatus(0, 1, "Check the RRF settings before fitting the first run."); + return 0; + } + let t0BgValidation = validateT0BgOverrides( + config.all, [plan.entries[0].run] + ); + if (!t0BgValidation.ok) { + finishJobStatus(0, 1, t0BgValidation.message); + return 0; + } + + updateJobStatus({ + state: "Preparing first run", + message: "Preparing the first-run template.", + messageDetail: "No reusable reference exists; generating the first selected run from the GUI.", + noticeLevel: "info" + }); + appendJobLog("No reusable reference found; generating run " + + plan.entries[0].run + " from the GUI."); + let bootstrap = prepareSeriesBootstrapCandidate(plan, config); + if (!bootstrap.ok) { + let message = seriesFitFailureMessage(bootstrap.reason, bootstrap); + finishJobStatus(0, 1, message + "\nReason: " + bootstrap.reason); + return 0; + } + bootstrapSequence = await runSeriesSequenceWithProgress( + bootstrap, adapter, runner, workPath, { + positionOffset: 0, + total: pendingTotal, + transactionId: "series-bootstrap-" + Date.now().toString(36) + } + ); + if (!bootstrapSequence.ok) { + let message = seriesFitFailureMessage(bootstrapSequence.reason, bootstrapSequence); + let details = seriesSequenceFailureDetails(bootstrapSequence); + finishJobStatus(0, 1, message + "\nReason: " + bootstrapSequence.reason + + (details ? "\n" + details : "")); + return 0; + } + + let promoted = await promoteBootstrapAsSeriesReference( + plan, config, bootstrapSequence + ); + if (!promoted.ok) { + let message = seriesFitFailureMessage(promoted.reason, promoted); + finishJobStatus(0, 1, message + "\nReason: " + promoted.reason); + return 0; + } + plan = promoted.plan; + referenceEntry = plan.entries[0]; + config.referenceRun = referenceEntry.run; + config.referenceMSRText = promoted.referenceMSRText; + } else { + config.referenceMSRText = referenceEntry.msrText; + } + + let prepared = await prepareSeriesMSRCandidates(plan, config); + if (!prepared.ok) { + let message = seriesFitFailureMessage(prepared.reason, prepared); + finishJobStatus(0, 1, message + "\nReason: " + prepared.reason); return 0; } - bootstrapSequence = await runSeriesSequenceWithProgress( - bootstrap, adapter, runner, workPath, { - positionOffset: 0, + + let positionOffset = bootstrapSequence ? bootstrapSequence.completed.length : 0; + sequence = await runSeriesSequenceWithProgress( + prepared, adapter, runner, workPath, { + positionOffset: positionOffset, total: pendingTotal, - transactionId: "series-bootstrap-" + Date.now().toString(36) + transactionId: "series-" + Date.now().toString(36) } ); - if (!bootstrapSequence.ok) { - let message = seriesFitFailureMessage(bootstrapSequence.reason, bootstrapSequence); - let details = seriesSequenceFailureDetails(bootstrapSequence); - finishJobStatus(0, 1, message + "\nReason: " + bootstrapSequence.reason - + (details ? "\n" + details : "")); - return 0; + if (bootstrapSequence) { + sequence.completed = bootstrapSequence.completed.concat(sequence.completed); } - - let promoted = await promoteBootstrapAsSeriesReference( - plan, config, bootstrapSequence - ); - if (!promoted.ok) { - let message = seriesFitFailureMessage(promoted.reason, promoted); - finishJobStatus(0, 1, message + "\nReason: " + promoted.reason); - return 0; - } - plan = promoted.plan; - referenceEntry = plan.entries[0]; - config.referenceRun = referenceEntry.run; - config.referenceMSRText = promoted.referenceMSRText; - } else { - config.referenceMSRText = referenceEntry.msrText; - } - - let prepared = await prepareSeriesMSRCandidates(plan, config); - if (!prepared.ok) { - let message = seriesFitFailureMessage(prepared.reason, prepared); - finishJobStatus(0, 1, message + "\nReason: " + prepared.reason); - return 0; - } - - let positionOffset = bootstrapSequence ? bootstrapSequence.completed.length : 0; - let sequence = await runSeriesSequenceWithProgress( - prepared, adapter, runner, workPath, { - positionOffset: positionOffset, - total: pendingTotal, - transactionId: "series-" + Date.now().toString(36) - } - ); - if (bootstrapSequence) { - sequence.completed = bootstrapSequence.completed.concat(sequence.completed); } await synchronizeLastSeriesResult(sequence); @@ -10699,10 +10734,11 @@ async function runIndependentSeriesFit(execution, webMode) { } } } + let summary = fitted + " fitted, " + reused + " reused, " + skipped + + " skipped, 0 missing, 0 failed."; let message = sequence.reason == "series-already-complete" - ? "All selected runs are already fitted." - : "Independent series fit completed: " + fitted + " fitted, " - + reused + " reused."; + ? "No fits were run: " + summary + : "Independent series fit completed: " + summary; if (artifacts.parameterDataPath) message += " Parameter table prepared."; if (artifacts.plotStarted || artifacts.plotPath) message += " Combined plot opened."; let warnings = Array.isArray(artifacts.warnings) @@ -10713,8 +10749,9 @@ async function runIndependentSeriesFit(execution, webMode) { } if (sequence.reason == "series-sequence-completed-with-blocked-runs" || sequence.reason == "series-no-runnable-candidates") { - let message = "Independent fits completed, but " + blocked.length - + " run" + (blocked.length == 1 ? " is" : "s are") + " missing data."; + let message = "Independent series fit incomplete: " + fitted + " fitted, " + + reused + " reused, " + skipped + " skipped, " + blocked.length + + " missing, 0 failed."; updateJobStatus({ state: "Incomplete", message: message, @@ -10728,14 +10765,14 @@ async function runIndependentSeriesFit(execution, webMode) { } let failure = seriesFitFailureMessage(sequence.reason, sequence); - let prefix = fitted > 0 - ? fitted + " earlier run" + (fitted == 1 ? " was" : "s were") - + " fitted and kept.\n" : ""; + let outcome = "Independent series fit stopped: " + fitted + " fitted, " + + reused + " reused, " + skipped + " skipped, " + blocked.length + + " missing, " + (sequence.failed ? 1 : 0) + " failed."; let code = sequence.failed && sequence.failed.runResult && sequence.failed.runResult.code !== undefined ? sequence.failed.runResult.code : 1; let details = seriesSequenceFailureDetails(sequence); - finishJobStatus(0, code, failure + "\n" + prefix + "Reason: " + sequence.reason + finishJobStatus(0, code, failure + "\n" + outcome + "\nReason: " + sequence.reason + (details ? "\n" + details : "")); return 0; } diff --git a/tests/cgi-series-collection.test.js b/tests/cgi-series-collection.test.js index 1160e43..1d2f4c4 100644 --- a/tests/cgi-series-collection.test.js +++ b/tests/cgi-series-collection.test.js @@ -163,9 +163,13 @@ try { // The first fitted run's own MSR is the per-run template, and the archive // bundles every generated artifact for the Results "All files" link. assert.strictEqual(response.template_url, "/tmp/seriesjob/514_asy.msr"); - assert.strictEqual(response.package_url, "/tmp/514+series_asy.tgz"); - assert.strictEqual(fs.existsSync(path.join(tmpRoot, "514+series_asy.tgz")), true); - assert.ok(fs.statSync(path.join(tmpRoot, "514+series_asy.tgz")).size > 0); + assert.strictEqual(response.package_url, + "/tmp/seriesjob/514+series_asy.tgz"); + const packagePath = path.join(workPath, "514+series_asy.tgz"); + assert.strictEqual(fs.existsSync(packagePath), true); + assert.ok(fs.statSync(packagePath).size > 0); + assert.strictEqual(fs.existsSync(path.join(tmpRoot, "514+series_asy.tgz")), false, + "the archive must not escape the session-specific job directory"); assert.deepStrictEqual(response.root_urls, ["/tmp/seriesjob/514+series_asy_0.root"]); assert.match(response.warnings.join("\n"), /returned code 9/); diff --git a/tests/electron-missing-data.test.js b/tests/electron-missing-data.test.js index 6805518..426d992 100644 --- a/tests/electron-missing-data.test.js +++ b/tests/electron-missing-data.test.js @@ -10,11 +10,17 @@ const vm = require("vm"); const repoRoot = path.resolve(__dirname, ".."); const spawnedCommands = []; const completions = []; +const notices = []; +let runnerScenario = "missing-data"; const missingOutput = [ "PRunDataHandler::FileExistsCheck(): **ERROR** Couldn't find " + "'/psi.ch/group/lmu/public/data/flame/his/2026/flame26_his_01285' in any standard path.", "musrfit **ERROR** Couldn't read all data files, will quit ..." ].join("\n"); +const failedFitOutput = [ + "musrfit **ERROR** minimization did not converge.", + "The fitted MSR remains available for inspection." +].join("\n"); const fakeFs = { appendFileSync: function() { @@ -32,8 +38,17 @@ const fakeChildProcess = { child.stdout = new events.EventEmitter(); child.stderr = new events.EventEmitter(); setImmediate(function() { - child.stderr.emit("data", Buffer.from(missingOutput)); - child.emit("close", 1, null); + if (command === "musrview") { + child.emit("close", 0, null); + return; + } + if (runnerScenario === "missing-data") { + child.stderr.emit("data", Buffer.from(missingOutput)); + child.emit("close", 1, null); + return; + } + child.stderr.emit("data", Buffer.from(failedFitOutput)); + child.emit("close", 2, null); }); return child; } @@ -75,7 +90,8 @@ sandbox.appendJobLog = function() { }; sandbox.updateJobStatus = function() { }; -sandbox.setFitStatus = function() { +sandbox.setFitStatus = function(message, options) { + notices.push({ message: message, options: options || {} }); }; sandbox.finishJobStatus = function(ok, code, message, outcome) { completions.push({ ok: ok, code: code, message: message, outcome: outcome }); @@ -89,7 +105,33 @@ sandbox.runMSRFileLocal("/tmp/1285_asy.msr", { plotOnly: false }).then(function( assert.strictEqual(completions.length, 1); assert.strictEqual(completions[0].outcome, "missing-data"); assert.match(completions[0].message, /Data file not found: flame26_his_01285/); - console.log("Electron missing-data tests passed"); + spawnedCommands.length = 0; + completions.length = 0; + notices.length = 0; + runnerScenario = "fit-failure"; + return sandbox.runMSRFileLocal("/tmp/1285_asy.msr", { plotOnly: false }); +}).then(function(result) { + assert.strictEqual(result, 0, "a failed fit must remain a failed action"); + assert.strictEqual(spawnedCommands.length, 2, + "a generic fit failure should still open one diagnostic musrview"); + assert.strictEqual(spawnedCommands[0].command, "musrfit"); + assert.strictEqual(spawnedCommands[1].command, "musrview"); + + // Allow the diagnostic viewer's close handler to run. It must not add a + // successful completion or turn the visible notice into a success. + return new Promise(function(resolve) { + setImmediate(resolve); + }); +}).then(function() { + assert.strictEqual(completions.length, 1, + "diagnostic musrview must not own the main job completion status"); + assert.strictEqual(completions[0].ok, 0); + assert.strictEqual(completions[0].code, 2); + assert.strictEqual(notices.some(function(notice) { + return notice.options.level === "success"; + }), false, "a diagnostic plot must not emit a success notice after a failed fit"); + assert.match(notices[0].message, /Fit failed; opening plot/); + console.log("Electron missing-data and failed-fit status tests passed"); }).catch(function(error) { console.error(error); process.exitCode = 1; diff --git a/tests/series-action-chips.test.js b/tests/series-action-chips.test.js index bdcfa1d..dc8c655 100644 --- a/tests/series-action-chips.test.js +++ b/tests/series-action-chips.test.js @@ -63,8 +63,8 @@ function testCollectSeriesActionOverrides() { // Objects returned from a vm-isolated context use a different Object.prototype, // so compare by content rather than deepStrictEqual (which checks prototypes). assert.strictEqual(JSON.stringify(overrides), - JSON.stringify({ 2534: "refit", 2536: "skip" }), - "only refit/skip chips should be returned, reuse should be omitted"); + JSON.stringify({ 2531: "reuse", 2534: "refit", 2536: "skip" }), + "all visible chip choices must be passed to the series planner"); } function testCollectSeriesActionOverridesEmptyContainer() { @@ -285,7 +285,7 @@ async function testInspectPlanAppliesRefitOverride() { sharedFlags: {}, fileNameSuffix: "_asy", workPath: "/work", - actionOverrides: { 2534: "refit", 2536: "skip" } + actionOverrides: { 2531: "reuse", 2534: "refit", 2536: "skip" } }; lib.parseRunSelection = function(text) { return config.runSelection; }; lib.planFitExecution = function() { @@ -294,11 +294,12 @@ async function testInspectPlanAppliesRefitOverride() { lib.seriesDataFilePath = function(run) { return "/data/run" + run; }; lib.collectSharedParameterControls = function() { return {}; }; lib.classifySeriesFitResult = function(opts) { + const recent = opts.expectedDataSet.main === 2531; return { - state: "converged-complete", - action: "skip", - reusable: true, - reason: "converged-with-newer-data-runs", + state: recent ? "converged-recent" : "converged-complete", + action: recent ? "refit" : "skip", + reusable: !recent, + reason: recent ? "not-enough-newer-data-runs" : "converged-with-newer-data-runs", statistic: { found: true, converged: true, kind: "chi2", value: "1.0", ndf: "10", text: "" } }; }; @@ -317,9 +318,11 @@ async function testInspectPlanAppliesRefitOverride() { const byRun = {}; for (const entry of plan.entries) byRun[entry.run] = entry; - // 2531 unchanged — natural converged-complete → skip. + // Explicit reuse accepts an identity-checked converged file even when the + // automatic maturity rule would otherwise schedule a conservative refit. assert.strictEqual(byRun[2531].action, "skip"); assert.strictEqual(byRun[2531].reusable, true); + assert.strictEqual(byRun[2531].reason, "user-requested-reuse"); // 2534 forced to refit from GUI template. assert.strictEqual(byRun[2534].action, "refit"); diff --git a/tests/series-artifact-contract.test.js b/tests/series-artifact-contract.test.js index 481f10b..e94db55 100644 --- a/tests/series-artifact-contract.test.js +++ b/tests/series-artifact-contract.test.js @@ -37,6 +37,45 @@ assert.strictEqual(sandbox.prepareIndependentSeriesCollection(Object.assign({}, runs: ["514", "514"] })).reason, "series-artifact-source-invalid"); +// Explicitly skipped runs are not fitted or collected. Reusable converged runs +// and runs fitted in the current sequence both belong in the aggregate. +const filteredSpec = sandbox.buildIndependentSeriesArtifactSpec({ + ok: true, + mode: "series", + entries: [ + { action: "skip", reusable: true, dataSet: { main: 514, add: [] }, + msrPath: "/tmp/job/514_asy.msr" }, + { action: "skip", reusable: false, dataSet: { main: 515, add: [] }, + msrPath: "/tmp/job/515_asy.msr" }, + { action: "refit", reusable: false, dataSet: { main: 516, add: [] }, + msrPath: "/tmp/job/516_asy.msr" } + ] +}, { + workPath: "/tmp/job", + fileNameSuffix: "_asy" +}); +assert.strictEqual(filteredSpec.ok, true); +assert.deepStrictEqual(Array.from(filteredSpec.runs), ["514", "516"]); +assert.deepStrictEqual(Array.from(filteredSpec.msrPaths), [ + "/tmp/job/514_asy.msr", "/tmp/job/516_asy.msr" +]); + +const insufficientSpec = sandbox.buildIndependentSeriesArtifactSpec({ + ok: true, + mode: "series", + entries: [ + { action: "skip", reusable: true, dataSet: { main: 514, add: [] }, + msrPath: "/tmp/job/514_asy.msr" }, + { action: "skip", reusable: false, dataSet: { main: 515, add: [] }, + msrPath: "/tmp/job/515_asy.msr" } + ] +}, { + workPath: "/tmp/job", + fileNameSuffix: "_asy" +}); +assert.strictEqual(insufficientSpec.ok, false); +assert.strictEqual(insufficientSpec.reason, "insufficient-series-artifact-runs"); + const dataOnly = sandbox.normalizeIndependentSeriesCollectionResult({ ok: false, reason: "series-display-merge-failed", diff --git a/tests/series-fit-inventory.test.js b/tests/series-fit-inventory.test.js index bd85493..29b1cc0 100644 --- a/tests/series-fit-inventory.test.js +++ b/tests/series-fit-inventory.test.js @@ -181,6 +181,25 @@ async function testRefitOfMissingMSRBecomesFit(lib) { "the user's rebuild-from-GUI intent must be preserved for the missing MSR"); } +async function testRefitWithoutDataRemainsBlocked(lib) { + const files = { + [lemPath(2274)]: "data", + [lemPath(2275)]: "data", + "/work/2274_asy.msr": fittedMSR(2274), + "/work/2275_asy.msr": fittedMSR(2275), + "/work/2276_asy.msr": fittedMSR(2276) + }; + const config = seriesConfig("2274-2276"); + config.actionOverrides = { 2276: "refit" }; + + const plan = await lib.inspectSeriesFitPlan(config, memoryAccess(files)); + const entry = plan.entries.find(function(e) { return e.run === 2276; }); + assert.strictEqual(entry.action, "blocked", + "a requested refit must not bypass the raw-data availability check"); + assert.strictEqual(entry.reason, "required-data-not-found"); + assert.strictEqual(entry.useGuiTemplate, true); +} + async function testAddRunAgeBoundary(lib) { const files = { [lemPath(2274)]: "data", @@ -233,6 +252,7 @@ async function testWebRequiresServerDataAdapter(lib) { await testInventoryActions(lib); await testSkippedMissingMSRFailsPlan(lib); await testRefitOfMissingMSRBecomesFit(lib); + await testRefitWithoutDataRemainsBlocked(lib); await testAddRunAgeBoundary(lib); await testNonSeriesDoesNotInspectFiles(lib); await testWebRequiresServerDataAdapter(lib); diff --git a/tests/series-fit-routing.test.js b/tests/series-fit-routing.test.js index cf9b586..f4258b2 100644 --- a/tests/series-fit-routing.test.js +++ b/tests/series-fit-routing.test.js @@ -253,7 +253,8 @@ async function testMissingReferenceBootstrapsFirstRun() { assert.deepStrictEqual(finished, { ok: 1, code: 0, - message: "Independent series fit completed: 3 fitted, 0 reused." + message: "Independent series fit completed: 3 fitted, 0 reused, 0 skipped," + + " 0 missing, 0 failed." + " Parameter table prepared. Combined plot opened." }); } @@ -267,7 +268,10 @@ async function testAllRunsSkippedReportsNothingToFit() { lib.All.runNumbers = "2531,2534,2536"; lib.collectSharedParameterControls = function() { return {}; }; lib.checkDir = function() { return 1; }; - lib.updateJobStatus = function() {}; + let status = null; + lib.updateJobStatus = function(update) { + status = update; + }; lib.appendJobLog = function() {}; lib.validateRRFSettings = function() { return 1; }; lib.validateT0BgOverrides = function() { return { ok: 1, message: "" }; }; @@ -303,6 +307,85 @@ async function testAllRunsSkippedReportsNothingToFit() { "bootstrap must not be attempted when all runs are skipped"); assert.strictEqual(finished, null, "the all-skip case should be reported via updateJobStatus, not finishJobStatus failure"); + assert.strictEqual(status.state, "Complete"); + assert.strictEqual(status.message, + "No run is scheduled: 0 fitted, 0 reused, 3 skipped, 0 missing, 0 failed." + + " Set at least one run to reuse or refit."); +} + +async function testAllReusableRunsRefreshSeriesArtifacts(webMode) { + const lib = loadLibrary(); + lib.All.runNumbers = "2531,2534,2536"; + lib.All.fitAsyType = "AsymmetryGLB"; + lib.All.asymmetrySuffix = "_asy"; + lib.All.workPath = "/work"; + lib.collectSharedParameterControls = function() { return {}; }; + lib.checkDir = function() { return 1; }; + lib.updateJobStatus = function() {}; + lib.appendJobLog = function() {}; + + const plan = { + ok: true, + mode: "series", + entries: [2531, 2534, 2536].map(function(run) { + return { + run: run, + action: "skip", + reusable: true, + msrExists: true, + msrText: "converged " + run, + dataSet: { main: run, add: [] }, + msrPath: "/work/" + run + "_asy.msr" + }; + }), + summary: { total: 3, fit: 0, retry: 0, refit: 0, skip: 3, blocked: 0 } + }; + if (webMode) { + lib.loadWebDataRoot = async function() { return true; }; + lib.inspectSeriesFitPlanWeb = async function() { return plan; }; + } else { + lib.inspectSeriesFitPlan = async function() { return plan; }; + } + + let artifactSpec = null; + const collectorName = webMode + ? "collectIndependentSeriesArtifactsWeb" + : "collectIndependentSeriesArtifactsLocal"; + lib.window[collectorName] = async function(spec) { + artifactSpec = spec; + return { + ok: true, + parameterDataPath: "/work/2531+series_asy_par.dat", + combinedMSRPath: "/work/2531+series_asy.msr", + plotStarted: true, + warnings: [] + }; + }; + lib.readAsciiFile = async function(filePath) { + assert.strictEqual(filePath, "/work/2531+series_asy.msr"); + return "combined fitted series"; + }; + let syncedText = ""; + lib.syncSeriesParametersToGUI = function(text) { + syncedText = text; + }; + let finished = null; + lib.finishJobStatus = function(ok, code, message) { + finished = { ok: ok, code: code, message: message }; + }; + + const result = await lib.runIndependentSeriesFit( + { mode: "series", dataSetCount: 3 }, webMode + ); + assert.strictEqual(result, 1); + assert.deepStrictEqual(Array.from(artifactSpec.runs), ["2531", "2534", "2536"]); + assert.strictEqual(syncedText, "combined fitted series"); + assert.deepStrictEqual(finished, { + ok: 1, + code: 0, + message: "No fits were run: 0 fitted, 3 reused, 0 skipped, 0 missing, 0 failed." + + " Parameter table prepared. Combined plot opened." + }); } (async function main() { @@ -311,6 +394,8 @@ async function testAllRunsSkippedReportsNothingToFit() { await testEstablishedRoutesRemainUnchanged(); await testMissingReferenceBootstrapsFirstRun(); await testAllRunsSkippedReportsNothingToFit(); + await testAllReusableRunsRefreshSeriesArtifacts(false); + await testAllReusableRunsRefreshSeriesArtifacts(true); console.log("series fit routing tests passed"); })().catch(function(error) { console.error(error); diff --git a/tests/series-runner-adapters.test.js b/tests/series-runner-adapters.test.js index 7e6ffac..b334b7b 100644 --- a/tests/series-runner-adapters.test.js +++ b/tests/series-runner-adapters.test.js @@ -214,12 +214,6 @@ async function testElectronSeriesArtifactCollection() { const fakeChildProcess = { spawnSync: function(command, args, options) { syncCalls.push({ command: command, args: args.slice(), cwd: options.cwd }); - if (command === "tar") { - // Emulate the archive creation; args are - // -czf -C . - fs.writeFileSync(args[1], "PK\u0003\u0004archive\n", "utf8"); - return { status: 0, stdout: "", stderr: "" }; - } fs.writeFileSync(path.join(options.cwd, "514+series_asy_par.dat"), [ "dataT dataTErr Alpha_0514 Alpha_0514PosErr Alpha_0514NegErr " + "my_parameter_0514 my_parameter_0514PosErr CHISQ NDF CHISQred RUN", @@ -259,15 +253,12 @@ async function testElectronSeriesArtifactCollection() { assert.strictEqual(result.plotStarted, true); assert.strictEqual(result.templatePath, path.join(tempDirectory, "514_asy.msr")); - assert.strictEqual(result.packagePath, - path.join(tempDirectory, "514+series_asy.tgz")); - assert.strictEqual(fs.existsSync(result.packagePath), true); + assert.strictEqual(result.packagePath, "", + "Electron keeps artifacts in the working directory without packaging them"); assert.match(result.warnings.join("\n"), /returned code 7/); - assert.strictEqual(syncCalls.length, 2); + assert.strictEqual(syncCalls.length, 1); assert.deepStrictEqual(Array.from(syncCalls[0].args), ["[514 515]", "_asy", "new", "data", "-o", "514+series_asy_par.dat"]); - assert.strictEqual(syncCalls[1].command, "tar"); - assert.match(Array.from(syncCalls[1].args).join(" "), /514\+series_asy\.tgz/); assert.deepStrictEqual(Array.from(asyncCalls[0].args), ["514+series_asy.msr"]); const parameterHeader = fs.readFileSync(result.parameterDataPath, "utf8") .split(/\r?\n/)[0]; @@ -306,10 +297,6 @@ async function testElectronParameterCollectionSurvivesDisplayMergeFailure() { const sandbox = loadElectronSandbox({ spawnSync: function(command, args, options) { syncCalls.push(args.slice()); - if (command === "tar") { - fs.writeFileSync(args[1], "PK\u0003\u0004archive\n", "utf8"); - return { status: 0, stdout: "", stderr: "" }; - } fs.writeFileSync(path.join(options.cwd, "514+series_asy_par.dat"), "RUN Asy_\n514 0.2\n515 0.21\n", "utf8"); return { status: 0, stdout: "", stderr: "" }; @@ -339,10 +326,9 @@ async function testElectronParameterCollectionSurvivesDisplayMergeFailure() { path.join(tempDirectory, "514+series_asy_par.dat")); assert.strictEqual(result.combinedMSRPath, ""); assert.strictEqual(result.plotStarted, false); - assert.strictEqual(syncCalls.length, 2); + assert.strictEqual(syncCalls.length, 1); assert.strictEqual(asyncCalls.length, 0); - assert.strictEqual(result.packagePath, - path.join(tempDirectory, "514+series_asy.tgz")); + assert.strictEqual(result.packagePath, ""); assert.match(result.warnings.join("\n"), /series-merge-theory-mismatch/); } finally { fs.rmSync(tempDirectory, { recursive: true, force: true });