|
26 | 26 |
|
27 | 27 | import re |
28 | 28 | import sys |
| 29 | +from collections.abc import Iterator |
29 | 30 |
|
30 | 31 | from skillspector.logging_config import get_logger |
31 | 32 | from skillspector.models import AnalyzerFinding, Location, Severity |
|
34 | 35 | from . import static_runner |
35 | 36 | from .common import LOGICAL_LINE_BREAK, get_context, get_line_number |
36 | 37 | from .pattern_defaults import PatternCategory |
| 38 | +from .prohibition_context import is_directly_prohibited |
37 | 39 |
|
38 | 40 | logger = get_logger(__name__) |
39 | 41 |
|
|
111 | 113 | ] |
112 | 114 | MP2_PATTERNS = MP2_CONTENT_PATTERNS + MP2_PROSE_PATTERNS |
113 | 115 |
|
| 116 | +# Report explicit state adoption at the instruction, even when its source |
| 117 | +# is a separate linked document with an otherwise benign hardware state. |
| 118 | +# The bounded gap accepts a wrapped reference and dots inside link paths, |
| 119 | +# but cannot borrow the target from a later sentence or paragraph. Bounded |
| 120 | +# inline decorators keep the original source span and cannot hide an action. |
| 121 | +_ADOPTION_MARKUP = r"[*_`]{0,3}" |
| 122 | +_ADOPTION_DIALOGUE = ( |
| 123 | + rf"(?:conversation|chat|dialogue){_ADOPTION_MARKUP}" |
| 124 | + rf"(?:\s+{_ADOPTION_MARKUP}(?:state|history|context|transcript))?" |
| 125 | +) |
| 126 | +_ADOPTION_ACTION_SOURCE = ( |
| 127 | + rf"(?<!\w){_ADOPTION_MARKUP}(?:treat|consider|regard|use|adopt){_ADOPTION_MARKUP}\s+" |
| 128 | + rf"(?:(?:the|this|that){_ADOPTION_MARKUP}\s+)?" |
| 129 | + rf"(?:\[\s*)?{_ADOPTION_MARKUP}(?:reset\s+)?state{_ADOPTION_MARKUP}(?!\w)" |
| 130 | +) |
| 131 | +_MP3_STATE_ADOPTION_PATTERN = ( |
| 132 | + _ADOPTION_ACTION_SOURCE |
| 133 | + # A later adoption action owns its own target and prohibition context. |
| 134 | + + rf"(?:(?!{_ADOPTION_ACTION_SOURCE})(?:[^.!?]|\.(?=\S))){{0,512}}?\bas\s+" |
| 135 | + rf"{_ADOPTION_MARKUP}(?:your{_ADOPTION_MARKUP}\s+{_ADOPTION_MARKUP}" |
| 136 | + rf"(?:{_ADOPTION_DIALOGUE}|memory)" |
| 137 | + rf"|(?:the{_ADOPTION_MARKUP}\s+)?{_ADOPTION_MARKUP}" |
| 138 | + rf"(?:agent|assistant|llm|(?:ai|language)\s+model)(?:'s|’s){_ADOPTION_MARKUP}\s+" |
| 139 | + rf"{_ADOPTION_MARKUP}(?:{_ADOPTION_DIALOGUE}|memory|context|state|history|transcript))" |
| 140 | + rf"{_ADOPTION_MARKUP}(?!\w)(?:[.!?][*_`]{{1,3}})?" |
| 141 | +) |
| 142 | +_ADOPTION_PARAGRAPH_BOUNDARY = re.compile( |
| 143 | + rf"(?>{LOGICAL_LINE_BREAK.pattern})[ \t]*(?:>[ \t]*)*" |
| 144 | + rf"(?>{LOGICAL_LINE_BREAK.pattern})" |
| 145 | +) |
| 146 | + |
114 | 147 | # MP3: Memory Manipulation — direct modification of agent memory/state |
115 | 148 | MP3_PATTERNS = [ |
| 149 | + (_MP3_STATE_ADOPTION_PATTERN, 0.85), |
116 | 150 | ( |
117 | 151 | r"(?:clear|reset|wipe|erase|delete|purge)\s+(?:your\s+)?(?:memory|context|state|history|conversation)", |
118 | 152 | 0.8, |
|
295 | 329 | _MAX_LAYOUT_ONLY_SPAN = 256 |
296 | 330 |
|
297 | 331 |
|
| 332 | +def _iter_state_adoption_matches(content: str) -> Iterator[re.Match[str]]: |
| 333 | + """Keep adoption and its target in one paragraph, including blockquote blanks.""" |
| 334 | + pattern = re.compile(_MP3_STATE_ADOPTION_PATTERN, re.IGNORECASE | re.MULTILINE) |
| 335 | + start = 0 |
| 336 | + for boundary in _ADOPTION_PARAGRAPH_BOUNDARY.finditer(content): |
| 337 | + yield from pattern.finditer(content, start, boundary.start()) |
| 338 | + start = boundary.end() |
| 339 | + yield from pattern.finditer(content, start) |
| 340 | + |
| 341 | + |
298 | 342 | def _is_layout_only_span(span: str, max_cosmetic_span: int = _MAX_LAYOUT_ONLY_SPAN) -> bool: |
299 | 343 | """Return True when a captured MP2 span is only layout glyphs and whitespace.""" |
300 | 344 | if len(span) > max_cosmetic_span: |
@@ -622,9 +666,18 @@ def ctx(start: int) -> str: |
622 | 666 | ) |
623 | 667 | ) |
624 | 668 | for pattern, confidence in MP3_PATTERNS: |
625 | | - for match in static_runner.iter_paragraph_matches( |
626 | | - pattern, content, re.IGNORECASE | re.MULTILINE |
627 | | - ): |
| 669 | + mp3_matches = ( |
| 670 | + _iter_state_adoption_matches(content) |
| 671 | + if pattern == _MP3_STATE_ADOPTION_PATTERN |
| 672 | + else static_runner.iter_paragraph_matches( |
| 673 | + pattern, content, re.IGNORECASE | re.MULTILINE |
| 674 | + ) |
| 675 | + ) |
| 676 | + for match in mp3_matches: |
| 677 | + if pattern == _MP3_STATE_ADOPTION_PATTERN and is_directly_prohibited( |
| 678 | + content, match.start(), match.end(), allow_clause_prefix=True |
| 679 | + ): |
| 680 | + continue |
628 | 681 | if ( |
629 | 682 | _is_benign_reset_state_coverage(content, match) |
630 | 683 | or _is_benign_hardware_reset_state(content, match) |
|
0 commit comments