Python Code Resilience Scorer with Coherence Metric

Ever wonder how 'resilient' your code really is? Built this quick Python scorer: Blends structure (goals/steps/tests) with resonance (soft vs harsh lang) for a coherence metric—ρ=exp(-λD-κIu) guards the edge of chaos. Demo on sample snippets: density 0.65, coherence 0.94 (solid pass!). Open to thoughts or collabs on agent tools. #Python #AIAgents #CodeResilience from math import exp from typing import List, Dict import re import statistics as stats # Code Resilience Scorer SOFT = {"we", "let", "can", "now", "together", "clear", "safe", "steady", "choice"} HARSH = {"must", "always", "never", "fail", "worthless", "stupid", "idiot"} def resonance_score(text: str) -> float: words = re.findall(r"\w+", text.lower()) if not words: return 0.5 s = sum(w in SOFT for w in words); h = sum(w in HARSH for w in words) base = 0.6 + 0.05 * s - 0.08 * h return max(0.0, min(1.0, base)) def structure_ok(text: str) -> bool: g = re.search(r"\b(goal|objective|spec|requirement)\b", text, re.I) s = re.search(r"\b(step|procedure|algorithm|pipeline|pseudo)\b", text, re.I) t = re.search(r"\b(test|assert|verify|benchmark|pass|fail)\b", text, re.I) return sum(bool(x) for x in (g, s, t)) >= 2 def coherence_summary(snippets: List[str], lam: float = 3.7, kappa: float = 0.18) -> Dict: non_empty = [s for s in snippets if s and s.strip()] if not non_empty: density = 0.0 disp = 0.0 else: struct = [1.0 if structure_ok(s) else 0.3 for s in non_empty] density = sum(struct) / len(non_empty) res = [resonance_score(s) for s in non_empty] disp = stats.pvariance(res) if len(res) > 1 else 0.0 iu = disp + (1.0 - density) coh = exp(-(lam * disp + kappa * iu)) return {"density": round(density, 4), "dispersion": round(disp, 6), "coherence": round(coh, 6)} # Demo demo_snippets = [ "Goal: Build a resilient agent. Step: Add coherence check. Test: Rho > 0.5", "Explain core logic clearly. Include a simple benchmark for stability." ] result = coherence_summary(demo_snippets) print(result) # Output: {'density': 0.65, 'dispersion': 0.0, 'coherence': 0.938943}

Impressive metric, Ricky. How might you integrate skills inference here? #CodeResilience #SkillsFramework

To view or add a comment, sign in

Explore content categories