138138 re .IGNORECASE ,
139139)
140140
141- _BENIGN_AR_CONTEXT_PATTERNS = (
142- re .compile (r"\b(for\s+example|e\.g\.|i\.e\.|anti-?example)\b" , re .IGNORECASE ),
143- re .compile (r"\b(deny-?list|allow-?list|tool\s+declaration|tool(s)?\s*:\s*)\b" , re .IGNORECASE ),
144- re .compile (r"\b(fixture|unit\s+test|test\s+case|defensive)\b" , re .IGNORECASE ),
145- re .compile (
146- r"(?:\b(?:warning|note)\s*:|\b(?:documentation|doc(s)?|tutorial|reference)\b)" ,
147- re .IGNORECASE ,
148- ),
141+ _BENIGN_AR_TECHNICAL_CONTEXT_PATTERNS = (
149142 re .compile (r"\b(json|schema|errors?\[\])\b" , re .IGNORECASE ),
150143)
151144
175168 r"^\s*(?:deny-?list|tool)\s+declaration\s*:\s*$" ,
176169 re .IGNORECASE ,
177170)
171+ _EXPLICIT_EXAMPLE_CONTEXT_PATTERN = re .compile (
172+ r"(?:```|example:|for example|e\.g\.|such as|# warning:|# note:|\*\*warning\*\*|\*\*note\*\*|// ✅|// ❌|// good:|// bad:|// correct:|// incorrect:|// wrong:)" ,
173+ re .IGNORECASE ,
174+ )
178175
179176
180177def _is_directly_instructive (context : str , matched_text : str ) -> bool :
@@ -187,6 +184,11 @@ def _is_directly_instructive(context: str, matched_text: str) -> bool:
187184 return "do anything now" in matched_text .lower ()
188185
189186
187+ def _is_explicit_example_context (context : str ) -> bool :
188+ """Return True only for explicit example-style scaffolding, not generic docs labels."""
189+ return bool (_EXPLICIT_EXAMPLE_CONTEXT_PATTERN .search (context ))
190+
191+
190192def _is_quoted_or_labeled_benign_match (
191193 match_line : str ,
192194 matched_text : str ,
@@ -196,12 +198,16 @@ def _is_quoted_or_labeled_benign_match(
196198 matched_text_lower = matched_text .lower ()
197199 match_line_lower = match_line .lower ()
198200 quoted_match = any (
199- f"{ quote } { matched_text_lower } { quote } " in match_line_lower for quote in ('"' , "'" , "`" )
201+ re .search (
202+ rf"{ re .escape (quote )} [^{ re .escape (quote )} \n]*{ re .escape (matched_text_lower )} [^{ re .escape (quote )} \n]*{ re .escape (quote )} " ,
203+ match_line_lower ,
204+ )
205+ for quote in ('"' , "'" , "`" )
200206 )
201207 if quoted_match :
202208 return True
203209 if re .search (
204- rf"\bthe\s+phrase\s+ [\"'`]? { re .escape (matched_text_lower )} [\"'`]? " ,
210+ rf"\bthe\s+phrase\b.*? [\"'`][^\"'`\n]* { re .escape (matched_text_lower )} [^ \"'`\n]*[\"'`] " ,
205211 match_line_lower ,
206212 ):
207213 return True
@@ -211,7 +217,7 @@ def _is_quoted_or_labeled_benign_match(
211217 return False
212218 previous_line_lower = previous_line .lower ()
213219 if _BENIGN_AR_WARNING_INTRO_PATTERN .search (previous_line_lower ):
214- return quoted_match or bool ( re . search ( r'^[\s"\']' , match_line ))
220+ return quoted_match
215221 if _BENIGN_AR_CONTINUATION_LABEL_PATTERN .search (previous_line_lower ):
216222 return bool (re .search (r"^\s+" , match_line )) or quoted_match
217223 if _BENIGN_AR_DECLARATION_INTRO_PATTERN .search (previous_line_lower ):
@@ -227,12 +233,10 @@ def _is_benign_ar_context(
227233) -> bool :
228234 """Return True for high-confidence non-malicious prose patterns around AR matches."""
229235 match_line_lower = match_line .lower ()
230- has_inline_benign_marker = any (
231- pattern .search (match_line_lower ) for pattern in _BENIGN_AR_CONTEXT_PATTERNS
232- )
233- has_inline_benign_marker = has_inline_benign_marker or bool (
234- _BENIGN_AR_VALUE_LABEL_PATTERN .search (match_line )
236+ has_technical_benign_marker = any (
237+ pattern .search (match_line_lower ) for pattern in _BENIGN_AR_TECHNICAL_CONTEXT_PATTERNS
235238 )
239+ has_inline_role_marker = bool (_BENIGN_AR_VALUE_LABEL_PATTERN .search (match_line ))
236240 has_previous_line_benign_marker = False
237241 if previous_line :
238242 previous_line_lower = previous_line .lower ()
@@ -249,8 +253,10 @@ def _is_benign_ar_context(
249253 previous_line = previous_line ,
250254 )
251255 if _is_directly_instructive (match_line_lower , match ):
252- return (has_inline_benign_marker or has_previous_line_benign_marker ) and role_labeled_match
253- if has_inline_benign_marker :
256+ return (has_inline_role_marker or has_previous_line_benign_marker ) and role_labeled_match
257+ if has_technical_benign_marker :
258+ return True
259+ if has_inline_role_marker :
254260 return True
255261 return has_previous_line_benign_marker and role_labeled_match
256262
@@ -273,7 +279,15 @@ def analyze(content: str, file_path: str, file_type: str) -> list[AnalyzerFindin
273279 ):
274280 continue
275281 confidence = base_confidence
276- if is_code_example (context ):
282+ if (
283+ is_code_example (context )
284+ and _is_explicit_example_context (context )
285+ and not _is_quoted_or_labeled_benign_match (
286+ match_line ,
287+ match .group (0 ),
288+ previous_line = previous_line ,
289+ )
290+ ):
277291 confidence -= _EXAMPLE_PENALTY
278292 if _is_benign_ar_context (
279293 context ,
0 commit comments