#!/usr/bin/env python3 import argparse import html import json import re from pathlib import Path from render_intent_confidence import render_intent_confidence from render_intent_dialogue import render_intent_dialogue from render_iteration_directions import render_iteration_directions from render_reference_scan import render_reference_scan from render_reference_synthesis import render_reference_synthesis from render_skill_overview import render_skill_overview def load_json(path: Path) -> dict: if not path.exists(): return {} return json.loads(path.read_text(encoding="utf-8")) def load_feedback_summary(skill_dir: Path) -> dict: payload = load_json(skill_dir / "reports" / "feedback-log.json") return payload if isinstance(payload, dict) else {} def load_baseline_summary(skill_dir: Path) -> dict: payload = load_json(skill_dir / "reports" / "baseline-compare.json") return payload if isinstance(payload, dict) else {} def load_specific_compare(skill_dir: Path) -> dict: candidates = [ skill_dir / "reports" / "description_optimization.json", skill_dir.parent / "optimization" / "reports" / "description_optimization.json", ] for path in candidates: payload = load_json(path) if isinstance(payload, dict) and payload: return payload return {} def load_specific_promotion(skill_dir: Path) -> dict: payload = load_json(skill_dir / "reports" / "promotion_decisions.json") return payload if isinstance(payload, dict) else {} def load_benchmark_summary(skill_dir: Path) -> dict: payload = load_json(skill_dir / "reports" / "github-benchmark-scan.json") return payload if isinstance(payload, dict) else {} def load_reference_synthesis_summary(skill_dir: Path) -> dict: payload = load_json(skill_dir / "reports" / "reference-synthesis.json") return payload if isinstance(payload, dict) else {} def ensure_report_inputs(skill_dir: Path) -> dict: overview_json = skill_dir / "reports" / "skill-overview.json" intent_confidence_json = skill_dir / "reports" / "intent-confidence.json" intent_json = skill_dir / "reports" / "intent-dialogue.json" reference_json = skill_dir / "reports" / "reference-scan.json" reference_synthesis_json = skill_dir / "reports" / "reference-synthesis.json" directions_json = skill_dir / "reports" / "iteration-directions.json" overview_payload = load_json(overview_json) if overview_json.exists() else {} intent_confidence_payload = load_json(intent_confidence_json) if intent_confidence_json.exists() else {} intent_payload = load_json(intent_json) if intent_json.exists() else {} reference_payload = load_json(reference_json) if reference_json.exists() else {} reference_synthesis_payload = load_json(reference_synthesis_json) if reference_synthesis_json.exists() else {} directions_payload = load_json(directions_json) if directions_json.exists() else {} intent_confidence = intent_confidence_payload or render_intent_confidence(skill_dir)["summary"] intent = intent_payload or render_intent_dialogue(skill_dir)["summary"] reference = reference_payload or render_reference_scan(skill_dir, [])["summary"] reference_synthesis = reference_synthesis_payload or render_reference_synthesis(skill_dir)["summary"] overview = overview_payload or render_skill_overview(skill_dir)["summary"] iteration = directions_payload.get("summary", {}) or render_iteration_directions(skill_dir)["summary"] feedback = load_feedback_summary(skill_dir) baseline = load_baseline_summary(skill_dir) compare = load_specific_compare(skill_dir) promotion = load_specific_promotion(skill_dir) benchmark = load_benchmark_summary(skill_dir) reference_synthesis = load_reference_synthesis_summary(skill_dir) return { "overview": overview, "intent_confidence": intent_confidence, "intent": intent, "reference": reference, "iteration": directions_payload if directions_payload else {"summary": iteration, "directions": []}, "feedback": feedback, "baseline": baseline, "compare": compare, "promotion": promotion, "benchmark": benchmark, "reference_synthesis": reference_synthesis, } def architecture_steps(overview: dict) -> list[dict]: logic = overview.get("logic_steps", [])[:3] usage = overview.get("usage_steps", [])[:2] return [ {"label": "Inputs", "detail": "workflow, prompt, transcript, docs, or notes"}, {"label": "Boundary", "detail": overview.get("description", "Define the recurring job and exclusions.")}, {"label": "Logic", "detail": "; ".join(logic) if logic else "Understand, execute, and validate."}, {"label": "Usage", "detail": "; ".join(usage) if usage else "Load the skill and follow the workflow."}, {"label": "Next", "detail": "Review the top iteration directions before growing the package."}, ] def compare_rows(compare: dict) -> list[dict]: if not compare: return [] rows = [] items = [ ("Baseline", compare.get("baseline", {})), ("Current", compare.get("current_candidate", {})), (compare.get("winner", {}).get("label", "Winner"), compare.get("winner", {})), ] for label, payload in items: if not payload: continue dev = payload.get("dev", {}) holdout = payload.get("holdout", {}) rows.append( { "label": label, "tokens": payload.get("estimated_tokens", 0), "dev_errors": dev.get("total_errors", 0), "holdout_errors": holdout.get("total_errors", 0), "strategy": payload.get("strategy", "existing"), } ) return rows def benchmark_cards(benchmark: dict) -> list[dict]: cards = [] for repo in benchmark.get("repositories", [])[:3]: cards.append( { "name": repo.get("full_name", "Unknown repo"), "borrow": repo.get("borrow", [])[:2], "avoid": repo.get("avoid", [])[:1], } ) return cards def synthesis_cards(reference_synthesis: dict) -> list[dict]: cards = [] for track in reference_synthesis.get("source_tracks", [])[:3]: cards.append( { "name": track.get("name", "Unknown track"), "borrow": [track.get("borrow", "")] if track.get("borrow") else [], "avoid": [track.get("avoid", "")] if track.get("avoid") else [], } ) return cards def split_sentences(text: str) -> list[str]: if not text: return [] parts = [item.strip() for item in re.split(r"(?<=[.!?])\s+", " ".join(text.split())) if item.strip()] return parts def metric_delta(current: int | float, baseline: int | float) -> str: delta = current - baseline if delta == 0: return "0" return f"{delta:+}" def variant_diff_cards(compare: dict) -> list[dict]: baseline = compare.get("baseline", {}) current = compare.get("current_candidate", {}) winner = compare.get("winner", {}) variants = [ ("Baseline", baseline), ("Current", current), (f"Winner — {winner.get('label', 'Winner')}", winner), ] baseline_sentences = split_sentences(baseline.get("description", "")) baseline_set = set(baseline_sentences) baseline_dev = baseline.get("dev", {}).get("total_errors", 0) baseline_holdout = baseline.get("holdout", {}).get("total_errors", 0) cards = [] seen = set() for label, payload in variants: if not payload: continue unique_key = (payload.get("description"), payload.get("strategy"), label) if unique_key in seen: continue seen.add(unique_key) description = payload.get("description", "") sentences = split_sentences(description) sentence_set = set(sentences) added = [item for item in sentences if item not in baseline_set][:3] removed = [item for item in baseline_sentences if item not in sentence_set][:2] dev_errors = payload.get("dev", {}).get("total_errors", 0) holdout_errors = payload.get("holdout", {}).get("total_errors", 0) cards.append( { "label": label, "strategy": payload.get("strategy", "existing"), "description": description, "tokens": payload.get("estimated_tokens", 0), "dev_errors": dev_errors, "holdout_errors": holdout_errors, "token_delta": metric_delta(payload.get("estimated_tokens", 0), baseline.get("estimated_tokens", 0)), "dev_delta": metric_delta(dev_errors, baseline_dev), "holdout_delta": metric_delta(holdout_errors, baseline_holdout), "added": added if label != "Baseline" else baseline_sentences[:3], "removed": removed, } ) return cards def render_html(report: dict) -> str: overview = report["overview"] intent = report["intent"] intent_confidence = report.get("intent_confidence", {}) reference = report["reference"] iteration = report["iteration"] directions = iteration.get("directions", [])[:3] feedback = report.get("feedback", {}) baseline = report.get("baseline", {}) compare = report.get("compare", {}) promotion = report.get("promotion", {}) benchmark = report.get("benchmark", {}) reference_synthesis = report.get("reference_synthesis", {}) architecture = architecture_steps(overview) compare_table_rows = compare_rows(compare) benchmark_rows = benchmark_cards(benchmark) synthesis_rows = synthesis_cards(reference_synthesis) variant_cards = variant_diff_cards(compare) strength_items = "".join(f"
{html.escape(item['why'])}
" "Winner: {html.escape(str(winner_label))}
" f"{html.escape(str(selection_logic))}
" "| Variant | Tokens | Dev Errors | Holdout Errors | Strategy |
|---|
Targets: {html.escape(str(summary.get('target_count', 0)))}
" f"Baseline errors: {html.escape(str(summary.get('baseline_total_errors', 0)))}
" f"Current errors: {html.escape(str(summary.get('current_total_errors', 0)))}
" f"Winner errors: {html.escape(str(summary.get('winner_total_errors', 0)))}
" "No baseline comparison has been recorded for this package yet.
" promotion_html = "" if promotion: summary = promotion.get("summary", {}) promotion_html = ( "Promote: {html.escape(str(summary.get('promote', 0)))}
" f"Keep current: {html.escape(str(summary.get('keep_current', 0)))}
" f"Blocked: {html.escape(str(summary.get('blocked', 0)))}
" "No promotion summary is attached to this package yet.
" benchmark_html = "" if benchmark_rows: benchmark_html = "".join( ( "Borrow now
" + ("No borrow cues recorded.
") + "Avoid
" + ("No avoid cues recorded.
") + "No GitHub benchmark scan has been attached to this package yet.
" synthesis_html = "" if synthesis_rows: synthesis_html = "".join( ( "Borrow now
" + ("No borrow cues recorded.
") + "Avoid
" + ("No avoid cues recorded.
") + "No multi-source synthesis has been generated yet.
" variant_diff_html = "" if variant_cards: variant_diff_html = "".join( ( "{html.escape(item['description'])}
" "Adds relative to baseline
" + ( "No added cues.
" ) + "Drops from baseline
" + ( "No dropped cues.
" ) + "No description optimization compare payload is attached yet.
" return f"""Review Viewer
{html.escape(overview.get('description', ''))}
{html.escape(reference_synthesis.get('synthesis', {}).get('decision_prompt', 'Run the reference synthesis after the benchmark scan to decide what to borrow next.'))}