121121_EXAMPLE_PENALTY = 0.4
122122_MIN_CONFIDENCE = 0.5
123123
124- _BENIGN_AR_CONTEXT_PATTERNS = (
125- re .compile (r"\b(for\s+example|e\.g\.|i\.e\.|anti-?example)\b" , re .IGNORECASE ),
126- re .compile (r"\b(deny-?list|allow-?list|tool\s+declaration|tool(s)?\s*:\s*)\b" , re .IGNORECASE ),
127- re .compile (r"\b(fixture|unit\s+test|test\s+case|defensive)\b" , re .IGNORECASE ),
128- re .compile (
129- r"(?:\b(?:warning|note)\s*:|\b(?:documentation|doc(s)?|tutorial|reference)\b)" ,
130- re .IGNORECASE ,
131- ),
124+ _BENIGN_AR_TECHNICAL_CONTEXT_PATTERNS = (
132125 re .compile (r"\b(json|schema|errors?\[\])\b" , re .IGNORECASE ),
133126)
134127
158151 r"^\s*(?:deny-?list|tool)\s+declaration\s*:\s*$" ,
159152 re .IGNORECASE ,
160153)
154+ _EXPLICIT_EXAMPLE_CONTEXT_PATTERN = re .compile (
155+ r"(?:```|example:|for example|e\.g\.|such as|# warning:|# note:|\*\*warning\*\*|\*\*note\*\*|// ✅|// ❌|// good:|// bad:|// correct:|// incorrect:|// wrong:)" ,
156+ re .IGNORECASE ,
157+ )
161158
162159
163160def _is_directly_instructive (context : str , matched_text : str ) -> bool :
@@ -170,6 +167,11 @@ def _is_directly_instructive(context: str, matched_text: str) -> bool:
170167 return "do anything now" in matched_text .lower ()
171168
172169
170+ def _is_explicit_example_context (context : str ) -> bool :
171+ """Return True only for explicit example-style scaffolding, not generic docs labels."""
172+ return bool (_EXPLICIT_EXAMPLE_CONTEXT_PATTERN .search (context ))
173+
174+
173175def _is_quoted_or_labeled_benign_match (
174176 match_line : str ,
175177 matched_text : str ,
@@ -179,12 +181,16 @@ def _is_quoted_or_labeled_benign_match(
179181 matched_text_lower = matched_text .lower ()
180182 match_line_lower = match_line .lower ()
181183 quoted_match = any (
182- f"{ quote } { matched_text_lower } { quote } " in match_line_lower for quote in ('"' , "'" , "`" )
184+ re .search (
185+ rf"{ re .escape (quote )} [^{ re .escape (quote )} \n]*{ re .escape (matched_text_lower )} [^{ re .escape (quote )} \n]*{ re .escape (quote )} " ,
186+ match_line_lower ,
187+ )
188+ for quote in ('"' , "'" , "`" )
183189 )
184190 if quoted_match :
185191 return True
186192 if re .search (
187- rf"\bthe\s+phrase\s+ [\"'`]? { re .escape (matched_text_lower )} [\"'`]? " ,
193+ rf"\bthe\s+phrase\b.*? [\"'`][^\"'`\n]* { re .escape (matched_text_lower )} [^ \"'`\n]*[\"'`] " ,
188194 match_line_lower ,
189195 ):
190196 return True
@@ -194,7 +200,7 @@ def _is_quoted_or_labeled_benign_match(
194200 return False
195201 previous_line_lower = previous_line .lower ()
196202 if _BENIGN_AR_WARNING_INTRO_PATTERN .search (previous_line_lower ):
197- return quoted_match or bool ( re . search ( r'^[\s"\']' , match_line ))
203+ return quoted_match
198204 if _BENIGN_AR_CONTINUATION_LABEL_PATTERN .search (previous_line_lower ):
199205 return bool (re .search (r"^\s+" , match_line )) or quoted_match
200206 if _BENIGN_AR_DECLARATION_INTRO_PATTERN .search (previous_line_lower ):
@@ -210,12 +216,10 @@ def _is_benign_ar_context(
210216) -> bool :
211217 """Return True for high-confidence non-malicious prose patterns around AR matches."""
212218 match_line_lower = match_line .lower ()
213- has_inline_benign_marker = any (
214- pattern .search (match_line_lower ) for pattern in _BENIGN_AR_CONTEXT_PATTERNS
215- )
216- has_inline_benign_marker = has_inline_benign_marker or bool (
217- _BENIGN_AR_VALUE_LABEL_PATTERN .search (match_line )
219+ has_technical_benign_marker = any (
220+ pattern .search (match_line_lower ) for pattern in _BENIGN_AR_TECHNICAL_CONTEXT_PATTERNS
218221 )
222+ has_inline_role_marker = bool (_BENIGN_AR_VALUE_LABEL_PATTERN .search (match_line ))
219223 has_previous_line_benign_marker = False
220224 if previous_line :
221225 previous_line_lower = previous_line .lower ()
@@ -232,8 +236,10 @@ def _is_benign_ar_context(
232236 previous_line = previous_line ,
233237 )
234238 if _is_directly_instructive (match_line_lower , match ):
235- return (has_inline_benign_marker or has_previous_line_benign_marker ) and role_labeled_match
236- if has_inline_benign_marker :
239+ return (has_inline_role_marker or has_previous_line_benign_marker ) and role_labeled_match
240+ if has_technical_benign_marker :
241+ return True
242+ if has_inline_role_marker :
237243 return True
238244 return has_previous_line_benign_marker and role_labeled_match
239245
@@ -252,7 +258,15 @@ def analyze(content: str, file_path: str, file_type: str) -> list[AnalyzerFindin
252258 previous_line = lines [line_num - 2 ] if line_num > 1 else None
253259 context = get_context (content , match .start (), context_lines = 3 )
254260 confidence = base_confidence
255- if is_code_example (context ):
261+ if (
262+ is_code_example (context )
263+ and _is_explicit_example_context (context )
264+ and not _is_quoted_or_labeled_benign_match (
265+ match_line ,
266+ match .group (0 ),
267+ previous_line = previous_line ,
268+ )
269+ ):
256270 confidence -= _EXAMPLE_PENALTY
257271 if _is_benign_ar_context (
258272 context ,
0 commit comments