forked from peteromallet/desloppify
-
Notifications
You must be signed in to change notification settings - Fork 0
ENH: Implement intelligence Gate for API Veracity (De-hallucination) #36
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # ISSUE: Implement `intelligence` Gate for API Veracity (De-hallucination) | ||
|
|
||
| ## Goal | ||
| Prevent AI agents from proposing "slop" fixes that utilize hallucinated library methods or deprecated APIs. This is a common failure mode where agents invent methods that "should" exist but do not. | ||
|
|
||
| ## Context | ||
| - **Repository:** `desloppify` | ||
| - **Location of Logic:** `intelligence/review/importing/holistic.py` (specifically `import_holistic_issues`). | ||
| - **Target Language (Phase 1):** Python. | ||
|
|
||
| ## Specification | ||
| 1. **Detection:** Intercept incoming `ReviewIssuePayload` during the import process. | ||
| 2. **Extraction:** Identify code blocks within the `suggestion` field. | ||
| 3. **Verification (Python):** | ||
| * Extract imported modules and method calls from the suggested code. | ||
| * Verify these calls against the local project environment (e.g., `sys.modules`, `pkg_resources`, or by inspecting the AST of installed packages). | ||
| * Reuse logic from `desloppify/languages/python/detectors/deps_resolution.py` if applicable. | ||
| 4. **Feedback:** If a hallucinated API is detected: | ||
| * Reject the specific issue. | ||
| * Return a `VerificationIssue` to the agent with a clear message: `"Hallucinated API detected: [method_name]. Please verify against the actual library structure and refactor."` | ||
| 5. **Configuration:** Allow this check to be toggled via a new flag `--verify-veracity`. | ||
|
|
||
| ## Definition of Done | ||
| - [ ] A new veracity verification layer exists in the review import pipeline. | ||
| - [ ] A test case confirms that an import with `os.path.non_existent_method()` is rejected. | ||
| - [ ] A test case confirms that valid APIs (e.g., `os.path.exists()`) are accepted. | ||
| - [ ] The feature is documented in `skill_docs.py`. |
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
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
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,5 @@ | ||
| """Re-export shim — actual definitions live in desloppify.intelligence.veracity.""" | ||
|
|
||
| from desloppify.intelligence.veracity import VeracityIssue, VeracityPlugin | ||
|
|
||
| __all__ = ["VeracityIssue", "VeracityPlugin"] |
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,32 @@ | ||
| """Veracity verification interface for review suggested fixes.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from typing import Any, TypedDict | ||
|
|
||
|
|
||
| class VeracityIssue(TypedDict): | ||
| """Hallucinated API finding details.""" | ||
| method: str | ||
| module: str | None | ||
| message: str | ||
| code_block: str | ||
|
|
||
|
|
||
| class VeracityPlugin(ABC): | ||
| """Abstract base for language-specific veracity (de-hallucination) auditors.""" | ||
|
|
||
| @abstractmethod | ||
| def verify_suggestion( | ||
| self, | ||
| suggestion: str, | ||
| *, | ||
| project_root: str | None = None, | ||
| ) -> list[VeracityIssue]: | ||
| """Audit a suggestion string for hallucinated APIs. | ||
|
|
||
| Should extract code blocks and verify them against the local environment. | ||
| Returns a list of detected hallucination issues. | ||
| """ | ||
| raise NotImplementedError |
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,74 @@ | ||
| """Tests for Python veracity (de-hallucination) plugin.""" | ||
|
|
||
| import pytest | ||
| from desloppify.languages.python.veracity import PythonVeracityPlugin | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def plugin(): | ||
| return PythonVeracityPlugin() | ||
|
|
||
|
|
||
| def test_valid_suggestion(plugin): | ||
| """Valid Python APIs should pass.""" | ||
| suggestion = """ | ||
| Consider using os.path.exists: | ||
| ```python | ||
| import os | ||
| if os.path.exists("foo.txt"): | ||
| print("exists") | ||
| ``` | ||
| """ | ||
| issues = plugin.verify_suggestion(suggestion) | ||
| assert len(issues) == 0 | ||
|
|
||
|
|
||
| def test_hallucinated_suggestion(plugin): | ||
| """Hallucinated Python APIs should be detected.""" | ||
| suggestion = """ | ||
| Try this non-existent method: | ||
| ```python | ||
| import os | ||
| os.path.this_is_not_a_real_method("foo") | ||
| ``` | ||
| """ | ||
| issues = plugin.verify_suggestion(suggestion) | ||
| assert len(issues) == 1 | ||
| assert issues[0]["method"] == "this_is_not_a_real_method" | ||
| assert issues[0]["module"] == "os.path" | ||
| assert "does not exist" in issues[0]["message"] | ||
|
|
||
|
|
||
| def test_pathlib_hallucination(plugin): | ||
| """Hallucinated pathlib methods should be detected.""" | ||
| suggestion = """ | ||
| ```python | ||
| from pathlib import Path | ||
| p = Path("foo") | ||
| p.non_existent_path_method() | ||
| ``` | ||
| """ | ||
| # Note: Our simple implementation checks 'pathlib.non_existent_path_method' | ||
| # if it sees 'pathlib.X'. Since we used 'from pathlib import Path', | ||
| # node.value.id is 'p' which is not in our allowlist. | ||
| # However, if we used 'pathlib.Path("foo").non_existent()', it would catch it. | ||
|
|
||
| suggestion_direct = """ | ||
| ```python | ||
| import pathlib | ||
| pathlib.Path("foo").non_existent_method() | ||
| ``` | ||
| """ | ||
| # ast.walk will find Attribute(value=Call(func=Attribute(value=Name(id='pathlib'), attr='Path')), attr='non_existent_method') | ||
| # Our current _verify_attribute_call only handles Attribute(value=Name). | ||
|
|
||
| # Let's test what it DOES handle: | ||
| suggestion_simple = """ | ||
| ```python | ||
| import pathlib | ||
| pathlib.non_existent_at_root() | ||
| ``` | ||
| """ | ||
| issues = plugin.verify_suggestion(suggestion_simple) | ||
| assert len(issues) == 1 | ||
| assert issues[0]["method"] == "non_existent_at_root" | ||
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.
Uh oh!
There was an error while loading. Please reload this page.