Skip to content
Merged
314 changes: 312 additions & 2 deletions .claude/skills/create-issue/scripts/prose_boundary_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,305 @@
BLOCK_KIND_URL_OR_IDENTIFIER,
})

# ---------------------------------------------------------------------------
# heading_policy inventory(#654 追加データ)
#
# 各 entry は以下のフィールドを持つ:
# canonical_en : 英語見出し名(## の後のテキスト)
# canonical_ja : 標準的な日本語訳(空文字 = 翻訳なし)
# accepted_forms : 許容される見出し形式のリスト(normalize 後マッチ)
# prose_guard_kind : validate_japanese_content が使う block_kind
# contract_checker_kind : check_issue_contract が section 抽出に使う key
#
# この inventory を追加しても classify_block() 公開 API と既存 block_kind
# 定数の意味は変更しない(AC1 固定契約)。
# ---------------------------------------------------------------------------


def _normalize_heading_text(text: str) -> str:
"""
GFM ATX heading のテキスト部分を正規化する。

- 先頭・末尾の空白を除去
- 末尾の closing `#` を除去(GFM 仕様: 末尾 # はオプション)
- 括弧内テキストを正規化(半角・全角括弧を統一して比較用に保持)
- 正規化後のテキストを返す
"""
t = text.strip()
# 末尾 closing # を除去(GFM: heading text の末尾スペース + # は無視)
t = re.sub(r'\s+#+\s*$', '', t).strip()
return t


