Skip to content

fix(703): carry decision specs through to_stickler_config() and model_from_json() - #724

Merged
digalaviz merged 3 commits into
devfrom
fix/703-config-decision-specs
Dec 3, 2026
Merged

digalaviz merged 3 commits into
devfrom
fix/703-config-decision-specs

Conversation

@sromoam

@sromoam sromoam commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

What changes

to_stickler_config() wrote Choice, Score and Noul fields as plain exact-match fields, so model_from_json() rebuilt them without their questions. The rebuilt model scored an unasked Noul (gold None) as 0.0 instead of leaving it out, and its from_json() rejected answer objects. Nothing warned. to_json_schema() already kept the specs.

The config now records each question under decision, with the same content as the schema's x-aws-stickler-decision:

"urgent": {"type": "bool", "comparator": "ExactComparator", "threshold": 1.0, "weight": 1.0, "clip_under_threshold": true, "decision": {"type": "noul", "on_invalid": "missing"}, "required": false, "default": null}

model_from_json() rebuilds the decision field from it, with the schema importer's checks, and keeps the field's settings:

  • default is passed through and checked against the question's answers. A default the question doesn't have raises Invalid default for decision field .... The schema importer (JsonSchemaFieldImporter._decision_field) now reads a schema default the same way. It used to hard-code None.
  • required reads as on the plain field path: required wins over a default, and a field with neither defaults to None.
  • alias is refused, as Choice(alias=...) is.

It also refuses:

  • a comparator or threshold other than exact match at 1.0;
  • a decision that isn't an object with a type;
  • an unknown kind, or an invalid on_invalid;
  • a type that isn't the answer type (str for Choice, int for Score, bool for Noul; Optional[...] is fine).

In a hand-written config, decision stands in for a comparator.

Not changed here:

  • to_json_schema() writes no literal default for any scalar field, decision or not, so from_json_schema(T.to_json_schema()) still has no default to read. That's the general export gap.
  • The schema importer doesn't read required for a decision field, as on dev.
  • Choice() doesn't validate its own default.

The last two are a follow-up.

The decision API is unreleased, so the CHANGELOG change is a clause in the #469 entry.

Tests

tests/decisions/test_config_round_trip.py: 14 test functions, 24 cases. They cover:

  • the issue's repro: the same decision specs, the unasked Noul left out, and answer objects read;
  • on_invalid="missing" and a non-default weight surviving the round trip;
  • identical bulk decision_metrics for the original and the rebuilt model;
  • decision fields in a nested model and in a list of models;
  • default and required surviving the round trip, and the schema importer reading a default;
  • each refusal above.

Full suite: 7254 passed, 13 skipped, 1 xfailed. ruff check . and docs/check_links.py pass.

Fixes #703. @vawsgit, this was assigned to you with no branch yet, and it comes from #540, so I picked it up to make 1.1.0. Shout if you'd started.

…_from_json()

to_stickler_config() wrote Choice, Score and Noul fields as plain exact-match
fields, so model_from_json() rebuilt them without their questions: an unasked
Noul scored 0.0 instead of being left out, and from_json() rejected answer
objects. The config now records each question under 'decision', with the same
content as the schema's x-aws-stickler-decision, and model_from_json() rebuilds
the decision field from it with the schema importer's checks: a comparator or
threshold other than exact match at 1.0, a malformed question, or a type that
is not the answer type is refused. A 'decision' key stands in for a comparator
in a hand-written config.

Fixes #703.
@sromoam
sromoam requested a review from digalaviz as a code owner December 2, 2026 19:15
@sromoam sromoam added this to the 1.1.0 milestone Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Security Scan Results

ASH Security Scan Report

  • Report generated: 2026-12-03T21:57:46+00:00
  • Time since scan: 4 minutes

Scan Metadata

  • Project: ASH
  • Scan executed: 2026-12-03T21:53:16+00:00
  • ASH version: 3.1.2

