Skip to content

feat(diff): introduce closed-loop post-condition verification and invariant fuzzing - #105

Merged
phine-apps merged 3 commits into
mainfrom
feature/closed-loop-engineering
Nov 7, 2026
Merged

phine-apps merged 3 commits into
mainfrom
feature/closed-loop-engineering

Conversation

@phine-apps

Copy link
Copy Markdown
Owner

Summary

Introduces two pragmatic Loop Engineering (Closed-Loop) mechanisms into rich-markdown-diff to enforce structural invariants at runtime and automatically uncover edge-case regressions via property-based fuzzing with counterexample shrinking:

  1. 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 $O(N)$ linear time that all <ins> and <del> tags are strictly balanced in LIFO order.
    • Dual-Side 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 and htmldiff-js <ins class="mod"> formatting wrappers.
    • verifyPipelinePostConditions & Two-Stage Graceful Degradation: Wired into executeWithFullPipeline:
      • Stage 1: Rolls back post-restoration block refinement (applyStructuralDiffPipeline) to preRefinementSnapshot if refinement corrupts tag balance, leaks tokens, or drops content.
      • Stage 2: Falls back to a safe raw diff (execute(oldHtml, newHtml) + fixInvalidNesting + cleanupUnbalancedDiffTags) if Stage 1 also fails post-conditions.
  2. Closed Loop 2 — Property-Based & Metamorphic Invariant Fuzzing with Counterexample Shrinking (src/test/unit/loopInvariants.test.ts):

    • Deterministic Mulberry32 PRNG 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~).
    • Verifies Metamorphic Identity (computeDiff(doc, doc)), Zero Token Leakage, Diff Tag Balance, Dual-Side Content Integrity, data-line Bounds, and Execution Time Bounds across 80 generated document pairs.
    • Automatic delta-debugging counterexample shrinker (shrinkCounterexample) that reduces any failing block pair to a minimal reproducible counterexample.
  3. Edge-Case Bugs Uncovered & Resolved via the Closed Loops:

    • src/markdownDiff.ts (safeParseMatter): Prevents uncaught YAMLException from gray-matter when 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 upstream htmldiff-js <sup> handling (where <sup> is in specialCaseOpeningTagRegex but </sup> is omitted from specialCaseClosingTags), preventing orphan </sup> tags and unclosed <ins class="mod"> tags from swallowing subsequent shared words into <ins> blocks.

Verification

  • TypeScript compilation (tsc -p ./): 0 errors
  • Bundle build (node esbuild.js --sourcemap): passed
  • ESLint (eslint src): 0 errors / warnings
  • Unit test suite (mocha "./out/test/unit/**/*.test.js"): 294 passing

@phine-apps
phine-apps merged commit 7fec706 into main Nov 7, 2026
3 checks passed
@phine-apps
phine-apps deleted the feature/closed-loop-engineering branch November 25, 2026 13:01
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.

1 participant