Skip to content

Add Target::can_emit capability query; registry generator uses it (#137) - #158

Merged
petlenz merged 11 commits into
mainfrom
fix-137-can-emit
Nov 29, 2026
Merged

petlenz merged 11 commits into
mainfrom
fix-137-can-emit

Conversation

@petlenz

@petlenz petlenz commented Aug 2, 2026

Copy link
Copy Markdown
Member

Closes #137. Also resolves #135's symptom (see note).

Target::emit preconditions were discoverable only by catching std::runtime_error. New API:

[[nodiscard]] virtual auto can_emit(ConstitutiveModel const &) const
    -> std::expected<void, std::string>;  // conservative default: success

Each target's up-front scope guards are extracted into one shared rejection function (scope_rejection / rate_scope_rejection / residual_scope_rejection) used by BOTH can_emit (unexpected) and emit (throw) — message text single-sourced, byte-equality pinned by tests. Documented limit: can_emit success ≠ emit success (emit-time validation may still throw); covered by an explicit test.

recipe_registry_gen now queries can_emit and prints SKIPPED (<reason>) with no catch around emit — unexpected exceptions stay crash-loud. Verified: numsim_material traverses all 6 recipes with per-recipe reasons and exits 0 (previously aborted on the first); standalone emits all 6.

Supersedes PR #149 (the interim blanket-catch fix for #135) — recommend closing #149 unmerged; if #149 merges first, this branch's registry-gen hunk conflicts trivially. One semantic difference vs #149: all-recipes-skipped exits 0 here (a query-based skip is not an error) vs #149's exit 3 — happy to flip if you prefer the nonzero signal.

Tests: new CanEmitTest.cpp (9 tests: accept/reject per target, reason==throw-message equality, polymorphic use via make_target, the NaN-default "success ≠ emit success" case). gcc-14 Debug (-Werror): ctest 331/331. Based on current main (86e1862).

…ing emitted + ctests; keep tensor-state diagnostic order
# Conflicts:
#	src/targets/moose_material.cpp
#	tests/CMakeLists.txt
# Conflicts:
#	src/targets/numsim_material.cpp
@petlenz

petlenz commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

C++ review

Verdict: the design (shared rejection reasons) was sound but can_emit disagreed with emit in several reachable ways. Fixed: Target::can_emit now dry-runs emit() by default, so the two agree by construction for every target (including CalculiX and any guard added later), with a permanent agreement test. The fixes are on the branch.

Findings

# Sev Where Problem Observed Fix
1 Medium standalone_cxx.h comment "accepts every recipe shape"; can_emit default Standalone and MOOSE reject implicit-residual recipes and tensor state variables (from emit_compute_function), yet can_emit returned success. recipe_registry_gen has no catch around emit, so such a recipe in the registry would crash it for those targets (the #135 failure again). Harness, registry + 40 adversarial recipes x 3 targets: resid_ok, resid_tangent, tensor_state_only -> can_emit: OK, emit: THROW "implicit residual equations ... not supported by the self-contained path" 96544ab (shared ConstitutiveModel::self_contained_rejection), superseded in generality by #5
2 High (after main moved) moose_material.cpp scope_rejection, stateful-output guard from #120 The textual merge with main is clean, but #120's guard is a throw; inside the shared query function it makes can_emit throw. Read of merged code; same for #124 (OnFailure::Throw) and #154 (dim != 3) which I converted to returns while resolving the merge 0f460b1 and merge ee54e4f: all MOOSE guards return their reason; comment states "new guards return, never throw"
3 Medium can_emit contract "success does not guarantee emit success": measured can_emit == true but emit throws for non-finite parameter default (numsim_material, MOOSE), residual tangent referencing an undeclared output/input, output/parameter names colliding with synthesized members (residual, jacobian, ...), MOOSE tensor rank not 2/4, MOOSE non-constant state initial. 9 of the cases I tried. agreement harness d61676c: default Target::can_emit runs emit() and returns unexpected(what()) on std::runtime_error. No second list of checks, so #124/#148/#154/#127/CalculiX guards are covered automatically. Only runtime_error is translated; logic_error, bad_alloc propagate (defects, not recipe verdicts); documented in target.h. emit is const and pure, cost is one emission; the shape pre-checks in Moose/NumSim remain as a cheap fast path and delegate to the base afterwards.
4 Medium tests SuccessDoesNotGuaranteeEmitSuccess pinned the disagreement as intended behaviour. No test covered all targets. Replaced by NonFiniteDefaultIsReportedWithEmitMessage and AgreesWithEmitForEveryTargetOnRegistryAndAdversarialRecipes: for every name in target_names() over the registry + ~55 adversarial recipes, can_emit.has_value() == !emit throws and error == thrown message, and each target must both accept and reject at least one recipe (non-vacuous).
5 Low recipe_registry_gen All-skipped run exited 0 with no output beyond the SKIPPED lines. recipe_registry_gen out numsim_material -> exit 0, nothing written Adopted #149's design on top of can_emit: N emitted, M skipped line, exit 3 when nothing emitted (README + usage documented; nothing in the repo relied on exit 0), ctests recipe_registry_gen_skips_unsupported, recipe_registry_gen_standalone_all, and recipe_registry_gen_nothing_emitted_exits_3 (via examples/expect_exit3.cmake). 4af70c9

Own regression caught on the way: hoisting the tensor-state check ahead of the passes changed the diagnostic that #119's tests pin (AlgorithmicTangent.StateVariable*RejectedAtEmit failed); the early check now covers residual equations only, tensor state is reported through the dry run with the original message. 4af70c9.

Checked and held up

  • Registry: 6 recipes x {numsim_material, standalone, moose, calculix}: agree; messages byte-equal.
  • Adversarial recipes covering: reserved/colliding names on rate and residual paths, non-finite defaults, update equations, two tangents, Jacobian_mult output, ncg_ reserved prefix, dim-2, rank-3, stateful input/output, OnFailure::Throw, empty recipe. After the change: 0 disagreements.
  • No side effects, const&, no mutation. Virtual added before target_name shifts the vtable (static lib, no ABI promise; just a rebuild for out-of-tree targets).
  • Mutation: with the dry run removed from the final code, AgreesWithEmitForEveryTargetOnRegistryAndAdversarialRecipes reports disagreements on standalone, MOOSE, NumSim and CalculiX recipes, and the two StandaloneRejects* tests fail; restored, all 402 unit tests pass.
  • Review-and-merge-ready interplay: Validate MooseMaterialTarget app_name (#143) #148 (app_name) needs no extra work now (it is an emit throw, seen by the dry run); CalculiX target: dlopen'd .so material (no ccx recompile per material) #127 CalculiX needed nothing.

Outside this PR

none.

Final

wt-158 @ ea9cf1c (merges origin/main 7da7e1a incl. #120 #123 #124 #127 #129 #148 #154 #155 #156 #157 #121 #122). Resolved: moose scope_rejection now takes app_name (the #148 check is part of it); #123's new residual-path guards need no work (dry run). gcc-14 Debug -Werror full build green; ctest: 100% tests passed, 0 tests failed out of 483.

Note on the PR description: it still says an all-skipped run exits 0; with these changes it exits 3 (as in #149).

@petlenz
petlenz merged commit 6e8d95f into main Nov 29, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant