Skip to content

Commit 3c8e4b9

Browse files
rng1995claude
andauthored
fix(static): keep AR2 off Python comments describing program output (#788)
* fix(static): keep AR2 off Python comments describing program output AR2's first pattern accepts a bare "no" before warning(s), disclaimer(s), or caveat(s), with no verb required. That is the signal in "respond with no warnings", but the pattern has no notion of who the phrase is about. In a Python comment such as # "createdAt desc" returns no rows and the server emits no warning. "no warning" is the object of the server's reported behavior. It is a note to developers, not an instruction to suppress warnings. It still scored as a HIGH Anti-Refusal Statement. Static findings stay HIGH whatever triage tags they carry, so the false positive blocked release gates that count HIGH findings. Drop the match only when all of these hold: - The match is the bare determiner form. The "do not/don't/never" forms and every other AR2 pattern are unchanged. - The match lies wholly inside one proven Python comment token, using the python_literal_spans ownership proof the static runner already shares. SKILL.md, markdown, prompts, string literals, docstrings, other languages, fragments, and malformed source never qualify. - A finite report verb from a closed allowlist directly precedes the match ("emits", "returns", "printed", ...). Base forms such as "give" read as imperatives, so they are not on it. Its subject is a program noun from a closed allowlist ("the server", "this endpoint", "the CLI"), or the comment opens with a code-behavior verb ("# Returns no warning when ..."). An unlisted subject or verb keeps the finding, so "the assistant emits no warnings" stays active. - The contiguous comment block never addresses an agent or the reader (you, assistant, agent, model, AI, LLM, prompt, ...). The block walk is bounded, and a block past the bound keeps the finding. Both grammar checks read at most 160 characters before the match, so a long comment line stays linear. The ownership proof parses the module, so the analyzer now opts into the runner's check_runtime deadline. Homoglyph or zero-width addressing is still caught, because the normalized view reproduces the match and fails the address check. This is independent of the open "without warning" mandate change in #461. That change touches a different AR2 pattern and different lines, and its diff still applies cleanly on top of this one. Tests: - descriptive comments are clean: an explicit program subject with modifiers and adverbs, a trailing comment, an opening verb, a wrapped sentence, past tense, and disclaimers; - a scan-level case drops the comment but keeps a prompt string literal in the same file, and the proof honors check_runtime; - fail-closed controls stay active: agent-addressed comments (including "# Assistant: never include warnings or disclaimers" and an address on an earlier line of the block), imperative, negated, and verbless forms, unlisted subjects and opening verbs, a later directive in the same comment, string literals, a "#" line inside a prompt string, a docstring, malformed source, a match spanning comment and code, SKILL.md, markdown, shell, JavaScript, an over-long comment block, and a homoglyph-addressed block at scan level. Signed-off-by: Narendran Raghavan <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> * perf(static): size the Python span memo for three concurrent consumers AR2 now asks python_literal_spans for comment ownership, so one module's spans have three consumers in a scan: the declared-marker pass, the tool-misuse shell parser, and AR2. They run in different analyzer nodes, and the graph starts all analyzer nodes together on worker threads. Every node walks the components in the same order at its own pace, so requests for other modules land between one module's consumers. With two entries, that gap could evict the module and repeat its ast.parse and tokenize. Raise the memo to eight entries and update its comment. An entry holds one module however many consumers ask, so a module keeps its result while up to seven other modules are requested in between. A wider gap only repeats the parse; it never changes a result. Oversized source is never stored, so eight entries hold at most eight times MAX_PYTHON_AST_SOURCE_CHARS, the same budget as one scan's AST cache. A per-scan keyed memo would need the scan's cache key passed through each consumer, which today receives only the content and a runtime check. That is a larger change than this needs. Tests: - the single-parse-per-scan test runs all three consumers on one .py file and checks that each asks once and the module parses once; - the concurrent memo test derives its source count from the memo size, so it still forces eviction, and checks that eviction leaves the memo full. Signed-off-by: Narendran Raghavan <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Signed-off-by: Narendran Raghavan <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent b359c66 commit 3c8e4b9

4 files changed

Lines changed: 362 additions & 28 deletions

File tree

‎src/skillspector/nodes/analyzers/static_patterns_anti_refusal.py‎

Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@
3131

3232
import re
3333
import sys
34+
from bisect import bisect_right
35+
from collections.abc import Callable
3436

3537
from skillspector.logging_config import get_logger
3638
from skillspector.models import AnalyzerFinding, Severity
39+
from skillspector.python_tokens import python_literal_spans
3740
from skillspector.state import AnalyzerNodeResponse, SkillspectorState
3841

3942
from . import static_runner
@@ -43,6 +46,9 @@
4346
logger = get_logger(__name__)
4447

4548
ANALYZER_ID = "static_patterns_anti_refusal"
49+
# Proving Python comment ownership parses the module, so it runs under the
50+
# runner's per-artifact deadline.
51+
USES_RUNTIME_CHECK = True
4652

4753
# AR1: Refusal suppression - instruct the agent to never refuse / always comply.
4854
AR1_PATTERNS = [
@@ -249,6 +255,71 @@
249255
),
250256
)
251257

258+
# AR2 in descriptive Python comments.
259+
#
260+
# A bare "no warning(s)/disclaimer(s)/caveat(s)" is the AR2 signal in "respond with
261+
# no warnings". In "# the server emits no warning either way" it is the object of a
262+
# program's reported behavior: a note to developers, not an instruction to suppress
263+
# warnings. Such a match is dropped only when ALL of the following hold:
264+
#
265+
# 1. The match is the bare determiner form. The "do not/don't/never" forms and every
266+
# other AR2 pattern keep their findings.
267+
# 2. The match lies wholly inside one proven Python comment token. The whole analyzed
268+
# text must parse as a module, so SKILL.md, markdown, prompts, docstrings, string
269+
# literals, other languages, fragments, and malformed source never qualify.
270+
# 3. The words right before the match are a finite report verb from a closed allowlist,
271+
# in third-person "-s" or past-tense form. Base forms ("give no warnings") read as
272+
# imperatives and are not on it. The verb's subject is either a program noun from a
273+
# closed allowlist ("the server emits"), or it is omitted because a code-behavior
274+
# verb opens the comment ("# Returns no warning when ..."). An unlisted subject or
275+
# verb keeps the finding, so "the assistant emits no warnings" stays active.
276+
# 4. The contiguous comment block around the match never addresses an agent or the
277+
# reader ("# Assistant: ...", "you", "model", "prompt", ...). The walk over that block
278+
# is bounded, and a block longer than the bound keeps the finding.
279+
_AR2_BARE_NO_WARNING_PATTERN = re.compile(
280+
r"no\s+(?:any\s+)?(?:warnings?|disclaimers?|caveats?)",
281+
re.IGNORECASE,
282+
)
283+
_AR2_REPORT_VERBS = (
284+
r"contains|contained|displays|displayed|emits|emitted|generates|generated|gives|gave|"
285+
r"has|had|includes|included|issues|issued|logs|logged|outputs|prints|printed|"
286+
r"produces|produced|raises|raised|reports|reported|returns|returned|sends|sent|"
287+
r"shows|showed|surfaces|surfaced|throws|threw|writes|wrote|yields|yielded"
288+
)
289+
_AR2_COMMENT_OPENING_REPORT_VERBS = (
290+
r"emits|emitted|logs|logged|prints|printed|raises|raised|returns|returned|"
291+
r"throws|threw|yields|yielded"
292+
)
293+
_AR2_PROGRAM_SUBJECTS = (
294+
r"apis?|backends?|binary|binaries|builds?|calls?|checks?|cli|clients?|commands?|"
295+
r"compilers?|daemons?|databases?|db|drivers?|endpoints?|functions?|handlers?|helpers?|"
296+
r"interpreters?|jobs?|library|libraries|linters?|methods?|modules?|packages?|"
297+
r"parsers?|pipelines?|process|processes|programs?|query|queries|requests?|runtimes?|"
298+
r"scripts?|sdks?|servers?|services?|subprocess|subprocesses|tests?|tools?|upstream|"
299+
r"validators?|wrappers?"
300+
)
301+
_AR2_REPORT_ADVERBS = r"(?:(?:[a-z]+ly|also|already|always|even|just|now|still|then)\s+){0,2}"
302+
_AR2_PROGRAM_SUBJECT_REPORT_PATTERN = re.compile(
303+
rf"(?<![\w-])(?:{_AR2_PROGRAM_SUBJECTS})\s+{_AR2_REPORT_ADVERBS}"
304+
rf"(?:{_AR2_REPORT_VERBS})\s+\Z",
305+
re.IGNORECASE,
306+
)
307+
_AR2_COMMENT_OPENING_REPORT_PATTERN = re.compile(
308+
rf"#+[ \t]*{_AR2_REPORT_ADVERBS}(?:{_AR2_COMMENT_OPENING_REPORT_VERBS})[ \t]+",
309+
re.IGNORECASE,
310+
)
311+
_AR2_AGENT_ADDRESS_PATTERN = re.compile(
312+
r"\b(?:you|your|yours|yourself|yourselves|assistants?|agents?|models?|ai|llms?|"
313+
r"chatbots?|bots?|claude|chatgpt|gpt|copilot|gemini|codex|personas?|prompts?|"
314+
r"instructions?)\b",
315+
re.IGNORECASE,
316+
)
317+
# A subject or comment opening, two adverbs, and a verb fit well inside this many
318+
# characters. Bounding both grammar checks keeps a long comment line linear.
319+
_AR2_REPORT_PREFIX_CHARS = 160
320+
# A comment block reaching this many lines on either side of the match keeps it.
321+
_AR2_COMMENT_BLOCK_MAX_LINES = 64
322+
252323

