Skip to content

Improve list unapplied rules validation trace#158

Draft
Soupstraw wants to merge 4 commits intomasterfrom
jj/validator-trace-improvements
Draft

Improve list unapplied rules validation trace#158
Soupstraw wants to merge 4 commits intomasterfrom
jj/validator-trace-improvements

Conversation

@Soupstraw
Copy link
Contributor

@Soupstraw Soupstraw commented Mar 16, 2026

This PR improves the trace error message when there are leftover terms. It now shows the trace for the best matching rule.

Show which rules remain unapplied when a CBOR list runs out of
elements, mirroring the existing map validation behaviour.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Soupstraw Soupstraw force-pushed the jj/validator-trace-improvements branch 2 times, most recently from 6d03195 to 162c209 Compare March 17, 2026 11:20
Show the closest matching skipped rule when leftover elements remain
after all rules have been applied. Rules that were tried but skipped
(e.g. optional rules) are tracked and re-evaluated against the first
leftover element, with the best match (by measureProgress) shown.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Soupstraw Soupstraw force-pushed the jj/validator-trace-improvements branch from 162c209 to d212208 Compare March 17, 2026 11:20
Soupstraw and others added 2 commits March 17, 2026 13:29
Show the closest matching postponed rule when leftover key-value pairs
remain after all rules have been tried. The exhausted rules are
re-evaluated against the first leftover KV pair, and the best match
(key matched, value failed) is shown with its trace.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- mapNoMatchingKey: map leftover with no matching key (generic message)
- listZeroOrMore: list with zero-or-more rule and leftover elements
- mapNestedValue: map with nested value that partially matches

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances CBOR/CDDL validation diagnostics by improving list/map failure traces (especially “leftover” cases) and expanding golden-test coverage to lock in the new pretty-printed error output.

Changes:

  • Extend list/map validation traces to optionally include a “closest matching rule” attempt and improve pretty-print output for leftover/unapplied-rule failures.
  • Update list/map validator logic to track skipped/exhausted rules and select a best-effort diagnostic trace.
  • Add multiple new Huddle examples and golden tests covering list/map failure scenarios; update existing golden outputs; ignore cabal.project.local.

Reviewed changes

Copilot reviewed 13 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/Codec/CBOR/Cuddle/CBOR/Validator.hs Tracks skipped/exhausted rules and computes best-effort “closest match” traces for leftover list/map elements.
src/Codec/CBOR/Cuddle/CBOR/Validator/Trace.hs Updates trace types, exports IsValidationTrace, and improves pretty-printing for leftover/unapplied-rule cases.
test/Test/Codec/CBOR/Cuddle/CDDL/Examples/Huddle.hs Adds new Huddle examples to exercise list/map validator edge cases.
test/Test/Codec/CBOR/Cuddle/CDDL/Validator/Golden.hs Adds new golden tests and corresponding failing CBOR Term inputs.
golden/*.txt Adds/updates golden outputs for the new diagnostics.
.gitignore Ignores cabal.project.local.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

validate f skipped tss [] = (f skipped tss, tss)
validate f skipped [] (r : rs)
| isOptional r = validate f skipped [] rs
| otherwise = (evidence $ ListValidationUnappliedRules (mapIndex <$> r :| rs), [])
Comment on lines +853 to +869
validate exhausted (kv : _) [] =
let
unwrapOccur (Occur ct _) = ct
unwrapOccur ct = ct
attempts =
[ (mapIndex r, MapValidationInvalidValue (mapIndex r) kTrc vTrc)
| r <- exhausted
, KV k v _ <- [unwrapOccur r]
, Evidenced SValid kTrc <- [validateTerm cddl (fst kv) k]
, Evidenced SInvalid vTrc <- [validateTerm cddl (snd kv) v]
, measureProgress (MapValidationInvalidValue (mapIndex r) kTrc vTrc) > 0
]
bestAttempt = case attempts of
[] -> Nothing
_ -> Just $ maximumBy (compare `on` (measureProgress . snd)) attempts
in
evidence $ MapValidationLeftoverKVs kv bestAttempt
MapValidationLeftoverKVs _ (Just (_, trc)) -> measureProgress trc
MapValidationUnappliedRules _ -> 0
MapValidationConsume _ _ _ x -> 3 + measureProgress x
MapValidationInvalidValue {} -> 2
Comment on lines 21 to 25
Evidenced (..),
IsValidationTrace (..),
ControlInfo (..),
TraceOptions (..),
defaultTraceOptions,
@Soupstraw Soupstraw marked this pull request as draft March 17, 2026 17:00
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.

2 participants