Skip to content

fix(agent): gate SD capability claim guardrail on user image intent (bug #4582) - #4583

Closed
madcow4242 wants to merge 1 commit into
amd:mainfrom
madcow4242:fix/sd-guardrail-false-positive
Closed

madcow4242 wants to merge 1 commit into
amd:mainfrom
madcow4242:fix/sd-guardrail-false-positive

Conversation

@madcow4242

Copy link
Copy Markdown
Contributor

Closes #4582

Summary

Add user-intent checking before triggering the Stable Diffusion capability guardrail in gaia/agents/base/agent.py. This prevents GAIA from blocking plain-text answers and injecting forced generate_image tool calls on general Q&A queries.

Why

When an agent answers a general prompt (e.g., "What capabilities do you have?" or "What tools do you support?") with text mentioning image generation, the SD guardrail previously flagged it as an unattempted tool claim. The framework then injected a synthetic prompt (SYSTEM: STOP. Do NOT write text. You must output a JSON tool call...), forcing the agent into a second turn where it declined the forced call—overwriting its original complete answer with a refusal summary.

Linked issue

Closes #4582

Changes

  • Extract _last_user_msg prior to evaluating capability claim blocks in gaia/agents/base/agent.py.
  • Add _USER_IMAGE_PATTERNS regex matching to scan the last user message for explicit image creation verbs/nouns (e.g., generate, draw, create, picture, image, generate_image).
  • Require _user_requested_image == True in _should_block_sd before blocking text capability claims.
  • Preserve existing guardrail behavior when a user explicitly requests an image but the model outputs a text capability claim instead of executing the tool.

Test plan

  • Run linter and unit tests:
    python util/lint.py --all
    pytest tests/unit/

@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Verdict: Request changes

This PR has the right goal. The image-generation guardrail should only fire when the user actually asked for an image, so questions like "what can you do?" stop getting their answers overwritten. The problem is the check that decides whether the user asked for an image. It misses the most common ways people ask, so the guardrail now goes quiet exactly when it is needed.

  • Common image requests aren't recognized. Tested against the PR's own patterns, "Draw a cat", "Paint me a sunset over the ocean", "Generate some images of dogs" and "Create pictures of a castle" are all treated as not asking for an image. Plurals are missed entirely. Meanwhile "What is the image file format for this photo?" counts as an image request. Please broaden the matching, including plurals and a bare action word followed by a subject.
  • It reads the wrong message. The check looks at the latest message sent with the user role. The agent loop also sends its own correction prompts under that role, so after any correction the check reads the agent's prompt instead of what the user asked. The user's original request is already available in the loop. Use that instead.
  • No tests. This is new decision logic in the core agent loop and ships without a test. Add a few unit tests covering image requests (should block) and capability questions (should pass).

Real-world evidence

N/A: no evidence bundle was produced for this run (agent-loop guardrail change, real inference is deferred to the strix-halo lane). This verdict rests on static review alone. The prompt results above come from running the PR's patterns directly, not from a live agent run. The PR doesn't show a gaia eval agent run either, which CLAUDE.md requires for changes to agent-loop guardrails like this one.

🔍 Technical details

🟡 Intent regex has large false-negative and false-positive gaps (src/gaia/agents/base/agent.py:9056-9060)

  • \b(image|picture|photo)\b fails on plurals ("images", "pictures") because the trailing \b needs a non-word character after the noun.
  • Pattern 1 requires both a verb and one of the listed nouns, so "draw a cat" and "paint a sunset" never match.
  • Pattern 2 matches any sentence that mentions "image" followed by "of/with/for", so questions about images count as requests.
  • Ran locally:
    False Draw a cat
    False Paint me a sunset over the ocean
    False Generate some images of dogs
    False Create pictures of a castle
    True  What is the image file format for this photo?
    True  Show me what tools you have for image editing
    
  • A tighter, plural-aware version that also accepts imperative verbs at the start of the message:
                _USER_IMAGE_PATTERNS = [
                    r"\b(generate|create|draw|make|render|produce|paint|design|sketch)\b.*\b(images?|pictures?|photos?|illustrations?|drawings?|graphics?|logos?|banners?|art(work)?)\b",
                    r"^\s*(please\s+)?(draw|paint|sketch|render|illustrate)\b",
                    r"\bgenerate_image\b",
                ]

Also consider hoisting this list (and the existing _CAPABILITY_CLAIM_PATTERNS / _SD_OUTCOME_ACKNOWLEDGMENT) to module-level compiled constants beside _SD_CAPABILITY_TOOLS (line 461), so they aren't rebuilt on every loop step.

🟡 Intent is read from the last role: "user" message, not the user's request (agent.py:9047-9055)

The loop appends many synthetic role: "user" messages: plan-format errors (~8108), dedup notices (~8584), corrections (~8916, 9163, 9277), and this guard's own "SYSTEM: STOP…" prompt. After any of those, _last_user_msg is agent text, so the intent check is wrong. It is also only coincidentally right on the guard's second pass, because its own injected prompt contains generate_image. user_input is already in scope (used at ~8107), so use it:

                _last_user_msg = user_input if isinstance(user_input, str) else ""

That also keeps the injected tool-call prompt seeded with the real request instead of a correction message.

🟡 No tests for the new gating

tests/ has no coverage of _should_block_sd or the capability-claim guard at all. Add unit tests with a mocked LLM:

🟢 Nit: the default for _last_user_msg changed from "the requested image" to "". That's harmless now that blocking requires a matched message, but it becomes dead if the user_input suggestion lands.

Strengths

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Thanks for tracking this down and fixing it — #4582 was real and your report pinned it. It's now fixed by #4809, which merged today and takes a different route: rather than deciding from the wording of the user's message (which misses requests like "Draw a cat"), it only steps in when an image was actually attempted this turn, judged from the turn's tool record. Closing this one as superseded. Thanks again for the contribution.

kovtcharov-amd pushed a commit to aksh8b/gaia that referenced this pull request Oct 6, 2026
…ation (amd#4809)

Asking GAIA "What can you do?" threw away its answer and tried to
generate an image of the question. Any answer that mentioned image
generation was treated as a failure to call the image tool, with no
check that the user wanted an image or that the agent could make one —
and the flagship has image generation on by default, so this hit the
most common first question. Now a plain answer is never overridden; the
agent only steps in when an image was actually attempted this turn and
the answer doesn't say what happened.

Closes amd#4582. Supersedes the approach in amd#4583, which decides from the
wording of the user's message and so misses requests like "Draw a cat";
this one decides from the turn's tool record instead.

<details>
<summary>🔍 Technical details</summary>

- Removed: the branch that matched the answer against capability phrases
and, with no `generate_image` call on record, demanded one built from
the user's last message. The tool record cannot show intent, so there is
nothing structural to gate it on; whether to call the tool is the
prompt's and tool schema's job (amd#4574).
- Kept, gated on a `generate_image` call that returned a result this
turn on an agent with the tool registered: an answer that says when
image generation is available instead of reporting the outcome gets one
correction, which now names the recorded outcome (succeeded / returned
an error). It was unbounded before. The amd#1023 failure override is
unchanged.
- A wording check on the answer remains in that kept path because the
record holds the outcome but not what the answer claims about it. It can
no longer run without a recorded outcome.
- A call to a `generate_image` the agent doesn't have no longer arms
either check.
</details>

## Test plan

- [x] `python -m pytest tests/unit/agents/test_image_outcome_guard.py
tests/unit/agents/test_parse_error_recovery.py` — the new reproduction
fails on main and passes here; the amd#1023 cases still pass
- [x] `python -m pytest tests/unit/agents
hub/agents/gaia/python/tests/test_prompt_prefix_stability.py`
- [ ] `eval_flagship.yml` — `gaia_media`
- [ ] `eval_flagship.yml` — `gaia_core`
- [ ] `eval_flagship.yml` — `personality`
- [ ] `eval_flagship.yml` — `tool_selection`
@madcow4242
madcow4242 deleted the fix/sd-guardrail-false-positive branch October 6, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: false-positive SD capability guardrail on non-image queries

2 participants