253324
def _is_directly_instructive(context: str, matched_text: str) -> bool:
254325
"""Return True when the match still looks like an active adversarial instruction."""
@@ -393,17 +464,126 @@ def _is_benign_ar_context(
393464
)
394465

395466

396-
def analyze(content: str, file_path: str, file_type: str) -> list[AnalyzerFinding]:
467+
def _no_runtime_check() -> None:
468+
"""Stand in for the runner deadline when analyze() is called directly."""
469+
470+
471+
class _PythonComments:
472+
"""Lazily proven comment tokens of one analyzed Python text."""
473+
474+
def __init__(self, content: str, check_runtime: Callable[[], None]) -> None:
475+
self._content = content
476+
self._check_runtime = check_runtime
477+
self._starts: tuple[int, ...] = ()
478+
self._ends: tuple[int, ...] = ()
479+
self._computed = False
480+
self._block_unaddressed: dict[int, bool] = {}
481+
482+
def comment_index(self, start: int, end: int) -> int | None:
483+
"""Return the comment token that wholly contains ``[start, end)``, if proven."""
484+
if not self._computed:
485+
spans = python_literal_spans(self._content, self._check_runtime)
486+
if spans is not None:
487+
self._starts, self._ends = spans
488+
self._computed = True
489+
index = bisect_right(self._starts, start) - 1
490+
if index < 0 or end > self._ends[index] or not self._is_comment(index):
491+
return None
492+
return index
493+
494+
def comment_start(self, index: int) -> int:
495+
return self._starts[index]
496+
497+
def block_is_unaddressed(self, index: int) -> bool:
498+
"""Return True when no agent or reader is addressed in the comment block."""
499+
bounds = self._block_bounds(index)
500+
if bounds is None:
501+
return False
502+
first, last = bounds
503+
cached = self._block_unaddressed.get(first)
504+
if cached is None:
505+
cached = (
506+
_AR2_AGENT_ADDRESS_PATTERN.search(
507+
self._content, self._starts[first], self._ends[last]
508+
)
509+
is None
510+
)
511+
self._block_unaddressed[first] = cached
512+
return cached
513+
514+
def _is_comment(self, index: int) -> bool:
515+
return self._content[self._starts[index]] == "#"
516+
517+
def _on_adjacent_lines(self, earlier: int, later: int) -> bool:
518+
if not (self._is_comment(earlier) and self._is_comment(later)):
519+
return False
520+
gap = self._content[self._ends[earlier] : self._starts[later]]
521+
return gap.count("\n") == 1 and not gap.strip()
522+
523+
def _block_bounds(self, index: int) -> tuple[int, int] | None:
524+
first = last = index
525+
for _ in range(_AR2_COMMENT_BLOCK_MAX_LINES):
526+
if first == 0 or not self._on_adjacent_lines(first - 1, first):
527+
break
528+
first -= 1
529+
else:
530+
return None
531+
for _ in range(_AR2_COMMENT_BLOCK_MAX_LINES):
532+
if last + 1 == len(self._starts) or not self._on_adjacent_lines(last, last + 1):
533+
break
534+
last += 1
535+
else:
536+
return None
537+
return first, last
538+
539+
540+
def _is_descriptive_python_comment(comments: _PythonComments, match: re.Match[str]) -> bool:
541+
"""Return True when an AR2 match reports a program's behavior in a Python comment."""
542+
if not _AR2_BARE_NO_WARNING_PATTERN.fullmatch(match.group(0)):
543+
return False
544+
index = comments.comment_index(match.start(), match.end())
545+
if index is None:
546+
return False
547+
content = match.string
548+
comment_start = comments.comment_start(index)
549+
prefix_start = max(comment_start, match.start() - _AR2_REPORT_PREFIX_CHARS)
550+
reports_program_behavior = bool(
551+
_AR2_PROGRAM_SUBJECT_REPORT_PATTERN.search(content, prefix_start, match.start())
552+
or (
553+
prefix_start == comment_start
554+
and _AR2_COMMENT_OPENING_REPORT_PATTERN.fullmatch(content, comment_start, match.start())
555+
)
556+
)
557+
return reports_program_behavior and comments.block_is_unaddressed(index)
558+
559+
560+
def analyze(
561+
content: str,
562+
file_path: str,
563+
file_type: str,
564+
check_runtime: Callable[[], None] | None = None,
565+
) -> list[AnalyzerFinding]:
397566
"""Analyze content for anti-refusal statements (AR1-AR3)."""
398567
findings: list[AnalyzerFinding] = []
399568
locations = SourceLocationIndex(content, file_path)
400569
tag = [PatternCategory.ANTI_REFUSAL.value]
570+
python_comments = (
571+
_PythonComments(content, check_runtime or _no_runtime_check)
572+
if file_type == "python"
573+
else None
574+
)
401575

