Repository navigation
feat(diff): introduce closed-loop post-condition verification and invariant fuzzing - #105
Merged
Merged
Conversation
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.
Summary
Introduces two pragmatic Loop Engineering (Closed-Loop) mechanisms into
rich-markdown-diffto enforce structural invariants at runtime and automatically uncover edge-case regressions via property-based fuzzing with counterexample shrinking:Closed Loop 1 — Runtime Post-Condition Sensor & Two-Stage Graceful Degradation (
src/markdown/structuralDiff.ts):hasUnrestoredPipelineTokens: Verifies that no internal pipeline placeholder or attribute mask tokens (TK...,data-attr-masked-...,__PRE_CLEANUP_...,PREDIFF_...) leaked into the final HTML output unless originally present as literal user text.areDiffTagsBalanced: Verifies in<ins>and<del>tags are strictly balanced in LIFO order.verifyDiffIntegrity(targetHtml, diffHtml, side: "new" | "old"): Extends content integrity verification to check both the"new"side (Shared + Inserted) and"old"side (Shared + Deleted), properly handling nested diff tags andhtmldiff-js<ins class="mod">formatting wrappers.verifyPipelinePostConditions& Two-Stage Graceful Degradation: Wired intoexecuteWithFullPipeline:applyStructuralDiffPipeline) topreRefinementSnapshotif refinement corrupts tag balance, leaks tokens, or drops content.execute(oldHtml, newHtml)+fixInvalidNesting+cleanupUnbalancedDiffTags) if Stage 1 also fails post-conditions.Closed Loop 2 — Property-Based & Metamorphic Invariant Fuzzing with Counterexample Shrinking (
src/test/unit/loopInvariants.test.ts):Mulberry32PRNG and Markdown block generator (generateMarkdownBlocks) spanning headings, nested lists, task lists, tables, code blocks, blockquotes, math blocks, Mermaid diagrams, horizontal rules, CJK/Unicode, and inline formatting (**,*,~~,==,^sup^,~sub~).computeDiff(doc, doc)), Zero Token Leakage, Diff Tag Balance, Dual-Side Content Integrity,data-lineBounds, and Execution Time Bounds across 80 generated document pairs.shrinkCounterexample) that reduces any failing block pair to a minimal reproducible counterexample.Edge-Case Bugs Uncovered & Resolved via the Closed Loops:
src/markdownDiff.ts(safeParseMatter): Prevents uncaughtYAMLExceptionfromgray-matterwhen a document starts with a horizontal rule---followed by non-YAML content (e.g. blockquotes> ...) and another---.src/markdown/structuralDiff.ts(balanceDiffTags&cleanupUnbalancedDiffTags): Fixes crossed closing diff tags (<ins><del>...</ins></del>) and removes the unconditional.replace(/<\/del>\s*<\/ins>/g, "</ins></del>")that previously inverted valid<ins><del>...</del></ins>closing pairs.src/markdown/structuralDiff.ts(flattenDiffNesting): Restricts the inner diff tag match to innermost diff tags ((?:(?!<\/?(?:ins|del)\b)[\s\S])*?) so sibling inner diff tags do not swallow intervening shared text.src/markdown/structuralDiff.ts(fixInvalidNesting): Repairs upstreamhtmldiff-js<sup>handling (where<sup>is inspecialCaseOpeningTagRegexbut</sup>is omitted fromspecialCaseClosingTags), preventing orphan</sup>tags and unclosed<ins class="mod">tags from swallowing subsequent shared words into<ins>blocks.Verification
tsc -p ./): 0 errorsnode esbuild.js --sourcemap): passedeslint src): 0 errors / warningsmocha "./out/test/unit/**/*.test.js"): 294 passing