summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorMohamed Hegazy <8000722+mhegazy@users.noreply.github.com>2026-11-12 23:54:15 +0000
committerClaude <noreply@anthropic.com>2026-11-12 23:54:15 +0000
commitbf3dec204d794c54dcc1723c1cc37cdf3b6e4e61 (patch)
treef91d15fbad187fc419e515de034c0b1707cb05a7 /plugins
parent23c304a6387ed2fa207ceff27646d8514ee75235 (diff)
security-guidance: tell the commit reviewer its text is not read
The reviewer returns its findings through a tool call, and nothing reads what it writes beside it. With only the file-reading tools it spends much of the review writing prose before that call. The investigate stages now get a rule that says so, kept out of the shared prompt in review_api and left out when thinking is off or the user names the model, where the text is the reasoning. Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins')
-rw-r--r--plugins/security-guidance/hooks/llm.py31
-rw-r--r--plugins/security-guidance/tests/test_reviewer_tools.py27
2 files changed, 56 insertions, 2 deletions
diff --git a/plugins/security-guidance/hooks/llm.py b/plugins/security-guidance/hooks/llm.py
index 793da3c3..79a1bd94 100644
--- a/plugins/security-guidance/hooks/llm.py
+++ b/plugins/security-guidance/hooks/llm.py
@@ -1937,6 +1937,33 @@ def _agentic_commit_review_enabled() -> bool:
# precedents and an adversarial refute taxonomy to drop low-signal findings.
_AGENTIC_INVESTIGATE_SYSTEM = review_api.AGENTIC_INVESTIGATE_SYSTEM
+
+# The investigate stages return their findings through the StructuredOutput
+# tool and nothing reads what they write beside it. Told so here, not in
+# review_api: a caller of that module may take its answer from the text.
+_REVIEWER_OUTPUT_RULE = (
+ "\n\nOUTPUT: only the `findings` you return are read, not text you write. "
+ "Write no prose before, between or after tool calls, and no verdicts "
+ "for files you clear: think them through instead. Ask for the files "
+ "and searches you can already name in one message, within the budget "
+ "above. `explanation` must still name the source, the sink and the path "
+ "between them. When you are done, return the findings at once."
+)
+
+
+def _investigate_system() -> str:
+ """The investigate stages' system prompt. Text between tool calls is where
+ a model reasons when thinking is off or the user picked the model, so the
+ output rule is left out there."""
+ thinking_off = (os.environ.get("MAX_THINKING_TOKENS", "").strip() == "0"
+ or any(os.environ.get(v, "").strip().lower() in ("1", "true", "yes", "on")
+ for v in ("CLAUDE_CODE_DISABLE_THINKING",
+ "CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING")))
+ if thinking_off or os.environ.get("SG_AGENTIC_MODEL", "").strip():
+ return _AGENTIC_INVESTIGATE_SYSTEM
+ return _AGENTIC_INVESTIGATE_SYSTEM + _REVIEWER_OUTPUT_RULE
+
+
_FINDINGS_SCHEMA = review_api.FINDINGS_SCHEMA
_SURVIVED_SCHEMA = review_api.SURVIVED_SCHEMA
@@ -2468,7 +2495,7 @@ def _agentic_review(
t0 = _t.time()
try:
inv, inv_turns, inv_subtype = _run(
- _AGENTIC_INVESTIGATE_SYSTEM, user_prompt, schema=_FINDINGS_SCHEMA
+ _investigate_system(), user_prompt, schema=_FINDINGS_SCHEMA
)
if os.environ.get("SG_AGENTIC_DEBUG_DIR"):
_dd = os.environ["SG_AGENTIC_DEBUG_DIR"]
@@ -2534,7 +2561,7 @@ def _agentic_review(
)
try:
inv2, _, _ = _run(
- _AGENTIC_INVESTIGATE_SYSTEM, iter2_prompt, schema=_FINDINGS_SCHEMA
+ _investigate_system(), iter2_prompt, schema=_FINDINGS_SCHEMA
)
if inv2:
seen = {(c.get("filePath"), c.get("category")) for c in candidates}
diff --git a/plugins/security-guidance/tests/test_reviewer_tools.py b/plugins/security-guidance/tests/test_reviewer_tools.py
index 555ef0ad..aa9238fd 100644
--- a/plugins/security-guidance/tests/test_reviewer_tools.py
+++ b/plugins/security-guidance/tests/test_reviewer_tools.py
@@ -104,6 +104,33 @@ def test_every_agentic_stage_gets_only_the_read_tools(sdk, tmp_path):
assert "permission_mode" not in opts
+def _systems(sdk, tmp_path):
+ llm.agentic_review(str(tmp_path), [("a.py", "+x = 1\n")], ["a.py"])
+ return [opts["system_prompt"] for opts in sdk]
+
+
+def test_investigate_stages_are_told_their_text_is_not_read(sdk, tmp_path, monkeypatch):
+ for var in ("MAX_THINKING_TOKENS", "CLAUDE_CODE_DISABLE_THINKING",
+ "CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING"):
+ monkeypatch.delenv(var, raising=False)
+ investigate, second, refute = _systems(sdk, tmp_path)
+ for system in (investigate, second):
+ assert system == llm._AGENTIC_INVESTIGATE_SYSTEM + llm._REVIEWER_OUTPUT_RULE
+ assert llm._REVIEWER_OUTPUT_RULE not in refute
+ # review_api's prompt, which other harnesses use as it is, keeps its text.
+ assert "OUTPUT:" not in llm._AGENTIC_INVESTIGATE_SYSTEM
+ assert "Aim for at least one candidate" in llm._AGENTIC_INVESTIGATE_SYSTEM
+
+
+@pytest.mark.parametrize("name,value", [
+ ("MAX_THINKING_TOKENS", "0"), ("CLAUDE_CODE_DISABLE_THINKING", "1"),
+ ("CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING", "true"), ("SG_AGENTIC_MODEL", "some-model")])
+def test_output_rule_is_left_out_where_text_is_the_reasoning(sdk, tmp_path, monkeypatch, name, value):
+ monkeypatch.setenv(name, value)
+ for system in _systems(sdk, tmp_path)[:2]:
+ assert system == llm._AGENTIC_INVESTIGATE_SYSTEM
+
+
def test_single_turn_sdk_review_gets_no_tools(sdk):
schema = {"type": "object", "properties": {}}
assert llm._call_claude_via_sdk("review this", schema) is not None