402576
for rule_id, patterns in _RULES:
403577
for pattern, base_confidence in patterns:
404578
for match in static_runner.iter_paragraph_matches(
405579
pattern, content, re.IGNORECASE | re.MULTILINE
406580
):
581+
if (
582+
rule_id == "AR2"
583+
and python_comments is not None
584+
and _is_descriptive_python_comment(python_comments, match)
585+
):
586+
continue
407587
lines = content.splitlines()
408588
line_num = get_line_number(content, match.start())
409589
match_line = lines[line_num - 1] if lines else content

‎src/skillspector/python_tokens.py‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,27 @@
4949
# comment tokens, in source order. Results are shared, so they are immutable.
5050
PythonLiteralSpans = tuple[tuple[int, ...], tuple[int, ...]]
5151

52-
# One static scan of a module asks for its spans twice: the declared-marker
53-
# pass proves string closers, and the tool-misuse shell parser proves token
54-
# ownership. The most recent results are remembered so both share one parse.
55-
# Entries are keyed by length and hash and confirmed by string equality, which
56-
# is an identity check when both callers hold the same file-cache string. A
57-
# second entry absorbs a scan of another file on a concurrent analyzer thread.
58-
# Oversized source is never stored, and eviction bounds how long a module's
59-
# text outlives its scan. This lock guards only the entries, so a lookup never
60-
# waits for another thread's parse.
61-
_PYTHON_LITERAL_SPANS_CACHE_SIZE = 2
52+
# Three consumers ask for a module's spans during one scan: the declared-marker
53+
# pass proves string closers, the tool-misuse shell parser proves token
54+
# ownership, and AR2 proves comment ownership. Recent results are remembered so
55+
# they share one parse. Entries are keyed by length and hash and confirmed by
56+
# string equality, which is an identity check when the callers hold the same
57+
# file-cache string.
58+
#
59+
# The consumers run in different analyzer nodes (the marker pass in every
60+
# static-pattern node). The graph starts all analyzer nodes together on worker
61+
# threads; the CLI and MCP server set no ``max_concurrency``, so the pool has
62+
# Python's default size, min(32, CPUs + 4). Every node walks the components in
63+
# the same order but at its own pace, so requests for other modules arrive
64+
# between a module's first and last consumer. An entry holds one module however
65+
# many consumers ask, so eight entries keep a result while up to seven other
66+
# modules are requested in that gap. A wider gap only repeats the parse.
67+
# Oversized source is never stored, so eight entries hold at most eight times
68+
# MAX_PYTHON_AST_SOURCE_CHARS, which equals one scan's AST cache budget
69+
# (MAX_PYTHON_AST_CACHE_SOURCE_CHARS), and eviction bounds how long that text
70+
# outlives its scan. This lock guards only the entries, so a lookup never waits
71+
# for another thread's parse.
72+
_PYTHON_LITERAL_SPANS_CACHE_SIZE = 8
6273
_PYTHON_LITERAL_SPANS_CACHE_LOCK = Lock()
6374
_PYTHON_LITERAL_SPANS_CACHE: OrderedDict[tuple[int, int], tuple[str, PythonLiteralSpans | None]] = (
6475
OrderedDict()

‎tests/nodes/analyzers/test_python_string_marker_ownership.py‎

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from skillspector.artifacts import SecurityTextView
2727
from skillspector.graph import graph
2828
from skillspector.inspection_ledger import LedgerOutcome, LedgerReason
29+
from skillspector.nodes.analyzers import static_patterns_anti_refusal as ar_module
2930
from skillspector.nodes.analyzers import static_patterns_prompt_injection as pi_module
3031
from skillspector.nodes.analyzers import static_patterns_tool_misuse as tm_module
3132
from skillspector.nodes.analyzers import static_runner
@@ -211,9 +212,15 @@ def counted(content: str, check_runtime):
211212

212213
# --- One ownership parse per module ----------------------------------------
213214

214-
# The marker pass proves the closing quote after "omit", and the tool-misuse
215-
# shell parser proves the fence backticks literal: both consumers ask.
216-
_BOTH_CONSUMERS = _ASSERT_FLAG + 'FENCE = "\\n```\\n"\n' + _UNQUOTED_TAIL
215+
# The marker pass proves the closing quote after "omit", the tool-misuse shell
216+
# parser proves the fence backticks literal, and AR2 proves that "no warning"
217+
# lies in a comment that reports program output: all three consumers ask.
218+
_ALL_CONSUMERS = (
219+
_ASSERT_FLAG
220+
+ 'FENCE = "\\n```\\n"\n'
221+
+ "# The server emits no warning when the cache is cold.\n"
222+
+ _UNQUOTED_TAIL
223+
)
217224

218225

219226
def _count_parses(monkeypatch: pytest.MonkeyPatch) -> list[str]:
@@ -234,25 +241,34 @@ def _literals(content: str, spans: python_tokens.PythonLiteralSpans | None) -> l
234241
return [content[start:end] for start, end in zip(*spans, strict=True)]
235242

236243

237-
def test_marker_pass_and_shell_parser_share_one_parse_per_scan(
244+
def test_marker_pass_shell_parser_and_ar2_share_one_parse_per_scan(
238245
monkeypatch: pytest.MonkeyPatch,
239246
) -> None:
240247
parses = _count_parses(monkeypatch)
241-
requests: list[int] = []
248+
requests: list[tuple[str, int]] = []
242249
real_spans = python_tokens.python_literal_spans
243250

244-
def requested(content: str, check_runtime: Callable[[], None]):
245-
requests.append(len(content))
246-
return real_spans(content, check_runtime)
251+
def requested_by(consumer: str):
252+
def requested(content: str, check_runtime: Callable[[], None]):
253+
requests.append((consumer, len(content)))
254+
return real_spans(content, check_runtime)
255+
256+
return requested
247257

248-
monkeypatch.setattr(python_tokens, "python_literal_spans", requested)
249-
monkeypatch.setattr(tm_module, "_python_literal_spans", requested)
258+
monkeypatch.setattr(python_tokens, "python_literal_spans", requested_by("marker"))
259+
monkeypatch.setattr(tm_module, "_python_literal_spans", requested_by("tool-misuse"))
260+
monkeypatch.setattr(ar_module, "python_literal_spans", requested_by("anti-refusal"))
250261

251-
result = _ledger("scripts/check.py", _BOTH_CONSUMERS, tm_module, pi_module)
262+
result = _ledger("scripts/check.py", _ALL_CONSUMERS, tm_module, pi_module, ar_module)
252263

264+
assert result["findings"] == []
253265
assert result["inspection_ledger"][0]["outcome"] is LedgerOutcome.COMPLETED
254-
assert requests == [len(_BOTH_CONSUMERS)] * 2
255-
assert [len(content) for content in parses] == [len(_BOTH_CONSUMERS)]
266+
assert sorted(requests) == [
267+
("anti-refusal", len(_ALL_CONSUMERS)),
268+
("marker", len(_ALL_CONSUMERS)),
269+
("tool-misuse", len(_ALL_CONSUMERS)),
270+
]
271+
assert [len(content) for content in parses] == [len(_ALL_CONSUMERS)]
256272

257273

258274
def test_memo_reuses_proven_and_unproven_results(monkeypatch: pytest.MonkeyPatch) -> None:
@@ -324,15 +340,17 @@ def expires_after_the_module_parse() -> None:
324340
def test_concurrent_sources_each_get_their_own_spans() -> None:
325341
# Analyzer nodes run on worker threads. More sources than memo entries
326342
# keep entries being replaced while other threads look them up.
343+
cache_size = python_tokens._PYTHON_LITERAL_SPANS_CACHE_SIZE
344+
proven_count = cache_size + 2
327345
sources = [
328346
"".join(f'value_{index} = "{worker}-{index}" # w{worker}\n' for index in range(worker + 1))
329-
for worker in range(6)
347+
for worker in range(proven_count)
330348
] + ['broken = "x" + $(\n']
331349
expected = {
332350
source: python_tokens._python_literal_spans_uncached(source, lambda: None)
333351
for source in sources
334352
}
335-
assert [spans is None for spans in expected.values()] == [False] * 6 + [True]
353+
assert [spans is None for spans in expected.values()] == [False] * proven_count + [True]
336354
barrier = threading.Barrier(8)
337355
correct: list[bool] = []
338356

@@ -351,8 +369,8 @@ def scan(worker: int) -> None:
351369
thread.join()
352370

353371
assert correct == [True] * 480
354-
cache_size = python_tokens._PYTHON_LITERAL_SPANS_CACHE_SIZE
355-
assert len(python_tokens._PYTHON_LITERAL_SPANS_CACHE) <= cache_size
372+
# More distinct sources than entries were stored, so eviction left it full.
373+
assert len(python_tokens._PYTHON_LITERAL_SPANS_CACHE) == cache_size
356374

357375

358376
# --- Readings that stay fail-closed ----------------------------------------

0 commit comments

Comments
 (0)