Skip to main content

pounce_solve_report/
lib.rs

1//! Machine-readable JSON solve report (pounce#8).
2//!
3//! Bundles the same payload AMPL's `.sol` carries (status, primal,
4//! dual, suffixes) with FAIR-aligned provenance metadata (solver
5//! identity, input descriptor, timestamp) and per-iteration history
6//! when requested. Schema is versioned via the top-level `schema`
7//! field so future extensions don't silently change semantics.
8//!
9//! FAIR reference: Wilkinson et al. (2016). *The FAIR Guiding
10//! Principles for scientific data management and stewardship.*
11//! Scientific Data, 3, 160018. DOI:
12//! [10.1038/sdata.2016.18](https://doi.org/10.1038/sdata.2016.18).
13//! Verified via Crossref on 2026-05-14.
14//!
15//! # Schema versioning
16//!
17//! The current schema tag is `pounce.solve-report/v1`. Breaking
18//! changes bump the major version (v2 etc.). Adding fields without
19//! removing or renaming existing ones is non-breaking — JSON
20//! consumers should tolerate unknown fields.
21//!
22//! # Detail levels
23//!
24//! [`ReportDetail::Summary`] (default) emits the FAIR metadata,
25//! problem dimensions, final solution, and aggregate statistics
26//! — equivalent to a `.sol` plus provenance. [`ReportDetail::Full`]
27//! additionally emits the per-iteration history (when captured by
28//! [`pounce_algorithm::application::IpoptApplication::enable_iter_history`])
29//! and any `solution.suffixes`. Choose `Summary` for production logs
30//! and `Full` for debug captures.
31
32use std::path::{Path, PathBuf};
33use std::time::{SystemTime, UNIX_EPOCH};
34
35use pounce_common::types::{Index, Number};
36use pounce_linsol::summary::LinearSolverSummary;
37use pounce_nlp::return_codes::ApplicationReturnStatus;
38use pounce_nlp::solve_statistics::{IterRecord, SolveStatistics};
39use serde::{Deserialize, Serialize};
40
41/// Ipopt-style console printers (banner, problem statistics, end-of-run
42/// summary). The single source of truth for the text log: the algorithm's
43/// output layer emits these gated on `print_level`, and the CLI reuses the
44/// banner. Moved out of `pounce-cli` so `pounce-algorithm` can emit them
45/// natively (#206).
46pub mod console;
47
48/// Verbosity knob for the JSON report. Maps onto the `--json-detail`
49/// CLI flag.
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub enum ReportDetail {
52    /// FAIR metadata, problem, solution scalars + arrays, aggregate
53    /// stats. Per-iteration history and suffix blocks omitted.
54    Summary,
55    /// Everything in `Summary` plus per-iteration history and any
56    /// suffix outputs (`sens_sol_state_1`, reduced-Hessian blocks).
57    Full,
58}
59
60impl ReportDetail {
61    pub fn parse(s: &str) -> Result<Self, String> {
62        match s.to_ascii_lowercase().as_str() {
63            "summary" => Ok(ReportDetail::Summary),
64            "full" => Ok(ReportDetail::Full),
65            other => Err(format!(
66                "unknown --json-detail '{other}' (expected: summary | full)"
67            )),
68        }
69    }
70}
71
72/// Top-level report struct. Fields are ordered so the JSON has the
73/// most identifying / metadata fields first when pretty-printed.
74#[derive(Debug, Clone, Serialize, Deserialize)]
75pub struct SolveReport {
76    /// Schema identifier. Always
77    /// `"pounce.solve-report/v1"` for this version of the writer.
78    pub schema: String,
79    /// FAIR provenance metadata.
80    pub fair_metadata: FairMetadata,
81    /// Problem dimensions and shape.
82    pub problem: ProblemInfo,
83    /// Final solution payload (status, primal, dual, suffixes).
84    pub solution: SolutionInfo,
85    /// Aggregate statistics (eval counts, KKT residuals, timing).
86    pub statistics: StatisticsInfo,
87    /// Per-iteration history. Empty when the report is at
88    /// [`ReportDetail::Summary`] or iter history was never enabled.
89    #[serde(skip_serializing_if = "Vec::is_empty", default)]
90    pub iterations: Vec<IterRecord>,
91    /// Aggregate linear-solver post-mortem. Populated when the
92    /// workspace-default FERAL backend ran (it self-instruments via
93    /// `feral::Solver::last_factor_stats()`); `None` for HSL MA57 and
94    /// for custom backends plugged through
95    /// [`pounce_algorithm::application::IpoptApplication::set_linear_backend_factory`].
96    /// Additive — older `pounce.solve-report/v1` JSON without this
97    /// field deserializes unchanged.
98    #[serde(skip_serializing_if = "Option::is_none", default)]
99    pub linear_solver: Option<LinearSolverSummaryInfo>,
100    /// What the second-opinion ladder did, when it ran at all. `None` when the
101    /// verdict opened no ladder — which is every ordinary solve.
102    ///
103    /// Additive; older `pounce.solve-report/v1` JSON without this field
104    /// deserializes unchanged.
105    #[serde(skip_serializing_if = "Option::is_none", default)]
106    pub second_opinion: Option<SecondOpinionInfo>,
107}
108
109/// The second-opinion ladder's record: which rungs ran, which (if any) was
110/// promoted, and **what the base solve did before any of it** (gh #850).
111///
112/// # Why the base solve's numbers are the point
113///
114/// On a promotion the reported `status` and `statistics.iteration_count` both
115/// become the promoted rung's, and nothing else in the report says the base
116/// solver failed. That makes a *lost* solve indistinguishable from a faster
117/// one, and it is worse than a gap in the evidence: it produces positive
118/// evidence for the wrong conclusion.
119///
120/// The case that exposed it is `square_flowsheet_resto`, where `v0.10.0`'s
121/// base solver converged in 116 iterations and HEAD's does not converge at all
122/// — `RestorationFailed` at 131 — with the answer coming from a ladder rung
123/// (`start_point_perturbation=1e-2`) added in the same release, which promotes
124/// at 54. `scripts/sweep-fixtures.sh` read that as `116 -> 54`, **a 2× win**.
125///
126/// The cost is understated on top of that: `statistics.iteration_count` is the
127/// promoted rung's alone, so the fixture's true cost is `131 + 54`, 3.4× what
128/// the report says. [`Self::total_iteration_count`] is the honest number.
129///
130/// This is the same shape of invisibility the sweep's engine column was added
131/// to close, and CLAUDE.md's rule applies verbatim: a line whose only moving
132/// field is "solved directly" → "promoted from a rung" is a trajectory change,
133/// and is as reportable as a moved iteration count.
134#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct SecondOpinionInfo {
136    /// Rung labels actually run, in order.
137    pub tried: Vec<String>,
138    /// The rung whose re-solve was promoted, or `None` when the original
139    /// verdict survived every rung and shipped unchanged.
140    #[serde(skip_serializing_if = "Option::is_none", default)]
141    pub promoted_by: Option<String>,
142    /// The verdict the ladder was opened on, before any rung ran.
143    pub base_status: String,
144    /// Iterations the base solve spent. Not included in
145    /// `statistics.iteration_count` when a rung was promoted.
146    pub base_iteration_count: usize,
147    /// Iterations each rung in `tried` spent, in the same order.
148    pub rung_iteration_counts: Vec<usize>,
149    /// Base plus every rung: what the solve actually cost.
150    pub total_iteration_count: usize,
151}
152
153/// Serializable mirror of [`pounce_linsol::summary::LinearSolverSummary`].
154/// Lives in the CLI crate (rather than `pounce-linsol`) so the linsol
155/// trait crate stays serde-free. Field shape is identical; serde
156/// defaults keep it forward-compatible with future additions.
157#[derive(Debug, Clone, Serialize, Deserialize)]
158pub struct LinearSolverSummaryInfo {
159    pub solver_name: String,
160    /// The `linear_solver` option as requested (gh#990 item 7); differs from
161    /// `solver_name` when the backend that ran is not the one asked for.
162    #[serde(skip_serializing_if = "Option::is_none", default)]
163    pub requested: Option<String>,
164    pub n_factors: u64,
165    pub n_pattern_reuse: u64,
166    pub n_pattern_changes: u64,
167    #[serde(skip_serializing_if = "Option::is_none", default)]
168    pub max_fill_ratio: Option<f64>,
169    #[serde(skip_serializing_if = "Option::is_none", default)]
170    pub min_abs_pivot: Option<f64>,
171    #[serde(skip_serializing_if = "Option::is_none", default)]
172    pub max_abs_pivot: Option<f64>,
173    /// `(positive, negative, zero)` inertia of the final factorisation.
174    #[serde(skip_serializing_if = "Option::is_none", default)]
175    pub last_inertia: Option<(usize, usize, usize)>,
176    /// `(positive, negative, zero)` inertia of the last factorization tried
177    /// with **no** regularization, i.e. the KKT matrix's own curvature
178    /// verdict (gh#987). `last_inertia` is the post-shift value.
179    #[serde(skip_serializing_if = "Option::is_none", default)]
180    pub last_inertia_unregularized: Option<(usize, usize, usize)>,
181    #[serde(skip_serializing_if = "Option::is_none", default)]
182    pub last_nnz_a: Option<usize>,
183    #[serde(skip_serializing_if = "Option::is_none", default)]
184    pub last_nnz_l: Option<usize>,
185
186    // Work and pivoting totals over every factorization. `default` keeps
187    // reports written before these fields existed readable.
188    #[serde(default)]
189    pub total_factor_secs: f64,
190    #[serde(default)]
191    pub total_factor_flops: f64,
192    #[serde(default)]
193    pub total_delayed_cols: u64,
194    #[serde(default)]
195    pub total_two_by_two: u64,
196    #[serde(default)]
197    pub total_n_tiny: u64,
198    #[serde(skip_serializing_if = "Option::is_none", default)]
199    pub last_factor_flops: Option<f64>,
200    #[serde(skip_serializing_if = "Option::is_none", default)]
201    pub last_peak_bytes: Option<usize>,
202    #[serde(skip_serializing_if = "Option::is_none", default)]
203    pub last_n_supernodes: Option<usize>,
204    #[serde(skip_serializing_if = "Option::is_none", default)]
205    pub last_max_front_rows: Option<usize>,
206    #[serde(skip_serializing_if = "Option::is_none", default)]
207    pub last_delayed_cols: Option<usize>,
208    #[serde(skip_serializing_if = "Option::is_none", default)]
209    pub last_two_by_two: Option<usize>,
210    #[serde(skip_serializing_if = "Option::is_none", default)]
211    pub last_n_tiny: Option<usize>,
212    #[serde(skip_serializing_if = "Option::is_none", default)]
213    pub last_ordering: Option<String>,
214    #[serde(skip_serializing_if = "Option::is_none", default)]
215    pub last_ordering_preprocess: Option<String>,
216    /// Present only when the Schur KKT path actually factored.
217    #[serde(skip_serializing_if = "Option::is_none", default)]
218    pub schur: Option<SchurSummaryInfo>,
219    /// Present only when the block-parallel KKT path actually factored.
220    #[serde(skip_serializing_if = "Option::is_none", default)]
221    pub blocks: Option<BlockSummaryInfo>,
222    /// The restoration phase's factorizations, same shape, kept apart from
223    /// the main-solve fields above. Present only when restoration factored.
224    #[serde(skip_serializing_if = "Option::is_none", default)]
225    pub restoration: Option<Box<LinearSolverSummaryInfo>>,
226}
227
228/// Serializable mirror of [`pounce_linsol::summary::BlockSummary`].
229#[derive(Debug, Clone, Serialize, Deserialize)]
230pub struct BlockSummaryInfo {
231    pub n_blocks: usize,
232    pub border_dim: usize,
233    pub largest_block: usize,
234    pub n_factors: u64,
235    pub factor_secs: f64,
236}
237
238/// Serializable mirror of [`pounce_linsol::summary::SchurSummary`].
239#[derive(Debug, Clone, Serialize, Deserialize)]
240pub struct SchurSummaryInfo {
241    pub n_eliminated: usize,
242    pub n_schur: usize,
243    pub n_factors: u64,
244    pub eliminated_factor_secs: f64,
245    pub form_schur_secs: f64,
246    pub schur_factor_secs: f64,
247}
248
249impl From<LinearSolverSummary> for LinearSolverSummaryInfo {
250    fn from(s: LinearSolverSummary) -> Self {
251        Self {
252            solver_name: s.solver_name,
253            requested: s.requested,
254            n_factors: s.n_factors,
255            n_pattern_reuse: s.n_pattern_reuse,
256            n_pattern_changes: s.n_pattern_changes,
257            max_fill_ratio: s.max_fill_ratio,
258            min_abs_pivot: s.min_abs_pivot,
259            max_abs_pivot: s.max_abs_pivot,
260            last_inertia: s.last_inertia,
261            last_inertia_unregularized: s.last_inertia_unregularized,
262            last_nnz_a: s.last_nnz_a,
263            last_nnz_l: s.last_nnz_l,
264            total_factor_secs: s.total_factor_secs,
265            total_factor_flops: s.total_factor_flops,
266            total_delayed_cols: s.total_delayed_cols,
267            total_two_by_two: s.total_two_by_two,
268            total_n_tiny: s.total_n_tiny,
269            last_factor_flops: s.last_factor_flops,
270            last_peak_bytes: s.last_peak_bytes,
271            last_n_supernodes: s.last_n_supernodes,
272            last_max_front_rows: s.last_max_front_rows,
273            last_delayed_cols: s.last_delayed_cols,
274            last_two_by_two: s.last_two_by_two,
275            last_n_tiny: s.last_n_tiny,
276            last_ordering: s.last_ordering,
277            last_ordering_preprocess: s.last_ordering_preprocess,
278            schur: s.schur.map(|c| SchurSummaryInfo {
279                n_eliminated: c.n_eliminated,
280                n_schur: c.n_schur,
281                n_factors: c.n_factors,
282                eliminated_factor_secs: c.eliminated_factor_secs,
283                form_schur_secs: c.form_schur_secs,
284                schur_factor_secs: c.schur_factor_secs,
285            }),
286            blocks: s.blocks.map(|b| BlockSummaryInfo {
287                n_blocks: b.n_blocks,
288                border_dim: b.border_dim,
289                largest_block: b.largest_block,
290                n_factors: b.n_factors,
291                factor_secs: b.factor_secs,
292            }),
293            restoration: s.restoration.map(|r| Box::new(Self::from(*r))),
294        }
295    }
296}
297
298/// FAIR-aligned provenance block. The four FAIR principles
299/// (Wilkinson et al., 2016) map onto fields here as:
300/// * **F**indable: `result_id` (unique per solve), `created_at_iso`.
301/// * **A**ccessible: this JSON file is the artifact — no protocol
302///   gating, plain text on disk.
303/// * **I**nteroperable: schema versioned, types are JSON primitives,
304///   units documented in field doc comments.
305/// * **R**eusable: `solver`, `license`, `input` describe what was
306///   solved with what code, enough to reproduce.
307#[derive(Debug, Clone, Serialize, Deserialize)]
308pub struct FairMetadata {
309    /// Unique per-solve identifier. Composed as
310    /// `<unix_nanos>-<process_id>` so it is monotonically ordered
311    /// within a process and globally unique across processes.
312    pub result_id: String,
313    /// Solve start time as ISO-8601 UTC (`YYYY-MM-DDTHH:MM:SS.sssZ`).
314    pub created_at_iso: String,
315    /// Same instant in Unix nanoseconds (since 1970-01-01 UTC).
316    /// Provided alongside the ISO string for callers that prefer
317    /// integer arithmetic over date parsing.
318    pub created_at_unix_nanos: i128,
319    /// Wallclock seconds the solve took. Mirrors
320    /// [`SolveStatistics::total_wallclock_time_secs`].
321    pub elapsed_seconds: Number,
322    /// Solver identity — name + version + (best-effort) git commit.
323    pub solver: SolverIdentity,
324    /// SPDX license string. Always `"EPL-2.0"` for this crate.
325    pub license: String,
326    /// Input descriptor. `kind` is `nl-file`, `builtin`, or
327    /// `tnlp-direct` (for library callers).
328    pub input: InputDescriptor,
329    /// Solve-affecting environment variables present in the process
330    /// environment at report time (`POUNCE_FERAL_*` numerics knobs and
331    /// the legacy `FERAL_PIVTOL` / `FERAL_PARALLEL`). These alter the
332    /// factorization or parallelism and can otherwise silently differ a
333    /// run between two machines — e.g. one with `POUNCE_FERAL_PIVTOL`
334    /// exported in a shell profile — with nothing in the report saying
335    /// so (pounce#235). Recorded for reproducibility (the FAIR **R**
336    /// principle). Presence here means the variable was set, not
337    /// necessarily that it took effect: an explicit `OptionsList` setting
338    /// (e.g. a `feral_pivtol` in an options file) takes precedence over
339    /// the env fallback. Empty (and omitted from the JSON) when none are
340    /// set. Additive — older `pounce.solve-report/v1` JSON without this
341    /// field deserializes unchanged.
342    #[serde(skip_serializing_if = "Vec::is_empty", default)]
343    pub environment: Vec<EnvOverride>,
344}
345
346/// One solve-affecting environment variable and its value, as captured
347/// into [`FairMetadata::environment`].
348#[derive(Debug, Clone, Serialize, Deserialize)]
349pub struct EnvOverride {
350    /// Variable name, e.g. `POUNCE_FERAL_PIVTOL`.
351    pub name: String,
352    /// Its value verbatim, as read from the environment.
353    pub value: String,
354}
355
356/// Environment variables that change pounce's numerics or parallelism —
357/// the ones worth recording for reproducibility. Deliberately excludes the
358/// `POUNCE_DBG_*` debug gates (they only add diagnostic output, never
359/// altering the result) and compile-time / logging vars. Kept in a fixed
360/// order so [`capture_solve_env_overrides`] is deterministic.
361const SOLVE_AFFECTING_ENV_VARS: &[&str] = &[
362    "POUNCE_FERAL_ORDERING",
363    "POUNCE_FERAL_SCALING",
364    "POUNCE_FERAL_PIVTOL",
365    "POUNCE_FERAL_REFINE",
366    "POUNCE_FERAL_CASCADE_BREAK",
367    "POUNCE_FERAL_FMA",
368    "POUNCE_FERAL_SINGULAR_PIVOT_FLOOR",
369    "POUNCE_FERAL_MIN_PAR_FLOPS",
370    // Legacy aliases without the `POUNCE_` prefix (still honored by the
371    // FERAL backend): the deprecated pivot-threshold spelling and the
372    // process-wide internal-parallelism switch.
373    "FERAL_PIVTOL",
374    "FERAL_PARALLEL",
375];
376
377/// Snapshot the solve-affecting environment variables ([`SOLVE_AFFECTING_ENV_VARS`])
378/// that are currently set, for [`FairMetadata::environment`]. Reads the
379/// process environment, so call it in the solving process. Returns them in
380/// the fixed list order; absent variables are simply skipped.
381pub fn capture_solve_env_overrides() -> Vec<EnvOverride> {
382    SOLVE_AFFECTING_ENV_VARS
383        .iter()
384        .filter_map(|&name| {
385            std::env::var(name).ok().map(|value| EnvOverride {
386                name: name.to_string(),
387                value,
388            })
389        })
390        .collect()
391}
392
393#[derive(Debug, Clone, Serialize, Deserialize)]
394pub struct SolverIdentity {
395    pub name: String,
396    pub version: String,
397    /// Git commit hash, captured at build time from the
398    /// `POUNCE_GIT_COMMIT` environment variable. `None` if the build
399    /// environment didn't set it — set via
400    /// `POUNCE_GIT_COMMIT=$(git rev-parse HEAD) cargo build`.
401    #[serde(skip_serializing_if = "Option::is_none")]
402    pub git_commit: Option<String>,
403    /// Build target triple (e.g. `x86_64-apple-darwin`). Captured at
404    /// build time from `TARGET` (Cargo standard env var).
405    pub target_triple: String,
406}
407
408#[derive(Debug, Clone, Serialize, Deserialize)]
409#[serde(tag = "kind", rename_all = "kebab-case")]
410pub enum InputDescriptor {
411    NlFile {
412        path: PathBuf,
413        #[serde(skip_serializing_if = "Option::is_none")]
414        size_bytes: Option<u64>,
415    },
416    /// A Conic Benchmark Format (`.cbf`) instance — e.g. a CBLIB problem
417    /// solved through the convex conic driver.
418    CbfFile {
419        path: PathBuf,
420        #[serde(skip_serializing_if = "Option::is_none")]
421        size_bytes: Option<u64>,
422    },
423    Builtin {
424        name: String,
425    },
426    TnlpDirect,
427}
428
429#[derive(Debug, Clone, Serialize, Deserialize)]
430pub struct ProblemInfo {
431    pub n_variables: Index,
432    pub n_constraints: Index,
433    pub n_objectives: Index,
434    pub minimize: bool,
435    /// Number of non-zeros declared by the TNLP for the constraint
436    /// Jacobian. `None` if not exposed by the input path.
437    #[serde(skip_serializing_if = "Option::is_none")]
438    pub nnz_jac_g: Option<Index>,
439    /// Number of non-zeros declared for the Lagrangian Hessian.
440    #[serde(skip_serializing_if = "Option::is_none")]
441    pub nnz_h_lag: Option<Index>,
442}
443
444#[derive(Debug, Clone, Serialize, Deserialize)]
445pub struct SolutionInfo {
446    /// Which engine produced this verdict: `"cvx-qp"`, `"cvx-qcqp"`,
447    /// `"qp-active-set"`, `"sqp-active-set"`, `"square-lm"` or `"nlp"`.
448    ///
449    /// `"square-lm"` is the second-opinion ladder's square-system stage
450    /// (`pounce_algorithm::square_system`): the NLP arm ran and failed, and
451    /// the reported point came from Levenberg–Marquardt on `c(x) = 0`.
452    ///
453    /// `"qp-active-set"` and `"sqp-active-set"` are different engines and
454    /// not synonyms: the first is `pounce_convex::active_set` reached by
455    /// `solver_selection=qp-active-set` on an LP or convex QP, the second is
456    /// `pounce_algorithm::sqp`'s outer loop reached by
457    /// `algorithm=active-set-sqp` on a general NLP. Both solve their
458    /// subproblems with `pounce-qp`, which is why the names are close; they
459    /// wrap it in different algorithms, which is why they are not the same
460    /// string.
461    ///
462    /// The `Selected solver:` banner names the engine *routing* chose, which
463    /// is not always the one that answered: a convex solve that declines its
464    /// own result hands the model to the NLP arm (gh #535), and the banner
465    /// has already been printed by then. `scripts/sweep-fixtures.sh` scraped
466    /// that banner because the report carried nothing better, so a reroute
467    /// left no trace in the sweep diff — the precise blind spot CLAUDE.md
468    /// names when it says a routing regression "used to leave no trace".
469    ///
470    /// Empty when a path does not set it, which a consumer should read as
471    /// "unknown" rather than as any particular arm.
472    #[serde(default, skip_serializing_if = "String::is_empty")]
473    pub engine: String,
474    /// `SolveSucceeded`, `MaximumIterationsExceeded`, etc. The string
475    /// form is the Rust enum variant name verbatim.
476    pub status: ApplicationReturnStatus,
477    /// The same verdict in upstream Ipopt's C enumerator spelling —
478    /// `Solve_Succeeded`, `Infeasible_Problem_Detected` — from
479    /// `IpReturnCodes_inc.h`.
480    ///
481    /// [`Self::status`] carries the Rust variant name, which is *not* the
482    /// name any Ipopt-facing consumer already keys off: CUTEst status
483    /// tables, `benchmarks/scripts/run_nl_bench.sh`, the reference JSONs
484    /// under `benchmarks/*/ipopt_ma57.json` and the CLI's own `Status:`
485    /// line all spell it with separators. A consumer comparing
486    /// `solution.status == "Solve_Succeeded"` against the report matched
487    /// nothing and silently classified every solve as a failure (gh #767).
488    /// This field is that spelling, so the comparison can be literal.
489    ///
490    /// Derived from [`Self::status`] by [`ReportBuilder::finish`] — never
491    /// set by a caller, so the two cannot disagree. Empty when read back
492    /// from a pre-#767 report.
493    #[serde(default)]
494    pub status_upstream: String,
495    /// AMPL-style solve-result code (Gay 2005, §5 p. 23 table).
496    pub solve_result_num: i32,
497    /// Final unscaled objective value (mirrors
498    /// `SolveStatistics::final_objective`). `NaN` if unknown.
499    pub objective: Number,
500    /// Final primal vector, length `problem.n_variables`. Empty if
501    /// not captured.
502    #[serde(skip_serializing_if = "Vec::is_empty", default)]
503    pub x: Vec<Number>,
504    /// Final dual (constraint multiplier) vector, length
505    /// `problem.n_constraints`.
506    #[serde(skip_serializing_if = "Vec::is_empty", default)]
507    pub lambda: Vec<Number>,
508    /// Optional sIPOPT-style suffix blocks (`sens_sol_state_1` etc.).
509    /// Stored as a flat map keyed by suffix name → list of
510    /// `(index, value)` pairs, matching the AMPL `.sol` shape.
511    /// Empty when no sensitivity / reduced-Hessian step ran.
512    #[serde(skip_serializing_if = "Vec::is_empty", default)]
513    pub suffixes: Vec<SolutionSuffix>,
514}
515
516#[derive(Debug, Clone, Serialize, Deserialize)]
517pub struct SolutionSuffix {
518    pub name: String,
519    /// `"var" | "con" | "obj" | "problem"` per AMPL convention.
520    pub target: String,
521    /// `"int"` or `"real"`.
522    pub kind: String,
523    /// Dense values (length = target dimension); zero-filled for
524    /// slots the writer didn't populate. Real-typed values are stored
525    /// here; int-typed in `int_values`.
526    #[serde(skip_serializing_if = "Vec::is_empty", default)]
527    pub values: Vec<Number>,
528    #[serde(skip_serializing_if = "Vec::is_empty", default)]
529    pub int_values: Vec<Index>,
530}
531
532/// NaN, for a residual slot that was never filled in.
533fn uncomputed() -> Number {
534    Number::NAN
535}
536
537/// Accept `null` for a residual the solve never computed.
538///
539/// `SolveStatistics` defaults its residual fields to NaN rather than `0.0`, so
540/// that "the convergence check never ran" is distinguishable from "converged
541/// exactly". `serde_json` renders a non-finite float as `null`, so a report
542/// written for a solve that was refused during setup carries `null` in these
543/// slots. Without this the report round-trip fails — pounce would write
544/// reports its own `--cite` / studio / verify paths could not read back.
545fn null_as_nan<'de, D>(de: D) -> Result<Number, D::Error>
546where
547    D: serde::Deserializer<'de>,
548{
549    Ok(Option::<Number>::deserialize(de)?.unwrap_or_else(uncomputed))
550}
551
552/// One flagged derivative-checker comparison (gh#990 item 3).
553#[derive(Debug, Clone, Serialize, Deserialize)]
554pub struct DerivativeEntryInfo {
555    /// `gradient`, `jacobian`, `hessian`, `jacobian_structure` or
556    /// `hessian_structure`.
557    pub kind: String,
558    /// Hessian block (`obj` or `g[<row>]`); omitted otherwise.
559    #[serde(skip_serializing_if = "String::is_empty", default)]
560    pub block: String,
561    /// Row (`-1` for a gradient entry).
562    pub row: i64,
563    pub col: i64,
564    /// `None` for a structural miss (the entry is not in the declared pattern).
565    #[serde(skip_serializing_if = "Option::is_none", default)]
566    pub analytic: Option<f64>,
567    pub finite_difference: f64,
568    #[serde(skip_serializing_if = "Option::is_none", default)]
569    pub relative_error: Option<f64>,
570}
571
572/// The derivative checker's machine-readable verdict (gh#990 item 3); the
573/// console report on stderr is unchanged.
574#[derive(Debug, Clone, Serialize, Deserialize)]
575pub struct DerivativeCheckInfo {
576    /// `first-order`, `second-order` or `only-second-order`.
577    pub mode: String,
578    pub tolerance: f64,
579    pub perturbation: f64,
580    pub checked: usize,
581    pub suspicious: usize,
582    pub missing_structure: usize,
583    pub evaluations: usize,
584    /// Nothing flagged and something compared.
585    pub clean: bool,
586    #[serde(skip_serializing_if = "Option::is_none", default)]
587    pub max_rel_error_gradient: Option<f64>,
588    #[serde(skip_serializing_if = "Option::is_none", default)]
589    pub max_rel_error_jacobian: Option<f64>,
590    #[serde(skip_serializing_if = "Option::is_none", default)]
591    pub max_rel_error_hessian: Option<f64>,
592    /// The flagged entries (at most 200; `suspicious` is the exact count).
593    #[serde(default)]
594    pub flagged: Vec<DerivativeEntryInfo>,
595}
596
597/// The objective-scaling decision (gh#990 item 13).
598#[derive(Debug, Clone, Serialize, Deserialize)]
599pub struct ObjectiveScalingInfo {
600    /// The solver-computed objective scaling factor the run ended with
601    /// (excluding the caller's own `obj_scaling_factor`); `1` when none was
602    /// applied.
603    #[serde(skip_serializing_if = "Option::is_none", default)]
604    pub factor: Option<f64>,
605    /// gh#1001. `|obj_scaling_factor · factor · audit upscale|`: the whole
606    /// multiplier on the objective the returned run's IPM minimized, so
607    /// `statistics.final_scaled_objective` and each iteration's `mu` are in
608    /// units of `total_factor · f`. Differs from `factor` when the caller
609    /// set `obj_scaling_factor` or the gh#983 audit re-solved with the
610    /// objective scaled up.
611    #[serde(skip_serializing_if = "Option::is_none", default)]
612    pub total_factor: Option<f64>,
613    /// `max |grad f|` at the starting point, as gradient-based scaling
614    /// measured it.
615    #[serde(skip_serializing_if = "Option::is_none", default)]
616    pub start_gradient_max: Option<f64>,
617    /// A strict termination certificate was refused because the objective
618    /// scaling masked it (gh#200), and the solve kept iterating.
619    #[serde(default)]
620    pub certificate_refused: bool,
621    /// Same, for an acceptable-level certificate.
622    #[serde(default)]
623    pub acceptable_certificate_refused: bool,
624}
625
626impl From<&pounce_nlp::derivative_test::DerivativeCheckSummary> for DerivativeCheckInfo {
627    fn from(c: &pounce_nlp::derivative_test::DerivativeCheckSummary) -> Self {
628        let fin = |v: Option<f64>| v.filter(|x| x.is_finite());
629        Self {
630            mode: c.mode.clone(),
631            tolerance: c.tolerance,
632            perturbation: c.perturbation,
633            checked: c.checked,
634            suspicious: c.suspicious,
635            missing_structure: c.missing_structure,
636            evaluations: c.evaluations,
637            clean: c.clean,
638            max_rel_error_gradient: fin(c.max_rel_error_gradient),
639            max_rel_error_jacobian: fin(c.max_rel_error_jacobian),
640            max_rel_error_hessian: fin(c.max_rel_error_hessian),
641            flagged: c
642                .flagged
643                .iter()
644                .map(|e| DerivativeEntryInfo {
645                    kind: e.kind.to_string(),
646                    block: e.block.clone(),
647                    row: e.row,
648                    col: e.col,
649                    analytic: fin(e.analytic),
650                    finite_difference: if e.finite_difference.is_finite() {
651                        e.finite_difference
652                    } else {
653                        0.0
654                    },
655                    relative_error: fin(e.relative_error),
656                })
657                .collect(),
658        }
659    }
660}
661
662/// Subset of `SolveStatistics` projected for the report. Mirrors the
663/// fields the existing console summary prints.
664#[derive(Debug, Clone, Serialize, Deserialize)]
665pub struct StatisticsInfo {
666    pub iteration_count: Index,
667    #[serde(default = "uncomputed", deserialize_with = "null_as_nan")]
668    pub final_objective: Number,
669    #[serde(default = "uncomputed", deserialize_with = "null_as_nan")]
670    pub final_scaled_objective: Number,
671    #[serde(default = "uncomputed", deserialize_with = "null_as_nan")]
672    pub final_dual_inf: Number,
673    #[serde(default = "uncomputed", deserialize_with = "null_as_nan")]
674    pub final_constr_viol: Number,
675    /// Primal violation against the model **as declared**, before the convex
676    /// arm's `bound_relax_factor` widening (`qp_extract::BoundRelax`, gh
677    /// #744/#745).
678    ///
679    /// `final_constr_viol` measures the model the solver was HANDED, whose
680    /// inequality rows and variable box are widened by `min(factor,cap)·|b|`.
681    /// That is the model its convergence test is about and every acceptance
682    /// gate reads — and it is not how far the returned point sits outside the
683    /// model the caller wrote. On netlib `afiro` the point is `4.99e-06`
684    /// outside a declared row `b = 500` (exactly `1e-8·500`) while
685    /// `final_constr_viol` reads `8.68e-13`; `25fv47` reports `2.19e-11`
686    /// against `1.97e-05`.
687    ///
688    /// Reported on every solve of the NLP and convex arms, widened or not
689    /// (gh#987: it is no longer `NaN` as a "no widening" signal); under
690    /// `l1_exact_penalty_barrier` it is the caller's own rows, not the
691    /// wrapper's augmented ones. `NaN` only on a path that does not compute
692    /// it. Additive to `pounce.solve-report/v1`: readers predating it are
693    /// unaffected.
694    #[serde(default = "uncomputed", deserialize_with = "null_as_nan")]
695    pub final_declared_constr_viol: Number,
696    /// How far the returned point sits outside the **declared** variable box —
697    /// the box the caller wrote, before the `bound_relax_factor` widening.
698    /// Ipopt's `Variable bound violation`, and the box half of
699    /// `final_declared_constr_viol` reported on its own: maxed together, a box
700    /// violation and a row violation cannot be told apart.
701    ///
702    /// Variable bounds carry no scaling, so there is one number rather than a
703    /// scaled/unscaled pair. `NaN` on a path that does not compute it.
704    /// Additive to `pounce.solve-report/v1`: readers predating it are
705    /// unaffected.
706    #[serde(default = "uncomputed", deserialize_with = "null_as_nan")]
707    pub final_declared_box_viol: Number,
708    #[serde(default = "uncomputed", deserialize_with = "null_as_nan")]
709    pub final_compl: Number,
710    #[serde(default = "uncomputed", deserialize_with = "null_as_nan")]
711    pub final_kkt_error: Number,
712    /// The aggregate the strict convergence gate tested (gh #528): as
713    /// `final_kkt_error`, but counting each constraint row's residual only
714    /// where it exceeds what that row can represent in floating point. Equal
715    /// to `final_kkt_error` unless a row is at its own resolution limit.
716    #[serde(default = "uncomputed", deserialize_with = "null_as_nan")]
717    pub final_kkt_error_above_noise: Number,
718    pub num_obj_evals: Index,
719    pub num_constr_evals: Index,
720    pub num_obj_grad_evals: Index,
721    pub num_constr_jac_evals: Index,
722    pub num_hess_evals: Index,
723    pub total_wallclock_time_secs: Number,
724    pub restoration_calls: Index,
725    pub restoration_inner_iters: Index,
726    pub restoration_outer_iters: Index,
727    pub restoration_wall_secs: Number,
728    /// Successful linear-solver quality escalations over the whole solve,
729    /// restoration sub-solves included (gh#857). `0` on paths that never
730    /// escalate. `serde(default)` so a report written before this field
731    /// existed still deserializes — it reads as "no escalations", which
732    /// is wrong-but-harmless for an old file and correct for every new
733    /// one.
734    #[serde(default)]
735    pub quality_escalations: Index,
736    /// The solve saw the gh#884 biactive dual-divergence signature: at one
737    /// and the same iterate a converged primal, a scale-relative step at
738    /// zero, and an *unscaled* dual infeasibility far above `dual_inf_tol`.
739    /// Reported whether or not a retry ran or promoted, because the
740    /// distinction it draws — the multipliers ran away on a settled
741    /// iterate, as against the iterate itself never settling — is not
742    /// visible in any other field. `serde(default)` for the same reason as
743    /// `quality_escalations`.
744    #[serde(default)]
745    pub dual_divergence_signature: bool,
746    /// A gh#884 dual-divergence retry ran *and* its answer was returned.
747    /// `false` both when no retry ran and when one ran and lost — in the
748    /// latter case every other field here describes the base attempt.
749    #[serde(default)]
750    pub dual_divergence_retry_promoted: bool,
751    /// gh#983. Structured solve-quality warnings about the returned point,
752    /// each `"<code>: <text>"` (`objective_scale_small`,
753    /// `unscaled_stationarity_above_tol`, `large_dual_scale`,
754    /// `unscaled_dual_inf_above_acceptable`). Empty on a clean run; never
755    /// changes the status. `serde(default)` so older reports still load.
756    #[serde(default)]
757    pub warnings: Vec<String>,
758    /// gh#990 item 3. The derivative checker's machine-readable verdict;
759    /// present only when `derivative_test` ran for this solve.
760    #[serde(skip_serializing_if = "Option::is_none", default)]
761    pub derivative_check: Option<DerivativeCheckInfo>,
762    /// gh#990 item 13. The objective-scaling decision: the factor
763    /// gradient-based scaling chose, the gradient scale it measured, and
764    /// whether a termination certificate was refused because the scaling
765    /// masked it. Absent when none of that is known (a path that does not
766    /// scale the objective).
767    #[serde(skip_serializing_if = "Option::is_none", default)]
768    pub objective_scaling: Option<ObjectiveScalingInfo>,
769    /// Per-pass summary of a multi-pass solve (gh#987 item 6): one entry per
770    /// ℓ₁ exact-penalty ρ pass, preceded by the unwrapped attempt when the ℓ₁
771    /// fallback ran. Omitted on an ordinary single-pass solve. When present,
772    /// `iteration_count` is the total across the passes and `iterations`
773    /// (at full detail) their concatenated rows; `first_row` in each entry
774    /// marks where a pass's rows start. Additive to `pounce.solve-report/v1`.
775    #[serde(default, skip_serializing_if = "Vec::is_empty")]
776    pub passes: Vec<pounce_nlp::SolvePassRecord>,
777}
778
779/// Builder collecting the inputs for a [`SolveReport`]. The CLI
780/// drivers populate one of these as they walk through the solve and
781/// `finish()` it at the end.
782pub struct ReportBuilder {
783    detail: ReportDetail,
784    started_at: SystemTime,
785    started_unix_nanos: i128,
786    pub input: InputDescriptor,
787    pub problem: ProblemInfo,
788    pub solution: SolutionInfo,
789    pub stats: StatisticsInfo,
790    pub iterations: Vec<IterRecord>,
791    pub linear_solver: Option<LinearSolverSummaryInfo>,
792    pub second_opinion: Option<SecondOpinionInfo>,
793}
794
795impl ReportBuilder {
796    pub fn new(detail: ReportDetail, input: InputDescriptor) -> Self {
797        let now = SystemTime::now();
798        let nanos = now
799            .duration_since(UNIX_EPOCH)
800            .map(|d| d.as_nanos() as i128)
801            .unwrap_or(0);
802        Self {
803            detail,
804            started_at: now,
805            started_unix_nanos: nanos,
806            input,
807            problem: ProblemInfo {
808                n_variables: 0,
809                n_constraints: 0,
810                n_objectives: 0,
811                minimize: true,
812                nnz_jac_g: None,
813                nnz_h_lag: None,
814            },
815            solution: SolutionInfo {
816                engine: String::new(),
817                status: ApplicationReturnStatus::InternalError,
818                // Overwritten from `status` by `finish`; see the field docs.
819                status_upstream: String::new(),
820                solve_result_num: 500,
821                // 0.0 (not NaN) so JSON round-trips. Callers that
822                // need "unknown objective" semantics check
823                // `statistics.iteration_count > 0` first.
824                objective: 0.0,
825                x: Vec::new(),
826                lambda: Vec::new(),
827                suffixes: Vec::new(),
828            },
829            stats: empty_stats(),
830            iterations: Vec::new(),
831            linear_solver: None,
832            second_opinion: None,
833        }
834    }
835
836    /// Record what the second-opinion ladder did. Called only when it ran; a
837    /// verdict that opens no ladder leaves this `None` and the field out of
838    /// the JSON entirely (gh #850).
839    pub fn set_second_opinion(&mut self, info: SecondOpinionInfo) {
840        self.second_opinion = Some(info);
841    }
842
843    /// Attach a linear-solver post-mortem. Called once per solve after
844    /// `optimize_tnlp` returns and before [`Self::finish`].
845    pub fn set_linear_solver_summary(&mut self, summary: LinearSolverSummary) {
846        self.linear_solver = Some(summary.into());
847    }
848
849    /// Pull `iteration_count`, `final_*`, and counters into the
850    /// `stats` slot; copy `iterations` only if detail = Full.
851    pub fn ingest_stats(&mut self, src: &SolveStatistics) {
852        self.stats = StatisticsInfo {
853            iteration_count: src.iteration_count,
854            final_objective: src.final_objective,
855            final_scaled_objective: src.final_scaled_objective,
856            final_dual_inf: src.final_dual_inf,
857            final_constr_viol: src.final_constr_viol,
858            final_declared_constr_viol: src.final_declared_constr_viol,
859            final_declared_box_viol: src.final_declared_box_viol,
860            final_compl: src.final_compl,
861            final_kkt_error: src.final_kkt_error,
862            final_kkt_error_above_noise: src.final_kkt_error_above_noise,
863            num_obj_evals: src.num_obj_evals,
864            num_constr_evals: src.num_constr_evals,
865            num_obj_grad_evals: src.num_obj_grad_evals,
866            num_constr_jac_evals: src.num_constr_jac_evals,
867            num_hess_evals: src.num_hess_evals,
868            total_wallclock_time_secs: src.total_wallclock_time_secs,
869            restoration_calls: src.restoration_calls,
870            restoration_inner_iters: src.restoration_inner_iters,
871            restoration_outer_iters: src.restoration_outer_iters,
872            restoration_wall_secs: src.restoration_wall_secs,
873            quality_escalations: src.quality_escalations,
874            dual_divergence_signature: src.dual_divergence_signature,
875            dual_divergence_retry_promoted: src.dual_divergence_retry_promoted,
876            warnings: src.warnings.clone(),
877            derivative_check: src.derivative_check.as_ref().map(DerivativeCheckInfo::from),
878            objective_scaling: {
879                let fin = |v: f64| v.is_finite().then_some(v);
880                let (factor, total, start) = (
881                    fin(src.final_obj_scaling_factor),
882                    fin(src.final_total_obj_scaling_factor),
883                    fin(src.start_obj_grad_max),
884                );
885                (factor.is_some()
886                    || total.is_some()
887                    || start.is_some()
888                    || src.obj_scale_certificate_refused
889                    || src.obj_scale_acceptable_refused)
890                    .then_some(ObjectiveScalingInfo {
891                        factor,
892                        total_factor: total,
893                        start_gradient_max: start,
894                        certificate_refused: src.obj_scale_certificate_refused,
895                        acceptable_certificate_refused: src.obj_scale_acceptable_refused,
896                    })
897            },
898            passes: src.passes.clone(),
899        };
900        if matches!(self.detail, ReportDetail::Full) {
901            self.iterations = src.iterations.clone();
902        }
903    }
904
905    pub fn finish(self) -> SolveReport {
906        let elapsed = self
907            .started_at
908            .elapsed()
909            .map(|d| d.as_secs_f64())
910            .unwrap_or(0.0);
911        // Derived here rather than at each call site: every producer of a
912        // report (CLI, C interface, Python bindings, the CBF driver) sets
913        // `solution.status` and none of them can forget the upstream
914        // spelling, nor set one that disagrees with the other (gh #767).
915        let mut solution = self.solution;
916        solution.status_upstream = solution.status.upstream_name().to_string();
917        let result_id = format!("{}-{}", self.started_unix_nanos, std::process::id());
918        let created_at_iso = unix_nanos_to_iso(self.started_unix_nanos);
919
920        SolveReport {
921            schema: "pounce.solve-report/v1".to_string(),
922            fair_metadata: FairMetadata {
923                result_id,
924                created_at_iso,
925                created_at_unix_nanos: self.started_unix_nanos,
926                elapsed_seconds: elapsed,
927                solver: SolverIdentity {
928                    name: "pounce".to_string(),
929                    version: env!("CARGO_PKG_VERSION").to_string(),
930                    git_commit: option_env!("POUNCE_GIT_COMMIT").map(String::from),
931                    target_triple: TARGET_TRIPLE.to_string(),
932                },
933                license: "EPL-2.0".to_string(),
934                input: self.input,
935                environment: capture_solve_env_overrides(),
936            },
937            problem: self.problem,
938            solution,
939            statistics: self.stats,
940            iterations: self.iterations,
941            linear_solver: self.linear_solver,
942            second_opinion: self.second_opinion,
943        }
944    }
945}
946
947/// The build target triple (e.g. `aarch64-apple-darwin`).
948///
949/// Cargo only exposes `TARGET` to *build scripts*, not to crate source, so
950/// `option_env!("TARGET")` here is always `None`. Our `build.rs` re-exports
951/// the build script's `TARGET` as `POUNCE_TARGET_TRIPLE`, which we read
952/// instead. Falls back to "unknown" if the build script did not run (e.g.
953/// some non-Cargo tooling).
954const TARGET_TRIPLE: &str = match option_env!("POUNCE_TARGET_TRIPLE") {
955    Some(t) => t,
956    None => "unknown",
957};
958
959fn empty_stats() -> StatisticsInfo {
960    // All scalar fields start at 0.0 (not NaN) so the report
961    // round-trips through `serde_json` — JSON has no NaN literal, and
962    // serde_json's default is to write `null` for NaN, which then
963    // fails to deserialize back into `Number`. Callers reading these
964    // pre-solve treat `iteration_count == 0` as "no solve yet".
965    StatisticsInfo {
966        iteration_count: 0,
967        final_objective: 0.0,
968        final_scaled_objective: 0.0,
969        final_dual_inf: 0.0,
970        final_constr_viol: 0.0,
971        // not "uncomputed": this is the pre-solve placeholder, and 0.0 is
972        // what every residual beside it carries here.
973        final_declared_constr_viol: 0.0,
974        final_declared_box_viol: 0.0,
975        final_compl: 0.0,
976        final_kkt_error: 0.0,
977        final_kkt_error_above_noise: 0.0,
978        num_obj_evals: 0,
979        num_constr_evals: 0,
980        num_obj_grad_evals: 0,
981        num_constr_jac_evals: 0,
982        num_hess_evals: 0,
983        total_wallclock_time_secs: 0.0,
984        restoration_calls: 0,
985        restoration_inner_iters: 0,
986        restoration_outer_iters: 0,
987        restoration_wall_secs: 0.0,
988        quality_escalations: 0,
989        dual_divergence_signature: false,
990        dual_divergence_retry_promoted: false,
991        warnings: Vec::new(),
992        derivative_check: None,
993        objective_scaling: None,
994        passes: Vec::new(),
995    }
996}
997
998/// AMPL-style `solve_result_num` per Gay 2005 (Hooking Your Solver to
999/// AMPL §5, p. 23 table): 0 = solved, 100s = warning, 200s =
1000/// infeasible, 300s = unbounded, 400s = limit reached, 500s = failure.
1001/// Shared by the CLI and cinterface report writers so both encode the
1002/// same int codes into `SolutionInfo::solve_result_num`.
1003///
1004/// `DivergingIterates` is Ipopt's unboundedness signal (the iterates run
1005/// off to infinity), so it maps to the 300 "unbounded" range — matching
1006/// upstream Ipopt's ASL driver and the CLI's own convex path, which
1007/// reports `QpStatus::DualInfeasible` (unbounded) as 300 (`main.rs`). It
1008/// is *not* a limit (400) condition.
1009///
1010/// `SolvedToAcceptableLevel` is `1`, not the 100 band, matching Ipopt's
1011/// ASL driver exactly (`Ipopt/src/Apps/AmplSolver/AmplTNLP.cpp`:
1012/// `STOP_AT_ACCEPTABLE_POINT` → `solve_result_num = 1`, message
1013/// "Solved To Acceptable Level."). The band is what consumers key on, and
1014/// the two bands are not interchangeable here: Pyomo's legacy `.sol`
1015/// reader turns `0..=99` into `status=ok` but `100..=199` into
1016/// `status=warning` with the same `termination_condition=optimal`, so the
1017/// 100 band made Pyomo log a "Loading a SolverResults object with a
1018/// warning status" warning on an accepted solve that Ipopt loads clean —
1019/// breaking solver-swappable clients whose accepted-solve contract
1020/// includes `status == ok` (gh #591). The reduced-accuracy convergence
1021/// stays visible in the status name and the `.sol` message line; it just
1022/// no longer reads as a warning.
1023///
1024/// `FeasiblePointFound` is `2`, Ipopt's own code, and therefore in the
1025/// `0..=99` solved band. It used to be `100`, justified by the claim that
1026/// the two statuses do not mean the same thing — that Ipopt returns
1027/// `FEASIBLE_POINT_FOUND` only for a square problem, where a feasible
1028/// point *is* the solution, while POUNCE used it more loosely for any
1029/// usable feasible point that missed the convergence criteria.
1030///
1031/// That claim was false about POUNCE's own code. The status has exactly
1032/// one production site: `min_c_1nrm.rs` returns
1033/// `RestorationOutcome::FeasiblePointFound`, reached only through the
1034/// gate at `resto_inner_solver.rs`, which is `is_square_problem && ...`.
1035/// `is_square_problem()` (`ipopt_alg.rs`) is `c.x.dim() == c.y_c.dim()`,
1036/// a port of `IpoptCalculatedQuantities::IsSquareProblem` — the same
1037/// condition Ipopt uses. So POUNCE emits this status *only* for square
1038/// problems, carrying Ipopt's meaning precisely, and on a square problem
1039/// there is no further convergence criterion to miss: the objective is
1040/// constant, so a feasible point is the solution.
1041///
1042/// The band is what consumers key on, and `100` was not a softer way of
1043/// saying the same thing — it inverted the answer. Pyomo's v2 reader
1044/// (`pyomo/contrib/solver/solvers/asl_sol_reader.py`) maps `100..=199` to
1045/// `TerminationCondition.error`, so a correct square-problem solve
1046/// reached the caller as a *solver error*; the legacy reader
1047/// (`pyomo/opt/plugins/sol.py`) maps it to `optimal` + `status=warning`,
1048/// the same gh #591 warning `SolvedToAcceptableLevel` was moved out of the
1049/// band to escape. Ipopt on the identical solve loads clean in both. This
1050/// is what gh #815 surfaced: an IDAES square flowsheet that POUNCE solves
1051/// to a constraint violation of 2.2e-06 was reported as a failure.
1052///
1053/// This crate is not the only place that had to agree. `python/pounce/gams/link.py`
1054/// already mapped the status to `(MODELSTAT_FEASIBLE, SOLVESTAT_NORMAL)`
1055/// and listed it as a success, and
1056/// `crates/pounce-algorithm/tests/issue_390_nonlinear_equality_scale.rs`
1057/// already called it "a success-band answer — AMPL `objno` code 2, which
1058/// every band table reads as SOLVED". The two Pyomo tables
1059/// (`pyomo_pounce.v2._V2_STATUS`, `pyomo_pounce.sens._STATUS_RESULT`) were
1060/// the dissenters and moved with this change.
1061///
1062/// Being in the solved band is *not* a claim that any feasible point is
1063/// acceptable. `issue_390_nonlinear_equality_scale.rs` is the guard that a
1064/// model with no solution is never reported feasible at any row scale; the
1065/// relative-violation threshold at `resto_inner_solver.rs` is what keeps
1066/// that true. This mapping decides how a verdict is reported, not when it
1067/// is reached.
1068pub fn status_to_solve_result_num(status: ApplicationReturnStatus) -> i32 {
1069    use ApplicationReturnStatus::*;
1070    match status {
1071        SolveSucceeded => 0,
1072        SolvedToAcceptableLevel => 1,
1073        FeasiblePointFound => 2,
1074        InfeasibleProblemDetected => 200,
1075        DivergingIterates => 300,
1076        SearchDirectionBecomesTooSmall => 400,
1077        MaximumIterationsExceeded => 400,
1078        MaximumCpuTimeExceeded => 400,
1079        MaximumWallTimeExceeded => 400,
1080        UserRequestedStop => 502,
1081        RestorationFailed => 500,
1082        ErrorInStepComputation => 500,
1083        InvalidNumberDetected => 500,
1084        InternalError => 500,
1085        UnrecoverableException => 500,
1086        NonIpoptExceptionThrown => 500,
1087        InsufficientMemory => 503,
1088        InvalidProblemDefinition => 504,
1089        InvalidOption => 504,
1090        NotEnoughDegreesOfFreedom => 504,
1091    }
1092}
1093
1094/// Write a [`SolveReport`] to `path` as pretty-printed JSON. Returns
1095/// bytes written on success.
1096pub fn write_report_file(path: &Path, report: &SolveReport) -> std::io::Result<usize> {
1097    let s = serde_json::to_string_pretty(report)
1098        .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
1099    std::fs::write(path, &s)?;
1100    Ok(s.len())
1101}
1102
1103/// Convert Unix nanoseconds since the epoch to an ISO-8601 UTC
1104/// timestamp `YYYY-MM-DDTHH:MM:SS.sssZ`. Pure stdlib; no chrono /
1105/// time dependency. The conversion is based on the proleptic
1106/// Gregorian calendar formula from Howard Hinnant's "date" reference
1107/// (https://howardhinnant.github.io/date_algorithms.html), `days_from_civil`
1108/// in reverse — verified against `date -u -r <secs>` for several
1109/// epochs on 2026-05-14.
1110fn unix_nanos_to_iso(nanos: i128) -> String {
1111    let total_secs = nanos.div_euclid(1_000_000_000) as i64;
1112    let frac_nanos = nanos.rem_euclid(1_000_000_000) as i64;
1113    let millis = frac_nanos / 1_000_000;
1114
1115    let days = total_secs.div_euclid(86_400);
1116    let secs_of_day = total_secs.rem_euclid(86_400);
1117    let hh = (secs_of_day / 3600) as i32;
1118    let mm = ((secs_of_day % 3600) / 60) as i32;
1119    let ss = (secs_of_day % 60) as i32;
1120
1121    // Howard Hinnant's `civil_from_days` algorithm:
1122    //   z = days + 719468
1123    //   era = (z >= 0 ? z : z - 146096) / 146097
1124    //   doe = z - era*146097
1125    //   yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365
1126    //   y = yoe + era*400
1127    //   doy = doe - (365*yoe + yoe/4 - yoe/100)
1128    //   mp = (5*doy + 2) / 153
1129    //   d = doy - (153*mp + 2)/5 + 1
1130    //   m = mp < 10 ? mp + 3 : mp - 9
1131    //   y += (m <= 2)
1132    let z: i64 = days + 719468;
1133    let era = if z >= 0 { z } else { z - 146096 } / 146097;
1134    let doe = (z - era * 146097) as i64; // [0, 146096]
1135    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399]
1136    let mut y = yoe + era * 400;
1137    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
1138    let mp = (5 * doy + 2) / 153; // [0, 11]
1139    let d = (doy - (153 * mp + 2) / 5 + 1) as i32;
1140    let m = if mp < 10 { mp + 3 } else { mp - 9 } as i32;
1141    if m <= 2 {
1142        y += 1;
1143    }
1144
1145    format!(
1146        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}Z",
1147        y, m, d, hh, mm, ss, millis
1148    )
1149}
1150
1151#[cfg(test)]
1152mod tests {
1153    use super::*;
1154
1155    #[test]
1156    fn iso_formatter_matches_known_epochs() {
1157        // Epoch.
1158        assert_eq!(unix_nanos_to_iso(0), "1970-01-01T00:00:00.000Z");
1159        // 2000-01-01T00:00:00Z = 946684800 seconds.
1160        assert_eq!(
1161            unix_nanos_to_iso(946_684_800_000_000_000),
1162            "2000-01-01T00:00:00.000Z",
1163        );
1164        // 2024-02-29T12:34:56.789Z (leap-year sanity check).
1165        // Seconds: (2024 - 1970) * 365.25 days * 86400 ≈ 1709209296 — let's compute exactly.
1166        // Days from 1970-01-01 to 2024-02-29: 19782.
1167        // 19782 * 86400 = 1709164800. Plus 12*3600 + 34*60 + 56 = 45296.
1168        // Total = 1709210096.
1169        let s = unix_nanos_to_iso(1_709_210_096_789_000_000);
1170        assert_eq!(s, "2024-02-29T12:34:56.789Z", "got: {s}");
1171    }
1172
1173    #[test]
1174    fn target_triple_resolves_to_real_triple_not_unknown() {
1175        // Fail-first: before the build.rs re-export this constant read
1176        // `option_env!("TARGET")`, which is `None` at crate-source compile
1177        // time (Cargo only exposes TARGET to build scripts), so it was always
1178        // "unknown". The build.rs now re-exports TARGET as
1179        // POUNCE_TARGET_TRIPLE, which resolves it to the real build triple.
1180        assert_ne!(
1181            TARGET_TRIPLE, "unknown",
1182            "build.rs should re-export the build target triple"
1183        );
1184        // A real triple has the `arch-vendor-os[-abi]` shape (>= 2 dashes).
1185        assert!(
1186            TARGET_TRIPLE.matches('-').count() >= 2,
1187            "unexpected target triple: {TARGET_TRIPLE:?}"
1188        );
1189
1190        // And it must propagate into the finished report.
1191        let b = ReportBuilder::new(
1192            ReportDetail::Summary,
1193            InputDescriptor::NlFile {
1194                path: PathBuf::from("/tmp/foo.nl"),
1195                size_bytes: None,
1196            },
1197        );
1198        let report = b.finish();
1199        assert_eq!(report.fair_metadata.solver.target_triple, TARGET_TRIPLE);
1200        assert_ne!(report.fair_metadata.solver.target_triple, "unknown");
1201    }
1202
1203    /// The Phase-0a work / pivoting fields and the Schur breakdown survive a
1204    /// JSON round trip, and a `linear_solver` object written before those
1205    /// fields existed still deserializes (the change is additive within v1).
1206    #[test]
1207    fn linear_solver_summary_round_trips_and_stays_backward_compatible() {
1208        use pounce_linsol::summary::SchurSummary;
1209        let summary = LinearSolverSummary {
1210            solver_name: "feral".into(),
1211            n_factors: 3,
1212            total_factor_secs: 0.5,
1213            total_factor_flops: 1.0e6,
1214            total_delayed_cols: 7,
1215            total_two_by_two: 2,
1216            last_ordering: Some("metis".into()),
1217            last_max_front_rows: Some(40),
1218            schur: Some(SchurSummary {
1219                n_eliminated: 100,
1220                n_schur: 4,
1221                n_factors: 3,
1222                ..Default::default()
1223            }),
1224            ..Default::default()
1225        };
1226        let info = LinearSolverSummaryInfo::from(summary);
1227        let json = serde_json::to_string(&info).expect("serialize");
1228        let back: LinearSolverSummaryInfo = serde_json::from_str(&json).expect("deserialize");
1229        assert_eq!(back.total_delayed_cols, 7);
1230        assert_eq!(back.total_two_by_two, 2);
1231        assert_eq!(back.last_ordering.as_deref(), Some("metis"));
1232        assert_eq!(back.last_max_front_rows, Some(40));
1233        let sch = back.schur.expect("schur survives");
1234        assert_eq!((sch.n_eliminated, sch.n_schur, sch.n_factors), (100, 4, 3));
1235        // Unset optionals are omitted, not written as null.
1236        assert!(!json.contains("last_peak_bytes"));
1237
1238        let old = r#"{"solver_name":"feral","n_factors":2,"n_pattern_reuse":1,"n_pattern_changes":1,"last_nnz_l":10}"#;
1239        let back: LinearSolverSummaryInfo = serde_json::from_str(old).expect("old report parses");
1240        assert_eq!(back.n_factors, 2);
1241        assert_eq!(back.total_factor_secs, 0.0);
1242        assert!(back.schur.is_none());
1243        assert!(back.restoration.is_none());
1244    }
1245
1246    #[test]
1247    fn report_serializes_round_trip() {
1248        let mut b = ReportBuilder::new(
1249            ReportDetail::Summary,
1250            InputDescriptor::NlFile {
1251                path: PathBuf::from("/tmp/foo.nl"),
1252                size_bytes: Some(123),
1253            },
1254        );
1255        b.problem.n_variables = 5;
1256        b.problem.n_constraints = 4;
1257        b.solution.status = ApplicationReturnStatus::SolveSucceeded;
1258        b.solution.solve_result_num = 0;
1259        b.solution.objective = 0.55;
1260        b.solution.x = vec![0.63, 0.39, 0.02, 5.0, 1.0];
1261        b.solution.lambda = vec![-0.16, -0.29, -0.16, 0.18];
1262        b.stats.iteration_count = 9;
1263
1264        let report = b.finish();
1265        let json = serde_json::to_string_pretty(&report).expect("serialize");
1266        let back: SolveReport = serde_json::from_str(&json).expect("deserialize");
1267        assert_eq!(back.schema, "pounce.solve-report/v1");
1268        assert_eq!(back.problem.n_variables, 5);
1269        assert_eq!(back.solution.x.len(), 5);
1270        assert!(matches!(
1271            back.solution.status,
1272            ApplicationReturnStatus::SolveSucceeded,
1273        ));
1274    }
1275
1276    /// gh #767: the report is the FAIR-aligned machine surface, and a
1277    /// consumer keyed on Ipopt's own enumerator spelling — which is what
1278    /// CUTEst tables, the reference JSONs and the CLI's `Status:` line all
1279    /// use — must be able to compare a field literally. `status` carries
1280    /// the Rust variant name (`SolveSucceeded`); `status_upstream` carries
1281    /// `Solve_Succeeded`. A consumer that compared the former against the
1282    /// latter's spelling matched nothing and read every solve as a failure.
1283    #[test]
1284    fn report_carries_the_upstream_status_spelling_beside_the_rust_one() {
1285        let mut b = ReportBuilder::new(
1286            ReportDetail::Summary,
1287            InputDescriptor::Builtin {
1288                name: "rosenbrock".into(),
1289            },
1290        );
1291        b.solution.status = ApplicationReturnStatus::SolveSucceeded;
1292        let json = serde_json::to_value(b.finish()).expect("serialize");
1293        assert_eq!(json["solution"]["status"], "SolveSucceeded");
1294        assert_eq!(json["solution"]["status_upstream"], "Solve_Succeeded");
1295    }
1296
1297    /// The derived field tracks whatever `status` was last set to — it is
1298    /// computed in `finish`, so a caller cannot leave it stale or set the
1299    /// two to different verdicts.
1300    #[test]
1301    fn upstream_status_spelling_is_derived_not_stored() {
1302        for status in [
1303            ApplicationReturnStatus::MaximumIterationsExceeded,
1304            ApplicationReturnStatus::InfeasibleProblemDetected,
1305            ApplicationReturnStatus::SolvedToAcceptableLevel,
1306        ] {
1307            let mut b = ReportBuilder::new(
1308                ReportDetail::Summary,
1309                InputDescriptor::Builtin { name: "x".into() },
1310            );
1311            // Deliberately wrong; `finish` must overwrite it.
1312            b.solution.status_upstream = "Solve_Succeeded".to_string();
1313            b.solution.status = status;
1314            let report = b.finish();
1315            assert_eq!(report.solution.status_upstream, status.upstream_name());
1316        }
1317    }
1318
1319    #[test]
1320    fn summary_detail_omits_iterations_block() {
1321        let mut b = ReportBuilder::new(
1322            ReportDetail::Summary,
1323            InputDescriptor::Builtin {
1324                name: "rosenbrock".into(),
1325            },
1326        );
1327        let mut stats = SolveStatistics::default();
1328        stats.iterations.push(IterRecord {
1329            iter: 0,
1330            objective: 1.0,
1331            ..IterRecord::default()
1332        });
1333        b.ingest_stats(&stats);
1334        let r = b.finish();
1335        assert!(
1336            r.iterations.is_empty(),
1337            "Summary detail should drop iter history; got {} rows",
1338            r.iterations.len()
1339        );
1340        // And the JSON should not include the key at all (skip-empty).
1341        let json = serde_json::to_string(&r).unwrap();
1342        assert!(!json.contains("\"iterations\":"), "json: {json}");
1343    }
1344
1345    #[test]
1346    fn full_detail_includes_iteration_rows() {
1347        let mut b = ReportBuilder::new(ReportDetail::Full, InputDescriptor::TnlpDirect);
1348        let mut stats = SolveStatistics::default();
1349        stats.iterations.push(IterRecord {
1350            iter: 0,
1351            objective: 1.0,
1352            inf_pr: 0.5,
1353            ..IterRecord::default()
1354        });
1355        stats.iterations.push(IterRecord {
1356            iter: 1,
1357            objective: 0.5,
1358            inf_pr: 0.1,
1359            ..IterRecord::default()
1360        });
1361        b.ingest_stats(&stats);
1362        let r = b.finish();
1363        assert_eq!(r.iterations.len(), 2);
1364        assert_eq!(r.iterations[0].iter, 0);
1365        assert_eq!(r.iterations[1].iter, 1);
1366    }
1367
1368    #[test]
1369    fn detail_parser_accepts_known_values() {
1370        assert_eq!(
1371            ReportDetail::parse("summary").unwrap(),
1372            ReportDetail::Summary
1373        );
1374        assert_eq!(ReportDetail::parse("Full").unwrap(), ReportDetail::Full);
1375        assert!(ReportDetail::parse("verbose").is_err());
1376    }
1377
1378    #[test]
1379    fn diverging_iterates_maps_to_unbounded_range() {
1380        use ApplicationReturnStatus::*;
1381        // M12 regression: DivergingIterates is Ipopt's unboundedness
1382        // signal and must land in the AMPL 300 "unbounded" range, not
1383        // the 400 "limit" range — matching upstream Ipopt's ASL driver
1384        // and the CLI convex path (QpStatus::DualInfeasible → 300).
1385        assert_eq!(status_to_solve_result_num(DivergingIterates), 300);
1386
1387        // Lock the surrounding range convention so the fix can't silently
1388        // drift back: solved / infeasible / limit / failure buckets.
1389        assert_eq!(status_to_solve_result_num(SolveSucceeded), 0);
1390        assert_eq!(status_to_solve_result_num(InfeasibleProblemDetected), 200);
1391        assert_eq!(
1392            status_to_solve_result_num(MaximumIterationsExceeded),
1393            400,
1394            "iteration limit stays in the 400 range",
1395        );
1396        assert_eq!(
1397            status_to_solve_result_num(SearchDirectionBecomesTooSmall),
1398            400,
1399        );
1400        assert_eq!(status_to_solve_result_num(RestorationFailed), 500);
1401    }
1402
1403    /// gh #591: an accepted (reduced-accuracy) solve must land in AMPL's
1404    /// `0..=99` *solved* band with Ipopt's own code, `1`. In the 100 band
1405    /// Pyomo's legacy `.sol` reader loads the result as
1406    /// `status=warning, termination_condition=optimal` and logs a warning,
1407    /// while the identical Ipopt solve loads as `status=ok` — so a
1408    /// solver-swappable client that treats `status == ok` as part of its
1409    /// accepted-solve contract had to special-case POUNCE.
1410    #[test]
1411    fn solved_to_acceptable_level_is_in_the_solved_band_like_ipopt() {
1412        use ApplicationReturnStatus::*;
1413        let code = status_to_solve_result_num(SolvedToAcceptableLevel);
1414        assert_eq!(
1415            code, 1,
1416            "Ipopt's ASL driver emits 1 for STOP_AT_ACCEPTABLE_POINT",
1417        );
1418        assert!(
1419            (0..=99).contains(&code),
1420            "must be in the solved band Pyomo maps to status=ok, got {code}",
1421        );
1422        // Still distinguishable from a full-accuracy solve: the two codes
1423        // differ, and the status name carries the distinction verbatim into
1424        // the `.sol` message line.
1425        assert_ne!(code, status_to_solve_result_num(SolveSucceeded));
1426
1427        // `FeasiblePointFound` is also in the solved band, for its own
1428        // reason — see `a_square_problem_feasible_point_is_in_the_solved_band`.
1429        assert_ne!(code, status_to_solve_result_num(FeasiblePointFound));
1430    }
1431
1432    /// POUNCE emits `FeasiblePointFound` only for square problems — the
1433    /// status has one production site (`min_c_1nrm.rs`) behind one gate
1434    /// (`resto_inner_solver.rs`), and that gate is `is_square_problem &&
1435    /// ...`. That is exactly Ipopt's meaning, and on a square problem a
1436    /// feasible point *is* the solution, so the code is Ipopt's own `2`.
1437    ///
1438    /// The band, not the number, is what breaks: at `100` Pyomo's v2 ASL
1439    /// reader returns `TerminationCondition.error` for a correct solve
1440    /// (gh #815 — an IDAES flowsheet solved to a 2.2e-06 constraint
1441    /// violation and reported as a solver error), and the legacy reader
1442    /// returns `status=warning`, the same gh #591 complaint that moved
1443    /// `SolvedToAcceptableLevel` out of the band.
1444    #[test]
1445    fn a_square_problem_feasible_point_is_in_the_solved_band() {
1446        use ApplicationReturnStatus::*;
1447        let code = status_to_solve_result_num(FeasiblePointFound);
1448        assert_eq!(
1449            code, 2,
1450            "Ipopt's ASL driver emits 2 for FEASIBLE_POINT_FOUND"
1451        );
1452        assert!(
1453            (0..=99).contains(&code),
1454            "must be in the solved band both Pyomo readers accept, got {code}",
1455        );
1456        // Still its own verdict: distinguishable from both other members of
1457        // the band, with the distinction carried verbatim in the status name
1458        // and the `.sol` message line.
1459        assert_ne!(code, status_to_solve_result_num(SolveSucceeded));
1460        assert_ne!(code, status_to_solve_result_num(SolvedToAcceptableLevel));
1461    }
1462
1463    #[test]
1464    fn result_id_is_unique_and_time_ordered() {
1465        let a = ReportBuilder::new(ReportDetail::Summary, InputDescriptor::TnlpDirect).finish();
1466        std::thread::sleep(std::time::Duration::from_millis(2));
1467        let b = ReportBuilder::new(ReportDetail::Summary, InputDescriptor::TnlpDirect).finish();
1468        assert_ne!(a.fair_metadata.result_id, b.fair_metadata.result_id);
1469        assert!(
1470            b.fair_metadata.created_at_unix_nanos > a.fair_metadata.created_at_unix_nanos,
1471            "second result_id should sort after first"
1472        );
1473    }
1474}