Summary

Scanner Results

The table below shows findings by scanner, with status based on severity thresholds and dependencies:

  • Severity levels:
    • Suppressed (S): Findings that have been explicitly suppressed and don't affect scanner status
    • Critical (C): Highest severity findings that require immediate attention
    • High (H): Serious findings that should be addressed soon
    • Medium (M): Moderate risk findings
    • Low (L): Lower risk findings
    • Info (I): Informational findings with minimal risk
  • Duration (Time): Time taken by the scanner to complete its execution
  • Actionable: Number of findings at or above the threshold severity level that require attention
  • Result:
    • PASSED = No findings at or above threshold
    • FAILED = Findings at or above threshold
    • MISSING = Required dependencies not available
    • SKIPPED = Scanner explicitly disabled
    • ERROR = Scanner execution error
  • Threshold: The minimum severity level that will cause a scanner to fail
    • Thresholds: ALL, LOW, MEDIUM, HIGH, CRITICAL
    • Source: Values in parentheses indicate where the threshold is set:
      • global (global_settings section in the ASH_CONFIG used)
      • config (scanner config section in the ASH_CONFIG used)
      • scanner (default configuration in the plugin, if explicitly set)
  • Statistics calculation:
    • All statistics are calculated from the final aggregated SARIF report
    • Suppressed findings are counted separately and do not contribute to actionable findings
    • Scanner status is determined by comparing actionable findings to the threshold
Scanner Suppressed Critical High Medium Low Info Actionable Result Threshold
bandit 0 0 0 0 8179 0 0 PASSED MEDIUM (global)
cdk-nag 0 0 0 0 0 0 0 PASSED MEDIUM (global)
cfn-nag 0 0 0 0 0 0 0 MISSING MEDIUM (global)
checkov 0 0 0 0 0 0 0 PASSED MEDIUM (global)
detect-secrets 0 0 0 0 0 0 0 PASSED MEDIUM (global)
grype 0 0 0 0 0 0 0 MISSING MEDIUM (global)
npm-audit 0 0 0 0 0 0 0 PASSED MEDIUM (global)
opengrep 0 0 0 0 0 0 0 MISSING MEDIUM (global)
semgrep 7 0 0 0 0 0 0 PASSED MEDIUM (global)
syft 0 0 0 0 0 0 0 MISSING MEDIUM (global)

Report generated by Automated Security Helper (ASH) at 2026-12-03T21:57:47+00:00

@sromoam
sromoam requested review from harrycav and vawsgit December 3, 2026 15:42

@digalaviz digalaviz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is right for the #703 case, but one loss is still silent: a decision field's default does not survive the round trip, and that changes the score.

_convert_decision_field always builds the field with {"default": None}, while to_stickler_config() writes the real default. With queue: Optional[str] = Choice(criteria=["billing", "sales"], default="billing") and a plain note field:

  • original: T(note="x").compare_with(T(queue="sales", note="x"))["field_scores"] is {'queue': 0.0, 'note': 1.0}
  • rebuilt from the config: {'note': 1.0}, so queue is left out of the score
  • a second to_stickler_config() changes default from 'billing' to None

Also on the config path: {"type": "str", "decision": {...}, "alias": "Q"} builds a field with no alias and no warning, but Choice(alias=...) raises TypeError.

This PR copies the gap from JsonSchemaFieldImporter._decision_field, which also hard-codes {"default": None}, so from_json_schema(T.to_json_schema()) loses the default too. That path came in with #540. I can open a separate issue for it.

I measured this on 2f2be3d. Merged on top of dev plus #688, #690 and #710 it has no conflicts: 7430 passed, 13 skipped, 1 xfailed.

Do you want to pass default through and raise on alias, or raise on any non-None default?

From review: both importers built a decision field with default None, so a
Choice(default='billing') lost its default through to_stickler_config() and
model_from_json(), and the question dropped out of the score. They now pass
the default through, checked against the question's answers, and the config
path refuses 'alias' as Choice(alias=...) does.
@sromoam

