Improve list unapplied rules validation trace#158
Draft
Conversation
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>
6d03195 to
162c209
Compare
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>
162c209 to
d212208
Compare
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>
Contributor
There was a problem hiding this comment.
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR improves the trace error message when there are leftover terms. It now shows the trace for the best matching rule.