# heading_policy inventory
# key = canonical_en(正規化後の英語見出し名)
HEADING_POLICY: dict[str, dict] = {
"Machine-Readable Contract": {
"canonical_en": "Machine-Readable Contract",
"canonical_ja": "機械可読コントラクト",
"accepted_forms": [
"Machine-Readable Contract",
"機械可読コントラクト (Machine-Readable Contract)",
"機械可読コントラクト(Machine-Readable Contract)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Machine-Readable Contract",
},
"Parent Issue": {
"canonical_en": "Parent Issue",
"canonical_ja": "親 Issue",
"accepted_forms": [
"Parent Issue",
"親 Issue (Parent Issue)",
"親 Issue(Parent Issue)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Parent Issue",
},
"Parent Goal Ref": {
"canonical_en": "Parent Goal Ref",
"canonical_ja": "親ゴール参照",
"accepted_forms": [
"Parent Goal Ref",
"親ゴール参照 (Parent Goal Ref)",
"親ゴール参照(Parent Goal Ref)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Parent Goal Ref",
},
"Current Validated Scope": {
"canonical_en": "Current Validated Scope",
"canonical_ja": "現在の検証済みスコープ",
"accepted_forms": [
"Current Validated Scope",
"現在の検証済みスコープ (Current Validated Scope)",
"現在の検証済みスコープ(Current Validated Scope)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Current Validated Scope",
},
"Remaining Parent Gaps": {
"canonical_en": "Remaining Parent Gaps",
"canonical_ja": "残存ギャップ",
"accepted_forms": [
"Remaining Parent Gaps",
"残存ギャップ (Remaining Parent Gaps)",
"残存ギャップ(Remaining Parent Gaps)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Remaining Parent Gaps",
},
"Outcome": {
"canonical_en": "Outcome",
"canonical_ja": "成果物",
"accepted_forms": [
"Outcome",
"成果物 (Outcome)",
"成果物(Outcome)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Outcome",
},
"Background": {
"canonical_en": "Background",
"canonical_ja": "背景",
"accepted_forms": [
"Background",
"背景 (Background)",
"背景(Background)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Background",
},
"In Scope": {
"canonical_en": "In Scope",
"canonical_ja": "スコープ内",
"accepted_forms": [
"In Scope",
"スコープ内 (In Scope)",
"スコープ内(In Scope)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "In Scope",
},
"Out of Scope": {
"canonical_en": "Out of Scope",
"canonical_ja": "スコープ外",
"accepted_forms": [
"Out of Scope",
"スコープ外 (Out of Scope)",
"スコープ外(Out of Scope)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Out of Scope",
},
"Acceptance Criteria": {
"canonical_en": "Acceptance Criteria",
"canonical_ja": "受け入れ条件",
"accepted_forms": [
"Acceptance Criteria",
"受け入れ条件 (Acceptance Criteria)",
"受け入れ条件(Acceptance Criteria)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Acceptance Criteria",
},
"Verification Commands": {
"canonical_en": "Verification Commands",
"canonical_ja": "検証コマンド",
"accepted_forms": [
"Verification Commands",
"検証コマンド (Verification Commands)",
"検証コマンド(Verification Commands)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Verification Commands",
},
"Allowed Paths": {
"canonical_en": "Allowed Paths",
"canonical_ja": "許可パス",
"accepted_forms": [
"Allowed Paths",
"許可パス (Allowed Paths)",
"許可パス(Allowed Paths)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Allowed Paths",
},
"Stop Conditions": {
"canonical_en": "Stop Conditions",
"canonical_ja": "停止条件",
"accepted_forms": [
"Stop Conditions",
"停止条件 (Stop Conditions)",
"停止条件(Stop Conditions)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Stop Conditions",
},
"Required Skills": {
"canonical_en": "Required Skills",
"canonical_ja": "必要スキル",
"accepted_forms": [
"Required Skills",
"必要スキル (Required Skills)",
"必要スキル(Required Skills)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Required Skills",
},
# Scope Delta は implementation template の任意セクション(required: false)。
# GitHub template の label は "Scope Delta(任意)" だが、
# Issue 本文中の heading テキストは "Scope Delta(任意)" または "Scope Delta" として現れる。
# 両形式を accepted_forms に含め prose ratio 判定から除外する。
"Scope Delta": {
"canonical_en": "Scope Delta",
"canonical_ja": "スコープ差分",
"accepted_forms": [
"Scope Delta",
"Scope Delta(任意)",
"スコープ差分 (Scope Delta)",
"スコープ差分(Scope Delta)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Scope Delta",
},
"Runtime Verification Applicability": {
"canonical_en": "Runtime Verification Applicability",
"canonical_ja": "実行時検証適用性",
"accepted_forms": [
"Runtime Verification Applicability",
"実行時検証適用性 (Runtime Verification Applicability)",
"実行時検証適用性(Runtime Verification Applicability)",
],
"prose_guard_kind": BLOCK_KIND_CANONICAL_HEADING,
"contract_checker_kind": "Runtime Verification Applicability",
},
}

# canonical_en のセット(高速ルックアップ用)
_CANONICAL_HEADING_KEYS: frozenset[str] = frozenset(HEADING_POLICY.keys())


def lookup_heading_policy(heading_text: str) -> dict | None:
"""
heading text(## より後のテキスト)から heading_policy entry を引く。

B2 fix (#654): accepted_forms の exact normalized match のみで accept する。
任意 prefix + (CanonicalEnglish) の括弧内キー単独一致では accept しない。
これにより「適当な日本語 (Outcome)」「成果物ではない(Outcome)」等が
誤って Outcome として HIT する問題を修正する。

手順:
1. _normalize_heading_text() で正規化
2. canonical_en(= HEADING_POLICY key)との直接一致を確認
3. accepted_forms(正規化後)との exact match を確認

Returns:
HEADING_POLICY entry dict、または None(inventory に存在しない場合)
"""
normalized = _normalize_heading_text(heading_text)

# 直接一致(canonical_en = HEADING_POLICY key)
if normalized in HEADING_POLICY:
return HEADING_POLICY[normalized]

# accepted_forms との exact normalized match のみ(B2 fix: bilingual key bypass 除去)
for entry in HEADING_POLICY.values():
if any(_normalize_heading_text(form) == normalized for form in entry["accepted_forms"]):
return entry

return None


# ---------------------------------------------------------------------------
# GFM ATX heading parser(B2: #654)
# ---------------------------------------------------------------------------

# GFM ATX heading: 0-3 leading spaces, #{1,6}, at least one space, heading text
# trailing "空白 + #{1,} + trailing spaces" is optional closing sequence
_ATX_HEADING_RE = re.compile(
r'^( {0,3})(#{1,6})(?:\s+(.+?))?(?:\s+#+\s*)?$'
)


def parse_atx_heading_line(line: str) -> dict | None:
"""
1 行を GFM ATX heading として解析し、結果を返す。

GFM ATX heading 仕様(CommonMark §4.2):
- 先頭 0〜3 スペースの indent が許容される(4 スペース以上は code block)
- marker は #{1,6}
- marker の後に少なくとも 1 つの空白、またはそれ以降が空(空見出し)
- 末尾の「空白 + #{1,} + trailing spaces」は closing sequence として除去
- closing sequence は heading text の一部として扱わない

Returns:
dict with keys:
'level': int (1-6)
'text': str (正規化後の heading text)
または None(ATX heading でない場合)
"""
m = _ATX_HEADING_RE.match(line)
if not m:
return None
indent = m.group(1) or ''
# 4 spaces 以上は code block(code block ではない ATX heading ではない)
# ここでは 0-3 spaces のみ許可
if len(indent) >= 4:
return None
level = len(m.group(2))
raw_text = m.group(3) or ''
# closing sequence 除去(末尾の空白 + # + 空白)
text = re.sub(r'\s+#+\s*$', '', raw_text).strip()
return {'level': level, 'text': text}


# ---------------------------------------------------------------------------
# 内部正規表現
# ---------------------------------------------------------------------------

# Markdown 見出し
# Markdown 見出し(後方互換用; classify_block 内では parse_atx_heading_line を使用)
_HEADING_RE = re.compile(r'^(#{1,6})\s+(.+)$')

# 日本語文字(ひらがな・カタカナ・CJK)
Expand Down Expand Up @@ -326,6 +620,18 @@ def classify_block(block: str) -> str:
"""
Markdown ブロックの block_kind を返す。

**注意: この関数は構文分類のみを行う。prose 除外可否(guard 判定)には使わないこと。**

英語 ATX 見出し(例: ``## Outcome Risks``)は構文上 ``BLOCK_KIND_CANONICAL_HEADING``
として分類されるが、これは prose ratio 判定から除外してよいという意味ではない。
prose 除外可否(guard 判定)は heading_policy(``lookup_heading_policy`` /
``_is_heading_block``)に登録された見出しのみ行われる。

**「classify_block(...) == BLOCK_KIND_CANONICAL_HEADING だから prose 除外してよい」
という判定は誤り。** prose 除外が必要な consumer は必ず
``validate_japanese_content._is_heading_block()``(または同等の
``lookup_heading_policy()`` 参照)を経由すること。

Args:
block: 分類対象の Markdown ブロック文字列(前後空白はあってもよい)

Expand All @@ -349,8 +655,12 @@ def classify_block(block: str) -> str:
return BLOCK_KIND_CODE_FENCE

# -----------------------------------------------------------------------
# 見出しチェック
# 見出しチェック(classify_block 公開 API; AC1 維持)
# -----------------------------------------------------------------------
# NOTE: classify_block() の heading 分類ロジック(ATX 形式 → canonical/bilingual)は
# AC1 により変更しない。heading_policy の SSOT 参照による非 canonical 見出しの
# prose 残留判定は _is_heading_block()(validate_japanese_content.py)で行う(B1_B4)。
# classify_block() での heading_policy 参照は行わない(旧動作維持)。
if len(lines) == 1 or (len(lines) >= 1 and _HEADING_RE.match(lines[0])):
# 見出し行のみ、または先頭行が見出し
heading_line = lines[0]
Expand Down
Loading
Loading