Compare commits
18 Commits
main
...
feat/radic
| Author | SHA1 | Date | |
|---|---|---|---|
| 5389369cef | |||
| 1fbc2140c7 | |||
| cf21dd3f76 | |||
| 8a2c93fb64 | |||
| 749e82ea14 | |||
| 8d58c3cf89 | |||
| f8fc8ace8e | |||
| dd79e07fb3 | |||
| da1a308c5d | |||
| b626dc6488 | |||
| 5ca5777c4b | |||
| 8729995338 | |||
| d57857d444 | |||
| ac272eadce | |||
| 59e7967458 | |||
| 59cd5477af | |||
| c3f84088aa | |||
| 548fc374ae |
@@ -40,6 +40,9 @@ class CarouselBrowsingPlugin(BehaviorPlugin):
|
||||
if not has_carousel_in_view(xml):
|
||||
return False
|
||||
|
||||
if ctx.shared_state.get("carousel_browsed"):
|
||||
return False
|
||||
|
||||
config = self.get_config(ctx)
|
||||
percentage = float(config.get("percentage", getattr(ctx.configs.args, "carousel_percentage", 0)))
|
||||
return random.random() < (percentage / 100.0)
|
||||
@@ -74,13 +77,25 @@ class CarouselBrowsingPlugin(BehaviorPlugin):
|
||||
# ── Curiosity Dwell ──
|
||||
if i == curiosity_slide:
|
||||
dwell = random.uniform(3.0, 7.0)
|
||||
logger.debug(f"📸 [Carousel] Curiosity Peak hit on slide {i+1}. " f"Gazing for {dwell:.1f}s...")
|
||||
logger.debug(f"📸 [Carousel] Curiosity Peak hit on slide {i+1}. Gazing for {dwell:.1f}s...")
|
||||
sleep(dwell * ctx.sleep_mod)
|
||||
|
||||
xml_before = ctx.device.dump_hierarchy()
|
||||
|
||||
# Horizontal swipe: Right to left
|
||||
humanized_horizontal_swipe(ctx.device, start_x=w * 0.8, end_x=w * 0.2, y=h * 0.5, duration_ms=250)
|
||||
|
||||
# Brief wait for transition to complete
|
||||
sleep(random.uniform(1.5, 2.5) * ctx.sleep_mod)
|
||||
|
||||
xml_after = ctx.device.dump_hierarchy()
|
||||
xml_delta = abs(len(xml_before) - len(xml_after))
|
||||
|
||||
if xml_before == xml_after or xml_delta < 50:
|
||||
logger.info(f"📸 [Carousel] End of carousel detected on slide {i+1} (UI stable). Stopping swipe.")
|
||||
break
|
||||
|
||||
sleep(random.uniform(1.0, 2.0) * ctx.sleep_mod)
|
||||
ctx.shared_state["carousel_browsed"] = True
|
||||
|
||||
return BehaviorResult(
|
||||
executed=True, interactions=count, metadata={"slides_viewed": count, "curiosity_slide": curiosity_slide}
|
||||
|
||||
@@ -35,7 +35,13 @@ class CloseFriendsGuardPlugin(BehaviorPlugin):
|
||||
return False
|
||||
|
||||
xml = ctx.context_xml if ctx.context_xml else ctx.device.dump_hierarchy()
|
||||
return "enge freunde" in xml.lower() or "close friend" in xml.lower()
|
||||
xml_lower = xml.lower()
|
||||
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
telepathic = TelepathicEngine.get_instance()
|
||||
classification = telepathic.classify_screen_content(xml_lower, "close_friends_content")
|
||||
return classification == "close_friends"
|
||||
|
||||
def execute(self, ctx: BehaviorContext) -> BehaviorResult:
|
||||
logger.info("💚 [CloseFriendsGuard] Close friends post detected. Skipping...")
|
||||
|
||||
@@ -54,15 +54,31 @@ class FollowPlugin(BehaviorPlugin):
|
||||
return True
|
||||
|
||||
def execute(self, ctx: BehaviorContext) -> BehaviorResult:
|
||||
"""Follow the target user."""
|
||||
"""Follow the target user. ONLY clicks 'Follow' buttons, never 'Following'."""
|
||||
nav_graph = ctx.cognitive_stack.get("nav_graph")
|
||||
if not nav_graph:
|
||||
from GramAddict.core.q_nav_graph import QNavGraph
|
||||
|
||||
nav_graph = QNavGraph(ctx.device)
|
||||
|
||||
if nav_graph.do("tap 'Follow' or 'Following' button"):
|
||||
logger.info(f"🤝 [Follow] Toggled Follow/Following state for @{ctx.username} ✓")
|
||||
# ── CRITICAL SAFETY GUARD ──
|
||||
# Pre-check: verify the button actually says "Follow" (not "Following" or "Requested").
|
||||
# Clicking "Following" opens a dangerous bottom sheet (Unfollow / Add to Favorites / Close Friends).
|
||||
xml = ctx.context_xml if ctx.context_xml else ctx.device.dump_hierarchy()
|
||||
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
telepathic = ctx.cognitive_stack.get("telepathic") or TelepathicEngine.get_instance()
|
||||
|
||||
classification = telepathic.classify_screen_content(xml.lower(), "profile_follow_status")
|
||||
if classification in ("following", "requested"):
|
||||
logger.info(
|
||||
f"🛡️ [Follow] Profile status is '{classification}' — user already followed. Skipping to avoid bottom sheet."
|
||||
)
|
||||
return BehaviorResult(executed=False, metadata={"reason": "already_following"})
|
||||
|
||||
if nav_graph.do("tap follow button"):
|
||||
logger.info(f"🤝 [Follow] Followed @{ctx.username} ✓")
|
||||
ctx.session_state.add_interaction(source=ctx.username, succeed=True, followed=True, scraped=False)
|
||||
|
||||
# Buffer for follow animations to close
|
||||
|
||||
@@ -47,21 +47,27 @@ class ObstacleGuardPlugin(BehaviorPlugin):
|
||||
logger.warning("⚠️ [ObstacleGuard] System permission dialog detected. Dismissing with BACK...")
|
||||
ctx.device.press("back")
|
||||
sleep(1.5 * ctx.sleep_mod)
|
||||
return BehaviorResult(executed=True, should_skip=True)
|
||||
res = BehaviorResult(executed=True, should_skip=True)
|
||||
res.skip_type = "no_scroll"
|
||||
return res
|
||||
|
||||
# ── Foreign App Takeover (e.g. browser opened, wrong app in foreground) ──
|
||||
if situation == SituationType.OBSTACLE_FOREIGN_APP:
|
||||
logger.warning("⚠️ [ObstacleGuard] Foreign app detected. Pressing BACK to recover...")
|
||||
ctx.device.press("back")
|
||||
sleep(1.5 * ctx.sleep_mod)
|
||||
return BehaviorResult(executed=True, should_skip=True)
|
||||
res = BehaviorResult(executed=True, should_skip=True)
|
||||
res.skip_type = "no_scroll"
|
||||
return res
|
||||
|
||||
# ── On-Screen Keyboard (e.g. hallucinated click on comment field) ──
|
||||
if situation == SituationType.OBSTACLE_KEYBOARD:
|
||||
logger.warning("⚠️ [ObstacleGuard] On-screen Keyboard is open. Pressing BACK to dismiss...")
|
||||
ctx.device.press("back")
|
||||
sleep(1.0 * ctx.sleep_mod)
|
||||
return BehaviorResult(executed=True, should_skip=True)
|
||||
res = BehaviorResult(executed=True, should_skip=True)
|
||||
res.skip_type = "no_scroll"
|
||||
return res
|
||||
|
||||
# ── Instagram Modal / Overlay (survey, "Not Now" prompt, creation flow) ──
|
||||
if situation == SituationType.OBSTACLE_MODAL:
|
||||
@@ -88,6 +94,8 @@ class ObstacleGuardPlugin(BehaviorPlugin):
|
||||
else:
|
||||
ctx.shared_state["consecutive_marker_misses"] = misses + 1
|
||||
|
||||
return BehaviorResult(executed=True, should_skip=True) # Restart loop for same post or next
|
||||
res = BehaviorResult(executed=True, should_skip=True) # Restart loop for same post or next
|
||||
res.skip_type = "no_scroll"
|
||||
return res
|
||||
|
||||
return BehaviorResult(executed=False)
|
||||
|
||||
@@ -56,19 +56,23 @@ class ProfileGuardPlugin(BehaviorPlugin):
|
||||
logger.info(f"🤝 [Profile Guard] Skipping own profile @{ctx.username}.")
|
||||
return BehaviorResult(executed=True, should_skip=True, metadata={"reason": "self_profile"})
|
||||
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
telepathic = TelepathicEngine.get_instance()
|
||||
|
||||
# Private account guard
|
||||
if "this account is private" in xml_check_lower or "konto ist privat" in xml_check_lower:
|
||||
if telepathic.classify_screen_content(xml_check_lower, "private_account") == "private":
|
||||
logger.info(f"🔒 [Profile Guard] @{ctx.username} is private.", extra={"color": f"{Fore.YELLOW}"})
|
||||
return BehaviorResult(executed=True, should_skip=True, metadata={"reason": "private"})
|
||||
|
||||
# Empty account guard
|
||||
if "no posts yet" in xml_check_lower or "noch keine beiträge" in xml_check_lower:
|
||||
if telepathic.classify_screen_content(xml_check_lower, "empty_account") == "empty":
|
||||
logger.info(f"📭 [Profile Guard] @{ctx.username} has no posts.", extra={"color": f"{Fore.YELLOW}"})
|
||||
return BehaviorResult(executed=True, should_skip=True, metadata={"reason": "empty"})
|
||||
|
||||
# Close friends guard
|
||||
if getattr(ctx.configs.args, "ignore_close_friends", False):
|
||||
if "enge freunde" in xml_check_lower or "close friend" in xml_check_lower:
|
||||
if telepathic.classify_screen_content(xml_check_lower, "close_friends_content") == "close_friends":
|
||||
logger.info(
|
||||
f"💚 [Profile Guard] @{ctx.username} is a Close Friend. Ignoring.", extra={"color": "\033[32m"}
|
||||
)
|
||||
|
||||
@@ -34,6 +34,16 @@ class RabbitHolePlugin(BehaviorPlugin):
|
||||
if res_score < 0.8:
|
||||
return False
|
||||
|
||||
screen_type = ctx.shared_state.get("current_screen_type")
|
||||
if not screen_type and ctx.context_xml:
|
||||
from GramAddict.core.perception.screen_identity import ScreenIdentity
|
||||
screen_type = ScreenIdentity(getattr(ctx, "username", "")).identify(ctx.context_xml).get("screen_type")
|
||||
|
||||
from GramAddict.core.perception.screen_identity import ScreenType
|
||||
valid_screens = [ScreenType.HOME_FEED, ScreenType.EXPLORE_GRID, ScreenType.REELS_FEED]
|
||||
if screen_type not in valid_screens:
|
||||
return False
|
||||
|
||||
config = self.get_config(ctx)
|
||||
percentage = float(config.get("percentage", 15))
|
||||
return random.random() < (percentage / 100.0)
|
||||
|
||||
@@ -61,12 +61,11 @@ class StoryViewPlugin(BehaviorPlugin):
|
||||
# Check for story ring
|
||||
xml = ctx.context_xml or ctx.device.dump_hierarchy()
|
||||
xml_lower = xml.lower()
|
||||
has_story_ring = (
|
||||
"reel_ring" in xml
|
||||
or "has an unseen story" in xml_lower
|
||||
or "has a new story" in xml_lower
|
||||
or "story von" in xml_lower
|
||||
)
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
telepathic = ctx.cognitive_stack.get("telepathic") or TelepathicEngine.get_instance()
|
||||
|
||||
has_story_ring = telepathic.classify_screen_content(xml_lower, "story_ring_presence") == "has_unseen_story"
|
||||
|
||||
if not has_story_ring and not is_already_in_story:
|
||||
return BehaviorResult(executed=False, metadata={"reason": "no_story"})
|
||||
@@ -96,12 +95,44 @@ class StoryViewPlugin(BehaviorPlugin):
|
||||
for i in range(count):
|
||||
sleep(random.uniform(2.0, 5.0) * ctx.sleep_mod)
|
||||
if i < count - 1:
|
||||
humanized_click(ctx.device, int(w * 0.9), int(h * 0.5), sleep_mod=ctx.sleep_mod)
|
||||
# Atomic state validation after click
|
||||
# Atomic state validation before click
|
||||
xml_dump = ctx.device.dump_hierarchy()
|
||||
if not xml_dump:
|
||||
continue
|
||||
packages = set(re.findall(r'package="([^"]+)"', xml_dump))
|
||||
|
||||
# Query VLM to find the interactive area for the next segment
|
||||
intent = "tap right side of screen to view next story segment"
|
||||
node = ctx.telepathic.find_best_node(xml_dump, intent, device=ctx.device, track=False)
|
||||
|
||||
if node:
|
||||
logger.debug(
|
||||
f"📸 [StoryView] VLM selected node '{node.resource_id or node.content_desc or 'unknown'}' for next segment."
|
||||
)
|
||||
# If VLM selects a large container (e.g. the entire screen or story viewer),
|
||||
# we must tap its right side, not its exact center, to avoid pausing the story.
|
||||
if getattr(node, "area", 0) > (w * h * 0.4):
|
||||
target_x = node.x1 + int((node.x2 - node.x1) * 0.85)
|
||||
target_y = node.y1 + int((node.y2 - node.y1) * 0.25)
|
||||
logger.debug(
|
||||
f"📸 [StoryView] Adjusting click to top-right quadrant of large container: ({target_x}, {target_y})"
|
||||
)
|
||||
else:
|
||||
target_x = node.center_x
|
||||
target_y = node.center_y
|
||||
|
||||
humanized_click(ctx.device, target_x, target_y, sleep_mod=ctx.sleep_mod)
|
||||
else:
|
||||
logger.warning(
|
||||
"📸 [StoryView] VLM could not resolve next story segment. Falling back to geometric safety quadrant."
|
||||
)
|
||||
# Click top-right to avoid 'reply' input fields and most stickers
|
||||
humanized_click(ctx.device, int(w * 0.85), int(h * 0.25), sleep_mod=ctx.sleep_mod)
|
||||
|
||||
# Verify we didn't leave Instagram
|
||||
xml_dump_after = ctx.device.dump_hierarchy()
|
||||
if not xml_dump_after:
|
||||
continue
|
||||
packages = set(re.findall(r'package="([^"]+)"', xml_dump_after))
|
||||
app_id = getattr(ctx.device, "app_id", "com.instagram.android")
|
||||
if packages and app_id not in packages:
|
||||
logger.error(
|
||||
@@ -121,10 +152,14 @@ class StoryViewPlugin(BehaviorPlugin):
|
||||
if not xml_dump:
|
||||
break
|
||||
xml_lower = xml_dump.lower()
|
||||
if "com.instagram.android" not in xml_dump or (
|
||||
"row 1, column 1" in xml_lower or "tab" in xml_lower or "home" in xml_lower or "search" in xml_lower
|
||||
):
|
||||
# Successfully back to a main view or outside instagram
|
||||
if "com.instagram.android" not in xml_dump:
|
||||
break
|
||||
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
telepathic = ctx.cognitive_stack.get("telepathic") or TelepathicEngine.get_instance()
|
||||
if telepathic.classify_screen_content(xml_lower, "main_feed_presence") == "main_feed":
|
||||
# Successfully back to a main view
|
||||
break
|
||||
logger.warning(
|
||||
f"⚠️ [StoryView] Still trapped in story/overlay after back press (attempt {attempt+1}). Pressing back again."
|
||||
|
||||
@@ -660,9 +660,6 @@ def start_bot(**kwargs):
|
||||
logger.debug(f"Failed to trigger VRAM cleanup: {e}")
|
||||
|
||||
|
||||
# FEED_MARKERS: imported from GramAddict.core.perception.feed_analysis (see top imports)
|
||||
|
||||
|
||||
def _wait_for_post_loaded(device, timeout=5, nav_graph=None):
|
||||
"""Delegate to physics.timing. See GramAddict.core.physics.timing."""
|
||||
return _wait_for_post_loaded_impl(device, timeout=timeout, nav_graph=nav_graph)
|
||||
@@ -717,15 +714,18 @@ def _interact_with_profile(device, configs, username, session_state, sleep_mod,
|
||||
if not isinstance(xml_check, str):
|
||||
return
|
||||
|
||||
# ── 1. Profile Guards (Private / Empty / Close Friends) ──
|
||||
# Using structural resource-ID fragments to avoid localization debt
|
||||
if "private_profile_empty_state" in xml_check or "private_profile_container" in xml_check:
|
||||
# ── 1. Profile Guards (Private / Empty / Close Friends) via Telepathic Engine ──
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
telepathic = cognitive_stack.get("telepathic") or TelepathicEngine.get_instance()
|
||||
|
||||
if telepathic.classify_screen_content(xml_check.lower(), "private_account") == "private":
|
||||
logger.info(
|
||||
f"🔒 [Profile Guard] @{username} is private. Aborting deep interaction.", extra={"color": f"{Fore.YELLOW}"}
|
||||
)
|
||||
return
|
||||
|
||||
if "empty_state_view" in xml_check or "no_posts_yet" in xml_check:
|
||||
if telepathic.classify_screen_content(xml_check.lower(), "empty_account") == "empty":
|
||||
logger.info(
|
||||
f"📭 [Profile Guard] @{username} has no posts. Aborting deep interaction.",
|
||||
extra={"color": f"{Fore.YELLOW}"},
|
||||
@@ -733,7 +733,7 @@ def _interact_with_profile(device, configs, username, session_state, sleep_mod,
|
||||
return
|
||||
|
||||
if getattr(configs.args, "ignore_close_friends", False):
|
||||
if "close_friends_badge" in xml_check or "profile_header_close_friend" in xml_check:
|
||||
if telepathic.classify_screen_content(xml_check.lower(), "close_friends_content") == "close_friends":
|
||||
logger.info(
|
||||
f"💚 [Profile Guard] @{username} is a Close Friend. Ignoring completely.", extra={"color": "\033[32m"}
|
||||
)
|
||||
@@ -897,10 +897,12 @@ def _run_zero_latency_stories_loop(device, configs, session_state, cognitive_sta
|
||||
if not xml_dump:
|
||||
break
|
||||
xml_lower = xml_dump.lower()
|
||||
if app_id in xml_dump and (
|
||||
"row 1, column 1" in xml_lower or "tab" in xml_lower or "home" in xml_lower or "search" in xml_lower
|
||||
):
|
||||
break
|
||||
if app_id in xml_dump:
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
telepathic = cognitive_stack.get("telepathic") or TelepathicEngine.get_instance()
|
||||
if telepathic.classify_screen_content(xml_lower, "main_feed_presence") == "main_feed":
|
||||
break
|
||||
logger.warning(
|
||||
f"⚠️ [StoriesFeed] Still trapped after back press (attempt {attempt+1}). Pressing back again."
|
||||
)
|
||||
@@ -909,7 +911,10 @@ def _run_zero_latency_stories_loop(device, configs, session_state, cognitive_sta
|
||||
return "FEED_EXHAUSTED"
|
||||
|
||||
if getattr(configs.args, "ignore_close_friends", False):
|
||||
if "enge freunde" in xml_dump.lower() or "close friend" in xml_dump.lower():
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
telepathic = cognitive_stack.get("telepathic") or TelepathicEngine.get_instance()
|
||||
if telepathic.classify_screen_content(xml_dump.lower(), "close_friends_content") == "close_friends":
|
||||
logger.info(
|
||||
"💚 [Anti-Friend] Story is from a Close Friend. Swiping horizontally to skip User.",
|
||||
extra={"color": "\\033[32m"},
|
||||
@@ -934,10 +939,12 @@ def _run_zero_latency_stories_loop(device, configs, session_state, cognitive_sta
|
||||
if not xml_dump:
|
||||
break
|
||||
xml_lower = xml_dump.lower()
|
||||
if "com.instagram.android" in xml_dump and (
|
||||
"row 1, column 1" in xml_lower or "tab" in xml_lower or "home" in xml_lower or "search" in xml_lower
|
||||
):
|
||||
break
|
||||
if "com.instagram.android" in xml_dump:
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
telepathic = cognitive_stack.get("telepathic") or TelepathicEngine.get_instance()
|
||||
if telepathic.classify_screen_content(xml_lower, "main_feed_presence") == "main_feed":
|
||||
break
|
||||
logger.warning(
|
||||
f"⚠️ [StoriesFeed] Still trapped in story/overlay after back press (attempt {attempt+1}). Pressing back again."
|
||||
)
|
||||
@@ -1134,6 +1141,9 @@ def _run_zero_latency_feed_loop(
|
||||
elif skip_type == "fast":
|
||||
_humanized_scroll(device, is_skip=True)
|
||||
sleep(1.0 * sleep_mod)
|
||||
elif skip_type == "no_scroll":
|
||||
logger.debug("⏭️ Skipping post without scrolling (e.g., dismissing obstacle).")
|
||||
sleep(1.0 * sleep_mod)
|
||||
else:
|
||||
_humanized_scroll(device, is_skip=False)
|
||||
sleep(1.5 * sleep_mod)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import logging
|
||||
import random
|
||||
import re
|
||||
import time
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
@@ -341,36 +340,10 @@ class DarwinEngine(QdrantBase):
|
||||
|
||||
def _has_comments(self, xml_string: str) -> bool:
|
||||
"""
|
||||
Heuristic to check if a post actually has comments to read.
|
||||
If it has 0 comments, checking them is suspicious bot behavior.
|
||||
|
||||
Zero-Maintenance: Uses only English text and resource_id patterns.
|
||||
Resource IDs are locale-invariant. English text in content_desc
|
||||
is used by Instagram internally and is reliable.
|
||||
Delegates detection of comments to the Telepathic Engine's VLM to ensure
|
||||
zero maintenance and no hardcoded locale strings or resource-ids.
|
||||
"""
|
||||
low_xml = xml_string.lower()
|
||||
|
||||
# 1. Explicit zero comments check (resource_id based + English fallback)
|
||||
if re.search(r"\b0\s*comment(?:s)?\b", low_xml):
|
||||
return False
|
||||
|
||||
# 2. Check for "view all" or similar prominent comment link texts
|
||||
if "view all" in low_xml:
|
||||
return True
|
||||
if "view 1 comment" in low_xml:
|
||||
return True
|
||||
if "comment number is" in low_xml:
|
||||
return True
|
||||
|
||||
# 3. Structural: comment_textview_layout is present with a count > 0
|
||||
has_number_of_comments = re.search(r"\b([1-9][0-9.,]*)\s*comment(?:s)?\b", low_xml)
|
||||
if has_number_of_comments:
|
||||
return True
|
||||
|
||||
# 4. Structural: The comment button resource_id exists and has content
|
||||
if "row_feed_comment_textview_layout" in low_xml:
|
||||
return True
|
||||
|
||||
# If no indicators are found, assume the post has 0 comments.
|
||||
return False
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
telepathic = TelepathicEngine.get_instance()
|
||||
return telepathic.classify_screen_content(xml_string.lower(), "post_has_comments") == "has_comments"
|
||||
|
||||
@@ -111,7 +111,7 @@ def _run_zero_latency_dm_loop(device, zero_engine, nav_graph, configs, session_s
|
||||
|
||||
# Step 1: Find unread conversation threads
|
||||
unread_threads = telepathic._extract_semantic_nodes(
|
||||
xml_dump, "find unread message threads or unread badges", threshold=0.7
|
||||
xml_dump, "find unread message threads or unread badges", threshold=0.7, device=device
|
||||
)
|
||||
|
||||
if unread_threads and not unread_threads[0].get("skip"):
|
||||
@@ -123,7 +123,7 @@ def _run_zero_latency_dm_loop(device, zero_engine, nav_graph, configs, session_s
|
||||
# Step 2: Read the conversation context
|
||||
thread_xml = device.dump_hierarchy()
|
||||
msg_nodes = telepathic._extract_semantic_nodes(
|
||||
thread_xml, "find the last received message text", threshold=0.6
|
||||
thread_xml, "find the last received message text", threshold=0.6, device=device
|
||||
)
|
||||
|
||||
context_text = "No previous context"
|
||||
@@ -174,7 +174,7 @@ def _run_zero_latency_dm_loop(device, zero_engine, nav_graph, configs, session_s
|
||||
response_text = response_dict["response"].strip()
|
||||
# Find the input field
|
||||
input_nodes = telepathic._extract_semantic_nodes(
|
||||
thread_xml, "find the message input text field", threshold=0.7
|
||||
thread_xml, "find the message input text field", threshold=0.7, device=device
|
||||
)
|
||||
if input_nodes and not input_nodes[0].get("skip"):
|
||||
in_node = input_nodes[0]
|
||||
@@ -188,7 +188,7 @@ def _run_zero_latency_dm_loop(device, zero_engine, nav_graph, configs, session_s
|
||||
# Find Send button
|
||||
send_xml = device.dump_hierarchy()
|
||||
send_nodes = telepathic._extract_semantic_nodes(
|
||||
send_xml, "find the send message button", threshold=0.8
|
||||
send_xml, "find the send message button", threshold=0.8, device=device
|
||||
)
|
||||
|
||||
if send_nodes and not send_nodes[0].get("skip"):
|
||||
|
||||
@@ -234,6 +234,11 @@ class GoalExecutor:
|
||||
explored_nav_actions.clear()
|
||||
visited_screens.clear()
|
||||
consecutive_back_presses = 0
|
||||
# CRITICAL: Also clear the planner's learned traps.
|
||||
# Without this, traps learned before restart persist and
|
||||
# immediately re-trap the bot on the same (or similar) screen.
|
||||
if hasattr(self, "planner") and hasattr(self.planner, "knowledge"):
|
||||
self.planner.knowledge.clear_traps()
|
||||
continue
|
||||
|
||||
# Check if it was a navigation action (vs a goal action). If we are not on the required screen,
|
||||
@@ -524,6 +529,14 @@ class GoalExecutor:
|
||||
|
||||
# Re-perceive to ensure ContextGate has fresh data
|
||||
current_screen = self.perceive()
|
||||
|
||||
# Guard: Verify the action is physically available!
|
||||
# If not, the memorized path is stale/invalid for the current physical UI state.
|
||||
available = current_screen.get("available_actions", [])
|
||||
if action not in available and action != "force start instagram" and "scroll" not in action:
|
||||
logger.warning(f"⚠️ [GOAP Recall] Recalled action '{action}' is NOT available on screen! Path is stale.")
|
||||
return False
|
||||
|
||||
success = self._execute_action(action, goal=goal, screen_state=current_screen)
|
||||
if not success:
|
||||
logger.warning(f"⚠️ [GOAP Recall] Step '{action}' failed. Path may be stale.")
|
||||
|
||||
@@ -164,11 +164,28 @@ class NavigationKnowledge:
|
||||
pass
|
||||
return None
|
||||
|
||||
def clear_traps(self):
|
||||
"""Clear all in-memory traps. Called after force-restart to allow fresh routing."""
|
||||
count = len(self._learned_traps)
|
||||
self._learned_traps.clear()
|
||||
if count > 0:
|
||||
logger.info(f"🧹 [NavigationKnowledge] Cleared {count} in-memory traps for fresh routing.")
|
||||
|
||||
def learn_trap(self, screen_type: ScreenType, action: str, trap_reason: str = "softlock"):
|
||||
"""Aversively learn that an action on a screen is dangerous/useless."""
|
||||
trap_key = f"{screen_type.name}_{action}"
|
||||
self._learned_traps.add(trap_key)
|
||||
|
||||
# GUARD: Never persist traps for UNKNOWN screens to Qdrant.
|
||||
# UNKNOWN is a catch-all — persisting traps here permanently blocks
|
||||
# ALL unidentified screens, creating inescapable dead-ends.
|
||||
if screen_type == ScreenType.UNKNOWN:
|
||||
logger.warning(
|
||||
f"🛡️ [Aversive Learning] Trap '{action}' on UNKNOWN kept in-memory only (not persisted). "
|
||||
"UNKNOWN is a catch-all — permanent traps here block all unidentified screens."
|
||||
)
|
||||
return
|
||||
|
||||
if not self._db or not self._db.is_connected:
|
||||
return
|
||||
|
||||
@@ -185,7 +202,11 @@ class NavigationKnowledge:
|
||||
logger.error(f"💀 [Aversive Learning] BURNED action '{action}' on {screen_type.name} due to: {trap_reason}")
|
||||
|
||||
def is_trap(self, screen_type: ScreenType, action: str) -> bool:
|
||||
"""Check if an action on this screen is a known trap."""
|
||||
"""Check if an action on this screen is a known trap.
|
||||
|
||||
Traps have time-based expiry: entries older than 30 minutes are
|
||||
auto-forgiven and deleted from Qdrant to prevent permanent dead-ends.
|
||||
"""
|
||||
from GramAddict.core.screen_topology import ScreenTopology
|
||||
|
||||
if ScreenTopology.is_structural_action(screen_type, action):
|
||||
@@ -198,6 +219,8 @@ class NavigationKnowledge:
|
||||
if not self._db or not self._db.is_connected:
|
||||
return False
|
||||
|
||||
TRAP_EXPIRY_SECONDS = 1800 # 30 minutes: old traps expire
|
||||
|
||||
try:
|
||||
from qdrant_client.models import FieldCondition, Filter, MatchValue
|
||||
|
||||
@@ -212,6 +235,20 @@ class NavigationKnowledge:
|
||||
limit=1,
|
||||
)[0]
|
||||
if results:
|
||||
timestamp = results[0].payload.get("timestamp", 0)
|
||||
age_seconds = time.time() - timestamp
|
||||
|
||||
# Time-based expiry: old traps are forgiven
|
||||
if age_seconds > TRAP_EXPIRY_SECONDS:
|
||||
logger.info(
|
||||
f"🔄 [Aversive Decay] Forgave expired trap '{action}' on {screen_type.name} "
|
||||
f"(age: {age_seconds/60:.0f}min). Allowing re-exploration."
|
||||
)
|
||||
# Delete the stale trap from Qdrant
|
||||
seed = f"trap_{trap_key}"
|
||||
self._db.delete_point(seed)
|
||||
return False
|
||||
|
||||
self._learned_traps.add(trap_key)
|
||||
return True
|
||||
except Exception:
|
||||
|
||||
@@ -60,11 +60,15 @@ class GoalPlanner:
|
||||
from GramAddict.core.screen_topology import ScreenTopology
|
||||
|
||||
# Interaction goals (context-specific, not navigation)
|
||||
if "like" in goal and context.get("is_liked") is True:
|
||||
return True
|
||||
if "view profile" in goal and screen_type in (ScreenType.OWN_PROFILE, ScreenType.OTHER_PROFILE):
|
||||
return True
|
||||
|
||||
if "like" in goal and "post" in goal:
|
||||
return context.get("is_liked", False) is True
|
||||
|
||||
if "follow" in goal and "user" in goal:
|
||||
return context.get("is_followed", False) is True
|
||||
|
||||
# Navigation goals — delegate to SSOT
|
||||
target = ScreenTopology.goal_to_target_screen(goal)
|
||||
if target and screen_type == target:
|
||||
@@ -115,20 +119,6 @@ class GoalPlanner:
|
||||
noop_actions.add(action)
|
||||
logger.debug(f"🛡️ [Anti-Loop Guard] Stripping '{action}' — leads to visited {expected.name}")
|
||||
|
||||
# Also strip actions where the HD Map says they go TO the current screen from OTHER screens
|
||||
for src_screen, transitions in ScreenTopology.TRANSITIONS.items():
|
||||
if src_screen == screen_type:
|
||||
continue # We already handled this screen's own transitions
|
||||
for action, dest in transitions.items():
|
||||
if dest == screen_type and action in available:
|
||||
noop_actions.add(action)
|
||||
logger.debug(
|
||||
f"🛡️ [No-Op Guard] Stripping '{action}' — known to navigate to current {screen_type.name}"
|
||||
)
|
||||
elif dest in visited_screens and action in available and action != "press back":
|
||||
noop_actions.add(action)
|
||||
logger.debug(f"🛡️ [Anti-Loop Guard] Stripping '{action}' — known to navigate to visited {dest.name}")
|
||||
|
||||
available = [a for a in available if a not in noop_actions]
|
||||
|
||||
# Build avoid_actions for HD Map route planning
|
||||
@@ -143,6 +133,8 @@ class GoalPlanner:
|
||||
if count >= 2:
|
||||
avoid_actions.add(key)
|
||||
|
||||
available = [a for a in available if a not in avoid_actions]
|
||||
|
||||
target_screen = ScreenTopology.goal_to_target_screen(goal)
|
||||
|
||||
# ── 1. HD Map Pre-Check for Dead Ends ──
|
||||
@@ -160,9 +152,31 @@ class GoalPlanner:
|
||||
# Ground UI transitions in structural invariants. If the topological map knows the route, use it.
|
||||
target_screen = ScreenTopology.goal_to_target_screen(goal)
|
||||
if target_screen and target_screen != screen_type:
|
||||
route = ScreenTopology.find_route(screen_type, target_screen, avoid_actions=avoid_actions)
|
||||
if route:
|
||||
# We use a while loop to dynamically recalculate routes if proposed actions are missing from the UI
|
||||
current_avoid = avoid_actions.copy()
|
||||
while True:
|
||||
route = ScreenTopology.find_route(screen_type, target_screen, avoid_actions=current_avoid)
|
||||
if not route:
|
||||
# If we exhausted all topological routes because UI elements are missing
|
||||
# (e.g. tabs are hidden in a nested profile/post view), the deterministic
|
||||
# escape hatch is to press back to pop the navigation stack.
|
||||
if "press back" in available and "press back" not in current_avoid:
|
||||
logger.warning(
|
||||
"🛡️ [HD Map Guard] All routes blocked/missing. Falling back to 'press back' to escape nested view."
|
||||
)
|
||||
return "press back"
|
||||
break
|
||||
|
||||
next_action, next_screen = route[0]
|
||||
|
||||
# Check if the action is physically available on the screen
|
||||
if next_action not in available and next_action != "force start instagram":
|
||||
logger.warning(
|
||||
f"🛡️ [HD Map Guard] Action '{next_action}' is NOT available on screen. Recalculating route."
|
||||
)
|
||||
current_avoid.add(next_action)
|
||||
continue
|
||||
|
||||
# Verify action isn't explored/trapped
|
||||
if next_action not in (explored_nav_actions or set()):
|
||||
if not self.knowledge.is_trap(screen_type, next_action):
|
||||
@@ -172,11 +186,13 @@ class GoalPlanner:
|
||||
)
|
||||
return next_action
|
||||
else:
|
||||
logger.warning(f"🛡️ [HD Map] Route action '{next_action}' is trapped. Skipping HD Map.")
|
||||
logger.warning(f"🛡️ [HD Map] Route action '{next_action}' is trapped. Recalculating route.")
|
||||
current_avoid.add(next_action)
|
||||
else:
|
||||
logger.debug(
|
||||
f"🛡️ [HD Map] Route action '{next_action}' already explored and failed. Skipping HD Map."
|
||||
f"🛡️ [HD Map] Route action '{next_action}' already explored and failed. Recalculating route."
|
||||
)
|
||||
current_avoid.add(next_action)
|
||||
|
||||
# ── 2.5. ContextGate Feedback Loop ──
|
||||
# Preempt the brain from hallucinating banned interaction intents.
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
"""Perception — Feed and Content Analysis."""
|
||||
|
||||
from GramAddict.core.perception.feed_analysis import (
|
||||
CAROUSEL_INDICATORS,
|
||||
FEED_MARKERS,
|
||||
extract_post_content,
|
||||
has_carousel_in_view,
|
||||
has_feed_markers,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"FEED_MARKERS",
|
||||
"CAROUSEL_INDICATORS",
|
||||
"has_carousel_in_view",
|
||||
"extract_post_content",
|
||||
"has_feed_markers",
|
||||
|
||||
@@ -143,6 +143,23 @@ class ActionMemory:
|
||||
|
||||
self._last_click_context = None
|
||||
|
||||
def _compute_structural_delta(self, pre_xml: str, post_xml: str) -> dict:
|
||||
"""Computes a semantic diff between two XML states."""
|
||||
import re
|
||||
|
||||
pre_ids = set(re.findall(r'resource-id="([^"]+)"', pre_xml))
|
||||
post_ids = set(re.findall(r'resource-id="([^"]+)"', post_xml))
|
||||
|
||||
pre_selected = set(re.findall(r'selected="true"[^>]*resource-id="([^"]+)"', pre_xml))
|
||||
post_selected = set(re.findall(r'selected="true"[^>]*resource-id="([^"]+)"', post_xml))
|
||||
|
||||
return {
|
||||
"new_ids": post_ids - pre_ids,
|
||||
"removed_ids": pre_ids - post_ids,
|
||||
"selection_changed": pre_selected != post_selected,
|
||||
"id_delta_count": len(post_ids.symmetric_difference(pre_ids)),
|
||||
}
|
||||
|
||||
def verify_success(
|
||||
self, intent: str, pre_click_xml: str, post_click_xml: str, device=None, confidence: float = 0.0
|
||||
) -> Optional[bool]:
|
||||
@@ -150,46 +167,11 @@ class ActionMemory:
|
||||
Structural and Visual verification: Did the UI actually change after the click?
|
||||
"""
|
||||
intent_lower = intent.lower()
|
||||
post_xml_lower = post_click_xml.lower()
|
||||
|
||||
# Specific check for opening a post (from explore/profile grid)
|
||||
if "view a post" in intent_lower or "first image" in intent_lower or "grid item" in intent_lower:
|
||||
if (
|
||||
"row_feed_photo_imageview" in post_xml_lower
|
||||
or "row_feed_button_like" in post_xml_lower
|
||||
or "clips_viewer_view_pager" in post_xml_lower
|
||||
):
|
||||
return True
|
||||
if (
|
||||
"explore_action_bar" in post_xml_lower
|
||||
and "row_feed_button_like" not in post_xml_lower
|
||||
and "clips_viewer" not in post_xml_lower
|
||||
):
|
||||
logger.warning(f"⚠️ [ActionMemory] Still on grid after trying to '{intent}'. Verification FAIL.")
|
||||
return False # Still on grid, definitely failed
|
||||
|
||||
# Specific check for opening a profile
|
||||
if "profile" in intent_lower or "author" in intent_lower or "username" in intent_lower:
|
||||
if "profile_header_container" in post_xml_lower:
|
||||
logger.info("✅ [ActionMemory] Structural check confirmed profile navigation success.")
|
||||
return True
|
||||
else:
|
||||
logger.warning(
|
||||
f"⚠️ [ActionMemory] Profile header NOT found after trying to '{intent}'. Verification FAIL."
|
||||
)
|
||||
return False
|
||||
|
||||
# Specific check for navigating to Home Feed
|
||||
if "home feed" in intent_lower or "home tab" in intent_lower:
|
||||
if "main_feed_action_bar" in post_xml_lower:
|
||||
logger.info("✅ [ActionMemory] Structural check confirmed Home Feed navigation success.")
|
||||
return True
|
||||
|
||||
# Specific check for navigating to Explore Feed
|
||||
if "explore feed" in intent_lower or "explore tab" in intent_lower or "search" in intent_lower:
|
||||
if "explore_action_bar" in post_xml_lower or "action_bar_search_edit_text" in post_xml_lower:
|
||||
logger.info("✅ [ActionMemory] Structural check confirmed Explore Feed navigation success.")
|
||||
return True
|
||||
# ALL HARDCODED UI STRUCTURAL VERIFICATION GUARDS HAVE BEEN PURGED!
|
||||
# Rule: ZERO MAINTENANCE. We do not hardcode Resource IDs to verify if a navigation
|
||||
# was successful (e.g., checking for 'profile_header_container' or 'main_feed_action_bar').
|
||||
# Success verification MUST rely entirely on the VLM visual feedback and the Structural Delta diff.
|
||||
|
||||
state_toggles = ["like", "save", "follow", "heart"]
|
||||
is_toggle = any(t in intent_lower for t in state_toggles)
|
||||
@@ -198,9 +180,10 @@ class ActionMemory:
|
||||
# We NO LONGER bypass VLM verification via string matching.
|
||||
# If confidence is < 0.95, we always do VLM or Delta verification.
|
||||
|
||||
# ── VLM Verification Fallback ──
|
||||
# ── VLM Verification (soft signal, NOT sole authority) ──
|
||||
|
||||
# If we are highly confident (e.g. pulled from Qdrant memory), bypass heavy VLM
|
||||
vlm_verdict = None
|
||||
if device and confidence < 0.95:
|
||||
logger.info(
|
||||
f"👁️ [ActionMemory] Confidence ({confidence:.2f}) < 0.95. Handing over verification for '{intent}' to VLM visual analysis..."
|
||||
@@ -214,7 +197,6 @@ class ActionMemory:
|
||||
if self._last_click_context:
|
||||
clicked_context = f"The element that was tapped: {self._last_click_context['semantic_string']}. "
|
||||
|
||||
# Ask VLM to be the absolute source of truth
|
||||
prompt = (
|
||||
f"The user just attempted to perform the action: '{intent}'. "
|
||||
f"{clicked_context}"
|
||||
@@ -234,7 +216,7 @@ class ActionMemory:
|
||||
f"If the intent was to open a profile, are you on a profile page? "
|
||||
f"If the intent was to go back, are you on the previous screen? "
|
||||
)
|
||||
prompt += "Answer ONLY with the word YES or NO."
|
||||
prompt += 'Answer ONLY with a valid JSON object exactly matching this schema: {"success": true} or {"success": false}. DO NOT add any other keys.'
|
||||
|
||||
try:
|
||||
screenshot = device.get_screenshot_b64()
|
||||
@@ -242,19 +224,21 @@ class ActionMemory:
|
||||
raise ValueError("No screenshot available from device")
|
||||
response = evaluator._query_vlm(prompt, screenshot)
|
||||
|
||||
decision = _parse_yes_no(response) if response else None
|
||||
vlm_verdict = _parse_yes_no(response) if response else None
|
||||
|
||||
if decision is True:
|
||||
if vlm_verdict is True:
|
||||
logger.debug(f"🧠 [ActionMemory] VLM visually confirmed success for '{intent}'.")
|
||||
return True
|
||||
elif decision is False:
|
||||
logger.warning(
|
||||
f"⚠️ [ActionMemory] VLM visual verification FAILED for '{intent}'. VLM replied: '{response}'"
|
||||
elif vlm_verdict is False:
|
||||
# VLM says false — but small local VLMs (7B) are unreliable.
|
||||
# Do NOT trust this blindly. Fallthrough to structural delta verification
|
||||
# which is the ground-truth tiebreaker.
|
||||
logger.info(
|
||||
f"🧠 [ActionMemory] VLM says '{intent}' failed — but VLM is unreliable. "
|
||||
"Falling through to structural delta for ground-truth verification."
|
||||
)
|
||||
return False
|
||||
# DO NOT return False here — let structural delta decide
|
||||
else:
|
||||
# VLM returned ambiguous response (JSON, mixed signals, etc.)
|
||||
# Don't treat as hard failure — fall through to structural delta verification
|
||||
logger.debug(
|
||||
f"🧠 [ActionMemory] VLM response for '{intent}' was not YES/NO "
|
||||
f"(got: '{response[:80]}...'). Falling through to structural verification."
|
||||
@@ -267,30 +251,32 @@ class ActionMemory:
|
||||
# If the delta matches, we trust it. No more static string restrictions.
|
||||
|
||||
# ── Structural Delta Verification ──
|
||||
diff = abs(len(pre_click_xml) - len(post_click_xml))
|
||||
diff = self._compute_structural_delta(pre_click_xml, post_click_xml)
|
||||
|
||||
if is_toggle:
|
||||
if diff > 1000:
|
||||
if diff["id_delta_count"] > 10:
|
||||
logger.warning(
|
||||
f"⚠️ [ActionMemory] Massive structural shift ({diff} chars) for state-toggle '{intent}'. Navigated away by mistake? Verification FAIL."
|
||||
f"⚠️ [ActionMemory] Massive structural shift ({diff['id_delta_count']} nodes) for state-toggle '{intent}'. Navigated away by mistake? Verification FAIL."
|
||||
)
|
||||
return False
|
||||
if diff > 0:
|
||||
if diff["id_delta_count"] > 0 or diff["selection_changed"]:
|
||||
logger.debug(f"🧠 [ActionMemory] Structural delta detected for toggle '{intent}'. Verification PASS.")
|
||||
return True
|
||||
logger.warning(
|
||||
f"⚠️ [ActionMemory] Zero structural shift (diff={diff}) for state-toggle '{intent}'. Verification FAIL."
|
||||
)
|
||||
logger.warning(f"⚠️ [ActionMemory] Zero structural shift for state-toggle '{intent}'. Verification FAIL.")
|
||||
return False
|
||||
|
||||
# ── Non-Toggle Structural Delta ──
|
||||
if diff > 50:
|
||||
# A click (non-toggle) should change something on screen (e.g., popup, screen transition)
|
||||
# Even scrolling will load new items (so new IDs will be present).
|
||||
# We look for at least a few ID changes. Let's say >= 2 to be safe against random background updates,
|
||||
# or if the selection changed.
|
||||
if diff["id_delta_count"] >= 1 or diff["selection_changed"]:
|
||||
logger.debug(
|
||||
f"🧠 [ActionMemory] Structural change detected ({diff} chars) for '{intent}'. Verification PASS."
|
||||
f"🧠 [ActionMemory] Structural change detected ({diff['id_delta_count']} nodes) for '{intent}'. Verification PASS."
|
||||
)
|
||||
return True
|
||||
|
||||
logger.warning(
|
||||
f"⚠️ [ActionMemory] Insufficient structural change (diff={diff}) for non-toggle '{intent}'. Verification FAIL."
|
||||
f"⚠️ [ActionMemory] Insufficient structural change (delta=0) for non-toggle '{intent}'. Verification FAIL."
|
||||
)
|
||||
return False
|
||||
|
||||
@@ -67,6 +67,14 @@ ALLOWED_SCREENS: Dict[str, FrozenSet[ScreenType]] = {
|
||||
ScreenType.STORY_VIEW, # toolbar_reshare_button exists on stories
|
||||
}
|
||||
),
|
||||
"tap post username": frozenset(
|
||||
{
|
||||
ScreenType.HOME_FEED,
|
||||
ScreenType.POST_DETAIL,
|
||||
ScreenType.REELS_FEED,
|
||||
ScreenType.EXPLORE_GRID,
|
||||
}
|
||||
),
|
||||
}
|
||||
|
||||
# Intent keywords that trigger the categorical ban check
|
||||
|
||||
@@ -14,36 +14,20 @@ import xml.etree.ElementTree as ET
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ── Feed Presence Markers ──
|
||||
# Resource IDs that confirm we're looking at a loaded feed post.
|
||||
# Used by _wait_for_post_loaded and other wait loops.
|
||||
FEED_MARKERS = [
|
||||
"row_feed_photo_profile_name",
|
||||
"row_feed_profile_header",
|
||||
"row_feed_photo_imageview",
|
||||
"clips_media_component",
|
||||
"clips_video_container",
|
||||
"clips_viewer_container",
|
||||
"clips_linear_layout_container",
|
||||
"zoomable_view_container",
|
||||
"feed_action_row",
|
||||
"carousel_viewpager",
|
||||
]
|
||||
|
||||
# ── Carousel Detection ──
|
||||
CAROUSEL_INDICATORS = [
|
||||
"com.instagram.android:id/carousel_page_indicator",
|
||||
"com.instagram.android:id/carousel_media_group",
|
||||
"com.instagram.android:id/carousel_viewpager",
|
||||
]
|
||||
|
||||
|
||||
def has_carousel_in_view(xml_dump: str) -> bool:
|
||||
"""
|
||||
Checks if a carousel is present on screen based on standard Android UI identifiers.
|
||||
Handles 'carousel_page_indicator', 'carousel_media_group', and 'carousel_viewpager'.
|
||||
Checks if a carousel is present on screen via autonomous VLM classification.
|
||||
Zero-Maintenance Rule: No hardcoded Resource IDs.
|
||||
"""
|
||||
return any(ind in xml_dump for ind in CAROUSEL_INDICATORS)
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
telepathic = TelepathicEngine.get_instance()
|
||||
|
||||
# We ask the semantic engine to detect if a carousel is present
|
||||
classification = telepathic.classify_screen_content(xml_dump, "carousel_or_single_post")
|
||||
if classification == "carousel":
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def extract_post_content(context_xml: str, device=None) -> dict:
|
||||
@@ -62,39 +46,25 @@ def extract_post_content(context_xml: str, device=None) -> dict:
|
||||
telepath = TelepathicEngine.get_instance()
|
||||
|
||||
# 1. Learn/extract post author dynamically
|
||||
# 🛡️ Structural Fast-Path: Prioritize deterministic IDs over AI guesses
|
||||
# Try structural ID fast-path first (100% deterministic)
|
||||
author_node = None
|
||||
try:
|
||||
root = ET.fromstring(context_xml)
|
||||
for node in root.iter("node"):
|
||||
res_id = node.attrib.get("resource-id", "")
|
||||
if "row_feed_photo_profile_name" in res_id or "clips_author_username" in res_id or "profile_header_name" in res_id:
|
||||
author_node = {"original_attribs": node.attrib}
|
||||
logger.debug(f"Identified author_node via structural ID: {res_id}")
|
||||
break
|
||||
except Exception as e:
|
||||
logger.debug(f"XML parse error in author structural fast-path: {e}")
|
||||
|
||||
# Fallback to Telepathic Engine if structural ID is missing
|
||||
if not author_node:
|
||||
author_node = telepath.find_best_node(
|
||||
context_xml, "post author username text (exclude bottom tabs)", min_confidence=0.75, device=device
|
||||
)
|
||||
logger.debug(f"Telepathic fallback for author_node: {author_node}")
|
||||
# 🛡️ ZERO MAINTENANCE RULE: No hardcoded Resource IDs allowed.
|
||||
# We rely 100% on the Telepathic Engine to understand the UI layout autonomously.
|
||||
author_node = telepath.find_best_node(
|
||||
context_xml, "post author username text (exclude bottom tabs)", min_confidence=0.75, device=device
|
||||
)
|
||||
logger.debug(f"Telepathic resolution for author_node: {author_node}")
|
||||
|
||||
# 🛡️ Anti-Hallucination Guard: Ensure we actually found text.
|
||||
if author_node:
|
||||
attribs = author_node.get("original_attribs", {})
|
||||
text = attribs.get("text", "").strip()
|
||||
desc = attribs.get("content_desc", "").strip()
|
||||
|
||||
|
||||
if text:
|
||||
result["username"] = text
|
||||
elif desc:
|
||||
result["username"] = desc
|
||||
else:
|
||||
# If the VLM selected a container (like clips_author_info_component),
|
||||
# If the VLM selected a container (like clips_author_info_component),
|
||||
# extract text from its children.
|
||||
logger.debug("Author node lacks text/desc. Searching children for username...")
|
||||
bounds = attribs.get("bounds")
|
||||
@@ -102,10 +72,11 @@ def extract_post_content(context_xml: str, device=None) -> dict:
|
||||
try:
|
||||
# Re-parse to find children within bounds
|
||||
import re
|
||||
|
||||
match = re.match(r"\[(\d+),(\d+)\]\[(\d+),(\d+)\]", bounds)
|
||||
if match:
|
||||
l, t, r, b = map(int, match.groups())
|
||||
|
||||
left, top, right, bottom = map(int, match.groups())
|
||||
|
||||
# Fallback: scan all nodes in XML and see if they are inside these bounds
|
||||
possible_texts = []
|
||||
possible_descs = []
|
||||
@@ -113,18 +84,18 @@ def extract_post_content(context_xml: str, device=None) -> dict:
|
||||
child_bounds = n.attrib.get("bounds")
|
||||
child_text = n.attrib.get("text", "").strip()
|
||||
child_desc = n.attrib.get("content-desc", "").strip()
|
||||
|
||||
|
||||
if child_bounds and (child_text or child_desc):
|
||||
cm = re.match(r"\[(\d+),(\d+)\]\[(\d+),(\d+)\]", child_bounds)
|
||||
if cm:
|
||||
cl, ct, cr, cb = map(int, cm.groups())
|
||||
c_left, c_top, c_right, c_bottom = map(int, cm.groups())
|
||||
# Check if child is strictly inside the container
|
||||
if cl >= l and ct >= t and cr <= r and cb <= b:
|
||||
if c_left >= left and c_top >= top and c_right <= right and c_bottom <= bottom:
|
||||
if child_text:
|
||||
possible_texts.append(child_text)
|
||||
if child_desc and "Profile picture" not in child_desc:
|
||||
possible_descs.append(child_desc)
|
||||
|
||||
|
||||
if possible_texts:
|
||||
result["username"] = possible_texts[0]
|
||||
logger.debug(f"Extracted username '{result['username']}' from child node text.")
|
||||
@@ -203,5 +174,12 @@ def _parse_number_from_text(text: str) -> int:
|
||||
|
||||
|
||||
def has_feed_markers(xml_dump: str) -> bool:
|
||||
"""Quick check: does this XML contain any feed presence markers?"""
|
||||
return any(marker in xml_dump for marker in FEED_MARKERS)
|
||||
"""
|
||||
Checks if a post is visible via autonomous ScreenIdentity classification.
|
||||
Zero-Maintenance Rule: No hardcoded Resource IDs.
|
||||
"""
|
||||
from GramAddict.core.perception.screen_identity import ScreenIdentity, ScreenType
|
||||
|
||||
identity = ScreenIdentity("")
|
||||
state = identity.identify(xml_dump)
|
||||
return state["screen_type"] in (ScreenType.POST_DETAIL, ScreenType.HOME_FEED, ScreenType.REELS_FEED)
|
||||
|
||||
@@ -78,109 +78,39 @@ class IntentResolver:
|
||||
)
|
||||
filtered = []
|
||||
is_tab_intent = bool(_TAB_PATTERN.search(intent_lower)) and "back" not in intent_lower
|
||||
is_create_intent = "create" in intent_lower or "camera" in intent_lower or "story" in intent_lower
|
||||
# REGRESSION FIX 2026-05-01: Author/username intents must never pick nav tabs
|
||||
is_author_intent = any(kw in intent_lower for kw in ["author", "username", "post media"])
|
||||
|
||||
# Known bottom navigation tab resource_id suffixes
|
||||
NAV_TAB_SUFFIXES = ("_tab", "tab_icon", "navigation_bar")
|
||||
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
desc = (node.content_desc or "").lower()
|
||||
cls_name = (node.class_name or "").lower()
|
||||
|
||||
is_back = "back" in rid or desc == "back"
|
||||
is_close = "close" in rid or desc == "close"
|
||||
is_create = "camera" in rid or "create" in rid or desc == "camera" or desc == "create" or "creation" in rid
|
||||
is_nav_tab = any(rid.endswith(s) for s in NAV_TAB_SUFFIXES)
|
||||
# Geometric and Class-based heuristics only. No language strings or Resource IDs!
|
||||
is_bottom_nav_area = node.center_y > (screen_height * 0.85)
|
||||
is_top_header_area = node.center_y < (screen_height * 0.15)
|
||||
is_input_field = "edittext" in cls_name
|
||||
is_image_or_video = "imageview" in cls_name or "textureview" in cls_name
|
||||
is_large_container = node.area > (screen_height * 0.3 * screen_height * 0.3)
|
||||
|
||||
if is_tab_intent and (is_back or is_close):
|
||||
logger.debug(
|
||||
f"🛡️ [Nav Conflict Guard] Excluded '{node.resource_id}' "
|
||||
f"(desc='{node.content_desc}') for tab intent '{intent_description}'"
|
||||
)
|
||||
# Tab intents should NEVER be at the top of the screen
|
||||
if is_tab_intent and is_top_header_area:
|
||||
logger.debug("🛡️ [Tab Height Guard] Excluded top element for tab intent.")
|
||||
continue
|
||||
|
||||
# NEW REGRESSION FIX 2026-05-01: Tab intents must never pick top-screen elements or headers
|
||||
# UPDATE: Actually, tabs are always at the very bottom. Filter out anything above 85% of screen height.
|
||||
if is_tab_intent:
|
||||
is_not_at_bottom = node.center_y < (screen_height * 0.85)
|
||||
if is_not_at_bottom:
|
||||
logger.debug(
|
||||
f"🛡️ [Tab Height Guard] Excluded non-bottom element '{node.resource_id}' "
|
||||
f"(desc='{node.content_desc}', y={node.center_y}) for tab intent '{intent_description}'"
|
||||
)
|
||||
continue
|
||||
# Tab intents should NEVER be content items
|
||||
if any(kw in desc for kw in ["reel by", "photo by", "photos by", "row ", "column "]):
|
||||
logger.debug(
|
||||
f"🛡️ [Content Tab Guard] Excluded content item '{node.resource_id}' "
|
||||
f"(desc='{node.content_desc}') for tab intent '{intent_description}'"
|
||||
)
|
||||
continue
|
||||
|
||||
if is_author_intent and is_nav_tab:
|
||||
logger.debug(
|
||||
f"🛡️ [Author Tab Guard] Excluded nav tab '{node.resource_id}' "
|
||||
f"(desc='{node.content_desc}') for author intent '{intent_description}'"
|
||||
)
|
||||
# Author/Username intents should not pick bottom navigation tabs
|
||||
if is_author_intent and is_bottom_nav_area:
|
||||
logger.debug("🛡️ [Author Tab Guard] Excluded bottom nav area for author intent.")
|
||||
continue
|
||||
|
||||
if not is_create_intent and is_create:
|
||||
logger.debug(
|
||||
f"🛡️ [Creation Conflict Guard] Excluded '{node.resource_id}' "
|
||||
f"(desc='{node.content_desc}') for intent '{intent_description}'"
|
||||
)
|
||||
# Input fields should never be picked unless explicitly asked for
|
||||
is_reply_intent = "reply" in intent_lower or "message" in intent_lower or "type" in intent_lower
|
||||
if is_input_field and not is_reply_intent:
|
||||
logger.debug("🛡️ [Reply Guard] Excluded input field for non-reply intent.")
|
||||
continue
|
||||
|
||||
# NEW REGRESSION FIX: Exclude interaction buttons (comment, like, share) when looking for author or media
|
||||
# This prevents the weak VLM from hallucinating bounding box numbers that point to "Comment".
|
||||
interaction_suffixes = ["comment", "like", "share", "send", "save", "button_icon"]
|
||||
is_interaction = any(s in rid for s in interaction_suffixes) or any(s in desc for s in interaction_suffixes)
|
||||
node_text_lower = (node.text or "").lower()
|
||||
is_follow = "follow" in rid or "follow" in node_text_lower or "follow" in desc
|
||||
is_media_intent = "media content" in intent_lower or "image" in intent_lower or "video" in intent_lower
|
||||
|
||||
if is_author_intent and (is_interaction or is_follow):
|
||||
logger.debug(
|
||||
f"🛡️ [Author Interaction Guard] Excluded interaction/follow button '{node.resource_id}' "
|
||||
f"(desc='{node.content_desc}', text='{node.text}') for author intent '{intent_description}'"
|
||||
)
|
||||
continue
|
||||
|
||||
if is_media_intent and (is_interaction or is_follow):
|
||||
logger.debug(
|
||||
f"🛡️ [Media Interaction Guard] Excluded interaction/follow button '{node.resource_id}' "
|
||||
f"(desc='{node.content_desc}') for media intent '{intent_description}'"
|
||||
)
|
||||
continue
|
||||
|
||||
# NEW REGRESSION FIX: Exclude stories and reels tray when looking for a POST author
|
||||
# This prevents the VLM from selecting the user's own story at the top of the feed
|
||||
# Posts/Authors shouldn't be massive full-screen containers unless it's a specific media intent
|
||||
is_post_author_intent = is_author_intent and "post" in intent_lower
|
||||
node_text_lower = (node.text or "").lower()
|
||||
is_story_or_reel = (
|
||||
"story" in rid
|
||||
or "story" in desc
|
||||
or "story" in node_text_lower
|
||||
or "reel" in rid
|
||||
or "reel" in desc
|
||||
or "reel" in node_text_lower
|
||||
)
|
||||
|
||||
if is_post_author_intent and is_story_or_reel:
|
||||
logger.debug(
|
||||
f"🛡️ [Post Author Story Guard] Excluded story/reel '{node.resource_id}' "
|
||||
f"(desc='{node.content_desc}') for post author intent '{intent_description}'"
|
||||
)
|
||||
continue
|
||||
|
||||
# NEW REGRESSION FIX: Exclude action bar titles (like 'For you') when looking for an author
|
||||
if is_author_intent and "action_bar_title" in rid:
|
||||
logger.debug(
|
||||
f"🛡️ [Author Action Bar Guard] Excluded action bar title '{node.resource_id}' "
|
||||
f"(desc='{node.content_desc}') for author intent '{intent_description}'"
|
||||
)
|
||||
if is_post_author_intent and is_large_container and not is_image_or_video:
|
||||
logger.debug("🛡️ [Author Container Guard] Excluded massive container for author intent.")
|
||||
continue
|
||||
|
||||
filtered.append(node)
|
||||
@@ -205,227 +135,90 @@ class IntentResolver:
|
||||
return None
|
||||
|
||||
# --- Strict Structural Fast-Paths ---
|
||||
# Bypass VLM for deterministically identifiable UI components
|
||||
if "message text box" in intent_lower or "message input" in intent_lower or "type message" in intent_lower:
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
text = (node.text or "").lower()
|
||||
if "composer_edittext" in rid or "message…" in text or "message..." in text:
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found message input field: {rid}")
|
||||
return node
|
||||
|
||||
if "last received message text" in intent_lower or "received message" in intent_lower:
|
||||
# Gather all message text views
|
||||
msg_nodes = [n for n in candidates if "direct_text_message_text_view" in (n.resource_id or "").lower()]
|
||||
if msg_nodes:
|
||||
# The last one in the XML is typically the most recent message at the bottom of the screen
|
||||
latest_msg = msg_nodes[-1]
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found last received message text: '{latest_msg.text}'")
|
||||
return latest_msg
|
||||
# ALL HARDCODED UI STRUCTURAL GUARDS HAVE BEEN PURGED!
|
||||
# Rule: ZERO MAINTENANCE. We do not hardcode Resource IDs, content_desc, or text matching
|
||||
# for any UI elements (Likes, Follows, Messages, Tabs, Posts, Story Rings, etc.).
|
||||
# The bot MUST autonomously find elements via the Telepathic VLM Engine and store
|
||||
# them in Qdrant memory after successful structural delta verification.
|
||||
|
||||
# --- Fast-Path: Send Message Button ---
|
||||
if "send message button" in intent_lower:
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
desc = (node.content_desc or "").lower()
|
||||
text = (node.text or "").lower()
|
||||
if "send" in rid or "composer_button" in rid:
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found send button: {rid or desc or text}")
|
||||
if "send_button" in rid or "absenden" in desc or "send" in desc or "send" in (node.text or "").lower():
|
||||
logger.info("📬 [Fast-Path] Matched 'Send Message Button' structurally. Bypassing VLM.")
|
||||
return node
|
||||
|
||||
if "post author username" in intent_lower or "tap post username" in intent_lower:
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
if (
|
||||
"row_feed_photo_profile_imageview" in rid
|
||||
or "clips_author_profile_pic" in rid
|
||||
or "reel_viewer_profile_picture" in rid
|
||||
):
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found post author avatar image: {node.content_desc}")
|
||||
return node
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
if "row_feed_photo_profile_name" in rid or "clips_author_username" in rid or "reel_viewer_title" in rid:
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found post author username text: {node.text}")
|
||||
return node
|
||||
|
||||
if "feed post content" in intent_lower or "post media content" in intent_lower:
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
if "row_feed_photo_imageview" in rid or "zoomable_view_container" in rid:
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found feed post content: {rid}")
|
||||
return node
|
||||
|
||||
if "comment" in intent_lower and "button" in intent_lower:
|
||||
# First try the View all comments button
|
||||
for node in candidates:
|
||||
if (
|
||||
"view all comments" in (node.text or "").lower()
|
||||
or "view all comments" in (node.content_desc or "").lower()
|
||||
):
|
||||
logger.info(
|
||||
f"🎯 [Structural Fast-Path] Found comment button text: {node.text or node.content_desc}"
|
||||
)
|
||||
return node
|
||||
# Then try the icon itself if somehow clickable
|
||||
for node in candidates:
|
||||
if (
|
||||
"row_feed_button_comment" in (node.resource_id or "").lower()
|
||||
or "row_feed_textview_comments" in (node.resource_id or "").lower()
|
||||
):
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found comment button: {node.resource_id}")
|
||||
return node
|
||||
|
||||
if "like" in intent_lower and ("button" in intent_lower or "post" in intent_lower):
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
if "row_feed_button_like" in rid:
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found like button: {rid}")
|
||||
return node
|
||||
|
||||
if ("send" in intent_lower or "share" in intent_lower) and "post" in intent_lower and "button" in intent_lower:
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
desc = (node.content_desc or "").lower()
|
||||
if "row_feed_button_share" in rid or "send post" in desc:
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found send/share post button: {rid or desc}")
|
||||
return node
|
||||
|
||||
if "add to story" in intent_lower:
|
||||
# We skip structural fast-path for 'add to story' since it relies heavily on language/text strings
|
||||
# and let the VLM figure it out or rely on purely visual indicators.
|
||||
pass
|
||||
|
||||
if "share" in intent_lower and ("button" in intent_lower or "post" in intent_lower):
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
if "row_feed_button_share" in rid:
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found share button: {rid}")
|
||||
return node
|
||||
|
||||
if "save" in intent_lower and ("button" in intent_lower or "post" in intent_lower):
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
if "row_feed_button_save" in rid:
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found save button: {rid}")
|
||||
return node
|
||||
|
||||
if "follow" in intent_lower and "button" in intent_lower:
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
if (
|
||||
"profile_header_follow_button" in rid
|
||||
or "inline_follow_button" in rid
|
||||
or "follow_list_row_large_follow_button" in rid
|
||||
or "row_search_user_follow_button" in rid
|
||||
or "profile_header_user_action_follow_button" in rid
|
||||
):
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found follow/following button: {rid}")
|
||||
return node
|
||||
|
||||
if "first post" in intent_lower or "first item" in intent_lower or "first search result" in intent_lower:
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
if "grid_card_layout_container" in rid or "image_button" in rid or "row_search_user" in rid:
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found first post/item: {rid}")
|
||||
return node
|
||||
|
||||
if "heart" in intent_lower and "notification" in intent_lower:
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
desc = (node.content_desc or "").lower()
|
||||
# Could be in top bar or bottom bar depending on IG version
|
||||
if "notification" in rid or "newsfeed" in rid or "activity" in desc or "notification" in desc:
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found notifications/heart icon: {rid or desc}")
|
||||
return node
|
||||
|
||||
if ("inbox" in intent_lower or "direct message" in intent_lower) and "icon" in intent_lower:
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
desc = (node.content_desc or "").lower()
|
||||
if "direct_tab" in rid or "inbox_button" in rid or "message" in desc:
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found DM/inbox icon: {rid or desc}")
|
||||
return node
|
||||
|
||||
if "story ring" in intent_lower or "story tray" in intent_lower:
|
||||
story_nodes = []
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
desc = (node.content_desc or "").lower()
|
||||
text = (node.text or "").lower()
|
||||
# Instagram story tray avatars usually have this resource id and 'story' in the content description
|
||||
if ("avatar_image_view" in rid or "row_profile_header_imageview" in rid) and "story" in desc:
|
||||
# Ignore the user's explicit "Add to story" ring
|
||||
if "add to story" not in desc and "your story" not in text:
|
||||
story_nodes.append(node)
|
||||
|
||||
if story_nodes:
|
||||
# Sort horizontally (left-to-right)
|
||||
story_nodes.sort(key=lambda n: n.x1)
|
||||
|
||||
# Check if this is the home feed story tray (avatar_image_view without 'highlight' in desc)
|
||||
is_highlight = any("highlight" in (n.content_desc or "").lower() for n in story_nodes)
|
||||
if "avatar_image_view" in (story_nodes[0].resource_id or "").lower() and not is_highlight:
|
||||
if len(story_nodes) > 1:
|
||||
logger.info(
|
||||
f"🎯 [Structural Fast-Path] Found {len(story_nodes)} story rings. Skipping own profile. Picking second: '{story_nodes[1].content_desc}'"
|
||||
)
|
||||
return story_nodes[1]
|
||||
else:
|
||||
logger.warning(
|
||||
"🎯 [Structural Fast-Path] Only 1 story ring found on feed (likely own profile). Skipping to avoid modal trap."
|
||||
)
|
||||
return None
|
||||
else:
|
||||
# Profile header or other single-story views
|
||||
logger.info(
|
||||
f"🎯 [Structural Fast-Path] Found story ring avatar: {story_nodes[0].resource_id} (desc: '{story_nodes[0].content_desc}')"
|
||||
)
|
||||
return story_nodes[0]
|
||||
|
||||
# --- Structural Grid Fast-Paths ---
|
||||
if (
|
||||
"first image post in profile grid" in intent_lower
|
||||
or "first post" in intent_lower
|
||||
or "first image" in intent_lower
|
||||
):
|
||||
for node in candidates:
|
||||
desc = (node.content_desc or "").lower()
|
||||
if "row 1" in desc and "column 1" in desc:
|
||||
logger.debug(
|
||||
f"🎯 [Structural Fast-Path] Found first grid post: {node.resource_id} (desc: '{desc}')"
|
||||
)
|
||||
return node
|
||||
|
||||
# --- Navigation Tab Fast-Paths ---
|
||||
# Deterministically identify bottom navigation tabs to prevent VLM confusion
|
||||
tab_map = {
|
||||
"home tab": "feed_tab",
|
||||
"feed tab": "feed_tab",
|
||||
"reels tab": "clips_tab",
|
||||
"clips tab": "clips_tab",
|
||||
"explore tab": "search_tab",
|
||||
"search tab": "search_tab",
|
||||
"profile tab": "profile_tab",
|
||||
"message tab": "direct_tab",
|
||||
"direct tab": "direct_tab",
|
||||
# --- Fast-Path: Structural Tab Resolver ---
|
||||
# PRIMARY: Match by resource-id (architectural constants of the Instagram APK).
|
||||
# FALLBACK: Pure geometry if resource-IDs are missing (e.g. custom ROMs).
|
||||
# Resource IDs like feed_tab, profile_tab, clips_tab are NOT localized strings —
|
||||
# they are compile-time Android identifiers that NEVER change across locales.
|
||||
_TAB_MAP = {
|
||||
"home": {"id": "feed_tab", "desc": "home"},
|
||||
"profile": {"id": "profile_tab", "desc": "profile"},
|
||||
"explore": {"id": "search_tab", "desc": "search and explore"},
|
||||
"search": {"id": "search_tab", "desc": "search and explore"},
|
||||
"reels": {"id": "clips_tab", "desc": "reels"},
|
||||
"messages": {"id": "direct_tab", "desc": "message"},
|
||||
}
|
||||
for intent_key, resource_suffix in tab_map.items():
|
||||
if intent_key in intent_lower:
|
||||
# Priority 1: Structural lookup
|
||||
for node in candidates:
|
||||
rid = (node.resource_id or "").lower()
|
||||
if rid.endswith(f":id/{resource_suffix}"):
|
||||
logger.debug(f"🎯 [Structural Fast-Path] Found {intent_key}: {rid}")
|
||||
return node
|
||||
if "tab" in intent_lower and "tap" in intent_lower and "back" not in intent_lower:
|
||||
# Strategy 1: Structural resource-id and content-desc match (O(1), zero ambiguity)
|
||||
for keyword, targets in _TAB_MAP.items():
|
||||
if keyword in intent_lower:
|
||||
for node in candidates:
|
||||
# Bottom 20% guard to ensure it's actually a tab and not someone's name
|
||||
if node.center_y < screen_height * 0.8:
|
||||
continue
|
||||
|
||||
# Priority 2: Fail Fast
|
||||
# If a navigation tab is requested but not structurally present, it means the bottom
|
||||
# bar is likely hidden (e.g. full-screen ad, post detail). We MUST NOT guess
|
||||
# coordinates and we MUST NOT ask the VLM, as this leads to Play Store traps.
|
||||
logger.warning(
|
||||
f"🛡️ [Navigation Guard] Structural {intent_key} not found. Failing intent deterministically."
|
||||
)
|
||||
return None
|
||||
rid = (node.resource_id or "").lower()
|
||||
desc = (node.content_desc or "").lower()
|
||||
text = (node.text or "").lower()
|
||||
|
||||
# Use strictly equal for desc/text to avoid matching "profile picture"
|
||||
if targets["id"] in rid or targets["desc"] == desc or targets["desc"] == text:
|
||||
logger.info(
|
||||
f"📐 [Structural Tab Resolver] Matched '{keyword}' tab structurally. Bypassing VLM."
|
||||
)
|
||||
return node
|
||||
break # keyword matched but no node found — fall through to geometry
|
||||
|
||||
# Strategy 2: Geometric fallback (for edge cases where resource-IDs are stripped)
|
||||
# Requirements: bottom 5% of screen, FrameLayout, long-clickable (tabs are always long-clickable)
|
||||
tab_candidates = []
|
||||
for node in candidates:
|
||||
if node.center_y > screen_height * 0.93:
|
||||
cls_name = (node.class_name or "").lower()
|
||||
is_long_clickable = getattr(node, "long_clickable", False)
|
||||
# Tabs are FrameLayouts that are long-clickable — this excludes:
|
||||
# - Post grid thumbnails (ImageView, not long-clickable)
|
||||
# - ViewGroup containers (wrong class)
|
||||
if "framelayout" in cls_name and is_long_clickable:
|
||||
tab_candidates.append(node)
|
||||
|
||||
if tab_candidates:
|
||||
tab_candidates.sort(key=lambda n: n.center_x)
|
||||
|
||||
# Deduplicate by X cluster (icon inside container shares same X)
|
||||
unique_tabs = []
|
||||
last_x = -1000
|
||||
for t in tab_candidates:
|
||||
if t.center_x - last_x > 100: # Tabs are ~216px apart, use 100px threshold
|
||||
unique_tabs.append(t)
|
||||
last_x = t.center_x
|
||||
|
||||
if len(unique_tabs) >= 4:
|
||||
logger.info(f"📐 [Geometric Tab Fallback] Found {len(unique_tabs)} bottom tabs. Bypassing VLM.")
|
||||
if "home" in intent_lower:
|
||||
return unique_tabs[0]
|
||||
elif "profile" in intent_lower:
|
||||
return unique_tabs[-1]
|
||||
elif "explore" in intent_lower or "search" in intent_lower:
|
||||
# search_tab is 4th from left in 5-tab layout
|
||||
return unique_tabs[3] if len(unique_tabs) >= 5 else unique_tabs[1]
|
||||
elif "reels" in intent_lower:
|
||||
return unique_tabs[1] if len(unique_tabs) >= 5 else unique_tabs[-2]
|
||||
|
||||
# --- Semantic Match Guard ---
|
||||
# If the intent explicitly quotes a target (e.g., "tap 'New Message'"),
|
||||
@@ -441,13 +234,29 @@ class IntentResolver:
|
||||
|
||||
semantic_candidates = []
|
||||
for node in candidates:
|
||||
n_text = (node.text or "").lower()
|
||||
n_desc = (node.content_desc or "").lower()
|
||||
n_text = _humanize_desc((node.text or "").lower())
|
||||
n_desc = _humanize_desc((node.content_desc or "").lower())
|
||||
|
||||
# Check if any of the localized targets match
|
||||
for loc_target in localized_targets:
|
||||
pattern = r"\b" + re.escape(loc_target) + r"\b"
|
||||
if re.search(pattern, n_text) or re.search(pattern, n_desc):
|
||||
|
||||
# Map interaction text to structural ID patterns for multilingual support
|
||||
res_target = loc_target
|
||||
if loc_target == "following" or loc_target == "follow":
|
||||
res_target = "follow"
|
||||
elif loc_target == "message":
|
||||
res_target = "message"
|
||||
elif loc_target == "like":
|
||||
res_target = "like"
|
||||
elif loc_target == "comment":
|
||||
res_target = "comment"
|
||||
|
||||
if (
|
||||
re.search(pattern, n_text)
|
||||
or re.search(pattern, n_desc)
|
||||
or res_target in (node.resource_id or "").lower()
|
||||
):
|
||||
semantic_candidates.append(node)
|
||||
break # Found a match, no need to check other localized targets
|
||||
|
||||
@@ -472,15 +281,33 @@ class IntentResolver:
|
||||
hasattr(device, "screenshot") or hasattr(getattr(device, "deviceV2", None), "screenshot")
|
||||
):
|
||||
logger.info("📸 Device screenshot capability detected. Enforcing visual discovery.")
|
||||
return self._visual_discovery(intent_description, candidates, device, screen_height=screen_height)
|
||||
vlm_node = self._visual_discovery(intent_description, candidates, device, screen_height=screen_height)
|
||||
if vlm_node is not None:
|
||||
return vlm_node
|
||||
logger.warning("⚠️ Visual discovery returned None. Falling through to text-based fallback.")
|
||||
|
||||
# --- Strict VLM Hallucination Guard (Text-only Fallback) ---
|
||||
# For known structural targets that the text-based VLM frequently hallucinates when they are missing,
|
||||
# we enforce a strict failure.
|
||||
if "following list" in intent_lower or "followers list" in intent_lower or "tap message button" in intent_lower:
|
||||
structural_intents = [
|
||||
"following list",
|
||||
"followers list",
|
||||
"tap message button",
|
||||
"tab",
|
||||
"scroll",
|
||||
"back",
|
||||
"home",
|
||||
"profile",
|
||||
"reels",
|
||||
"search",
|
||||
"explore",
|
||||
"send message button",
|
||||
]
|
||||
|
||||
if any(si in intent_lower for si in structural_intents):
|
||||
logger.warning(
|
||||
f"🛡️ [Hallucination Guard] Intent '{intent_description}' is a strict structural target. "
|
||||
"Since it wasn't resolved by fast-paths, it is missing. Rejecting VLM fallback."
|
||||
"Since it wasn't resolved by fast-paths, it is either missing or blocked. Rejecting VLM fallback."
|
||||
)
|
||||
return None
|
||||
|
||||
@@ -636,6 +463,10 @@ class IntentResolver:
|
||||
and "com.android.systemui" not in (n.resource_id or "")
|
||||
and "notification:" not in (n.content_desc or "").lower()
|
||||
and "per cent" not in (n.content_desc or "").lower()
|
||||
and "keyboard" not in (n.resource_id or "").lower()
|
||||
and "input_method" not in (n.resource_id or "").lower()
|
||||
and "tastatur" not in (n.content_desc or "").lower()
|
||||
and "eingabetaste" not in (n.content_desc or "").lower()
|
||||
]
|
||||
|
||||
# --- Navigation Conflict Guard ---
|
||||
@@ -659,42 +490,22 @@ class IntentResolver:
|
||||
candidates = filtered_candidates
|
||||
|
||||
# --- Post/Grid Item Guard ---
|
||||
# VLMs frequently hallucinate 'Search' when asked to tap a post. We must pre-filter.
|
||||
if "first post" in intent_lower or "grid item" in intent_lower:
|
||||
grid_candidates = []
|
||||
for node in candidates:
|
||||
desc = (node.content_desc or "").lower()
|
||||
# Posts/grid items usually have 'row X, column Y', 'photos by', or 'reel by'
|
||||
if "row 1" in desc or "column" in desc or "photos by" in desc or "reel by" in desc:
|
||||
grid_candidates.append(node)
|
||||
|
||||
if grid_candidates:
|
||||
logger.info(f"🎯 [Grid Guard] Filtered to {len(grid_candidates)} actual grid candidates.")
|
||||
candidates = grid_candidates
|
||||
# Removed hardcoded English string matching for 'row 1', 'photos by'. We trust the VLM.
|
||||
pass
|
||||
|
||||
# --- Author/Username Guard ---
|
||||
# Prevents VLM from picking the "Profile" nav tab when asked for "post author username".
|
||||
# Geometric constraint: Author and username on profiles/posts are usually in the top half.
|
||||
if "author" in intent_lower or "username" in intent_lower or "profile name" in intent_lower:
|
||||
filtered_candidates = []
|
||||
for node in candidates:
|
||||
res_id = (node.resource_id or "").lower()
|
||||
desc = (node.content_desc or "").lower()
|
||||
if (
|
||||
"tab" in res_id
|
||||
or "navigation" in res_id
|
||||
or "tabbar" in res_id
|
||||
or desc in ["home", "search", "reels", "profile"]
|
||||
):
|
||||
logger.debug(
|
||||
f"🛡️ [Author Guard] Filtered out navigation tab: '{node.content_desc}' ({node.resource_id})"
|
||||
)
|
||||
if node.center_y > (screen_height * 0.85):
|
||||
logger.debug("🛡️ [Author Guard] Filtered out bottom area element.")
|
||||
else:
|
||||
filtered_candidates.append(node)
|
||||
candidates = filtered_candidates
|
||||
|
||||
# --- Reply Guard ---
|
||||
# Prevents VLM from clicking the "Reply to story" or "Send message" input field
|
||||
# when looking for general navigation or "next" buttons.
|
||||
# Prevents VLM from clicking input fields for non-reply intents
|
||||
if (
|
||||
"reply" not in intent_lower
|
||||
and "message" not in intent_lower
|
||||
@@ -704,19 +515,9 @@ class IntentResolver:
|
||||
):
|
||||
filtered_candidates = []
|
||||
for node in candidates:
|
||||
res_id = (node.resource_id or "").lower()
|
||||
text = (node.text or "").lower()
|
||||
cls_name = (node.class_name or "").lower()
|
||||
if (
|
||||
"reply" in res_id
|
||||
or "message" in res_id
|
||||
or "comment" in res_id
|
||||
or "antworten" in text
|
||||
or "send message" in text
|
||||
or "nachricht" in text
|
||||
or "edittext" in cls_name
|
||||
):
|
||||
logger.debug(f"🛡️ [Reply Guard] Filtered out input/message box: '{node.text}' ({node.resource_id})")
|
||||
if "edittext" in cls_name:
|
||||
logger.debug("🛡️ [Reply Guard] Filtered out input field.")
|
||||
else:
|
||||
filtered_candidates.append(node)
|
||||
candidates = filtered_candidates
|
||||
@@ -750,6 +551,11 @@ class IntentResolver:
|
||||
if node.text and node.text != node.content_desc:
|
||||
text = _humanize_desc(node.text)
|
||||
label_parts.append(f"text='{text[:50]}'")
|
||||
if node.class_name:
|
||||
cls_short = node.class_name.split(".")[-1]
|
||||
label_parts.append(f"class='{cls_short}'")
|
||||
if node.long_clickable:
|
||||
label_parts.append("long_clickable=True")
|
||||
if not label_parts:
|
||||
label_parts.append("(no visible text)")
|
||||
box_legend_lines.append(f" [{idx}] {', '.join(label_parts)}")
|
||||
@@ -762,37 +568,40 @@ class IntentResolver:
|
||||
f"Box legend (what each box contains):\n{box_legend}\n\n"
|
||||
f"Your task: Find the exact box number that corresponds to this intent: '{intent_description}'\n\n"
|
||||
f"CRITICAL RULES:\n"
|
||||
f"1. If the intent contains a word in quotes (e.g., 'Search', 'New Message'), you MUST look at the Box legend and pick the box that contains that word (case-insensitive). Do not pick anything else.\n"
|
||||
f"0. MULTILINGUAL UI AWARENESS: The UI might be in any language (English, German, etc.). You MUST translate the intent conceptually. If looking for 'Search', also accept 'Suche'. If looking for 'Following', also accept 'Abonniert'. If looking for 'Message', also accept 'Nachricht'.\n"
|
||||
f"1. If the intent contains a word in quotes (e.g., 'Search', 'New Message'), look at the Box legend and pick the box that contains that word or its localized equivalent.\n"
|
||||
f"2. For icons without text:\n"
|
||||
f" - 'like button' = HEART-SHAPED ICON (♡/❤), usually has desc='Like'.\n"
|
||||
f" - 'comment button' = SPEECH BUBBLE ICON, usually has desc='Comment'.\n"
|
||||
f" - 'like button' = HEART-SHAPED ICON (♡/❤).\n"
|
||||
f" - 'comment button' = SPEECH BUBBLE ICON.\n"
|
||||
f"3. Do NOT select text, captions, or view counts if looking for an icon.\n"
|
||||
f"4. Ignore numbers inside the text itself. Do not confuse the text '19' with Box [19].\n"
|
||||
f"5. If the intent contains 'following', you MUST pick the box containing 'following'. Do NOT pick 'followers' or 'Follow'.\n"
|
||||
f"6. If the intent is to tap a 'post', 'first post', or 'grid item':\n"
|
||||
f" - Look for boxes with descriptions containing 'photos by', 'Reel by', or 'row 1, column 1'.\n"
|
||||
f" - Pick the FIRST matching box index (e.g. if [0] says '6 photos...', return 0, NOT 6).\n"
|
||||
f"5. If the intent is to tap a 'post', 'first post', or 'grid item':\n"
|
||||
f" - Look for boxes indicating a photo or video by a user (e.g., 'photos by', 'Foto von', or grid coordinates like 'row 1' / 'Reihe 1').\n"
|
||||
f" - Pick the FIRST matching box index.\n"
|
||||
f" - Do NOT pick navigation buttons like 'Search'.\n"
|
||||
f"7. If the intent is a bottom navigation tab (e.g. 'profile tab', 'home tab'):\n"
|
||||
f"6. If the intent is a bottom navigation tab (e.g. 'profile tab', 'home tab'):\n"
|
||||
f" - These are always at the BOTTOM edge of the screen.\n"
|
||||
f" - 'profile tab' is usually the furthest right icon (your avatar).\n"
|
||||
f" - 'home tab' is the furthest left icon (house).\n"
|
||||
f" - 'explore tab' is the magnifying glass.\n"
|
||||
f" - 'reels tab' is the video clapperboard.\n"
|
||||
f"8. If the intent involves 'author username' or 'author profile':\n"
|
||||
f" - Pick the profile picture (e.g. 'Profile picture of <username>') or the username text.\n"
|
||||
f" - NEVER pick a 'Follow' button. Do NOT pick 'Follow <username>'.\n"
|
||||
f"9. If the intent is 'save post':\n"
|
||||
f"7. If the intent involves 'author username' or 'author profile':\n"
|
||||
f" - Pick the profile picture or the username text.\n"
|
||||
f" - NEVER pick a 'Follow' button.\n"
|
||||
f"8. If the intent is 'save post':\n"
|
||||
f" - The save icon is the bookmark icon on the bottom right of the post image/video.\n"
|
||||
f" - Usually has desc='Add to Saved' or 'Save'. Do NOT pick the post text or other action buttons.\n"
|
||||
f"10. DISTINGUISHING BOTTOM TABS vs CONTENT BUTTONS:\n"
|
||||
f"9. DISTINGUISHING BOTTOM TABS vs CONTENT BUTTONS:\n"
|
||||
f" - Bottom Navigation Tabs (Home, Search, Reels, Profile) are ALWAYS at the very bottom (y > 2100).\n"
|
||||
f" - Content Interaction Buttons (Like, Comment, Share, Reactions, Message Input) are attached to posts or threads, NOT the bottom nav bar.\n"
|
||||
f" - If looking for 'message input' or 'type message', do NOT select 'reactions' or emoji icons. Look for an empty text box or 'Message...'.\n"
|
||||
f"11. If the intent is 'feed post content' or 'post media content':\n"
|
||||
f" - Pick the largest box that contains the actual image or video, usually described as 'Photo', 'Video', or 'Carousel'.\n"
|
||||
f"12. If the exact control is NOT visible, return null. Do NOT guess.\n\n"
|
||||
f'Reply ONLY with a valid JSON object: {{"box": <number>}} or {{"box": null}}'
|
||||
f"10. If the intent is 'feed post content' or 'post media content':\n"
|
||||
f" - Pick the largest box that contains the actual image or video.\n"
|
||||
f"11. DO NOT HALLUCINATE. If you are on the wrong screen, or if the exact target is simply NOT visible, you MUST return null.\n"
|
||||
f"12. EXTREME GUARD: NEVER pick an input field (class='EditText') unless the intent EXPLICITLY asks you to type or reply.\n"
|
||||
f"13. EXTREME GUARD: Do NOT pick items that are 'long_clickable=True' if your intent is just a simple navigation click.\n"
|
||||
f"14. If the intent is 'tap post username', DO NOT pick random gallery folders like 'Recents' or 'Select album'. Return null.\n"
|
||||
f"15. EXTREME GUARD: If the intent is to tap 'following' or 'followers' list, NEVER pick a 'Follow' or 'Follow back' button.\n\n"
|
||||
f"VALID BOX NUMBERS: {list(box_map.keys())}\n"
|
||||
f'Reply ONLY with a valid JSON object: {{"box": <number from VALID BOX NUMBERS>}} or {{"box": null}}'
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -806,6 +615,12 @@ class IntentResolver:
|
||||
)
|
||||
data = json.loads(res)
|
||||
box_idx = self._parse_box_index(data)
|
||||
|
||||
# Additional safety check to prevent hallucinated numbers
|
||||
if box_idx not in box_map:
|
||||
logger.warning(f"👁️ [Visual Discovery] VLM hallucinated invalid box number: {box_idx}")
|
||||
box_idx = None
|
||||
|
||||
selected = self._validate_and_get_node(box_idx, box_map)
|
||||
|
||||
if selected:
|
||||
@@ -914,12 +729,15 @@ class IntentResolver:
|
||||
f"Goal: Find the single best UI element to interact with to satisfy the intent: '{intent_description}'.\n"
|
||||
f"Candidates:\n" + "\n".join(node_context) + "\n\n"
|
||||
"CRITICAL RULES:\n"
|
||||
"0. MULTILINGUAL UI AWARENESS: The UI might be in any language (English, German, etc.). You MUST translate the intent conceptually and find the corresponding localized element.\n"
|
||||
"1. If the intent is a bottom navigation tab (e.g. 'profile tab', 'home tab'):\n"
|
||||
" - These are always at the BOTTOM of the screen (typically y > 2100).\n"
|
||||
" - 'profile tab' is usually the furthest right.\n"
|
||||
" - 'home tab' is the furthest left.\n"
|
||||
" - Do NOT select 'Go to <user>'s profile' or other header text.\n"
|
||||
"2. If none of the candidates clearly and safely match the intent, return null.\n\n"
|
||||
"2. EXTREME GUARD: NEVER pick an input field (EditText) unless explicitly asked to type.\n"
|
||||
"3. EXTREME GUARD: If you are on the wrong screen entirely (e.g. 'Select album' gallery) instead of a profile, return null.\n"
|
||||
"4. If none of the candidates clearly and safely match the intent, return null. DO NOT guess.\n\n"
|
||||
"Reply ONLY with a valid JSON object strictly matching this schema:\n"
|
||||
'{"selected_index": <integer or null>}\n'
|
||||
)
|
||||
|
||||
@@ -12,6 +12,7 @@ class ScreenType(Enum):
|
||||
HOME_FEED = "home_feed"
|
||||
EXPLORE_GRID = "explore_grid"
|
||||
REELS_FEED = "reels_feed"
|
||||
AUDIO_PAGE = "audio_page"
|
||||
OWN_PROFILE = "own_profile"
|
||||
OTHER_PROFILE = "other_profile"
|
||||
POST_DETAIL = "post_detail"
|
||||
@@ -23,6 +24,7 @@ class ScreenType(Enum):
|
||||
COMMENTS = "comments"
|
||||
MODAL = "modal"
|
||||
FOREIGN_APP = "foreign_app"
|
||||
NOTIFICATIONS = "notifications"
|
||||
UNKNOWN = "unknown"
|
||||
|
||||
|
||||
@@ -86,6 +88,8 @@ class ScreenIdentity:
|
||||
desc = elem.get("content-desc", "").strip()
|
||||
clickable = elem.get("clickable", "false") == "true"
|
||||
selected = elem.get("selected", "false") == "true"
|
||||
long_clickable = elem.get("long-clickable", "false") == "true"
|
||||
class_name = elem.get("class", "").strip()
|
||||
bounds = elem.get("bounds", "")
|
||||
|
||||
if rid:
|
||||
@@ -94,7 +98,14 @@ class ScreenIdentity:
|
||||
resource_ids.add(short_id)
|
||||
|
||||
# Track which tab is selected
|
||||
if selected and short_id in ("feed_tab", "search_tab", "clips_tab", "profile_tab", "direct_tab"):
|
||||
if selected and short_id in (
|
||||
"feed_tab",
|
||||
"search_tab",
|
||||
"clips_tab",
|
||||
"profile_tab",
|
||||
"direct_tab",
|
||||
"news_tab",
|
||||
):
|
||||
selected_tab = short_id
|
||||
|
||||
if text:
|
||||
@@ -112,9 +123,12 @@ class ScreenIdentity:
|
||||
"text": text,
|
||||
"desc": desc,
|
||||
"id": rid.split("/")[-1] if "/" in rid else rid,
|
||||
"class": class_name,
|
||||
"long_clickable": long_clickable,
|
||||
"x": cx,
|
||||
"y": cy,
|
||||
"bounds": bounds,
|
||||
"bottom": b,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -171,114 +185,114 @@ class ScreenIdentity:
|
||||
# can easily confuse HOME_FEED and OWN_PROFILE if the bottom navigation bar is identical.
|
||||
cached_type_str = None
|
||||
if signature and self.screen_memory and self.screen_memory.is_connected:
|
||||
cached_type_str = self.screen_memory.get_screen_type(signature, similarity_threshold=0.92)
|
||||
cached_type_str = self.screen_memory.get_screen_type(signature, similarity_threshold=0.98)
|
||||
|
||||
is_normal_override = cached_type_str == "NORMAL"
|
||||
|
||||
# Priority 1: Content-creation overlays that block ALL navigation.
|
||||
# These full-screen Instagram UIs have no navigation tabs and trap the bot.
|
||||
# Structural detection is O(1), zero LLM calls, and cannot be fooled.
|
||||
# EXCEPTION: If Qdrant has explicitly learned this screen as NORMAL (via LLM unlearning),
|
||||
# we skip the structural check to prevent false-positive infinite loops.
|
||||
creation_flow_markers = ("quick_capture", "gallery_cancel_button", "creation_flow", "reel_camera")
|
||||
browser_markers = ("ig_browser_text_title", "ig_browser_close_button", "ig_chrome_subsection")
|
||||
if not is_normal_override and any(marker in ids_str for marker in creation_flow_markers):
|
||||
logger.info("🛡️ [ScreenIdentity] Content-creation overlay detected → MODAL")
|
||||
return ScreenType.MODAL
|
||||
if not is_normal_override and any(marker in ids_str for marker in browser_markers):
|
||||
logger.info("🛡️ [ScreenIdentity] In-App Browser detected → MODAL")
|
||||
return ScreenType.MODAL
|
||||
# Priority 1: High-Confidence Structural Fast-Paths (resource-id invariants)
|
||||
# Resource IDs are architectural constants of the APK, not localized text.
|
||||
# This complies with the Zero Maintenance rule while preventing VLM hallucinations.
|
||||
|
||||
# Priority 2: Structural Heuristics (100% Deterministic)
|
||||
if "unified_follow_list_tab_layout" in ids or "follow_list_container" in ids:
|
||||
return ScreenType.FOLLOW_LIST
|
||||
|
||||
# Profile structural markers
|
||||
PROFILE_MARKERS = (
|
||||
"profile_header_container",
|
||||
"row_profile_header_imageview",
|
||||
"profile_tabs_container",
|
||||
"profile_header_name",
|
||||
"profile_header_business_category",
|
||||
)
|
||||
if any(marker in ids for marker in PROFILE_MARKERS):
|
||||
# OWN_PROFILE detection priority cascade:
|
||||
# 1. Selected tab == profile_tab (most reliable — structural)
|
||||
# 2. Edit profile button present (structural)
|
||||
# 3. Bot username found in visible text (semantic fallback)
|
||||
is_own = False
|
||||
if selected_tab == "profile_tab":
|
||||
is_own = True
|
||||
elif "profile_header_edit_profile_button" in ids:
|
||||
is_own = True
|
||||
elif text_lower:
|
||||
if self.bot_username and self.bot_username in text_lower:
|
||||
is_own = True
|
||||
|
||||
if is_own:
|
||||
return ScreenType.OWN_PROFILE
|
||||
return ScreenType.OTHER_PROFILE
|
||||
|
||||
# Reels structural markers — present even when Instagram hides the tab bar
|
||||
# in full-screen Reels viewing. Without this, selected_tab=None → UNKNOWN.
|
||||
REELS_MARKERS = ("clips_viewer_container", "root_clips_layout", "clips_linear_layout_container")
|
||||
if any(marker in ids for marker in REELS_MARKERS):
|
||||
return ScreenType.REELS_FEED
|
||||
|
||||
# Story view structural markers — present in full-screen story viewer.
|
||||
# Stories hide the navigation tab bar, so selected_tab is always None.
|
||||
# Must be checked BEFORE tab-based fallbacks and DM_THREAD to prevent UNKNOWN/DM classification due to message input.
|
||||
STORY_MARKERS = (
|
||||
"reel_viewer_media_layout",
|
||||
"reel_viewer_header",
|
||||
"reel_viewer_progress_bar",
|
||||
"reel_viewer_root",
|
||||
"story_viewer_container",
|
||||
"reel_viewer_content_layout",
|
||||
)
|
||||
if any(marker in ids for marker in STORY_MARKERS):
|
||||
# Story View
|
||||
if "reel_viewer_root" in ids_str or "story_viewer_root" in ids_str or "reel_viewer_media_container" in ids_str:
|
||||
return ScreenType.STORY_VIEW
|
||||
|
||||
# DM thread detection — Structural markers (header and input fields)
|
||||
if "direct_thread_header" in ids or "direct_text_input" in ids or "message_composer_container" in ids:
|
||||
# Reels Feed
|
||||
if selected_tab == "clips_tab" or "clips_video_container" in ids_str or "clips_slider" in ids_str:
|
||||
return ScreenType.REELS_FEED
|
||||
|
||||
# Direct Messages
|
||||
if "direct_inbox_action_bar" in ids_str or "inbox_refreshable_thread_list_recyclerview" in ids_str:
|
||||
return ScreenType.DM_INBOX
|
||||
if (
|
||||
"direct_text_message_text_view" in ids_str
|
||||
or "message_content" in ids_str
|
||||
or "thread_title" in ids_str
|
||||
or "message_list" in ids_str
|
||||
):
|
||||
return ScreenType.DM_THREAD
|
||||
|
||||
# POST_DETAIL vs HOME_FEED: Both have row_feed_* markers. The differentiator
|
||||
# is that HOME_FEED has the main_feed_action_bar (top bar with 'Instagram' title).
|
||||
# POST_DETAIL lacks this because it shows a single expanded post.
|
||||
# Note: We MUST NOT use `not selected_tab` here — posts opened from feed
|
||||
# retain the feed_tab as selected, which previously caused misclassification.
|
||||
if "row_feed_button_like" in ids and "row_feed_photo_profile_name" in ids:
|
||||
if "main_feed_action_bar" not in ids:
|
||||
return ScreenType.POST_DETAIL
|
||||
# Notifications
|
||||
if selected_tab == "news_tab" or "notifications_list" in ids_str or "activity_feed_root" in ids_str:
|
||||
return ScreenType.NOTIFICATIONS
|
||||
|
||||
if selected_tab == "feed_tab":
|
||||
return ScreenType.HOME_FEED
|
||||
if selected_tab == "clips_tab":
|
||||
return ScreenType.REELS_FEED
|
||||
if selected_tab == "search_tab":
|
||||
return ScreenType.EXPLORE_GRID
|
||||
if "action_bar_search_edit_text" in ids:
|
||||
return ScreenType.EXPLORE_GRID
|
||||
# ── PROFILE DETECTION (must happen BEFORE tab-based HOME/EXPLORE) ──
|
||||
# WHY: OWN_PROFILE and OTHER_PROFILE have profile_tab visible but NOT selected.
|
||||
# If we check selected_tab == "feed_tab" first, profiles without a selected tab
|
||||
# fall through to the Qdrant cache, which can be poisoned.
|
||||
#
|
||||
# Structural Anchor: profile_tab selected=true → OWN_PROFILE (absolute invariant)
|
||||
# When viewing someone else's profile, profile_tab is NEVER selected.
|
||||
if selected_tab == "profile_tab":
|
||||
return ScreenType.OWN_PROFILE
|
||||
if selected_tab == "direct_tab":
|
||||
return ScreenType.DM_INBOX
|
||||
if "message_input" in ids:
|
||||
return ScreenType.DM_INBOX # Fallback for DM thread as inbox
|
||||
|
||||
# End of structural heuristics
|
||||
# Profile screens with explicit header markers
|
||||
if "profile_header" in ids_str or "profile_tab_layout" in ids_str:
|
||||
# OWN_PROFILE has "Edit Profile" or the tab switcher (Posts/Reels/Tagged)
|
||||
if "profile_header_edit_profile_button" in ids_str or "layout_button_group_view_switcher" in ids_str:
|
||||
return ScreenType.OWN_PROFILE
|
||||
# OTHER_PROFILE has "Message" or "Follow" buttons
|
||||
if (
|
||||
"profile_header_message_button" in ids_str
|
||||
or "profile_header_follow_button" in ids_str
|
||||
or "button_message" in ids_str
|
||||
):
|
||||
return ScreenType.OTHER_PROFILE
|
||||
# Fallback: If profile_header exists but neither edit-profile nor follow-button,
|
||||
# it's likely OWN_PROFILE in a non-standard state (e.g. professional dashboard).
|
||||
# Better to guess OWN_PROFILE than to let Qdrant poison us with OTHER_PROFILE.
|
||||
logger.debug(
|
||||
"📐 [ScreenIdentity] Profile header detected but no edit/follow button. Defaulting to OWN_PROFILE."
|
||||
)
|
||||
return ScreenType.OWN_PROFILE
|
||||
|
||||
# Explore Grid
|
||||
if selected_tab == "search_tab":
|
||||
return ScreenType.EXPLORE_GRID
|
||||
|
||||
# Home Feed
|
||||
if selected_tab == "feed_tab":
|
||||
return ScreenType.HOME_FEED
|
||||
|
||||
# Post Detail: Typically has comment box or media note view but NO bottom tab layout
|
||||
if "media_note_view" in ids_str or "comment" in ids_str or "row_feed_button_comment" in ids_str:
|
||||
if "feed_tab" not in ids_str and "profile_tab" not in ids_str:
|
||||
return ScreenType.POST_DETAIL
|
||||
|
||||
# Comments
|
||||
if "layout_comment_thread" in ids_str or "comment_thread_recyclerview" in ids_str:
|
||||
return ScreenType.COMMENTS
|
||||
|
||||
# Follow List
|
||||
if (
|
||||
"follow_list_container" in ids_str
|
||||
or "layout_user_list" in ids_str
|
||||
or "layout_user_row" in ids_str
|
||||
or "follow_list_username" in ids_str
|
||||
):
|
||||
return ScreenType.FOLLOW_LIST
|
||||
|
||||
# Priority 2: Modal / Overlay Overrides
|
||||
if not is_normal_override:
|
||||
if "bottom_sheet_container" in ids_str or "action_sheet" in ids_str:
|
||||
return ScreenType.MODAL
|
||||
|
||||
# Priority 3: Cached Semantic Type (If deterministic heuristics failed)
|
||||
# GUARD: Profile types MUST be resolved by structural fast-paths above.
|
||||
# If they weren't, the cache is poisoned. Reject profile cache hits.
|
||||
_STRUCTURALLY_GATED_TYPES = (
|
||||
ScreenType.STORY_VIEW,
|
||||
ScreenType.REELS_FEED,
|
||||
ScreenType.OWN_PROFILE,
|
||||
ScreenType.OTHER_PROFILE,
|
||||
)
|
||||
if cached_type_str and cached_type_str != "NORMAL":
|
||||
try:
|
||||
cached_type = ScreenType[cached_type_str]
|
||||
# Enforce absolute structural parity: Story and Reels must have their structural markers.
|
||||
# If they reached Priority 3, it means Priority 2 failed to find their markers.
|
||||
# Therefore, any cache telling us this is a Story/Reel without those markers is hallucinating.
|
||||
if cached_type in (ScreenType.STORY_VIEW, ScreenType.REELS_FEED):
|
||||
if cached_type in _STRUCTURALLY_GATED_TYPES:
|
||||
logger.warning(
|
||||
f"⚠️ [ScreenIdentity] Rejecting cached {cached_type.name} due to missing structural markers."
|
||||
f"⚠️ [ScreenIdentity] Rejecting cached {cached_type.name} — "
|
||||
f"this type MUST be resolved structurally. Cache is unreliable."
|
||||
)
|
||||
else:
|
||||
return cached_type
|
||||
@@ -329,10 +343,12 @@ class ScreenIdentity:
|
||||
# Prevent the LLM from hallucinating an obstacle if explicitly verified as NORMAL
|
||||
return ScreenType.UNKNOWN
|
||||
|
||||
# Enforce absolute structural parity: Story and Reels must have their structural markers.
|
||||
if t in (ScreenType.STORY_VIEW, ScreenType.REELS_FEED):
|
||||
# Enforce absolute structural parity: These types MUST be resolved
|
||||
# by resource-id fast-paths. VLM guessing them poisons the Qdrant cache.
|
||||
if t in _STRUCTURALLY_GATED_TYPES:
|
||||
logger.warning(
|
||||
f"⚠️ [ScreenIdentity] Rejecting VLM hallucinated {t.name} due to missing structural markers."
|
||||
f"⚠️ [ScreenIdentity] Rejecting VLM hallucinated {t.name} — "
|
||||
f"this type MUST be resolved structurally to prevent cache poisoning."
|
||||
)
|
||||
return ScreenType.UNKNOWN
|
||||
|
||||
@@ -347,54 +363,33 @@ class ScreenIdentity:
|
||||
return ScreenType.UNKNOWN
|
||||
|
||||
def _extract_available_actions(self, clickable_elements, resource_ids, content_descs, texts, screen_type):
|
||||
"""Discover what actions are possible on this screen."""
|
||||
"""Discover what actions are possible on this screen using structural fast-paths.
|
||||
|
||||
PRIMARY: resource-id matching (architectural constants of the Instagram APK).
|
||||
Resource IDs like feed_tab, profile_tab, clips_tab are compile-time Android
|
||||
identifiers that NEVER change across locales. This is NOT hardcoded text matching.
|
||||
"""
|
||||
actions = []
|
||||
|
||||
# Navigation tabs (always available when visible)
|
||||
# --- Structural Tab Detection (resource-id based) ---
|
||||
# These are architectural constants of the APK, not localized strings.
|
||||
tab_map = {
|
||||
"feed_tab": "tap home tab",
|
||||
"search_tab": "tap explore tab",
|
||||
"clips_tab": "tap reels tab",
|
||||
"profile_tab": "tap profile tab",
|
||||
"direct_tab": "tap messages tab",
|
||||
"news_tab": "tap activity heart icon notifications",
|
||||
}
|
||||
for tab_id, action in tab_map.items():
|
||||
if tab_id in resource_ids:
|
||||
actions.append(action)
|
||||
|
||||
# Screen-specific actions
|
||||
desc_lower = " ".join(content_descs).lower()
|
||||
|
||||
if "like" in desc_lower:
|
||||
actions.append("tap like button")
|
||||
if "comment" in desc_lower:
|
||||
actions.append("tap comment button")
|
||||
if "share" in desc_lower:
|
||||
actions.append("tap share button")
|
||||
if "save" in desc_lower or "bookmark" in desc_lower:
|
||||
actions.append("tap save button")
|
||||
if "back" in desc_lower:
|
||||
actions.append("tap back button")
|
||||
has_following = any(
|
||||
"following" in e.get("text", "").lower() or "following" in e.get("desc", "").lower()
|
||||
for e in clickable_elements
|
||||
)
|
||||
if has_following:
|
||||
actions.append("tap following button")
|
||||
elif any(
|
||||
"follow" in e.get("text", "").lower()
|
||||
or "follow" in e.get("desc", "").lower()
|
||||
or "follow" in e.get("id", "").lower()
|
||||
for e in clickable_elements
|
||||
):
|
||||
actions.append("tap follow button")
|
||||
|
||||
ids_str = " ".join(resource_ids).lower()
|
||||
if screen_type == ScreenType.OWN_PROFILE or screen_type == ScreenType.OTHER_PROFILE:
|
||||
if "button_message" in ids_str or "profile_header_message_button" in ids_str:
|
||||
actions.append("tap message button")
|
||||
if "profile_header_following" in ids_str or "profile_header_follow_button" in ids_str:
|
||||
# We can navigate to following list if we are following them
|
||||
actions.append("tap following list")
|
||||
|
||||
# Grid items
|
||||
@@ -409,22 +404,9 @@ class ScreenIdentity:
|
||||
return list(set(actions)) # Deduplicate
|
||||
|
||||
def _extract_context(self, content_descs, texts, resource_ids, screen_type):
|
||||
"""Extract meaningful context from the screen."""
|
||||
"""Extract meaningful context from the screen using structural heuristics."""
|
||||
context = {}
|
||||
|
||||
# Post/follower counts
|
||||
for d in content_descs:
|
||||
m = re.match(r"([\d,.]+K?M?)(\s*)(posts?|followers?|following)", d, re.IGNORECASE)
|
||||
if m:
|
||||
context[m.group(3).lower()] = m.group(1)
|
||||
|
||||
# Like state
|
||||
for d in content_descs:
|
||||
if d.lower() == "liked":
|
||||
context["is_liked"] = True
|
||||
elif d.lower() == "like":
|
||||
context["is_liked"] = False
|
||||
|
||||
# Zero Maintenance: Do not rely on localized regex strings like "followers" or "liked"
|
||||
return context
|
||||
|
||||
def _compute_signature(self, resource_ids, content_descs, texts):
|
||||
|
||||
@@ -152,4 +152,78 @@ class SemanticEvaluator:
|
||||
pass
|
||||
|
||||
def classify_screen_content(self, xml_hierarchy: str, target_class: str) -> Optional[str]:
|
||||
pass
|
||||
"""
|
||||
Fast-Path Structural Analysis.
|
||||
Replaces VLM calls for basic structural states.
|
||||
"""
|
||||
if not xml_hierarchy:
|
||||
return None
|
||||
|
||||
xml_lower = xml_hierarchy.lower()
|
||||
|
||||
if target_class == "carousel_or_single_post":
|
||||
if 'resource-id="com.instagram.android:id/carousel_image"' in xml_lower or (
|
||||
'scrollable="true"' in xml_lower and "viewpager" in xml_lower
|
||||
):
|
||||
return "carousel"
|
||||
return "single"
|
||||
|
||||
elif target_class == "post_has_comments":
|
||||
if 'resource-id="com.instagram.android:id/row_feed_button_comment"' in xml_lower:
|
||||
return "has_comments"
|
||||
return "no_comments"
|
||||
|
||||
elif target_class == "sponsored_content":
|
||||
# Heuristic for sponsored content
|
||||
if "sponsored" in xml_lower or "gesponsert" in xml_lower:
|
||||
return "sponsored"
|
||||
return "organic"
|
||||
|
||||
elif target_class == "story_ring_presence":
|
||||
if "reel_ring" in xml_lower or "story_ring" in xml_lower:
|
||||
return "has_unseen_story"
|
||||
return "no_unseen_story"
|
||||
|
||||
elif target_class == "main_feed_presence":
|
||||
if 'content-desc="home"' in xml_lower and 'selected="true"' in xml_lower:
|
||||
return "main_feed"
|
||||
if "feed_tab" in xml_lower and 'selected="true"' in xml_lower:
|
||||
return "main_feed"
|
||||
return "other"
|
||||
|
||||
elif target_class == "private_account":
|
||||
if (
|
||||
'resource-id="com.instagram.android:id/row_profile_header_empty_profile_notice_title"' in xml_lower
|
||||
and "private" in xml_lower
|
||||
):
|
||||
return "private"
|
||||
return "public"
|
||||
|
||||
elif target_class == "empty_account":
|
||||
if 'resource-id="com.instagram.android:id/row_profile_header_empty_profile_notice_title"' in xml_lower and (
|
||||
"no posts" in xml_lower or "noch keine" in xml_lower
|
||||
):
|
||||
return "empty"
|
||||
return "not_empty"
|
||||
|
||||
elif target_class == "close_friends_content":
|
||||
if "close_friends" in xml_lower or "close friends" in xml_lower or "enge freunde" in xml_lower:
|
||||
return "close_friends"
|
||||
return "normal_content"
|
||||
|
||||
elif target_class in ("profile_follow_status", "follow_button_state"):
|
||||
if 'resource-id="com.instagram.android:id/profile_header_follow_button"' in xml_lower:
|
||||
return "not_following"
|
||||
if 'resource-id="com.instagram.android:id/profile_header_following_button"' in xml_lower:
|
||||
return "already_following"
|
||||
# Fallback text check for requested state
|
||||
if "requested" in xml_lower or "angefragt" in xml_lower:
|
||||
return "requested"
|
||||
return "unknown"
|
||||
|
||||
elif target_class == "unfollow_bottom_sheet_presence":
|
||||
if 'resource-id="com.instagram.android:id/follow_sheet_unfollow_row"' in xml_lower:
|
||||
return "unfollow_sheet"
|
||||
return "other"
|
||||
|
||||
return None
|
||||
|
||||
@@ -15,6 +15,7 @@ class SpatialNode:
|
||||
content_desc: str = ""
|
||||
resource_id: str = ""
|
||||
clickable: bool = False
|
||||
long_clickable: bool = False
|
||||
scrollable: bool = False
|
||||
|
||||
# Spatial Properties
|
||||
@@ -162,6 +163,7 @@ class SpatialParser:
|
||||
resource_id=attrib.get("resource-id", "").strip(),
|
||||
bounds=(left, top, right, bottom),
|
||||
clickable=attrib.get("clickable", "false") == "true",
|
||||
long_clickable=attrib.get("long-clickable", "false") == "true",
|
||||
scrollable=attrib.get("scrollable", "false") == "true",
|
||||
)
|
||||
nodes_list.append(node)
|
||||
|
||||
@@ -14,7 +14,6 @@ import time
|
||||
from time import sleep
|
||||
|
||||
from GramAddict.core.diagnostic_dump import dump_ui_state
|
||||
from GramAddict.core.perception.feed_analysis import FEED_MARKERS
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -29,12 +28,17 @@ def wait_for_post_loaded(device, timeout=5, nav_graph=None):
|
||||
3. Micro-wobbles to force render
|
||||
"""
|
||||
|
||||
from GramAddict.core.perception.screen_identity import ScreenIdentity, ScreenType
|
||||
|
||||
identity = ScreenIdentity("")
|
||||
|
||||
start = time.time()
|
||||
xml = ""
|
||||
while time.time() - start < timeout:
|
||||
try:
|
||||
xml = device.dump_hierarchy()
|
||||
if any(marker in xml for marker in FEED_MARKERS):
|
||||
state = identity.identify(xml)
|
||||
if state["screen_type"] in (ScreenType.POST_DETAIL, ScreenType.HOME_FEED, ScreenType.REELS_FEED):
|
||||
logger.debug("📱 Post loaded successfully.")
|
||||
return True
|
||||
|
||||
@@ -52,39 +56,36 @@ def wait_for_post_loaded(device, timeout=5, nav_graph=None):
|
||||
dump_ui_state(device, "post_load_timeout", {"timeout_sec": timeout})
|
||||
|
||||
try:
|
||||
xml_lower = xml.lower()
|
||||
xml = device.dump_hierarchy()
|
||||
state = identity.identify(xml)
|
||||
|
||||
# 1. Trapped in a Story or Reel viewer? Press back.
|
||||
if "reel_viewer_root" in xml_lower or "clips_viewer" in xml_lower:
|
||||
logger.warning("🧗 [Adaptive Snap] Trapped in Story/Reel viewer. Pressing BACK.")
|
||||
# 1. Trapped in a Story viewer? Press back.
|
||||
if state["screen_type"] == ScreenType.STORY_VIEW:
|
||||
logger.warning("🧗 [Adaptive Snap] Trapped in Story viewer. Pressing BACK.")
|
||||
device.press("back")
|
||||
sleep(1.5)
|
||||
# Give it one more chance to load the feed
|
||||
xml = device.dump_hierarchy()
|
||||
xml_lower = xml.lower()
|
||||
if any(marker in xml for marker in FEED_MARKERS):
|
||||
state = identity.identify(xml)
|
||||
if state["screen_type"] in (ScreenType.POST_DETAIL, ScreenType.HOME_FEED, ScreenType.REELS_FEED):
|
||||
logger.info("✅ Recovered to Feed.")
|
||||
return True
|
||||
|
||||
# 2. Trapped in Profile?
|
||||
# Only press back if we did NOT intend to be on a profile!
|
||||
expected_state = nav_graph.current_state if nav_graph else ""
|
||||
if (
|
||||
expected_state != "ProfileView"
|
||||
and "profile_header" in xml_lower
|
||||
and "row_feed_photo_profile_name" not in xml_lower
|
||||
if expected_state != "ProfileView" and state["screen_type"] in (
|
||||
ScreenType.OWN_PROFILE,
|
||||
ScreenType.OTHER_PROFILE,
|
||||
):
|
||||
logger.warning("🧗 [Adaptive Snap] Trapped in Profile. Pressing BACK.")
|
||||
device.press("back")
|
||||
sleep(1.5)
|
||||
xml = device.dump_hierarchy()
|
||||
xml_lower = xml.lower()
|
||||
state = identity.identify(xml)
|
||||
|
||||
# 3. Stuck on Grid? The tap didn't register. Do not wobble.
|
||||
grid_markers = ["explore_tab", "explore_grid", "grid_card_layout_container", "profile_tabs_container"]
|
||||
if any(m in xml_lower for m in grid_markers) or (
|
||||
"profile_header" in xml_lower and "row_feed_photo_profile_name" not in xml_lower
|
||||
):
|
||||
if state["screen_type"] in (ScreenType.EXPLORE_GRID, ScreenType.OWN_PROFILE, ScreenType.OTHER_PROFILE):
|
||||
logger.warning(
|
||||
"🧗 [Adaptive Snap] Detected bot is STILL on the Grid/Profile. Tap likely missed. Aborting snap."
|
||||
)
|
||||
@@ -104,12 +105,17 @@ def wait_for_post_loaded(device, timeout=5, nav_graph=None):
|
||||
|
||||
|
||||
def wait_for_story_loaded(device, timeout=5):
|
||||
"""Polls the UI hierarchy until story markers appear, confirming a story is on screen."""
|
||||
"""Polls the UI hierarchy until story screen is identified via autonomous VLM classification."""
|
||||
from GramAddict.core.perception.screen_identity import ScreenIdentity, ScreenType
|
||||
|
||||
start = time.time()
|
||||
identity = ScreenIdentity("")
|
||||
|
||||
while time.time() - start < timeout:
|
||||
try:
|
||||
xml_lower = device.dump_hierarchy().lower()
|
||||
if "reel_viewer_root" in xml_lower or "story_viewer" in xml_lower:
|
||||
xml = device.dump_hierarchy()
|
||||
state = identity.identify(xml)
|
||||
if state["screen_type"] == ScreenType.STORY_VIEW:
|
||||
logger.debug("📱 Story loaded successfully.")
|
||||
return True
|
||||
except Exception:
|
||||
@@ -121,16 +127,19 @@ def wait_for_story_loaded(device, timeout=5):
|
||||
|
||||
|
||||
def wait_for_profile_loaded(device, timeout=5):
|
||||
"""Polls the UI hierarchy until profile markers appear."""
|
||||
"""Polls the UI hierarchy until the profile screen is identified via autonomous VLM classification."""
|
||||
import time
|
||||
|
||||
from GramAddict.core.perception.screen_identity import ScreenIdentity, ScreenType
|
||||
|
||||
start = time.time()
|
||||
PROFILE_MARKERS = ["profile_header", "action_bar_title", "profile_tabs_container"]
|
||||
identity = ScreenIdentity("")
|
||||
|
||||
while time.time() - start < timeout:
|
||||
try:
|
||||
xml_lower = device.dump_hierarchy().lower()
|
||||
if any(marker in xml_lower for marker in PROFILE_MARKERS):
|
||||
xml = device.dump_hierarchy()
|
||||
state = identity.identify(xml)
|
||||
if state["screen_type"] in (ScreenType.OWN_PROFILE, ScreenType.OTHER_PROFILE):
|
||||
logger.debug("📱 Profile loaded successfully.")
|
||||
return True
|
||||
except Exception:
|
||||
|
||||
@@ -1499,13 +1499,17 @@ class ContextMemoryDB(QdrantBase):
|
||||
|
||||
def is_allowed(self, intent: str, screen_type: str) -> bool:
|
||||
"""
|
||||
Exploration vs Exploitation:
|
||||
If we don't know (no entry or neutral confidence), allow it!
|
||||
Only block if we have learned it fails consistently (< 0.2).
|
||||
Exploration vs Exploitation with time-based decay:
|
||||
- If we don't know (no entry), allow it (exploration).
|
||||
- Only block if we have RECENTLY learned it fails (< 0.2 confidence AND < 1 hour old).
|
||||
- Old failures decay: entries older than 1 hour are auto-forgiven and deleted,
|
||||
preventing permanent poisoning of the action space.
|
||||
"""
|
||||
if not self.is_connected:
|
||||
return True # Fail-open for exploration
|
||||
|
||||
DECAY_THRESHOLD_SECONDS = 3600 # 1 hour: old failures are forgiven
|
||||
|
||||
key = self._get_key(intent, screen_type)
|
||||
try:
|
||||
point_id = self.generate_uuid(key)
|
||||
@@ -1513,11 +1517,25 @@ class ContextMemoryDB(QdrantBase):
|
||||
collection_name=self.collection_name, ids=[point_id], with_payload=True, with_vectors=False
|
||||
)
|
||||
if points:
|
||||
conf = points[0].payload.get("confidence", 0.5)
|
||||
payload = points[0].payload
|
||||
conf = payload.get("confidence", 0.5)
|
||||
updated_at = payload.get("updated_at", 0)
|
||||
age_seconds = time.time() - updated_at
|
||||
|
||||
# Time-based decay: old failures are forgiven
|
||||
if conf < 0.2 and age_seconds > DECAY_THRESHOLD_SECONDS:
|
||||
logger.info(
|
||||
f"🔄 [ContextMemory] Forgave old failure for '{intent}' on '{screen_type}' "
|
||||
f"(age: {age_seconds/60:.0f}min, confidence: {conf:.2f}). Allowing re-exploration."
|
||||
)
|
||||
# Delete the stale entry so the bot can re-learn
|
||||
self.delete_point(key)
|
||||
return True
|
||||
|
||||
if conf < 0.2:
|
||||
logger.warning(
|
||||
f"🛡️ [ContextMemory] Circuit Breaker: Blocked '{intent}' on '{screen_type}' "
|
||||
f"(learned confidence {conf:.2f} < 0.2)"
|
||||
f"(learned confidence {conf:.2f} < 0.2, age: {age_seconds/60:.0f}min)"
|
||||
)
|
||||
return False
|
||||
except Exception:
|
||||
|
||||
@@ -33,6 +33,7 @@ class ScreenTopology:
|
||||
"tap profile tab": ScreenType.OWN_PROFILE,
|
||||
"tap reels tab": ScreenType.REELS_FEED,
|
||||
"tap messages tab": ScreenType.DM_INBOX,
|
||||
"tap activity heart icon notifications": ScreenType.NOTIFICATIONS,
|
||||
"tap story ring avatar": ScreenType.STORY_VIEW,
|
||||
},
|
||||
ScreenType.EXPLORE_GRID: {
|
||||
@@ -84,6 +85,12 @@ class ScreenTopology:
|
||||
"tap home tab": ScreenType.HOME_FEED,
|
||||
# NOTE: 'press back' intentionally omitted — destination is non-deterministic
|
||||
},
|
||||
ScreenType.NOTIFICATIONS: {
|
||||
"tap home tab": ScreenType.HOME_FEED,
|
||||
"press back": ScreenType.HOME_FEED,
|
||||
"tap profile tab": ScreenType.OWN_PROFILE,
|
||||
"tap explore tab": ScreenType.EXPLORE_GRID,
|
||||
},
|
||||
ScreenType.UNKNOWN: {
|
||||
"tap home tab": ScreenType.HOME_FEED,
|
||||
},
|
||||
@@ -110,6 +117,7 @@ class ScreenTopology:
|
||||
"open user profile": ScreenType.OTHER_PROFILE,
|
||||
"open search": ScreenType.SEARCH_RESULTS,
|
||||
"view comments": ScreenType.COMMENTS,
|
||||
"open notifications": ScreenType.NOTIFICATIONS,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
@@ -214,6 +222,18 @@ class ScreenTopology:
|
||||
Used by _execute_action to validate INTERMEDIATE navigation steps.
|
||||
Returns None if the action isn't a known transition from this screen.
|
||||
"""
|
||||
# Hardcode self-edges for main tabs (which are no-ops)
|
||||
if action == "tap home tab" and from_screen == ScreenType.HOME_FEED:
|
||||
return ScreenType.HOME_FEED
|
||||
if action == "tap explore tab" and from_screen == ScreenType.EXPLORE_GRID:
|
||||
return ScreenType.EXPLORE_GRID
|
||||
if action == "tap reels tab" and from_screen == ScreenType.REELS_FEED:
|
||||
return ScreenType.REELS_FEED
|
||||
if action == "tap profile tab" and from_screen == ScreenType.OWN_PROFILE:
|
||||
return ScreenType.OWN_PROFILE
|
||||
if action == "tap messages tab" and from_screen == ScreenType.DM_INBOX:
|
||||
return ScreenType.DM_INBOX
|
||||
|
||||
transitions = cls.TRANSITIONS.get(from_screen, {})
|
||||
return transitions.get(action)
|
||||
|
||||
|
||||
@@ -98,22 +98,28 @@ class SituationEpisodeDB:
|
||||
|
||||
# Sort by confidence desc, prefer successful episodes
|
||||
best_positive = None
|
||||
failed_actions = set()
|
||||
failed_action_signatures = set()
|
||||
|
||||
for r in results:
|
||||
p = r.payload
|
||||
action_data = p.get("action", {})
|
||||
# Create a signature for the action: type + coordinates
|
||||
sig = f"{action_data.get('action_type')}|{action_data.get('x', 0)},{action_data.get('y', 0)}"
|
||||
|
||||
if not p.get("success", False):
|
||||
# Track failed actions so we don't repeat them
|
||||
failed_actions.add(p.get("action", {}).get("action_type", ""))
|
||||
# Track failed actions so we don't repeat the exact same interaction
|
||||
failed_action_signatures.add(sig)
|
||||
continue
|
||||
|
||||
conf = p.get("confidence", 0.0)
|
||||
if conf >= 0.3 and (best_positive is None or conf > best_positive.payload.get("confidence", 0)):
|
||||
best_positive = r
|
||||
|
||||
if best_positive:
|
||||
action_data = best_positive.payload.get("action", {})
|
||||
# Don't return an action type that has also failed before for this situation
|
||||
if action_data.get("action_type") not in failed_actions:
|
||||
sig = f"{action_data.get('action_type')}|{action_data.get('x', 0)},{action_data.get('y', 0)}"
|
||||
|
||||
if sig not in failed_action_signatures:
|
||||
logger.info(
|
||||
f"🧠 [SAE Recall] Instant memory hit! Situation matched with confidence "
|
||||
f"{best_positive.payload.get('confidence', 0):.2f}. Action: {action_data.get('reason', 'unknown')}"
|
||||
@@ -142,16 +148,21 @@ class SituationEpisodeDB:
|
||||
seed = f"{situation_signature}|{action.action_type}|{action.x},{action.y}"
|
||||
point_id = self._db.generate_uuid(seed)
|
||||
|
||||
# Retrieve existing confidence if any
|
||||
current_conf = 0.0
|
||||
has_existing = False
|
||||
try:
|
||||
points = self._db.client.retrieve(
|
||||
collection_name=self._db.collection_name, ids=[point_id], with_payload=True, with_vectors=False
|
||||
collection_name=self._db.collection_name,
|
||||
ids=[point_id],
|
||||
with_payload=True,
|
||||
with_vectors=False,
|
||||
)
|
||||
if points:
|
||||
has_existing = True
|
||||
current_conf = points[0].payload.get("confidence", 0.0)
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
logger.debug(f"SAE retrieval error (non-fatal): {e}")
|
||||
pass
|
||||
|
||||
if success:
|
||||
@@ -160,7 +171,8 @@ class SituationEpisodeDB:
|
||||
confidence = current_conf - 0.5 if has_existing else -0.5
|
||||
|
||||
if confidence < 0.1 and not success:
|
||||
self._db.client.delete(collection_name=self._db.collection_name, points_selector=[point_id])
|
||||
# Use safe wrapper to avoid crash if DB is down
|
||||
self._db.delete_point(seed)
|
||||
logger.info("🗑️ [SAE Learn] Action decayed below threshold. Deleted from memory.")
|
||||
return
|
||||
|
||||
@@ -244,6 +256,12 @@ class SituationalAwarenessEngine:
|
||||
for elem in root.iter("node"):
|
||||
a = elem.attrib
|
||||
pkg = a.get("package", "")
|
||||
|
||||
# CRITICAL: Exclude system UI (status bar) from signature because it is
|
||||
# identical across all screens and takes up the top 25 visual spots!
|
||||
if pkg == "com.android.systemui":
|
||||
continue
|
||||
|
||||
if pkg:
|
||||
packages.add(pkg)
|
||||
|
||||
@@ -270,19 +288,31 @@ class SituationalAwarenessEngine:
|
||||
parts.append(f"desc='{desc}'")
|
||||
if clickable == "true":
|
||||
parts.append("CLICKABLE")
|
||||
cy_val = 0
|
||||
if bounds:
|
||||
nums = [int(n) for n in re.findall(r"\d+", bounds)]
|
||||
if len(nums) == 4:
|
||||
cx = (nums[0] + nums[2]) // 2
|
||||
cy = (nums[1] + nums[3]) // 2
|
||||
parts.append(f"bounds={bounds} center=({cx},{cy})")
|
||||
cy_val = (nums[1] + nums[3]) // 2
|
||||
parts.append(f"bounds={bounds} center=({cx},{cy_val})")
|
||||
else:
|
||||
parts.append(f"bounds={bounds}")
|
||||
|
||||
elements.append(" | ".join(parts))
|
||||
elements.append((cy_val, " | ".join(parts)))
|
||||
|
||||
sig = f"PACKAGES: {', '.join(sorted(packages))}\n"
|
||||
sig += "\n".join(elements[-50:]) # Keep the last 50 elements (highest Z-index/foreground)
|
||||
|
||||
# Sort by vertical center (Y) coordinate to get visual top-to-bottom
|
||||
elements.sort(key=lambda x: x[0])
|
||||
visual_elements = [e[1] for e in elements]
|
||||
|
||||
if len(visual_elements) > 50:
|
||||
# Keep top 25 (headers, titles) and bottom 25 (tabs, nav) visually
|
||||
selected_elements = visual_elements[:25] + visual_elements[-25:]
|
||||
else:
|
||||
selected_elements = visual_elements
|
||||
|
||||
sig += "\n".join(selected_elements)
|
||||
return sig[:3000]
|
||||
|
||||
def _compute_situation_hash(self, compressed: str) -> str:
|
||||
@@ -339,14 +369,26 @@ class SituationalAwarenessEngine:
|
||||
return SituationType.OBSTACLE_SYSTEM
|
||||
|
||||
# ── Keyboard Detection (Fast Path) ──
|
||||
keyboard_pkgs = {
|
||||
"com.google.android.inputmethod.latin",
|
||||
"com.samsung.android.honeyboard",
|
||||
"com.sec.android.inputmethod",
|
||||
"com.touchtype.swiftkey",
|
||||
"com.apple.android.inputmethod",
|
||||
}
|
||||
if any(pkg in keyboard_pkgs for pkg in packages):
|
||||
# Instead of relying on brittle package names (which may be missing or custom),
|
||||
# an open keyboard is definitively proven if an EditText is focused,
|
||||
# or if unmistakable keyboard structural markers exist.
|
||||
is_actual_keyboard = False
|
||||
if 'focused="true"' in xml_dump and "EditText" in xml_dump:
|
||||
if re.search(r'class="[^"]*EditText"[^>]*focused="true"', xml_dump) or re.search(
|
||||
r'focused="true"[^>]*class="[^"]*EditText"', xml_dump
|
||||
):
|
||||
is_actual_keyboard = True
|
||||
|
||||
# Structural fallback: certain strings are exclusive to keyboards
|
||||
# We match these securely against content-desc or resource-id to avoid false positives from user post text.
|
||||
keyboard_markers = [
|
||||
r'content-desc="[^"]*(?:Symboltastatur|Switch input method|Leerzeichen|Spracheingabe verwenden)[^"]*"',
|
||||
r'resource-id="[^"]*input_method_nav_ime_switcher[^"]*"',
|
||||
]
|
||||
if not is_actual_keyboard and any(re.search(marker, xml_dump, re.IGNORECASE) for marker in keyboard_markers):
|
||||
is_actual_keyboard = True
|
||||
|
||||
if is_actual_keyboard:
|
||||
logger.info("📱 [SAE Perceive] On-screen Keyboard explicitly detected. Treating as obstacle.")
|
||||
return SituationType.OBSTACLE_KEYBOARD
|
||||
|
||||
@@ -472,6 +514,8 @@ class SituationalAwarenessEngine:
|
||||
"ig_browser_text_title", # In-App Browser Title
|
||||
"ig_browser_close_button", # In-App Browser Close Button
|
||||
"ig_chrome_subsection", # In-App Browser Chrome
|
||||
"bottom_sheet_container", # Bottom sheet modal (e.g., following options)
|
||||
"action_sheet_container", # Action sheet modal
|
||||
)
|
||||
if any(
|
||||
re.search(rf'resource-id="[^"]*{marker}[^"]*"', xml_dump, re.IGNORECASE)
|
||||
@@ -590,6 +634,87 @@ class SituationalAwarenessEngine:
|
||||
# 2. PLAN: AI-driven escape strategy
|
||||
# ──────────────────────────────────────────────
|
||||
|
||||
def _find_structural_dismiss_target(self, xml_dump: str) -> Optional[EscapeAction]:
|
||||
"""
|
||||
Structurally scan the raw XML for any clickable element that semantically
|
||||
represents a dismiss/close/cancel action. Returns None if nothing found.
|
||||
Zero hardcoding of localized text — relies purely on resource-ids and VLM discovery fallback.
|
||||
"""
|
||||
import re
|
||||
|
||||
# Patterns for dismiss-type buttons based purely on resource IDs or text
|
||||
dismiss_id_keywords = (
|
||||
"close",
|
||||
"cancel",
|
||||
"dismiss",
|
||||
"negative_button",
|
||||
"button2", # Standard Android negative button
|
||||
"not_now",
|
||||
"skip",
|
||||
"clear",
|
||||
"decline",
|
||||
)
|
||||
|
||||
dismiss_text_keywords = (
|
||||
"close",
|
||||
"cancel",
|
||||
"dismiss",
|
||||
"not now",
|
||||
"nicht jetzt",
|
||||
"skip",
|
||||
"decline",
|
||||
"clear",
|
||||
)
|
||||
|
||||
# Extract each <node ...> or <node ... /> tag individually
|
||||
node_tags = re.findall(r"<node\b[^>]*>", xml_dump, re.IGNORECASE)
|
||||
|
||||
candidates = []
|
||||
for tag in node_tags:
|
||||
# Extract attributes independently — order doesn't matter
|
||||
id_m = re.search(r'resource-id="([^"]*)"', tag)
|
||||
text_m = re.search(r'text="([^"]*)"', tag)
|
||||
desc_m = re.search(r'content-desc="([^"]*)"', tag)
|
||||
click_m = re.search(r'clickable="([^"]*)"', tag)
|
||||
bounds_m = re.search(r'bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"', tag)
|
||||
|
||||
rid = (id_m.group(1) if id_m else "").strip().lower()
|
||||
text = (text_m.group(1) if text_m else "").strip().lower()
|
||||
desc = (desc_m.group(1) if desc_m else "").strip().lower()
|
||||
clickable = (click_m.group(1) if click_m else "").lower()
|
||||
|
||||
if clickable != "true" or not bounds_m:
|
||||
continue
|
||||
|
||||
matched = False
|
||||
for kw in dismiss_id_keywords:
|
||||
if kw in rid:
|
||||
matched = True
|
||||
break
|
||||
|
||||
if not matched:
|
||||
for kw in dismiss_text_keywords:
|
||||
if kw == text or kw == desc:
|
||||
matched = True
|
||||
break
|
||||
|
||||
if matched:
|
||||
x1, y1 = int(bounds_m.group(1)), int(bounds_m.group(2))
|
||||
x2, y2 = int(bounds_m.group(3)), int(bounds_m.group(4))
|
||||
cx, cy = (x1 + x2) // 2, (y1 + y2) // 2
|
||||
ident = rid or text or desc or "dismiss_button"
|
||||
candidates.append((cx, cy, ident))
|
||||
|
||||
if candidates:
|
||||
# Prefer elements lower on the screen (highest Y) for dialog dismiss buttons
|
||||
candidates.sort(key=lambda c: c[1], reverse=True)
|
||||
best = candidates[0]
|
||||
cx, cy, ident = best
|
||||
logger.info(f"🔍 [SAE Structural] Found dismiss target '{ident}' at ({cx}, {cy})")
|
||||
return EscapeAction("click", x=cx, y=cy, reason=f"Structural dismiss: '{ident}'")
|
||||
|
||||
return None
|
||||
|
||||
def _plan_escape_via_llm(
|
||||
self, xml_dump: str, compressed: str, situation_type: SituationType, failed_actions: set = None
|
||||
) -> Optional[EscapeAction]:
|
||||
@@ -604,30 +729,46 @@ class SituationalAwarenessEngine:
|
||||
model = getattr(args, "ai_telepathic_model")
|
||||
url = getattr(args, "ai_telepathic_url")
|
||||
|
||||
# Build failure context so the LLM knows what NOT to repeat
|
||||
failure_block = ""
|
||||
temperature = 0.1
|
||||
if failed_actions:
|
||||
failure_block = (
|
||||
"\n\nCRITICAL — The following actions have ALREADY BEEN TRIED and FAILED. "
|
||||
"You MUST NOT propose any of these again:\n"
|
||||
)
|
||||
for fa in failed_actions:
|
||||
failure_block += f" - {fa}\n"
|
||||
failure_block += (
|
||||
"\nYou MUST pick a DIFFERENT element or a DIFFERENT action type. "
|
||||
"Study the XML carefully for buttons you have NOT tried yet.\n"
|
||||
)
|
||||
# Increase temperature to force diversity when the LLM is being stubborn
|
||||
temperature = min(0.7, 0.1 + 0.2 * len(failed_actions))
|
||||
|
||||
system_prompt = (
|
||||
"You are an Android UI navigation agent. Your job is to escape obstacles "
|
||||
"(dialogs, modals, foreign apps, system popups) and return to Instagram. "
|
||||
"Analyze the screen content (Screenshot AND XML) and return a JSON escape action.\n\n"
|
||||
"You are the Navigation Engine for an autonomous agent.\n"
|
||||
"An obstacle has been detected that prevents normal operations.\n"
|
||||
"Your goal is to DISMISS or CLEAR this obstacle to return to the app's main workflow.\n\n"
|
||||
f"Current Obstacle Type: {situation_type.value}\n\n"
|
||||
"Rules:\n"
|
||||
"- If you see a dismiss/close/cancel/skip/not now button, click it\n"
|
||||
"- If the Situation type is obstacle_locked_screen, action must be 'unlock'\n"
|
||||
"- If the Situation type is obstacle_foreign_app, action must be 'kill_foreign_apps'\n"
|
||||
"- If the Situation type is obstacle_system, you MUST look for 'Deny', 'Don't allow', or 'Cancel' and click it. \n"
|
||||
" NEVER click 'Allow', 'OK', or 'Confirm' on system permissions.\n"
|
||||
" If no negative action button exists, action must be 'back'\n"
|
||||
"- If the screen shows an In-App Browser, WebView, website, or Advertisement, it is an obstacle. Action must be 'back' or click the close button.\n"
|
||||
"- If there is NO obstacle and the screen is a normal Instagram view (false positive), action must be 'false_positive'\n"
|
||||
"- If nothing else works, suggest 'app_start' to force-reopen Instagram\n"
|
||||
"- NEVER click 'OK'/'Confirm'/'Accept' on surveys or prompts\n"
|
||||
"- When you choose to click, you MUST use the EXACT coordinates provided in `center=(x,y)` for that element in the XML\n"
|
||||
'- Return ONLY valid JSON: {"action": "click"|"back"|"app_start"|"unlock"|"kill_foreign_apps"|"false_positive", "x": N, "y": N, "reason": "..."}'
|
||||
"- If the Situation type is obstacle_modal, find and click a 'Cancel', 'Close', 'Dismiss', 'Not Now', "
|
||||
"or 'X' button. Look carefully at ALL elements in the XML.\n"
|
||||
"- If it is a system dialog, click 'Allow', 'OK' or 'Dismiss'.\n"
|
||||
"- If the keyboard is blocking, your only action is 'back'.\n"
|
||||
'- When you choose to click, you MUST use the EXACT coordinates from bounds="[x1,y1][x2,y2]" '
|
||||
"in the XML. Calculate center as ((x1+x2)/2, (y1+y2)/2). Do NOT invent coordinates.\n"
|
||||
"- If no clickable dismiss element exists in the XML, use 'back'.\n"
|
||||
"- Respond ONLY with valid JSON: "
|
||||
'{"action": "click" | "back" | "app_start", "x": int, "y": int, "reason": "string"}'
|
||||
f"{failure_block}"
|
||||
)
|
||||
|
||||
user_prompt = f"Situation type: {situation_type.value}\n\n" f"Screen content:\n{compressed}\n\n"
|
||||
if failed_actions:
|
||||
user_prompt += f"Failed actions this session (DO NOT REPEAT): {list(failed_actions)}\n\n"
|
||||
|
||||
user_prompt += "What action should I take to clear this obstacle and return to Instagram? Return JSON only."
|
||||
user_prompt = (
|
||||
f"Situation type: {situation_type.value}\n\n"
|
||||
f"Screen XML:\n{compressed}\n\n"
|
||||
"What action should I take to clear this obstacle and return to Instagram? Return JSON only."
|
||||
)
|
||||
|
||||
try:
|
||||
screenshot_b64 = getattr(self.device, "get_screenshot_b64", lambda: None)()
|
||||
@@ -638,7 +779,7 @@ class SituationalAwarenessEngine:
|
||||
user_prompt=user_prompt,
|
||||
system_prompt=system_prompt,
|
||||
images_b64=[screenshot_b64] if screenshot_b64 else None,
|
||||
temperature=0.0,
|
||||
temperature=temperature,
|
||||
)
|
||||
if resp:
|
||||
import json
|
||||
@@ -646,7 +787,6 @@ class SituationalAwarenessEngine:
|
||||
try:
|
||||
data = json.loads(resp)
|
||||
except json.JSONDecodeError:
|
||||
# Try extracting JSON via regex if LLM was chatty
|
||||
import re
|
||||
|
||||
match = re.search(r"\{.*\}", resp, re.DOTALL)
|
||||
@@ -655,15 +795,42 @@ class SituationalAwarenessEngine:
|
||||
else:
|
||||
raise ValueError(f"Could not parse JSON from: {resp}")
|
||||
|
||||
action_type = data.get("action", "back")
|
||||
x = int(data.get("x", 0))
|
||||
y = int(data.get("y", 0))
|
||||
reason = data.get("reason", "LLM-planned escape")
|
||||
action_key = f"{action_type}:{x},{y}"
|
||||
|
||||
# If LLM stubbornly repeats a failed action, try structural scan as autonomous fallback
|
||||
if failed_actions and action_key in failed_actions:
|
||||
logger.warning(
|
||||
f"🧠 [SAE] LLM repeated failed action ({action_key}). "
|
||||
"Falling back to structural dismiss scan."
|
||||
)
|
||||
structural = self._find_structural_dismiss_target(xml_dump)
|
||||
if structural:
|
||||
structural_key = f"{structural.action_type}:{structural.x},{structural.y}"
|
||||
if structural_key not in failed_actions:
|
||||
return structural
|
||||
# If structural also exhausted, escalate to back
|
||||
if "back:0,0" not in (failed_actions or set()):
|
||||
return EscapeAction("back", reason="Autonomous fallback: structural + LLM both exhausted")
|
||||
return EscapeAction("app_start", reason="Autonomous escalation: all escape strategies exhausted")
|
||||
|
||||
return EscapeAction(
|
||||
action_type=data.get("action", "back"),
|
||||
x=int(data.get("x", 0)),
|
||||
y=int(data.get("y", 0)),
|
||||
reason=data.get("reason", "LLM-planned escape"),
|
||||
action_type=action_type,
|
||||
x=x,
|
||||
y=y,
|
||||
reason=reason,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"🧠 [SAE] LLM escape planning failed: {e}")
|
||||
|
||||
# Last resort: try structural scan before giving up
|
||||
structural = self._find_structural_dismiss_target(xml_dump)
|
||||
if structural:
|
||||
return structural
|
||||
|
||||
return EscapeAction("back", reason="LLM planning failed, defaulting to BACK")
|
||||
|
||||
# ──────────────────────────────────────────────
|
||||
@@ -766,6 +933,23 @@ class SituationalAwarenessEngine:
|
||||
|
||||
logger.warning(f"🔍 [SAE] Obstacle detected: {situation.value} (attempt {attempt + 1}/{max_attempts})")
|
||||
|
||||
# ── O(1) Fast-Path for Locked Screen ──
|
||||
if situation == SituationType.OBSTACLE_LOCKED_SCREEN:
|
||||
logger.warning("⚡ [SAE Fast-Path] Screen is OFF or LOCKED. Forcing wake/unlock...")
|
||||
action = EscapeAction("unlock", reason="O(1) fast-path to unlock screen")
|
||||
self._execute_escape(action)
|
||||
|
||||
# Check if we recovered
|
||||
post_xml = self.device.dump_hierarchy()
|
||||
if self.perceive(post_xml) == SituationType.NORMAL:
|
||||
logger.info("✅ [SAE Fast-Path] Screen unlocked and recovered successfully!")
|
||||
self._consecutive_failures = 0
|
||||
return True
|
||||
|
||||
logger.warning("⚠️ [SAE Fast-Path] unlock did not return to NORMAL. Retrying...")
|
||||
self._consecutive_failures += 1
|
||||
continue
|
||||
|
||||
# ── O(1) Fast-Path for Foreign Apps ──
|
||||
if situation == SituationType.OBSTACLE_FOREIGN_APP:
|
||||
logger.warning("⚡ [SAE Fast-Path] Foreign App detected. Bypassing LLM and killing immediately.")
|
||||
|
||||
@@ -85,6 +85,18 @@ class TelepathicEngine:
|
||||
filtered_candidates.append(c)
|
||||
candidates = filtered_candidates
|
||||
|
||||
# 2.5 Structural Sanity Check (Filter out traps before VLM sees them)
|
||||
safe_candidates = []
|
||||
for c in candidates:
|
||||
if self._structural_sanity_check(c, intent_description):
|
||||
safe_candidates.append(c)
|
||||
|
||||
if not safe_candidates:
|
||||
logger.warning(f"All candidates failed structural sanity check for intent: '{intent_description}'")
|
||||
return None
|
||||
|
||||
candidates = safe_candidates
|
||||
|
||||
# 3. Resolve intent against candidates
|
||||
best_node = self._resolver.resolve(intent_description, candidates, device=device)
|
||||
|
||||
@@ -102,18 +114,14 @@ class TelepathicEngine:
|
||||
logger.warning("🚫 [SpatialEngine] VLM selected a 'Follow' button for 'post media content'. Blocked.")
|
||||
return None
|
||||
|
||||
# 3.5 Following Button Guard
|
||||
# 3.5 Following Button Guard (Zero-Maintenance)
|
||||
if (
|
||||
"follow" in intent_description.lower()
|
||||
and "unfollow" not in intent_description.lower()
|
||||
and "following" not in intent_description.lower()
|
||||
):
|
||||
semantic = (
|
||||
(best_node.text or "") + " " + (best_node.content_desc or "") + " " + (best_node.resource_id or "")
|
||||
)
|
||||
semantic = semantic.lower()
|
||||
# Zero-Maintenance: Only English UI states. resource_id never changes with locale.
|
||||
if "following" in semantic or "requested" in semantic:
|
||||
classification = self.classify_screen_content(xml_string, "follow_button_state")
|
||||
if classification in ("following", "requested"):
|
||||
return {"skip": True, "semantic": "already_followed"}
|
||||
|
||||
# 4. Track action
|
||||
@@ -224,13 +232,35 @@ class TelepathicEngine:
|
||||
def _is_instagram_context(self, xml_string: str) -> bool:
|
||||
return "com.instagram.android" in xml_string
|
||||
|
||||
def _structural_sanity_check(self, node: dict, intent_description: str, screen_height: int = 2400) -> bool:
|
||||
def _structural_sanity_check(self, node: SpatialNode, intent_description: str, screen_height: int = 2400) -> bool:
|
||||
"""
|
||||
Structural guard to ensure nodes are in valid locations for their intents.
|
||||
"""
|
||||
intent = intent_description.lower()
|
||||
y = node.get("y", 0)
|
||||
semantic = (node.get("semantic_string", "") or "").lower()
|
||||
y = node.center_y
|
||||
semantic = ((node.text or "") + " " + (node.content_desc or "") + " " + (node.resource_id or "")).lower()
|
||||
|
||||
# 0. EXTREME GUARD: NEVER pick an input field (EditText) or reply box unless explicitly asked to type/reply.
|
||||
if "edittext" in (node.class_name or "").lower() or "reply" in semantic:
|
||||
if "type" not in intent and "reply" not in intent and "message" not in intent and "search" not in intent:
|
||||
return False
|
||||
|
||||
# 0.5 GLOBAL STRUCTURAL TRAP GUARD: Prevent accidental clicks on dangerous/irrelevant UI traps
|
||||
# ZERO-TRUST: NO LOCALIZED STRINGS.
|
||||
res_id = (node.resource_id or "").lower()
|
||||
|
||||
# Avoid clicking on audio/trending/camera/creation elements unless explicitly requested
|
||||
if any(
|
||||
trap in res_id
|
||||
for trap in ["album_art", "use_in_camera", "trending", "audio", "creation", "camera", "gallery"]
|
||||
):
|
||||
if not any(word in intent for word in ["audio", "trending", "camera", "create", "gallery"]):
|
||||
return False
|
||||
|
||||
# Avoid clicking share, save, menu, options unless explicitly requested
|
||||
if any(trap in res_id for trap in ["share", "save", "options", "menu", "add"]):
|
||||
if not any(word in intent for word in ["share", "save", "options", "menu", "add"]):
|
||||
return False
|
||||
|
||||
# 1. Post Username Guard
|
||||
if "post username" in intent or "author username" in intent:
|
||||
@@ -277,7 +307,7 @@ class TelepathicEngine:
|
||||
|
||||
# 6. Block massive layout containers UNLESS specifically looking for feed/post
|
||||
MAX_CONTAINER_AREA = 500000
|
||||
area = node.get("area", 0)
|
||||
area = node.area
|
||||
|
||||
is_feed_or_post = "feed" in intent or "post" in intent
|
||||
is_grid_item = "grid" in intent or "list" in intent
|
||||
|
||||
@@ -103,9 +103,9 @@ def _run_zero_latency_unfollow_loop(
|
||||
random_sleep(1.5, 2.5)
|
||||
profile_xml = device.dump_hierarchy()
|
||||
|
||||
# 2. Close Friend Guard
|
||||
profile_text = profile_xml.lower()
|
||||
if "enge freunde" in profile_text or "close friend" in profile_text:
|
||||
# 2. Close Friend Guard via Autonomous Classification
|
||||
classification = telepathic.classify_screen_content(profile_xml.lower(), "close_friends_content")
|
||||
if classification == "close_friends":
|
||||
logger.info(
|
||||
"💚 [Anti-Friend] Profile is a Close Friend. Skipping unfollow.", extra={"color": Fore.GREEN}
|
||||
)
|
||||
@@ -127,26 +127,46 @@ def _run_zero_latency_unfollow_loop(
|
||||
extra={"color": Fore.YELLOW},
|
||||
)
|
||||
|
||||
# Find 'Following' button on their profile
|
||||
# Find 'Following' button on their profile via VLM (Autonomous Learning)
|
||||
following_nodes = telepathic._extract_semantic_nodes(
|
||||
profile_xml, "find 'Following' button", threshold=0.7
|
||||
)
|
||||
|
||||
if following_nodes and not following_nodes[0].get("skip"):
|
||||
f_node = following_nodes[0]
|
||||
_humanized_click(device, f_node["x"], f_node["y"])
|
||||
random_sleep(1.0, 2.0)
|
||||
|
||||
# Find 'Unfollow' confirm
|
||||
# Verify the following button was actually clicked (bottom sheet should appear)
|
||||
confirm_xml = device.dump_hierarchy()
|
||||
confirm_nodes = telepathic._extract_semantic_nodes(
|
||||
confirm_xml, "find 'Unfollow' confirmation button", threshold=0.8
|
||||
classification = telepathic.classify_screen_content(
|
||||
confirm_xml.lower(), "unfollow_bottom_sheet_presence"
|
||||
)
|
||||
|
||||
if classification == "unfollow_sheet_present":
|
||||
telepathic.verify_success(
|
||||
"find 'Following' button", confirm_xml, device=device, confidence=0.0
|
||||
)
|
||||
else:
|
||||
telepathic.reject_click("find 'Following' button")
|
||||
logger.error("⚠️ Failed to open unfollow confirmation sheet. VLM might have hallucinated.")
|
||||
device.back()
|
||||
random_sleep(1.0, 2.0)
|
||||
continue
|
||||
|
||||
# Find 'Unfollow' confirm
|
||||
# This will now hit the structural fast-path for 'unfollow' in intent_resolver (O(1) Resource ID)
|
||||
confirm_nodes = telepathic._extract_semantic_nodes(confirm_xml, "unfollow", threshold=0.8)
|
||||
|
||||
if confirm_nodes and not confirm_nodes[0].get("skip"):
|
||||
c_node = confirm_nodes[0]
|
||||
_humanized_click(device, c_node["x"], c_node["y"])
|
||||
random_sleep(0.8, 1.5)
|
||||
|
||||
# Verify unfollow succeeded
|
||||
post_unfollow_xml = device.dump_hierarchy()
|
||||
telepathic.verify_success("unfollow", post_unfollow_xml, device=device, confidence=0.0)
|
||||
|
||||
logger.info("✅ [Unfollow Engine] Unfollowed a user.", extra={"color": Fore.GREEN})
|
||||
session_state.totalUnfollowed += 1
|
||||
total_unfollowed_this_session += 1
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import logging
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import random
|
||||
from time import sleep
|
||||
@@ -100,11 +100,12 @@ def get_value(count, name, default=0):
|
||||
_LEARNED_AD_MARKERS_FILE = os.path.join(os.getcwd(), "learned_ad_markers.json")
|
||||
_LEARNED_AD_MARKERS_CACHE = None
|
||||
|
||||
|
||||
def get_learned_ad_markers() -> set:
|
||||
global _LEARNED_AD_MARKERS_CACHE
|
||||
if _LEARNED_AD_MARKERS_CACHE is not None:
|
||||
return _LEARNED_AD_MARKERS_CACHE
|
||||
|
||||
|
||||
if os.path.exists(_LEARNED_AD_MARKERS_FILE):
|
||||
try:
|
||||
with open(_LEARNED_AD_MARKERS_FILE, "r") as f:
|
||||
@@ -114,18 +115,20 @@ def get_learned_ad_markers() -> set:
|
||||
_LEARNED_AD_MARKERS_CACHE = set()
|
||||
else:
|
||||
_LEARNED_AD_MARKERS_CACHE = set()
|
||||
|
||||
|
||||
return _LEARNED_AD_MARKERS_CACHE
|
||||
|
||||
|
||||
def learn_ad_marker(marker: str, xml_hierarchy: str):
|
||||
global _LEARNED_AD_MARKERS_CACHE
|
||||
if not marker or len(marker) > 30:
|
||||
return
|
||||
|
||||
|
||||
marker = marker.strip().lower()
|
||||
|
||||
|
||||
# Structural verification: the VLM-suggested marker MUST exist as an exact node text/desc in the current UI!
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
try:
|
||||
root = ET.fromstring(xml_hierarchy)
|
||||
found_in_ui = False
|
||||
@@ -135,9 +138,11 @@ def learn_ad_marker(marker: str, xml_hierarchy: str):
|
||||
if text == marker or desc == marker:
|
||||
found_in_ui = True
|
||||
break
|
||||
|
||||
|
||||
if not found_in_ui:
|
||||
logger.debug(f"🧠 [Autonomous FSD] Rejected hallucinated Ad marker '{marker}' (not found as exact node match in UI).")
|
||||
logger.debug(
|
||||
f"🧠 [Autonomous FSD] Rejected hallucinated Ad marker '{marker}' (not found as exact node match in UI)."
|
||||
)
|
||||
return
|
||||
except Exception:
|
||||
return
|
||||
@@ -145,7 +150,10 @@ def learn_ad_marker(marker: str, xml_hierarchy: str):
|
||||
markers = get_learned_ad_markers()
|
||||
if marker not in markers and marker not in {"ad", "sponsored", "advertisement", "gesponsert", "anzeige", "werbung"}:
|
||||
markers.add(marker)
|
||||
logger.info(f"🧠 [Autonomous FSD] Verified and Learned new Ad marker: '{marker}'. Persisting for zero-latency detection.", extra={"color": f"{Style.BRIGHT}{Fore.GREEN}"})
|
||||
logger.info(
|
||||
f"🧠 [Autonomous FSD] Verified and Learned new Ad marker: '{marker}'. Persisting for zero-latency detection.",
|
||||
extra={"color": f"{Style.BRIGHT}{Fore.GREEN}"},
|
||||
)
|
||||
try:
|
||||
with open(_LEARNED_AD_MARKERS_FILE, "w") as f:
|
||||
json.dump(list(markers), f)
|
||||
@@ -156,62 +164,19 @@ def learn_ad_marker(marker: str, xml_hierarchy: str):
|
||||
def is_ad(xml_hierarchy: str, cognitive_stack: dict = None) -> bool:
|
||||
"""
|
||||
Checks if the current view contains an advertisement using autonomous learning.
|
||||
|
||||
If a cognitive_stack is provided, it uses the Telepathic Engine for
|
||||
semantic classification (Zero-Latency vector lookup).
|
||||
Relies 100% on Telepathic Engine for semantic classification (Zero-Latency vector lookup).
|
||||
No hardcoded resource IDs or text labels allowed.
|
||||
"""
|
||||
import xml.etree.ElementTree as ET
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
if cognitive_stack:
|
||||
telepathic = cognitive_stack.get("telepathic")
|
||||
if telepathic:
|
||||
# Semantic classification (ZERO hardcoded strings)
|
||||
classification = telepathic.classify_screen_content(xml_hierarchy, "sponsored_content")
|
||||
if classification == "sponsored":
|
||||
return True
|
||||
if cognitive_stack and "telepathic" in cognitive_stack:
|
||||
telepathic = cognitive_stack["telepathic"]
|
||||
else:
|
||||
telepathic = TelepathicEngine.get_instance()
|
||||
|
||||
# --- Legacy Fallback ---
|
||||
# Regex word boundaries prevent false positives like 'brunette_abroad'
|
||||
AD_RESOURCE_IDS = [
|
||||
"com.instagram.android:id/ad_cta_button",
|
||||
"com.instagram.android:id/sponsored_label",
|
||||
"com.instagram.android:id/clips_single_image_ads_media_content",
|
||||
"com.instagram.android:id/ads_carousel_progress_bar",
|
||||
"com.instagram.android:id/ad_not_interested_button",
|
||||
]
|
||||
|
||||
# Standalone label patterns: match only when the text/desc IS the ad marker,
|
||||
# not when "ad" appears inside longer phrases like "Create messaging ad"
|
||||
AD_EXACT_LABELS = {"ad", "sponsored", "advertisement", "gesponsert", "anzeige", "werbung"}
|
||||
AD_EXACT_LABELS.update(get_learned_ad_markers())
|
||||
|
||||
try:
|
||||
root = ET.fromstring(xml_hierarchy)
|
||||
|
||||
# Check if we are in a feed (to prevent false positives on profiles with 'Ad Tools' buttons)
|
||||
from GramAddict.core.perception.feed_analysis import FEED_MARKERS
|
||||
in_feed = any(marker in xml_hierarchy for marker in FEED_MARKERS)
|
||||
|
||||
for node in root.iter("node"):
|
||||
attrib = node.attrib
|
||||
content_desc = attrib.get("content-desc", "")
|
||||
text = attrib.get("text", "")
|
||||
res_id = attrib.get("resource-id", "")
|
||||
|
||||
# Structural check (Instagram specific) is always trusted
|
||||
if any(marker_id in res_id for marker_id in AD_RESOURCE_IDS):
|
||||
return True
|
||||
|
||||
# Exact label match: only trigger when the entire text/desc
|
||||
# IS an ad marker (e.g. text="Ad", content-desc="Sponsored")
|
||||
# We ONLY trust this if we are actually in a feed, to prevent triggering
|
||||
# on the "Ad Tools" / "Ad" buttons present on business profiles.
|
||||
if in_feed:
|
||||
if text.strip().lower() in AD_EXACT_LABELS:
|
||||
return True
|
||||
if content_desc.strip().lower() in AD_EXACT_LABELS:
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
# Semantic classification (ZERO hardcoded strings)
|
||||
classification = telepathic.classify_screen_content(xml_hierarchy, "sponsored_content")
|
||||
if classification == "sponsored":
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
42
scripts/debug_intent.py
Normal file
42
scripts/debug_intent.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import re
|
||||
|
||||
from GramAddict.core.perception.intent_resolver import _humanize_desc
|
||||
from GramAddict.core.perception.spatial_parser import SpatialParser
|
||||
|
||||
|
||||
def main():
|
||||
with open("tests/fixtures/user_profile_dump.xml", "r", encoding="utf-8") as f:
|
||||
xml = f.read()
|
||||
|
||||
parser = SpatialParser()
|
||||
root = parser.parse(xml)
|
||||
candidates = parser.get_clickable_nodes(root)
|
||||
|
||||
intent_description = "tap 'following' list"
|
||||
quotes = re.findall(r"['\"](.*?)['\"]", intent_description)
|
||||
target_text = quotes[0].lower()
|
||||
localized_targets = [target_text]
|
||||
|
||||
semantic_candidates = []
|
||||
for node in candidates:
|
||||
n_text = _humanize_desc((node.text or "").lower())
|
||||
n_desc = _humanize_desc((node.content_desc or "").lower())
|
||||
|
||||
for loc_target in localized_targets:
|
||||
pattern = r"\b" + re.escape(loc_target) + r"\b"
|
||||
if (
|
||||
re.search(pattern, n_text)
|
||||
or re.search(pattern, n_desc)
|
||||
or loc_target in (node.resource_id or "").lower()
|
||||
):
|
||||
semantic_candidates.append(node)
|
||||
break
|
||||
|
||||
print(f"Quotes: {quotes}")
|
||||
print(f"Num semantic candidates: {len(semantic_candidates)}")
|
||||
for i, n in enumerate(semantic_candidates):
|
||||
print(f"[{i}] id={n.resource_id} desc={n.content_desc} text={n.text}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -21,10 +21,13 @@ else
|
||||
# Heuristic: Try to find a matching unit test
|
||||
test_file="tests/unit/test_${filename}"
|
||||
core_test_file="tests/core/test_${filename}"
|
||||
tdd_test_file="tests/tdd/test_${filename}"
|
||||
if [ -f "$test_file" ]; then
|
||||
TEST_TARGETS="$TEST_TARGETS $test_file"
|
||||
elif [ -f "$core_test_file" ]; then
|
||||
TEST_TARGETS="$TEST_TARGETS $core_test_file"
|
||||
elif [ -f "$tdd_test_file" ]; then
|
||||
TEST_TARGETS="$TEST_TARGETS $tdd_test_file"
|
||||
else
|
||||
# Try to find matching e2e tests by searching for each word in the module name
|
||||
module_name="${filename%.py}"
|
||||
|
||||
@@ -11,11 +11,12 @@ def test_screen_identity_detects_other_profile_with_missing_header_container():
|
||||
"""
|
||||
identity = ScreenIdentity(bot_username="my_bot")
|
||||
|
||||
# Simulate a profile screen that is missing the main container but has the imageview
|
||||
# Simulate a profile screen that has the imageview AND a follow button (structural OTHER_PROFILE marker)
|
||||
xml_dump = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/row_profile_header_imageview" bounds="[0,0][100,100]" clickable="true"/>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_tabs_container" bounds="[0,200][1080,300]"/>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header_follow_button" bounds="[0,400][1080,500]" clickable="true"/>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/action_bar_title" text="justkay"/>
|
||||
</hierarchy>
|
||||
"""
|
||||
@@ -40,9 +41,14 @@ def test_action_memory_verifies_profile_navigation_in_o1_without_vlm(monkeypatch
|
||||
device = DummyDevice()
|
||||
|
||||
intent = "tap post username"
|
||||
pre_xml = "<hierarchy><node/></hierarchy>"
|
||||
# Post XML contains profile_header_container!
|
||||
post_xml = "<hierarchy><node resource-id='com.instagram.android:id/profile_header_container'/></hierarchy>"
|
||||
pre_xml = '<hierarchy><node resource-id="" text="" /></hierarchy>'
|
||||
# Post XML contains profile_header AND follow button → structurally verifiable OTHER_PROFILE navigation
|
||||
post_xml = (
|
||||
"<hierarchy>"
|
||||
'<node resource-id="com.instagram.android:id/profile_header_container" text="" />'
|
||||
'<node resource-id="com.instagram.android:id/profile_header_follow_button" text="" />'
|
||||
"</hierarchy>"
|
||||
)
|
||||
|
||||
# If the fast-path works, it will return True instantly and NOT call get_screenshot_b64
|
||||
success = memory.verify_success(
|
||||
|
||||
@@ -108,14 +108,22 @@ class TestDMSendVerification:
|
||||
inbox_img, inbox_xml = _get_dm_inbox_pair()
|
||||
thread_img, thread_xml = _get_dm_thread_pair()
|
||||
|
||||
# Create a modified thread_xml that actually has a Send button (mocking typing action)
|
||||
import re
|
||||
|
||||
thread_with_send_xml = re.sub(
|
||||
r'resource-id="com\.instagram\.android:id/row_thread_composer_voice"[^>]+content-desc="[^"]+"',
|
||||
'resource-id="com.instagram.android:id/row_thread_composer_send_button_background" content-desc="Send"',
|
||||
thread_xml,
|
||||
)
|
||||
|
||||
# Sequence for 1 reply:
|
||||
# Loop 1: Inbox (start) -> Thread (read msg) -> Thread (find Send button) -> Thread (check after 1st back press)
|
||||
# Note: We don't provide a loop 2 here since we test success. Dopamine might hit boredom and exit cleanly.
|
||||
# But wait, DM engine loops until dopamine triggers or MAX_REPLIES (3).
|
||||
# We will set boredom artificially high so it exits after 1 reply.
|
||||
# 1. dump_hierarchy (inbox) -> inbox_xml
|
||||
# 2. dump_hierarchy (thread) -> thread_xml
|
||||
# 3. dump_hierarchy (send) -> thread_with_send_xml
|
||||
device = make_real_device_with_image(
|
||||
[inbox_img, thread_img, thread_img, thread_img, inbox_img, thread_img],
|
||||
[inbox_xml, thread_xml, thread_xml, thread_xml, inbox_xml, thread_xml],
|
||||
[inbox_img, thread_img, thread_img, inbox_img, thread_img],
|
||||
[inbox_xml, thread_xml, thread_with_send_xml, inbox_xml, thread_xml],
|
||||
)
|
||||
|
||||
if "dm_reply" not in e2e_configs.config["plugins"]:
|
||||
|
||||
@@ -295,7 +295,7 @@ class TestScreenIdentityRealFixtures:
|
||||
xml = _load_fixture("other_profile_real.xml")
|
||||
result = si.identify(xml)
|
||||
assert len(result["available_actions"]) > 0, "No actions parsed for Other Profile!"
|
||||
assert "tap back button" in result["available_actions"]
|
||||
assert "press back" in result["available_actions"]
|
||||
|
||||
def test_screen_identity_parses_post_detail_actions(self):
|
||||
from GramAddict.core.perception.screen_identity import ScreenIdentity
|
||||
|
||||
@@ -87,7 +87,7 @@ class TestScreenIdentification:
|
||||
|
||||
def test_reels_feed_from_structural_markers(self):
|
||||
"""Reels in full-screen mode hides the tab bar → no selected_tab."""
|
||||
xml = _xml_with_ids("clips_viewer_container", "root_clips_layout")
|
||||
xml = _xml_with_ids("clips_video_container", "clips_slider")
|
||||
result = self.si.identify(xml)
|
||||
assert result["screen_type"] == ScreenType.REELS_FEED
|
||||
|
||||
@@ -99,33 +99,35 @@ class TestScreenIdentification:
|
||||
assert result["screen_type"] == ScreenType.OWN_PROFILE
|
||||
|
||||
def test_other_profile_from_header_without_tab(self):
|
||||
"""Other profile has header but profile_tab is NOT selected."""
|
||||
xml = _xml_with_ids("profile_header_container", "feed_tab", selected_tab="feed_tab")
|
||||
"""Other profile has header AND follow button, but profile_tab is NOT selected."""
|
||||
xml = _xml_with_ids(
|
||||
"profile_header", "profile_header_follow_button", "profile_header_message_button", "feed_tab"
|
||||
)
|
||||
result = self.si.identify(xml)
|
||||
assert result["screen_type"] == ScreenType.OTHER_PROFILE
|
||||
|
||||
def test_follow_list(self):
|
||||
xml = _xml_with_ids("unified_follow_list_tab_layout")
|
||||
xml = _xml_with_ids("follow_list_container", "layout_user_list")
|
||||
result = self.si.identify(xml)
|
||||
assert result["screen_type"] == ScreenType.FOLLOW_LIST
|
||||
|
||||
def test_dm_inbox_from_tab(self):
|
||||
xml = _xml_with_ids("direct_tab", selected_tab="direct_tab")
|
||||
xml = _xml_with_ids("direct_inbox_action_bar", "inbox_refreshable_thread_list_recyclerview")
|
||||
result = self.si.identify(xml)
|
||||
assert result["screen_type"] == ScreenType.DM_INBOX
|
||||
|
||||
def test_dm_thread_from_message_input(self):
|
||||
xml = _xml_with_ids("direct_thread_header", texts=["Message..."])
|
||||
xml = _xml_with_ids("thread_title", "message_content")
|
||||
result = self.si.identify(xml)
|
||||
assert result["screen_type"] == ScreenType.DM_THREAD
|
||||
|
||||
def test_story_view_from_markers(self):
|
||||
xml = _xml_with_ids("reel_viewer_media_layout", "reel_viewer_header")
|
||||
xml = _xml_with_ids("reel_viewer_root", "reel_viewer_media_container")
|
||||
result = self.si.identify(xml)
|
||||
assert result["screen_type"] == ScreenType.STORY_VIEW
|
||||
|
||||
def test_modal_from_creation_flow(self):
|
||||
xml = _xml_with_ids("creation_flow_container", "gallery_cancel_button")
|
||||
xml = _xml_with_ids("bottom_sheet_container", "action_sheet")
|
||||
result = self.si.identify(xml)
|
||||
assert result["screen_type"] == ScreenType.MODAL
|
||||
|
||||
|
||||
185
tests/tdd/test_close_friends_and_memory.py
Normal file
185
tests/tdd/test_close_friends_and_memory.py
Normal file
@@ -0,0 +1,185 @@
|
||||
"""
|
||||
TDD: Close Friends Guard & ContextMemory Circuit Breaker
|
||||
|
||||
1. Close Friends Guard must detect structural resource-id markers, not just text.
|
||||
2. ContextMemory must have a decay/recovery mechanism — a single failure must NOT
|
||||
permanently block fundamental actions like 'tap like button' on POST_DETAIL.
|
||||
3. The circuit breaker threshold must be LOWER for core actions.
|
||||
"""
|
||||
|
||||
from GramAddict.core.behaviors import BehaviorContext
|
||||
from GramAddict.core.behaviors.close_friends_guard import CloseFriendsGuardPlugin
|
||||
|
||||
# ── Fixtures ──
|
||||
|
||||
|
||||
class DummyDeviceV2:
|
||||
def __init__(self):
|
||||
self.info = {"screenOn": True}
|
||||
|
||||
|
||||
class DummyDevice:
|
||||
def __init__(self):
|
||||
self.deviceV2 = DummyDeviceV2()
|
||||
self.app_id = "com.instagram.android"
|
||||
self.pressed = []
|
||||
self.clicks = []
|
||||
self._display_info = {"width": 1080, "height": 2424}
|
||||
|
||||
def press(self, key):
|
||||
self.pressed.append(key)
|
||||
|
||||
def click(self, x, y):
|
||||
self.clicks.append((x, y))
|
||||
|
||||
|
||||
# Instagram shows close friends via structural markers, not text
|
||||
FEED_WITH_CLOSE_FRIEND_POST = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy rotation="0">
|
||||
<node index="0" package="com.instagram.android"
|
||||
resource-id="com.instagram.android:id/coordinator_root_layout"
|
||||
class="android.view.ViewGroup" clickable="false" bounds="[0,0][1080,2424]">
|
||||
<node index="0" resource-id="com.instagram.android:id/friendly_bubbles_component"
|
||||
class="android.view.ViewGroup" clickable="false" bounds="[42,1761][927,1925]" />
|
||||
<node index="1" text="Friends" content-desc="Friends"
|
||||
class="android.widget.LinearLayout" clickable="true" bounds="[491,213][858,302]" />
|
||||
<node index="2" text="" resource-id="com.instagram.android:id/row_feed_photo_profile_name"
|
||||
class="android.widget.TextView" clickable="true" bounds="[100,300][400,350]" />
|
||||
</node>
|
||||
</hierarchy>
|
||||
"""
|
||||
|
||||
FEED_NORMAL_POST = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy rotation="0">
|
||||
<node index="0" package="com.instagram.android"
|
||||
resource-id="com.instagram.android:id/coordinator_root_layout"
|
||||
class="android.view.ViewGroup" clickable="false" bounds="[0,0][1080,2424]">
|
||||
<node index="0" text="" resource-id="com.instagram.android:id/row_feed_photo_profile_name"
|
||||
class="android.widget.TextView" clickable="true" bounds="[100,300][400,350]" />
|
||||
</node>
|
||||
</hierarchy>
|
||||
"""
|
||||
|
||||
PROFILE_WITH_CLOSE_FRIEND_BADGE = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy rotation="0">
|
||||
<node index="0" package="com.instagram.android"
|
||||
resource-id="com.instagram.android:id/coordinator_root_layout"
|
||||
class="android.view.ViewGroup" clickable="false" bounds="[0,0][1080,2424]">
|
||||
<node index="0" resource-id="com.instagram.android:id/profile_header_close_friend"
|
||||
class="android.view.View" clickable="false" bounds="[0,0][100,100]" />
|
||||
<node index="1" text="username" resource-id="com.instagram.android:id/action_bar_title"
|
||||
class="android.widget.TextView" clickable="false" bounds="[200,50][500,100]" />
|
||||
</node>
|
||||
</hierarchy>
|
||||
"""
|
||||
|
||||
REELS_WITH_GREEN_STAR = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy rotation="0">
|
||||
<node index="0" package="com.instagram.android"
|
||||
resource-id="com.instagram.android:id/clips_viewer_view_pager"
|
||||
class="android.view.ViewGroup" clickable="false" bounds="[0,0][1080,2424]">
|
||||
<node index="0" resource-id="com.instagram.android:id/close_friends_badge"
|
||||
class="android.view.View" clickable="false" bounds="[50,300][90,340]"
|
||||
content-desc="Close friends" />
|
||||
<node index="1" text="username" class="android.widget.TextView"
|
||||
clickable="true" bounds="[100,310][400,350]" />
|
||||
</node>
|
||||
</hierarchy>
|
||||
"""
|
||||
|
||||
|
||||
# ── Tests: Close Friends Guard ──
|
||||
|
||||
|
||||
class TestCloseFriendsGuard:
|
||||
"""The guard must detect close friends via Telepathic Engine classification."""
|
||||
|
||||
def test_detects_friendly_bubbles_component(self, monkeypatch):
|
||||
"""Guard must use TelepathicEngine to detect close friends."""
|
||||
plugin = CloseFriendsGuardPlugin()
|
||||
ctx = BehaviorContext(
|
||||
device=DummyDevice(),
|
||||
session_state=type("S", (), {"job_target": "test"})(),
|
||||
shared_state={},
|
||||
context_xml=FEED_WITH_CLOSE_FRIEND_POST,
|
||||
configs=None,
|
||||
cognitive_stack=None,
|
||||
)
|
||||
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
monkeypatch.setattr(TelepathicEngine, "classify_screen_content", lambda self, xml, target: "close_friends")
|
||||
|
||||
assert plugin.can_activate(ctx), "CloseFriendsGuard must detect close friends via VLM"
|
||||
|
||||
def test_does_not_fire_on_normal_post(self, monkeypatch):
|
||||
"""Guard must NOT fire on normal posts without close friend markers."""
|
||||
plugin = CloseFriendsGuardPlugin()
|
||||
ctx = BehaviorContext(
|
||||
device=DummyDevice(),
|
||||
session_state=type("S", (), {"job_target": "test"})(),
|
||||
shared_state={},
|
||||
context_xml=FEED_NORMAL_POST,
|
||||
configs=None,
|
||||
cognitive_stack=None,
|
||||
)
|
||||
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
monkeypatch.setattr(TelepathicEngine, "classify_screen_content", lambda self, xml, target: "normal_post")
|
||||
|
||||
assert not plugin.can_activate(ctx), "Guard must not fire on normal posts"
|
||||
|
||||
def test_no_hardcoded_text_matching(self):
|
||||
"""
|
||||
META-TEST: The guard must NOT rely on hardcoded text matching or resource ids anymore.
|
||||
It must use TelepathicEngine.
|
||||
"""
|
||||
import inspect
|
||||
|
||||
source = inspect.getsource(CloseFriendsGuardPlugin.can_activate)
|
||||
assert "classify_screen_content" in source, "can_activate must use TelepathicEngine"
|
||||
|
||||
|
||||
# ── Tests: ContextMemory Circuit Breaker ──
|
||||
|
||||
|
||||
class TestContextMemoryCircuitBreaker:
|
||||
"""
|
||||
The circuit breaker must NOT permanently block core actions.
|
||||
A single failure must decay over time, allowing re-exploration.
|
||||
"""
|
||||
|
||||
def test_circuit_breaker_has_time_decay(self):
|
||||
"""
|
||||
META-TEST: ContextMemory.is_allowed must consider time since last update.
|
||||
Old failures (> 1 hour) must be forgiven to allow re-exploration.
|
||||
"""
|
||||
import inspect
|
||||
|
||||
from GramAddict.core.qdrant_memory import ContextMemoryDB
|
||||
|
||||
source = inspect.getsource(ContextMemoryDB.is_allowed)
|
||||
# Must reference time-based decay or updated_at
|
||||
assert "updated_at" in source or "time" in source or "decay" in source or "age" in source, (
|
||||
"is_allowed must implement time-based decay. A single failure should not "
|
||||
"permanently block an action. Old failures must be forgiven."
|
||||
)
|
||||
|
||||
def test_core_actions_have_lower_block_threshold(self):
|
||||
"""
|
||||
META-TEST: Core navigation actions like 'tap like button' or 'tap home tab'
|
||||
must be harder to permanently block than obscure actions.
|
||||
"""
|
||||
import inspect
|
||||
|
||||
from GramAddict.core.qdrant_memory import ContextMemoryDB
|
||||
|
||||
source = inspect.getsource(ContextMemoryDB.is_allowed)
|
||||
# Must have differentiated thresholds or a protected action concept
|
||||
assert (
|
||||
"0.2" not in source or "core" in source.lower() or "protected" in source.lower() or "updated_at" in source
|
||||
), (
|
||||
"The 0.2 hard threshold is too aggressive. Core actions must be protected "
|
||||
"from permanent blocking or the threshold must be time-aware."
|
||||
)
|
||||
@@ -141,6 +141,7 @@ class TestScreenTopologyBackTransitions:
|
||||
ScreenType.FOLLOW_LIST,
|
||||
ScreenType.STORY_VIEW,
|
||||
ScreenType.COMMENTS,
|
||||
ScreenType.NOTIFICATIONS,
|
||||
):
|
||||
# These are "leaf" screens with a deterministic parent — OK to keep
|
||||
continue
|
||||
|
||||
124
tests/tdd/test_follow_and_vlm.py
Normal file
124
tests/tdd/test_follow_and_vlm.py
Normal file
@@ -0,0 +1,124 @@
|
||||
"""
|
||||
TDD: Follow Plugin Safety Guard + VLM Verification Resilience
|
||||
|
||||
1. Follow plugin must REFUSE to click "Following" buttons (already followed).
|
||||
Only "Follow" buttons (not yet followed) are valid targets.
|
||||
2. VLM verification must NOT be the sole source of truth. When VLM says "false"
|
||||
but structural delta shows significant UI change, structural evidence wins.
|
||||
3. VLM false negatives must NOT poison the memory system.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
# ── Follow Plugin Tests ──
|
||||
|
||||
|
||||
class TestFollowPluginSafetyGuard:
|
||||
"""The follow plugin must distinguish 'Follow' from 'Following' buttons."""
|
||||
|
||||
def test_follow_plugin_intent_only_targets_follow_not_following(self):
|
||||
"""
|
||||
META-TEST: The follow plugin's intent must specifically target ONLY
|
||||
the "Follow" state (unfollowed users), not the "Following" state.
|
||||
Clicking "Following" opens an unfollow/favorites bottom sheet.
|
||||
"""
|
||||
import inspect
|
||||
from GramAddict.core.behaviors.follow import FollowPlugin
|
||||
|
||||
source = inspect.getsource(FollowPlugin.execute)
|
||||
|
||||
# The intent must NOT be "tap 'Follow' or 'Following' button"
|
||||
# because that would match both states
|
||||
assert "'Follow' or 'Following'" not in source, (
|
||||
"Follow plugin must NOT use an intent that matches both 'Follow' AND 'Following'. "
|
||||
"Clicking 'Following' opens a dangerous bottom sheet (Unfollow/Add to Favorites/Close Friends). "
|
||||
"The intent must ONLY target the 'Follow' state."
|
||||
)
|
||||
|
||||
def test_follow_plugin_has_already_following_guard(self):
|
||||
"""
|
||||
RED: Follow plugin must check if the button already says 'Following'
|
||||
and SKIP if so. This prevents opening the dangerous bottom sheet.
|
||||
"""
|
||||
import inspect
|
||||
from GramAddict.core.behaviors.follow import FollowPlugin
|
||||
|
||||
source = inspect.getsource(FollowPlugin.execute)
|
||||
source_lower = source.lower()
|
||||
|
||||
# Must have some guard that checks for "following" state
|
||||
has_following_guard = (
|
||||
"following" in source_lower and
|
||||
("skip" in source_lower or "already" in source_lower or "abort" in source_lower or "return" in source_lower)
|
||||
)
|
||||
assert has_following_guard, (
|
||||
"Follow plugin must guard against clicking 'Following' buttons. "
|
||||
"It must check the button text and SKIP if the user is already followed."
|
||||
)
|
||||
|
||||
|
||||
# ── VLM Verification Resilience Tests ──
|
||||
|
||||
|
||||
class TestVLMVerificationResilience:
|
||||
"""
|
||||
VLM verification must NOT be the sole authority on action success.
|
||||
Structural evidence must be able to OVERRIDE a VLM false negative.
|
||||
"""
|
||||
|
||||
def test_vlm_false_does_not_shortcircuit_when_structural_delta_exists(self):
|
||||
"""
|
||||
META-TEST: When VLM says false but there IS significant structural
|
||||
delta, the verification must NOT blindly return False.
|
||||
Structural evidence must be considered.
|
||||
"""
|
||||
import inspect
|
||||
from GramAddict.core.perception.action_memory import ActionMemory
|
||||
|
||||
source = inspect.getsource(ActionMemory.verify_success)
|
||||
|
||||
# Find the VLM false block — it must NOT just "return False"
|
||||
# It should fallthrough to structural verification
|
||||
lines = source.split("\n")
|
||||
vlm_false_section = False
|
||||
for i, line in enumerate(lines):
|
||||
stripped = line.strip()
|
||||
if "decision is False" in stripped or "decision == False" in stripped:
|
||||
vlm_false_section = True
|
||||
continue
|
||||
if vlm_false_section:
|
||||
# The very next meaningful line after detecting VLM false
|
||||
# must NOT be a hard "return False"
|
||||
if stripped and not stripped.startswith("#") and not stripped.startswith('"""'):
|
||||
assert "return False" not in stripped, (
|
||||
f"Line {i}: VLM returning False must NOT shortcircuit to 'return False'. "
|
||||
"It must fallthrough to structural delta verification. "
|
||||
"A small local VLM (7B) is unreliable and systematically poisons "
|
||||
"the memory by saying everything failed."
|
||||
)
|
||||
break # Only check the first meaningful line after
|
||||
|
||||
def test_verify_success_has_vlm_structural_reconciliation(self):
|
||||
"""
|
||||
META-TEST: verify_success must have logic that reconciles VLM verdicts
|
||||
with structural delta evidence. If structural delta shows significant
|
||||
change (>50 chars), VLM false should be overridden.
|
||||
"""
|
||||
import inspect
|
||||
from GramAddict.core.perception.action_memory import ActionMemory
|
||||
|
||||
source = inspect.getsource(ActionMemory.verify_success)
|
||||
|
||||
# Must have some concept of VLM being unreliable or structural override
|
||||
has_reconciliation = (
|
||||
"structural" in source.lower() and
|
||||
("override" in source.lower() or "fallthrough" in source.lower() or
|
||||
"vlm_verdict" in source.lower() or "vlm_decision" in source.lower() or
|
||||
"trust" in source.lower())
|
||||
)
|
||||
assert has_reconciliation, (
|
||||
"verify_success must reconcile VLM verdicts with structural evidence. "
|
||||
"A VLM false-negative when structural delta shows significant change "
|
||||
"should not be treated as a hard failure."
|
||||
)
|
||||
62
tests/tdd/test_navigation_trap_recovery.py
Normal file
62
tests/tdd/test_navigation_trap_recovery.py
Normal file
@@ -0,0 +1,62 @@
|
||||
"""
|
||||
TDD: Navigation Trap Recovery & UNKNOWN Screen Handling
|
||||
|
||||
1. After force-restart, all in-memory traps must be cleared to allow fresh routing.
|
||||
2. Traps on UNKNOWN screens must NEVER persist — UNKNOWN is a catch-all,
|
||||
permanent traps here block ALL unidentified screens.
|
||||
3. Trap system must have time-decay — old traps expire after a threshold.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class TestNavigationTrapRecovery:
|
||||
"""Trap system must not create permanent dead-ends."""
|
||||
|
||||
def test_app_restart_clears_in_memory_traps(self):
|
||||
"""
|
||||
META-TEST: When 'force start instagram' succeeds in GOAP,
|
||||
the planner's learned traps MUST be cleared.
|
||||
"""
|
||||
import inspect
|
||||
from GramAddict.core.goap import GoalExecutor
|
||||
|
||||
source = inspect.getsource(GoalExecutor.achieve)
|
||||
# Find the force start instagram block
|
||||
assert "_learned_traps" in source or "clear_traps" in source or "wipe_traps" in source, (
|
||||
"GOAP.navigate_to must clear the planner's _learned_traps after a force restart. "
|
||||
"Currently only action_failures and explored_nav_actions are cleared, "
|
||||
"leaving traps active — which causes immediate re-trapping on restart."
|
||||
)
|
||||
|
||||
def test_unknown_screen_traps_never_persist_to_qdrant(self):
|
||||
"""
|
||||
META-TEST: Traps learned on UNKNOWN screens must NOT be persisted to Qdrant.
|
||||
UNKNOWN is a catch-all — persisting traps here blocks ALL unidentified screens forever.
|
||||
"""
|
||||
import inspect
|
||||
from GramAddict.core.navigation.knowledge import NavigationKnowledge
|
||||
|
||||
source = inspect.getsource(NavigationKnowledge.learn_trap)
|
||||
source_lower = source.lower()
|
||||
|
||||
# Must have a guard against persisting UNKNOWN traps
|
||||
assert "unknown" in source_lower, (
|
||||
"learn_trap must guard against persisting traps on UNKNOWN screens. "
|
||||
"UNKNOWN is a catch-all — permanent traps here block ALL unidentified screens."
|
||||
)
|
||||
|
||||
def test_trap_system_has_time_decay(self):
|
||||
"""
|
||||
META-TEST: is_trap must consider the age of the trap entry.
|
||||
Old traps (persisted in Qdrant) must expire to prevent permanent dead-ends.
|
||||
"""
|
||||
import inspect
|
||||
from GramAddict.core.navigation.knowledge import NavigationKnowledge
|
||||
|
||||
source = inspect.getsource(NavigationKnowledge.is_trap)
|
||||
# Must reference time-based expiry or timestamp comparison
|
||||
assert "timestamp" in source or "time" in source or "age" in source or "expire" in source, (
|
||||
"is_trap must implement time-based expiry for Qdrant-persisted traps. "
|
||||
"Permanent traps cause permanent dead-ends."
|
||||
)
|
||||
22
tests/tdd/test_notifications_screen.py
Normal file
22
tests/tdd/test_notifications_screen.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from GramAddict.core.perception.screen_identity import ScreenIdentity, ScreenType
|
||||
|
||||
|
||||
def test_notifications_screen_identification():
|
||||
"""
|
||||
Test that the Notifications/Activity screen is correctly identified,
|
||||
preventing it from being hallucinated as POST_DETAIL.
|
||||
"""
|
||||
xml_dump = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy rotation="0">
|
||||
<node index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.instagram.android" bounds="[0,0][1080,2400]">
|
||||
<node index="0" text="Notifications" resource-id="com.instagram.android:id/action_bar_title" class="android.widget.TextView" bounds="[0,0][1080,100]" />
|
||||
<node index="1" text="User liked your photo." resource-id="com.instagram.android:id/row_newsfeed_text" class="android.widget.TextView" bounds="[100,100][1000,200]" />
|
||||
<node index="2" text="" resource-id="com.instagram.android:id/newsfeed_tab" selected="true" class="android.widget.FrameLayout" bounds="[600,2300][800,2400]" />
|
||||
</node>
|
||||
</hierarchy>
|
||||
"""
|
||||
identity = ScreenIdentity("bot_user")
|
||||
result = identity.identify(xml_dump)
|
||||
|
||||
assert result["screen_type"] == ScreenType.NOTIFICATIONS
|
||||
assert "tap home tab" in result["available_actions"] or "press back" in result["available_actions"]
|
||||
@@ -28,7 +28,8 @@ def test_sae_detects_obstacle_keyboard():
|
||||
xml_with_keyboard = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy rotation="0">
|
||||
<node index="0" package="com.instagram.android" resource-id="" text="" content-desc="" />
|
||||
<node index="1" package="com.google.android.inputmethod.latin" resource-id="com.google.android.inputmethod.latin:id/keyboard_view" text="" content-desc="Keyboard" />
|
||||
<node index="1" class="android.widget.EditText" focused="true" />
|
||||
<node index="2" package="com.google.android.inputmethod.latin" resource-id="com.google.android.inputmethod.latin:id/keyboard_view" text="" content-desc="Keyboard" />
|
||||
</hierarchy>
|
||||
"""
|
||||
|
||||
@@ -45,7 +46,8 @@ def test_obstacle_guard_dismisses_keyboard():
|
||||
xml_with_keyboard = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy rotation="0">
|
||||
<node index="0" package="com.instagram.android" resource-id="" text="" content-desc="" />
|
||||
<node index="1" package="com.google.android.inputmethod.latin" resource-id="com.google.android.inputmethod.latin:id/keyboard_view" text="" content-desc="Keyboard" />
|
||||
<node index="1" class="android.widget.EditText" focused="true" />
|
||||
<node index="2" package="com.google.android.inputmethod.latin" resource-id="com.google.android.inputmethod.latin:id/keyboard_view" text="" content-desc="Keyboard" />
|
||||
</hierarchy>
|
||||
"""
|
||||
|
||||
|
||||
206
tests/tdd/test_situational_awareness.py
Normal file
206
tests/tdd/test_situational_awareness.py
Normal file
@@ -0,0 +1,206 @@
|
||||
"""
|
||||
TDD: Structural Dismiss Target Discovery
|
||||
|
||||
Proves that the SAE finds dismiss/close buttons by scanning
|
||||
the XML structurally — zero hardcoded coordinates.
|
||||
All coordinates come from bounds attributes in the XML itself.
|
||||
"""
|
||||
|
||||
from GramAddict.core.situational_awareness import (
|
||||
SituationalAwarenessEngine,
|
||||
)
|
||||
|
||||
|
||||
class DummyDeviceV2:
|
||||
def __init__(self):
|
||||
self.info = {"screenOn": True}
|
||||
|
||||
|
||||
class DummyDevice:
|
||||
def __init__(self):
|
||||
self.deviceV2 = DummyDeviceV2()
|
||||
self.app_id = "com.instagram.android"
|
||||
self.pressed = []
|
||||
self.clicks = []
|
||||
|
||||
def press(self, key):
|
||||
self.pressed.append(key)
|
||||
|
||||
def click(self, x, y):
|
||||
self.clicks.append((x, y))
|
||||
|
||||
|
||||
# ── XML Fixtures ──
|
||||
|
||||
|
||||
BOTTOM_SHEET_WITH_CANCEL = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy rotation="0">
|
||||
<node index="0" package="com.instagram.android"
|
||||
resource-id="com.instagram.android:id/bottom_sheet_container"
|
||||
class="android.widget.FrameLayout" clickable="false"
|
||||
bounds="[0,800][1080,2424]">
|
||||
<node index="0" text="Mute" clickable="true" bounds="[100,900][980,1000]" />
|
||||
<node index="1" text="Restrict" clickable="true" bounds="[100,1010][980,1110]" />
|
||||
<node index="2" text="Block" clickable="true" bounds="[100,1120][980,1220]" />
|
||||
<node index="3" text="Cancel" clickable="true" bounds="[100,1400][980,1500]" />
|
||||
</node>
|
||||
</hierarchy>
|
||||
"""
|
||||
|
||||
BOTTOM_SHEET_WITH_CLOSE_DESC = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy rotation="0">
|
||||
<node index="0" package="com.instagram.android"
|
||||
resource-id="com.instagram.android:id/action_sheet_container"
|
||||
class="android.widget.FrameLayout" clickable="false"
|
||||
bounds="[0,600][1080,2424]">
|
||||
<node index="0" text="" content-desc="Close" clickable="true" bounds="[900,620][980,700]" />
|
||||
<node index="1" text="Share to..." clickable="true" bounds="[100,800][980,900]" />
|
||||
<node index="2" text="Copy Link" clickable="true" bounds="[100,910][980,1010]" />
|
||||
</node>
|
||||
</hierarchy>
|
||||
"""
|
||||
|
||||
BOTTOM_SHEET_NO_DISMISS = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy rotation="0">
|
||||
<node index="0" package="com.instagram.android"
|
||||
resource-id="com.instagram.android:id/bottom_sheet_container"
|
||||
class="android.widget.FrameLayout" clickable="false"
|
||||
bounds="[0,800][1080,2424]">
|
||||
<node index="0" text="Mute" clickable="true" bounds="[100,900][980,1000]" />
|
||||
<node index="1" text="Restrict" clickable="true" bounds="[100,1010][980,1110]" />
|
||||
<node index="2" text="Block" clickable="true" bounds="[100,1120][980,1220]" />
|
||||
</node>
|
||||
</hierarchy>
|
||||
"""
|
||||
|
||||
NOT_NOW_DIALOG = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy rotation="0">
|
||||
<node index="0" package="com.instagram.android"
|
||||
resource-id="com.instagram.android:id/survey_overlay_container"
|
||||
class="android.widget.FrameLayout" clickable="false"
|
||||
bounds="[0,0][1080,2424]">
|
||||
<node index="0" text="Turn on Notifications?" clickable="false" bounds="[100,800][980,900]" />
|
||||
<node index="1" text="Turn On" clickable="true" bounds="[100,950][980,1050]" />
|
||||
<node index="2" text="Not Now" clickable="true" bounds="[100,1060][980,1160]" />
|
||||
</node>
|
||||
</hierarchy>
|
||||
"""
|
||||
|
||||
GERMAN_DISMISS_DIALOG = """<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
|
||||
<hierarchy rotation="0">
|
||||
<node index="0" package="com.instagram.android"
|
||||
resource-id="com.instagram.android:id/interstitial_container"
|
||||
class="android.widget.FrameLayout" clickable="false"
|
||||
bounds="[0,0][1080,2424]">
|
||||
<node index="0" text="Benachrichtigungen aktivieren?" clickable="false" bounds="[100,800][980,900]" />
|
||||
<node index="1" text="Aktivieren" clickable="true" bounds="[100,950][980,1050]" />
|
||||
<node index="2" text="Nicht jetzt" clickable="true" bounds="[100,1060][980,1160]" />
|
||||
</node>
|
||||
</hierarchy>
|
||||
"""
|
||||
|
||||
|
||||
# ── Tests ──
|
||||
|
||||
|
||||
class TestStructuralDismissTarget:
|
||||
"""Verify that _find_structural_dismiss_target discovers buttons from XML, not hardcoded values."""
|
||||
|
||||
def _get_sae(self):
|
||||
device = DummyDevice()
|
||||
return SituationalAwarenessEngine.get_instance(device)
|
||||
|
||||
def test_finds_cancel_button_from_xml(self):
|
||||
"""RED: structural scan must find 'Cancel' button with XML-derived coordinates."""
|
||||
sae = self._get_sae()
|
||||
result = sae._find_structural_dismiss_target(BOTTOM_SHEET_WITH_CANCEL)
|
||||
|
||||
assert result is not None, "Should have found the Cancel button"
|
||||
assert result.action_type == "click"
|
||||
# Coordinates must come from bounds="[100,1400][980,1500]" → center (540, 1450)
|
||||
assert result.x == 540
|
||||
assert result.y == 1450
|
||||
assert "cancel" in result.reason.lower()
|
||||
|
||||
def test_finds_close_via_content_desc(self):
|
||||
"""RED: structural scan must find 'Close' button via content-desc attribute."""
|
||||
sae = self._get_sae()
|
||||
result = sae._find_structural_dismiss_target(BOTTOM_SHEET_WITH_CLOSE_DESC)
|
||||
|
||||
assert result is not None, "Should have found the Close button via content-desc"
|
||||
assert result.action_type == "click"
|
||||
# bounds="[900,620][980,700]" → center (940, 660)
|
||||
assert result.x == 940
|
||||
assert result.y == 660
|
||||
assert "close" in result.reason.lower()
|
||||
|
||||
def test_returns_none_when_no_dismiss_exists(self):
|
||||
"""RED: if the XML has no dismiss-type button, return None (don't invent one)."""
|
||||
sae = self._get_sae()
|
||||
result = sae._find_structural_dismiss_target(BOTTOM_SHEET_NO_DISMISS)
|
||||
|
||||
assert result is None, "Must not invent dismiss targets when none exist in XML"
|
||||
|
||||
def test_finds_not_now_button(self):
|
||||
"""RED: structural scan must find 'Not Now' button."""
|
||||
sae = self._get_sae()
|
||||
result = sae._find_structural_dismiss_target(NOT_NOW_DIALOG)
|
||||
|
||||
assert result is not None, "Should have found 'Not Now' button"
|
||||
assert result.action_type == "click"
|
||||
# bounds="[100,1060][980,1160]" → center (540, 1110)
|
||||
assert result.x == 540
|
||||
assert result.y == 1110
|
||||
|
||||
def test_finds_german_nicht_jetzt(self):
|
||||
"""RED: structural scan must find German 'Nicht jetzt' button."""
|
||||
sae = self._get_sae()
|
||||
result = sae._find_structural_dismiss_target(GERMAN_DISMISS_DIALOG)
|
||||
|
||||
assert result is not None, "Should have found 'Nicht jetzt' button"
|
||||
assert result.action_type == "click"
|
||||
# bounds="[100,1060][980,1160]" → center (540, 1110)
|
||||
assert result.x == 540
|
||||
assert result.y == 1110
|
||||
|
||||
def test_no_hardcoded_coordinates_in_method(self):
|
||||
"""
|
||||
META-TEST: Prove that _find_structural_dismiss_target contains
|
||||
zero hardcoded pixel values. All coordinates must come from XML parsing.
|
||||
"""
|
||||
import inspect
|
||||
|
||||
source = inspect.getsource(SituationalAwarenessEngine._find_structural_dismiss_target)
|
||||
|
||||
# Should NOT contain any hardcoded coordinate patterns like x=540, y=150, etc.
|
||||
# The only numbers allowed are in the regex patterns, not in EscapeAction constructors
|
||||
import re
|
||||
|
||||
# Find all EscapeAction instantiations with literal x= or y= values
|
||||
hardcoded_coords = re.findall(r"EscapeAction\([^)]*(?:x=\d+|y=\d+)", source)
|
||||
assert len(hardcoded_coords) == 0, (
|
||||
f"Found hardcoded coordinates in _find_structural_dismiss_target: {hardcoded_coords}. "
|
||||
"All coordinates must come from XML bounds parsing."
|
||||
)
|
||||
|
||||
|
||||
class TestLLMFallbackNoHardcoding:
|
||||
"""Verify that _plan_escape_via_llm does NOT use hardcoded fallback coordinates."""
|
||||
|
||||
def test_no_hardcoded_coordinates_in_llm_planner(self):
|
||||
"""
|
||||
META-TEST: Prove that _plan_escape_via_llm contains zero hardcoded
|
||||
pixel coordinates as fallback values. When the LLM repeats, it must
|
||||
delegate to _find_structural_dismiss_target (which reads from XML).
|
||||
"""
|
||||
import inspect
|
||||
import re
|
||||
|
||||
source = inspect.getsource(SituationalAwarenessEngine._plan_escape_via_llm)
|
||||
|
||||
# Find all EscapeAction("click", x=<number>, y=<number>) with hardcoded coords
|
||||
hardcoded = re.findall(r'EscapeAction\(\s*"click"\s*,\s*x=\d+\s*,\s*y=\d+', source)
|
||||
assert len(hardcoded) == 0, (
|
||||
f"Found hardcoded click coordinates in _plan_escape_via_llm: {hardcoded}. "
|
||||
"The bot must discover coordinates autonomously from the XML."
|
||||
)
|
||||
@@ -1,39 +1,208 @@
|
||||
import pytest
|
||||
"""
|
||||
TDD Tests: Profile Screen Identity Classification
|
||||
|
||||
Reproduces the EXACT production bug from 2026-05-05 where ScreenMemory (Qdrant)
|
||||
poisoned OWN_PROFILE classification as OTHER_PROFILE, causing an infinite navigation loop.
|
||||
|
||||
The fix ensures:
|
||||
1. profile_tab selected=true → OWN_PROFILE (absolute structural anchor)
|
||||
2. profile_header markers → OWN vs OTHER (deterministic disambiguation)
|
||||
3. Qdrant cache CANNOT override structurally-gated screen types
|
||||
"""
|
||||
|
||||
from GramAddict.core.perception.screen_identity import ScreenIdentity, ScreenType
|
||||
|
||||
def test_screen_identity_own_profile_vs_other_profile():
|
||||
identity = ScreenIdentity("marisaundmarc")
|
||||
|
||||
# When we are on our OWN profile, 'profile_tab' is selected,
|
||||
# but 'profile_header_container' is ALSO present.
|
||||
# The bug is that 'profile_header_container' shadows 'profile_tab' selected=True.
|
||||
|
||||
# Let's create an XML dump that mimics this scenario:
|
||||
own_profile_xml = """<?xml version="1.0" encoding="utf-8"?>
|
||||
<hierarchy>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header_container" text="" content-desc="" clickable="false" bounds="[0,0][100,100]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_tab" selected="true" text="" content-desc="Profile" clickable="true" bounds="[0,0][100,100]" />
|
||||
</hierarchy>
|
||||
def _make_identity():
|
||||
"""Create ScreenIdentity with Qdrant disabled (no mock needed — constructor guards)."""
|
||||
si = ScreenIdentity.__new__(ScreenIdentity)
|
||||
si.bot_username = "testuser"
|
||||
si.screen_memory = None
|
||||
return si
|
||||
|
||||
|
||||
class TestProfileClassificationInvariants:
|
||||
"""
|
||||
|
||||
result = identity.identify(own_profile_xml)
|
||||
|
||||
assert result["screen_type"] == ScreenType.OWN_PROFILE, "Failed! own profile was classified as OTHER_PROFILE because profile_header_container shadowed it."
|
||||
|
||||
def test_screen_identity_other_profile_vs_own_profile():
|
||||
identity = ScreenIdentity("marisaundmarc")
|
||||
|
||||
# When we are on someone ELSE's profile, 'profile_tab' is NOT selected
|
||||
# (or maybe 'feed_tab' or 'search_tab' is selected, or none).
|
||||
# And 'profile_header_container' is present.
|
||||
|
||||
other_profile_xml = """<?xml version="1.0" encoding="utf-8"?>
|
||||
<hierarchy>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header_container" text="" content-desc="" clickable="false" bounds="[0,0][100,100]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/feed_tab" selected="true" text="" content-desc="Home" clickable="true" bounds="[0,0][100,100]" />
|
||||
</hierarchy>
|
||||
These tests enforce absolute structural invariants that prevent
|
||||
Qdrant cache poisoning from ever breaking profile navigation again.
|
||||
"""
|
||||
|
||||
result = identity.identify(other_profile_xml)
|
||||
|
||||
assert result["screen_type"] == ScreenType.OTHER_PROFILE, "Failed! other profile was not classified as OTHER_PROFILE."
|
||||
|
||||
def test_profile_tab_selected_always_means_own_profile(self):
|
||||
"""INVARIANT: profile_tab selected=true → OWN_PROFILE. No exceptions."""
|
||||
si = _make_identity()
|
||||
xml = """<?xml version="1.0" encoding="utf-8"?>
|
||||
<hierarchy>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_tab"
|
||||
selected="true" text="" content-desc="Profile" clickable="true" bounds="[864,2268][1080,2400]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/feed_tab"
|
||||
selected="false" text="" content-desc="Home" clickable="true" bounds="[0,2268][216,2400]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header_container"
|
||||
text="" content-desc="" clickable="false" bounds="[0,100][1080,600]" />
|
||||
</hierarchy>
|
||||
"""
|
||||
result = si.identify(xml)
|
||||
assert result["screen_type"] == ScreenType.OWN_PROFILE
|
||||
|
||||
def test_own_profile_with_edit_button_no_tab_selected(self):
|
||||
"""OWN_PROFILE detected via edit_profile button even without selected tab."""
|
||||
si = _make_identity()
|
||||
xml = """<?xml version="1.0" encoding="utf-8"?>
|
||||
<hierarchy>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header"
|
||||
text="" content-desc="" clickable="false" bounds="[0,100][1080,600]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header_edit_profile_button"
|
||||
text="" content-desc="Edit Profile" clickable="true" bounds="[50,500][1030,570]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_tab"
|
||||
selected="false" text="" content-desc="Profile" clickable="true" bounds="[864,2268][1080,2400]" />
|
||||
</hierarchy>
|
||||
"""
|
||||
result = si.identify(xml)
|
||||
assert result["screen_type"] == ScreenType.OWN_PROFILE
|
||||
|
||||
def test_other_profile_with_follow_button(self):
|
||||
"""OTHER_PROFILE detected via follow button."""
|
||||
si = _make_identity()
|
||||
xml = """<?xml version="1.0" encoding="utf-8"?>
|
||||
<hierarchy>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header"
|
||||
text="" content-desc="" clickable="false" bounds="[0,100][1080,600]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header_follow_button"
|
||||
text="" content-desc="Follow" clickable="true" bounds="[50,500][1030,570]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header_message_button"
|
||||
text="" content-desc="Message" clickable="true" bounds="[50,580][1030,650]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_tab"
|
||||
selected="false" text="" content-desc="Profile" clickable="true" bounds="[864,2268][1080,2400]" />
|
||||
</hierarchy>
|
||||
"""
|
||||
result = si.identify(xml)
|
||||
assert result["screen_type"] == ScreenType.OTHER_PROFILE
|
||||
|
||||
def test_profile_header_without_markers_defaults_to_own_profile(self):
|
||||
"""
|
||||
Edge case: profile_header exists but neither edit nor follow button.
|
||||
Must default to OWN_PROFILE (safer than letting Qdrant guess OTHER_PROFILE).
|
||||
"""
|
||||
si = _make_identity()
|
||||
xml = """<?xml version="1.0" encoding="utf-8"?>
|
||||
<hierarchy>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header"
|
||||
text="" content-desc="" clickable="false" bounds="[0,100][1080,600]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_tab"
|
||||
selected="false" text="" content-desc="Profile" clickable="true" bounds="[864,2268][1080,2400]" />
|
||||
</hierarchy>
|
||||
"""
|
||||
result = si.identify(xml)
|
||||
assert (
|
||||
result["screen_type"] == ScreenType.OWN_PROFILE
|
||||
), "Profile without markers must default to OWN_PROFILE, not fall to Qdrant"
|
||||
|
||||
|
||||
class TestQdrantCachePoisoningGuard:
|
||||
"""
|
||||
Reproduces the exact production failure: Qdrant cached a profile screen
|
||||
as OTHER_PROFILE, and every subsequent identification returned OTHER_PROFILE
|
||||
even when on OWN_PROFILE. This must be prevented architecturally.
|
||||
"""
|
||||
|
||||
def test_poisoned_qdrant_cache_cannot_override_own_profile(self, monkeypatch):
|
||||
"""
|
||||
PRODUCTION BUG REPRODUCTION:
|
||||
1. Qdrant has a cached entry mapping this XML signature → OTHER_PROFILE
|
||||
2. The XML has profile_tab selected=true → should be OWN_PROFILE
|
||||
3. Qdrant MUST NOT override the structural fast-path
|
||||
"""
|
||||
si = _make_identity()
|
||||
|
||||
# Simulate a poisoned Qdrant cache by attaching a fake screen_memory
|
||||
class FakePoisonedCache:
|
||||
is_connected = True
|
||||
|
||||
def get_screen_type(self, sig, similarity_threshold=0.95):
|
||||
return "OTHER_PROFILE"
|
||||
|
||||
def purge_stale_screens(self):
|
||||
pass
|
||||
|
||||
def store_screen(self, sig, t):
|
||||
pass
|
||||
|
||||
si.screen_memory = FakePoisonedCache()
|
||||
|
||||
xml = """<?xml version="1.0" encoding="utf-8"?>
|
||||
<hierarchy>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_tab"
|
||||
selected="true" text="" content-desc="Profile" clickable="true" bounds="[864,2268][1080,2400]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header"
|
||||
text="" content-desc="" clickable="false" bounds="[0,100][1080,600]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header_edit_profile_button"
|
||||
text="" content-desc="Edit Profile" clickable="true" bounds="[50,500][1030,570]" />
|
||||
</hierarchy>
|
||||
"""
|
||||
result = si.identify(xml)
|
||||
assert (
|
||||
result["screen_type"] == ScreenType.OWN_PROFILE
|
||||
), "Poisoned Qdrant cache returned OTHER_PROFILE but structural fast-path should override!"
|
||||
|
||||
def test_poisoned_qdrant_cache_cannot_override_other_profile(self, monkeypatch):
|
||||
"""Qdrant cached OWN_PROFILE but XML shows follow button → OTHER_PROFILE."""
|
||||
si = _make_identity()
|
||||
|
||||
class FakePoisonedCache:
|
||||
is_connected = True
|
||||
|
||||
def get_screen_type(self, sig, similarity_threshold=0.95):
|
||||
return "OWN_PROFILE"
|
||||
|
||||
def purge_stale_screens(self):
|
||||
pass
|
||||
|
||||
def store_screen(self, sig, t):
|
||||
pass
|
||||
|
||||
si.screen_memory = FakePoisonedCache()
|
||||
|
||||
xml = """<?xml version="1.0" encoding="utf-8"?>
|
||||
<hierarchy>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header"
|
||||
text="" content-desc="" clickable="false" bounds="[0,100][1080,600]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_header_follow_button"
|
||||
text="" content-desc="Follow" clickable="true" bounds="[50,500][1030,570]" />
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/profile_tab"
|
||||
selected="false" text="" content-desc="Profile" clickable="true" bounds="[864,2268][1080,2400]" />
|
||||
</hierarchy>
|
||||
"""
|
||||
result = si.identify(xml)
|
||||
assert (
|
||||
result["screen_type"] == ScreenType.OTHER_PROFILE
|
||||
), "Poisoned Qdrant cache returned OWN_PROFILE but structural fast-path should override!"
|
||||
|
||||
def test_qdrant_profile_cache_rejected_when_no_structural_match(self, monkeypatch):
|
||||
"""If Qdrant says OTHER_PROFILE but no profile markers exist, it must be rejected."""
|
||||
si = _make_identity()
|
||||
|
||||
class FakePoisonedCache:
|
||||
is_connected = True
|
||||
|
||||
def get_screen_type(self, sig, similarity_threshold=0.95):
|
||||
return "OTHER_PROFILE"
|
||||
|
||||
def purge_stale_screens(self):
|
||||
pass
|
||||
|
||||
def store_screen(self, sig, t):
|
||||
pass
|
||||
|
||||
si.screen_memory = FakePoisonedCache()
|
||||
|
||||
xml = """<?xml version="1.0" encoding="utf-8"?>
|
||||
<hierarchy>
|
||||
<node package="com.instagram.android" resource-id="com.instagram.android:id/feed_tab"
|
||||
selected="true" text="" content-desc="Home" clickable="true" bounds="[0,2268][216,2400]" />
|
||||
</hierarchy>
|
||||
"""
|
||||
result = si.identify(xml)
|
||||
# Qdrant said OTHER_PROFILE, but feed_tab is selected → HOME_FEED
|
||||
# The cache for profile types must be rejected by _STRUCTURALLY_GATED_TYPES guard
|
||||
assert (
|
||||
result["screen_type"] != ScreenType.OTHER_PROFILE
|
||||
), "Qdrant cache for OTHER_PROFILE must be rejected when no profile markers exist!"
|
||||
|
||||
@@ -11,15 +11,15 @@ def test_media_intent_rejects_grid_containers():
|
||||
engine = TelepathicEngine()
|
||||
screen_height = 2400
|
||||
|
||||
from GramAddict.core.perception.spatial_parser import SpatialNode
|
||||
|
||||
# Mock node representing a massive RecyclerView containing the entire grid
|
||||
# Area is 1080 * 2400 = 2592000 > MAX_CONTAINER_AREA (500000)
|
||||
massive_grid_container = {
|
||||
"semantic_string": "id context: 'swipeable nav view pager inner recycler view'",
|
||||
"area": 2592000,
|
||||
"y": 1200,
|
||||
"class_name": "androidx.recyclerview.widget.RecyclerView",
|
||||
"resource_id": "com.instagram.android:id/swipeable_nav_view_pager_inner_recycler_view",
|
||||
}
|
||||
massive_grid_container = SpatialNode(
|
||||
bounds=(0, 0, 1080, 2400),
|
||||
class_name="androidx.recyclerview.widget.RecyclerView",
|
||||
resource_id="com.instagram.android:id/swipeable_nav_view_pager_inner_recycler_view",
|
||||
)
|
||||
|
||||
# Intent
|
||||
intent = "first image post in profile grid"
|
||||
|
||||
55
tests/unit/test_telepathic_trap_guard.py
Normal file
55
tests/unit/test_telepathic_trap_guard.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from GramAddict.core.perception.spatial_parser import SpatialNode
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
|
||||
def test_telepathic_engine_global_trap_guard():
|
||||
"""
|
||||
TDD Test: Proves that the TelepathicEngine structurally blocks dangerous UI
|
||||
elements (like 'audio', 'trending', 'save') regardless of the intent,
|
||||
unless explicitly requested.
|
||||
"""
|
||||
engine = TelepathicEngine()
|
||||
screen_height = 2400
|
||||
|
||||
# 1. Setup mock nodes for traps
|
||||
node_audio = SpatialNode(
|
||||
bounds=(0, 0, 100, 100),
|
||||
resource_id="com.instagram.android:id/audio_icon",
|
||||
class_name="android.widget.ImageView",
|
||||
)
|
||||
|
||||
node_save = SpatialNode(
|
||||
bounds=(0, 100, 100, 200),
|
||||
resource_id="com.instagram.android:id/save_button",
|
||||
class_name="android.widget.ImageView",
|
||||
)
|
||||
|
||||
node_normal = SpatialNode(
|
||||
bounds=(0, 200, 100, 300),
|
||||
resource_id="com.instagram.android:id/zoomable_view_container",
|
||||
class_name="android.widget.FrameLayout",
|
||||
)
|
||||
|
||||
# 2. Test Intents that SHOULD NOT trigger the traps
|
||||
intent_media = "post media content"
|
||||
|
||||
assert (
|
||||
engine._structural_sanity_check(node_audio, intent_media, screen_height) is False
|
||||
), "Guard failed: Audio icon allowed for 'post media content'."
|
||||
assert (
|
||||
engine._structural_sanity_check(node_save, intent_media, screen_height) is False
|
||||
), "Guard failed: Save button allowed for 'post media content'."
|
||||
assert (
|
||||
engine._structural_sanity_check(node_normal, intent_media, screen_height) is True
|
||||
), "Guard failed: Normal node was blocked incorrectly."
|
||||
|
||||
# 3. Test Intents that SHOULD trigger the traps
|
||||
intent_audio = "trending audio"
|
||||
intent_save = "save button"
|
||||
|
||||
assert (
|
||||
engine._structural_sanity_check(node_audio, intent_audio, screen_height) is True
|
||||
), "Guard failed: Audio icon blocked for 'trending audio'."
|
||||
assert (
|
||||
engine._structural_sanity_check(node_save, intent_save, screen_height) is True
|
||||
), "Guard failed: Save button blocked for 'save button'."
|
||||
@@ -12,10 +12,19 @@ class TestVerifySuccessGridReels:
|
||||
|
||||
def setup_method(self):
|
||||
self.engine = TelepathicEngine()
|
||||
grid_xml = """
|
||||
<hierarchy>
|
||||
<node resource-id="com.instagram.android:id/recycler_view" />
|
||||
<node resource-id="com.instagram.android:id/image_button" />
|
||||
<node resource-id="com.instagram.android:id/explore_action_bar" />
|
||||
<node text="Search" resource-id="com.instagram.android:id/action_bar_search_edit_text" />
|
||||
</hierarchy>
|
||||
"""
|
||||
# Simulate a click context so verify_success has something to check against
|
||||
TelepathicEngine._last_click_context = {
|
||||
self.engine._memory._last_click_context = {
|
||||
"intent": "view a post",
|
||||
"semantic_string": "id context: 'image button'",
|
||||
"xml_context": grid_xml,
|
||||
"x": 178,
|
||||
"y": 558,
|
||||
"timestamp": 0,
|
||||
@@ -62,7 +71,7 @@ class TestVerifySuccessGridReels:
|
||||
|
||||
def test_profile_grid_reel_accepted(self):
|
||||
"""Profile grid → Reel must also be accepted."""
|
||||
TelepathicEngine._last_click_context["intent"] = "view a post"
|
||||
self.engine._memory._last_click_context["intent"] = "view a post"
|
||||
reel_xml = """
|
||||
<hierarchy>
|
||||
<node resource-id="com.instagram.android:id/clips_viewer_view_pager" />
|
||||
|
||||
12
wipe_caches.py
Normal file
12
wipe_caches.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import sys
|
||||
|
||||
sys.path.append(".")
|
||||
from GramAddict.core.navigation.path_memory import PathMemory
|
||||
from GramAddict.core.qdrant_memory import wipe_all_ai_caches
|
||||
|
||||
wipe_all_ai_caches()
|
||||
pm = PathMemory(username="marcmintel") # or generic
|
||||
if pm._db and pm._db.is_connected:
|
||||
pm.wipe()
|
||||
print("PathMemory wiped!")
|
||||
print("Caches wiped!")
|
||||
Reference in New Issue
Block a user