-
Notifications
You must be signed in to change notification settings - Fork 162
feat(agent): state whether an answer was verified, on every exit path #3397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| # Copyright(C) 2025-2026 Advanced Micro Devices, Inc. All rights reserved. | ||
| # SPDX-License-Identifier: MIT | ||
| """Verification-scope statement appended to every emitted answer (#3376). | ||
|
|
||
| The agent loop used to report "done" in the same confident language whether it | ||
| ran the test suite or ran nothing at all. Every emitted answer now carries one | ||
| line saying which — derived from the turn's own tool-execution log, so it costs | ||
| no extra model call. | ||
|
|
||
| The line rides in the answer, which the surfaces persist and re-send as | ||
| conversation history, so it is HARD-CAPPED at ``VERIFICATION_SCOPE_MAX_CHARS``. | ||
| :func:`strip_verification_scope` removes it again for consumers that need the | ||
| answer text alone. | ||
|
|
||
| Pure and dependency-free on purpose: the agent loop, the Agent-UI SSE handler, | ||
| and hub agents all consume it. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from typing import Any, Dict, FrozenSet, List, Optional, Tuple | ||
|
|
||
| VERIFICATION_SCOPE_PREFIX = "Verification: " | ||
| VERIFICATION_SCOPE_MAX_CHARS = 200 | ||
|
|
||
| # Tools that ARE a check by name, whatever their arguments. | ||
| _CHECK_TOOLS: FrozenSet[str] = frozenset( | ||
| { | ||
| "build", | ||
| "lint", | ||
| "run_lint", | ||
| "run_test_suite", | ||
| "run_tests", | ||
| "typecheck", | ||
| } | ||
| ) | ||
|
|
||
| # A shell-style call is a check when its command names a test / lint / build | ||
| # runner. Deliberately conservative: a miss reads "unverified" (honest and | ||
| # cautious), a false positive would claim a check that never ran. | ||
| _CHECK_COMMAND_RE = re.compile( | ||
| r"\b(" | ||
| r"pytest|py\.test|tox|nox" | ||
| r"|python\s+-m\s+(?:pytest|unittest)" | ||
| r"|npm\s+(?:run\s+)?(?:test|lint|build|typecheck)" | ||
| r"|yarn\s+(?:test|lint|build)" | ||
| r"|pnpm\s+(?:run\s+)?(?:test|lint|build)" | ||
| r"|go\s+(?:test|vet|build)" | ||
| r"|cargo\s+(?:test|clippy|check|build)" | ||
| r"|dotnet\s+(?:test|build)" | ||
| r"|mvn\s+(?:test|verify)" | ||
| r"|make\s+(?:test|check|lint|build)" | ||
| r"|ctest|jest|vitest|mocha" | ||
| r"|ruff|flake8|pylint|mypy|pyright|eslint|tsc|shellcheck" | ||
| r"|util[/\\]lint\.py" | ||
| r")\b", | ||
| re.IGNORECASE, | ||
| ) | ||
|
|
||
| # Argument keys that carry a shell command, in priority order. | ||
| _COMMAND_KEYS: Tuple[str, ...] = ("command", "cmd", "script") | ||
|
|
||
| _SCOPE_LINE_RE = re.compile( | ||
| r"\n{1,2}" + re.escape(VERIFICATION_SCOPE_PREFIX) + r"[^\n]*\s*\Z" | ||
| ) | ||
|
|
||
|
|
||
| def verification_check_label(tool_name: str, tool_args: Any) -> Optional[str]: | ||
| """Short label when this call is a verification check, else ``None``. | ||
|
|
||
| ``pytest tests/unit -q`` → ``"pytest"``; ``read_file`` → ``None``. | ||
| """ | ||
| name = (tool_name or "").strip() | ||
| if name in _CHECK_TOOLS: | ||
| return name | ||
| if not isinstance(tool_args, dict): | ||
| return None | ||
| for key in _COMMAND_KEYS: | ||
| command = tool_args.get(key) | ||
| if isinstance(command, str) and command.strip(): | ||
| match = _CHECK_COMMAND_RE.search(command) | ||
| return " ".join(match.group(0).split()).lower() if match else None | ||
| return None | ||
|
|
||
|
|
||
| def _names(executions: List[Dict[str, Any]], limit: int = 3) -> str: | ||
| """Deduped, order-preserving, count-capped label list.""" | ||
| labels: List[str] = [] | ||
| for execution in executions: | ||
| label = execution.get("check_label") | ||
| if label and label not in labels: | ||
| labels.append(label) | ||
| if not labels: | ||
| return "a check" | ||
| shown = ", ".join(labels[:limit]) | ||
| extra = len(labels) - limit | ||
| return f"{shown} +{extra} more" if extra > 0 else shown | ||
|
|
||
|
|
||
| def build_verification_scope(executions: List[Dict[str, Any]]) -> str: | ||
| """One bounded line naming what ran, what passed, and what went unchecked. | ||
|
|
||
| Three distinguishable states: ``verified`` (checks ran and every one | ||
| passed), ``partially verified`` (checks ran, not all passed), and | ||
| ``unverified`` (no check ran at all). | ||
|
|
||
| Each execution is ``{"tool": str, "check_label": str | None, | ||
| "failed": bool}`` — see ``Agent._note_verification_signal``. | ||
| """ | ||
| executions = list(executions or []) | ||
| checks = [e for e in executions if e.get("check_label")] | ||
| if not checks: | ||
| total = len(executions) | ||
| if total == 0: | ||
| body = "unverified — no tools ran, so nothing was checked." | ||
| else: | ||
| plural = "" if total == 1 else "s" | ||
| body = ( | ||
| f"unverified — {total} tool call{plural} ran, none of them a " | ||
| "test, lint, or build." | ||
| ) | ||
| else: | ||
| passed = [e for e in checks if not e.get("failed")] | ||
| failed = [e for e in checks if e.get("failed")] | ||
| if not failed: | ||
| body = f"verified — {_names(passed)} ran and passed." | ||
| elif not passed: | ||
| body = ( | ||
| f"partially verified — {_names(failed)} ran and did not pass; " | ||
| "nothing else was checked." | ||
| ) | ||
| else: | ||
| body = ( | ||
| f"partially verified — {_names(passed)} passed, " | ||
| f"{_names(failed)} did not." | ||
| ) | ||
| statement = VERIFICATION_SCOPE_PREFIX + body | ||
| if len(statement) > VERIFICATION_SCOPE_MAX_CHARS: | ||
| statement = statement[: VERIFICATION_SCOPE_MAX_CHARS - 1].rstrip() + "..." | ||
| return statement | ||
|
|
||
|
|
||
| def strip_verification_scope(text: str) -> str: | ||
| """Remove a trailing verification-scope line added by the agent loop.""" | ||
| if not isinstance(text, str) or VERIFICATION_SCOPE_PREFIX not in text: | ||
| return text | ||
| return _SCOPE_LINE_RE.sub("", text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.