fix(core): add structural sanity guards to prevent post-related VLM hallucinations on search and profile screens

This commit is contained in:
2026-04-28 10:34:44 +02:00
parent cd64794f55
commit 52c553827f
8 changed files with 118 additions and 63 deletions

View File

@@ -9,10 +9,8 @@ Tests CommentPlugin against real XML fixtures to ensure:
import os
from unittest.mock import MagicMock
from GramAddict.core.behaviors import BehaviorContext
from GramAddict.core.behaviors.comment import CommentPlugin
FIX_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "fixtures")
@@ -46,10 +44,10 @@ def test_comment_plugin_can_activate_rejects_grids():
ctx.configs.args = MagicMock(comment_percentage=100)
ctx.configs.get_plugin_config.return_value = {}
ctx.shared_state = {}
ctx.context_xml = _get_fixture("explore_feed_dump.xml")
assert plugin.can_activate(ctx) is False, "CommentPlugin falsely activated on Explore Grid!"
ctx.context_xml = _get_fixture("user_profile_dump.xml")
assert plugin.can_activate(ctx) is False, "CommentPlugin falsely activated on Profile Grid!"
@@ -60,42 +58,42 @@ def test_comment_plugin_fails_safely_without_writer():
must abort safely and press BACK to exit the comment sheet.
"""
plugin = CommentPlugin()
ctx = MagicMock()
ctx.configs.get_plugin_config.return_value = {}
ctx.cognitive_stack = {} # No writer!
nav_graph = MagicMock()
nav_graph.do.return_value = True # Successfully opened comment sheet
ctx.cognitive_stack["nav_graph"] = nav_graph
result = plugin.execute(ctx)
assert result.executed is False, "CommentPlugin must not execute without a writer!"
ctx.device.press.assert_called_once_with("back")
def test_comment_plugin_dry_run_exits_safely():
"""
Test: If dry_run is true, the plugin generates the text but presses BACK
Test: If dry_run is true, the plugin generates the text but presses BACK
to cancel posting.
"""
plugin = CommentPlugin()
writer = MagicMock()
writer.generate_comment.return_value = "Awesome!"
ctx = MagicMock()
ctx.configs.get_plugin_config.return_value = {}
ctx.cognitive_stack = {"writer": writer}
ctx.configs.args = MagicMock(dry_run_comments=True)
nav_graph = MagicMock()
nav_graph.do.return_value = True # Successfully opened comment sheet
ctx.cognitive_stack["nav_graph"] = nav_graph
result = plugin.execute(ctx)
assert result.executed is True, "Dry run is considered a successful execution."
assert result.interactions == 0, "Dry run must yield 0 interactions."
assert result.metadata["text"] == "Awesome!"

View File

@@ -110,3 +110,41 @@ def test_structural_reels_first_grid_item_y_coords():
assert (
is_valid_nav is False
), "Structural Guard failed to reject a hallucinated navigation tab in the middle of the screen."
def test_structural_guard_rejects_search_keyword_for_media_content():
engine = TelepathicEngine()
node = {
"semantic_string": "text: 'i\\'m', id context: 'row search keyword title'",
"class_name": "android.widget.TextView",
"y": 500
}
is_valid = engine._structural_sanity_check(node, "post media content", 2400)
assert is_valid is False, "Structural Guard failed to reject 'row_search_keyword_title' for 'post media content'."
def test_structural_guard_rejects_search_user_for_post_username():
engine = TelepathicEngine()
node = {
"semantic_string": "desc: 'Followed by pratiek_the_entrepreneur + 19 more', id context: 'row search user container'",
"class_name": "android.widget.LinearLayout",
"y": 800
}
is_valid = engine._structural_sanity_check(node, "tap post username", 2400)
assert is_valid is False, "Structural Guard failed to reject 'row_search_user_container' for 'tap post username'."
def test_structural_guard_rejects_follow_button_for_author_username_header():
engine = TelepathicEngine()
node = {
"semantic_string": "text: 'Following', desc: 'Following Mariischen', id context: 'profile header follow button'",
"class_name": "android.widget.Button",
"y": 600
}
is_valid = engine._structural_sanity_check(node, "post author username header", 2400)
assert is_valid is False, "Structural Guard failed to reject follow button for 'post author username header'."