sromoam commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @digalaviz, good catch. Fixed in da7d6d09: I took the first option, passing default through and raising on alias.

  • default: both importers now pass it through, checked against the question's answers, so a default the question doesn't have raises Invalid default for decision field .... Your example now gives {'queue': 0.0, 'note': 1.0} from the original and from the rebuilt model, and a second to_stickler_config() still writes 'billing'. That covers the schema importer too, so no separate issue is needed for it.
  • alias: the config path refuses it, as Choice(alias=...) does. The schema importer drops alias for every field, so a decision field there can't end up with one.
  • Not fixed here: to_json_schema() doesn't write a literal default for any scalar field, decision or not, so from_json_schema(T.to_json_schema()) still has no default to read. That's the general export gap, not something this PR introduces. A hand-written schema default is now read.

5 new tests fail on 2f2be3d4 and pass here. Full suite: 7249 passed, 13 skipped, 1 xfailed. ruff and check_links.py are clean, and the branch merges cleanly with dev.

@sromoam
sromoam requested a review from digalaviz December 3, 2026 17:29

@digalaviz digalaviz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default and alias fixes work: I get {'queue': 0.0, 'note': 1.0} from both classes, a second export keeps 'billing', and an invalid default or an alias raises.

One more field setting is lost on the config path: required. With c: Optional[str] = Choice(criteria=["x", "y"], default=...), to_stickler_config() writes 'required': True, but _convert_decision_field never reads it, so the rebuilt class accepts an instance without c. On dev the same config builds a required field, so this is new in this PR. The schema path loses it too, but it already did on dev.

Two small notes, not blocking:

  • In Python, Choice(criteria=["a", "b"], default="zzz") builds without error, and model_from_json() now refuses its export. Raising is right; validating in Choice() too would make both sides agree. That can be a follow-up.
  • The PR body still describes the first commit: it says 14 tests (the file has 12 test functions) and does not mention the default, alias and schema importer changes.

I measured this on da7d6d0. Merged with dev it has no conflicts: 7446 passed, 13 skipped, 1 xfailed.

Can you read required in _convert_decision_field as the plain field path does?

model_from_json() now reads 'required' and 'default' for a decision field
the way the plain field path does: 'required' wins over a default, and a
field with neither defaults to None. A required Choice exported by
to_stickler_config() rebuilt as optional before.
@sromoam

sromoam commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @digalaviz. Fixed in 93b22c86: _convert_decision_field now reads required and default the way the plain field path does. required wins over a default, and a field with neither defaults to None.

  • Your c example is now required in the original class, the rebuilt one and a second rebuild, and c=None is still accepted, as in Python.
  • A hand-written {"required": true, "default": "x"} builds a required field, as it does for a plain field.
  • The decision-models page now says required works as on any field.

3 new tests fail on da7d6d09 and pass here. Full suite: 7254 passed, 13 skipped, 1 xfailed. ruff check . and check_links.py are clean, and the branch merges cleanly with dev.

On your notes:

  • Schema path: left as it is on dev, as you said. I'll fold it into the follow-up below.
  • Choice(default="zzz"): agreed, Choice() should validate its default too. I'll open a follow-up issue for that and the schema path's required.
  • PR body: updated to cover all three commits.

@digalaviz digalaviz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required now survives the config round trip: with c: Optional[str] = Choice(criteria=["x", "y"], default=...), the rebuilt class rejects an instance without c, as the original does. Default and alias handling still hold. Merged with dev at 545da9f it has no conflicts: 7451 passed, 13 skipped, 1 xfailed.

@digalaviz
digalaviz merged commit 60ccc07 into dev Dec 3, 2026
15 checks passed
@digalaviz
digalaviz deleted the fix/703-config-decision-specs branch December 3, 2026 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: to_stickler_config() drops decision specs without a warning

2 participants