test: finalize E2E timeout verification and purge global state leaks

This commit is contained in:
2026-05-02 18:59:35 +02:00
parent d2de5f91de
commit f32ee46d8c
44 changed files with 740 additions and 1387 deletions

View File

@@ -49,7 +49,7 @@ class FollowPlugin(BehaviorPlugin):
nav_graph = QNavGraph(ctx.device)
if nav_graph.do("tap 'Follow' button"):
if nav_graph.do("tap 'Follow' button") or nav_graph.do("tap 'Following' button"):
logger.info(f"🤝 [Follow] Followed @{ctx.username}")
ctx.session_state.add_interaction(source=ctx.username, succeed=True, followed=True, scraped=False)

View File

@@ -26,7 +26,9 @@ class PostDataExtractionPlugin(BehaviorPlugin):
return 85
def can_activate(self, ctx: BehaviorContext) -> bool:
return getattr(self, "_enabled", True) and ctx.context_xml is not None
from GramAddict.core.perception.feed_analysis import has_feed_markers
return getattr(self, "_enabled", True) and ctx.context_xml is not None and has_feed_markers(ctx.context_xml)
def execute(self, ctx: BehaviorContext) -> BehaviorResult:
logger.debug("🧩 [PostDataExtraction] Extracting post metadata...")
@@ -35,6 +37,13 @@ class PostDataExtractionPlugin(BehaviorPlugin):
if post_data:
ctx.post_data = post_data
ctx.username = post_data.get("username", "")
if post_data.get("username_missing") or not ctx.username:
logger.error(
"❌ [PostDataExtraction] FAILED: Post author username is empty or missing! Halting interaction."
)
return BehaviorResult(executed=False, metadata={"error": "Empty username extracted"})
logger.info(f"📝 [PostDataExtraction] Post by @{ctx.username} extracted.")
return BehaviorResult(executed=True)

View File

@@ -21,7 +21,6 @@ from GramAddict.core.dojo_engine import DojoEngine
# Cognitive Stack
from GramAddict.core.dopamine_engine import DopamineEngine
from GramAddict.core.goap import GoalExecutor
from GramAddict.core.growth_brain import GrowthBrain
from GramAddict.core.log import configure_logger
from GramAddict.core.perception.feed_analysis import (
@@ -180,8 +179,12 @@ def start_bot(**kwargs):
global_goal = getattr(configs.args, "goal", None)
if global_goal:
persona_interests.insert(0, global_goal)
logger.info(f"🎯 [Autonomous Directive] Overriding target audience with high-level goal: {global_goal}", extra={"color": f"{Style.BRIGHT}{Fore.GREEN}"})
logger.info(
f"🎯 [Autonomous Directive] Overriding target audience with high-level goal: {global_goal}",
extra={"color": f"{Style.BRIGHT}{Fore.GREEN}"},
)
from GramAddict.core.goap import GoalExecutor
from GramAddict.core.interaction import LLMWriter
from GramAddict.core.qdrant_memory import DMMemoryDB, ParasocialCRMDB
from GramAddict.core.resonance_engine import ResonanceEngine
@@ -303,7 +306,27 @@ def start_bot(**kwargs):
cognitive_stack["dojo"] = dojo
try:
bot_start_time = datetime.now()
configs.args.global_start_time = bot_start_time
max_runtime = getattr(configs.args, "max_runtime_minutes", None)
dopamine.global_start_time = bot_start_time
dopamine.global_max_runtime_minutes = max_runtime
GoalExecutor.global_start_time = bot_start_time
GoalExecutor.global_max_runtime_minutes = max_runtime
while True:
if max_runtime:
from datetime import timedelta
elapsed = datetime.now() - bot_start_time
if elapsed > timedelta(minutes=max_runtime):
logger.info(
f"🛑 [Timeout] Maximum runtime of {max_runtime} minutes reached. Stopping bot.",
extra={"color": f"{Fore.RED}"},
)
break
set_time_delta(configs.args)
inside_working_hours, time_left = SessionState.inside_working_hours(
configs.args.working_hours, configs.args.time_delta_session

View File

@@ -144,6 +144,9 @@ class Config:
self.parser.add_argument("--total-sessions", help="Total amount of sessions", default="-1")
self.parser.add_argument("--working-hours", help="Working hours", default=None)
self.parser.add_argument("--time-delta-session", help="Time delta between sessions", default=None)
self.parser.add_argument(
"--max-runtime-minutes", type=int, help="Maximum runtime in minutes before bot auto-exits", default=None
)
self.parser.add_argument("--restart-atx-agent", action="store_true", help="Restart atx agent")
self.parser.add_argument("--allow-untested-ig-version", action="store_true", help="Allow untested IG version")
self.parser.add_argument(

View File

@@ -157,6 +157,7 @@ def _run_zero_latency_dm_loop(device, zero_engine, nav_graph, configs, session_s
# Generate response
prompt = f"You are replying to a direct message on Instagram. The last message you received was: '{context_text}'. Keep it short, casual, and friendly. Do not use hashtags."
logger.info(">>> [DM Engine] ABOUT TO CALL LLM")
response_dict = query_llm(
url=url,
model=model,
@@ -166,6 +167,7 @@ def _run_zero_latency_dm_loop(device, zero_engine, nav_graph, configs, session_s
max_tokens=100,
temperature=0.7,
)
logger.info(f">>> [DM Engine] LLM RETURNED: {response_dict}")
if response_dict and "response" in response_dict:
response_text = response_dict["response"].strip()

View File

@@ -93,6 +93,18 @@ class DopamineEngine:
)
def is_app_session_over(self):
# Global Hard Kill check
if getattr(self, "global_max_runtime_minutes", None):
if hasattr(self, "global_start_time"):
from datetime import datetime, timedelta
if datetime.now() - self.global_start_time > timedelta(minutes=self.global_max_runtime_minutes):
logger.info(
f"🛑 [Timeout] Maximum runtime of {self.global_max_runtime_minutes} minutes reached (checked by DopamineEngine). Force-stopping session.",
extra={"color": f"{Fore.RED}"},
)
return True
# True if we have scrolled too long or hit absolute burnout
return (time.time() - self.session_start) > self.session_limit_seconds or self.boredom >= 100.0

View File

@@ -43,6 +43,8 @@ class GoalExecutor:
"""
_instance = None
global_start_time = None
global_max_runtime_minutes = None
@classmethod
def get_instance(cls, device=None, bot_username=""):
@@ -119,6 +121,19 @@ class GoalExecutor:
explored_nav_actions = set()
visited_screens = set()
for step_num in range(max_steps):
# ── Global Hard Kill Check ──
max_rt = GoalExecutor.global_max_runtime_minutes
start_time = GoalExecutor.global_start_time
if max_rt and start_time:
from datetime import datetime, timedelta
if datetime.now() - start_time > timedelta(minutes=max_rt):
logger.error(
f"🛑 [Timeout] Maximum runtime of {max_rt} minutes reached during GOAP execution. Hard stopping planner.",
extra={"color": "\\033[31m"},
)
return False
# PERCEIVE
screen = self.perceive()
screen_type = screen["screen_type"]
@@ -431,7 +446,12 @@ class GoalExecutor:
# (e.g. checked="false" → "true" = 1 byte). We must NOT gate interactions on
# MIN_UI_CHANGE_BYTES. ANY change at all warrants semantic verification.
interaction_xml_changed = post_xml != xml_dump
if interaction_xml_changed:
if post_screen_type == ScreenType.FOREIGN_APP:
logger.error(
f"❌ [GOAP Verify] Interaction '{action}' caused navigation to FOREIGN_APP (e.g. Play Store). Rejecting as catastrophic failure."
)
action_success = False
elif interaction_xml_changed:
score = best_node.get("score", 0.0) if best_node else 0.0
verification = engine.verify_success(action, post_xml, device=self.device, confidence=score)
if verification is True:

View File

@@ -62,13 +62,35 @@ def extract_post_content(context_xml: str, device=None) -> dict:
telepath = TelepathicEngine.get_instance()
# 1. Learn/extract post author dynamically
author_node = telepath.find_best_node(
context_xml, "post author username text (exclude bottom tabs)", min_confidence=0.75, device=device
)
# 🛡️ Structural Fast-Path: Prioritize deterministic IDs over AI guesses
author_node = None
try:
root = ET.fromstring(context_xml)
for node in root.iter():
res_id = node.attrib.get("resource-id", "")
if "row_feed_photo_profile_name" in res_id or "profile_header_name" in res_id:
author_node = {
"original_attribs": {
"text": node.attrib.get("text", ""),
"content_desc": node.attrib.get("content-desc", ""),
}
}
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}")
# 🛡️ Anti-Hallucination Guard: Ensure we actually found text.
if author_node and author_node.get("original_attribs", {}).get("text"):
result["username"] = author_node["original_attribs"]["text"].strip()
elif author_node and author_node.get("original_attribs", {}).get("content_desc"):
result["username"] = author_node["original_attribs"]["content_desc"].strip()
# 2. Learn/extract post media description dynamically
media_node = telepath.find_best_node(
@@ -77,8 +99,8 @@ def extract_post_content(context_xml: str, device=None) -> dict:
min_confidence=0.35,
device=device,
)
if media_node and media_node.get("original_attribs", {}).get("desc"):
result["description"] = media_node["original_attribs"]["desc"].strip()
if media_node and media_node.get("original_attribs", {}).get("content_desc"):
result["description"] = media_node["original_attribs"]["content_desc"].strip()
# 3. Visible caption text (heuristic fallback if node isn't explicitly found)
# Search all nodes for text that contains the username to find the caption body

View File

@@ -98,6 +98,13 @@ class IntentResolver:
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(
@@ -113,6 +120,56 @@ class IntentResolver:
)
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
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}'"
)
continue
filtered.append(node)
return filtered
@@ -144,6 +201,70 @@ class IntentResolver:
logger.info(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.info(f"🎯 [Structural Fast-Path] Found last received message text: '{latest_msg.text}'")
return latest_msg
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
or "send" in desc
or "absenden" in desc
or text in ["send", "absenden"]
):
logger.info(f"🎯 [Structural Fast-Path] Found send button: {rid or desc or text}")
return node
if "post author username" in intent_lower or "tap post username" in intent_lower:
for node in candidates:
if "row_feed_photo_profile_name" in (node.resource_id or "").lower():
logger.info(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.info(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.info(f"🎯 [Structural Fast-Path] Found comment button: {node.resource_id}")
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.info(f"🎯 [Structural Fast-Path] Found first post/item: {rid}")
return node
# --- Semantic Match Guard ---
# If the intent explicitly quotes a target (e.g., "tap 'New Message'"),
# we strictly filter candidates to those whose text or content_desc contains the quote.
@@ -180,9 +301,11 @@ class IntentResolver:
if device is not None and (
hasattr(device, "screenshot") or hasattr(getattr(device, "deviceV2", None), "screenshot")
):
print(f"DEBUG_INTENT: Entering Visual Discovery for '{intent_description}'")
logger.info("📸 Device screenshot capability detected. Enforcing visual discovery.")
return self._visual_discovery(intent_description, candidates, device, screen_height=screen_height)
print(f"DEBUG_INTENT: Falling back to Text-based VLM for '{intent_description}'")
# --- 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.
@@ -404,6 +527,9 @@ class IntentResolver:
try:
annotated_b64, box_map = self._annotate_screenshot_with_candidates(device, candidates)
except Exception as e:
import traceback
traceback.print_exc()
logger.warning(f"⚠️ [Visual Discovery] Screenshot annotation failed: {e}")
return None
@@ -466,7 +592,9 @@ class IntentResolver:
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 exact control is NOT visible, return null. Do NOT guess.\n\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}}'
)
@@ -479,6 +607,7 @@ class IntentResolver:
use_local_edge=True,
images_b64=[annotated_b64],
)
print(f"DEBUG_INTENT: VLM RAW RESPONSE for '{intent_description}': {res}")
data = json.loads(res)
box_idx = data.get("box")
if box_idx is None:
@@ -567,6 +696,7 @@ class IntentResolver:
user_prompt=prompt,
use_local_edge=True,
)
print(f"DEBUG_INTENT: TEXT LLM RAW RESPONSE for '{intent_description}': {res}")
data = json.loads(res)
idx = data.get("selected_index")
if idx is not None and 0 <= idx < len(filtered_candidates):

View File

@@ -184,15 +184,6 @@ class ScreenIdentity:
if any(marker in texts for marker in chat_input_markers) or "direct_thread_header" in ids:
return ScreenType.DM_THREAD
# Priority 2: Check Qdrant Semantic Cache (Fuzzy/VLM derived)
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)
if cached_type_str:
try:
return ScreenType[cached_type_str]
except KeyError:
pass
# 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.
@@ -225,7 +216,7 @@ class ScreenIdentity:
return ScreenType.REELS_FEED
if selected_tab == "search_tab":
return ScreenType.EXPLORE_GRID
if "action_bar_search_edit_text" in ids and "search_tab" in ids:
if "action_bar_search_edit_text" in ids:
return ScreenType.EXPLORE_GRID
if selected_tab == "profile_tab":
return ScreenType.OWN_PROFILE
@@ -234,6 +225,15 @@ class ScreenIdentity:
if "message_input" in ids:
return ScreenType.DM_INBOX # Fallback for DM thread as inbox
# Priority 2: Check Qdrant Semantic Cache (Fuzzy/VLM derived)
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)
if cached_type_str:
try:
return ScreenType[cached_type_str]
except KeyError:
pass
# Priority 3: Semantic VLM Classification Fallback
from GramAddict.core.config import Config
from GramAddict.core.llm_provider import query_llm
@@ -309,7 +309,18 @@ class ScreenIdentity:
actions.append("tap save button")
if "back" in desc_lower:
actions.append("tap back button")
if any("follow" in e.get("text", "").lower() for e in clickable_elements):
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")
if screen_type == ScreenType.OWN_PROFILE or screen_type == ScreenType.OTHER_PROFILE:

View File

@@ -184,7 +184,7 @@ class SpatialParser:
for n in all_nodes:
has_semantic = bool(n.text or n.content_desc)
semantic_res = n.resource_id and any(
x in n.resource_id.lower() for x in ["button", "tab", "icon", "action", "menu"]
x in n.resource_id.lower() for x in ["button", "tab", "icon", "action", "menu", "imageview"]
)
if n.clickable or n.scrollable or semantic_res or (has_semantic and n.area < 500000 and n.area > 0):

View File

@@ -135,18 +135,20 @@ class QNavGraph:
# Map goal to the action that should be available
action_checks = {
"like": "tap like button",
"comment": "tap comment button",
"share": "tap share button",
"follow": "tap follow button",
"like": ["tap like button"],
"comment": ["tap comment button"],
"share": ["tap share button"],
"follow": ["tap follow button", "tap following button"],
}
for keyword, required_action in action_checks.items():
if keyword in goal.lower() and required_action not in available:
logger.warning(
f"🚫 [GOAP] Cannot '{goal}' on {screen_type.value} "
f"('{required_action}' not available on this screen)"
)
return False
for keyword, required_actions in action_checks.items():
if keyword in goal.lower():
# If ANY of the required actions are available, it's valid
if not any(req in available for req in required_actions):
logger.warning(
f"🚫 [GOAP] Cannot '{goal}' on {screen_type.value} "
f"({required_actions} not available on this screen)"
)
return False
return self.goap._execute_action(goal)

View File

@@ -103,7 +103,11 @@ class TelepathicEngine:
return None
# 3.5 Following Button Guard
if "follow" in intent_description.lower() and "unfollow" not in intent_description.lower():
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 "")
)
@@ -224,13 +228,16 @@ class TelepathicEngine:
semantic = (node.get("semantic_string", "") or "").lower()
# 1. Post Username Guard
if "post username" in intent:
if "post username" in intent or "author username" in intent:
if "story" in semantic:
# E.g. "Your Story" circle at the top
return False
# Prevent tapping a search list item when looking for a post username
if "row search user container" in semantic.replace("_", " "):
return False
# Prevent tapping bottom tabs
if "tab" in semantic and "exclude bottom tabs" in intent:
return False
return True
# 3.5 Media Content Guard
@@ -238,6 +245,9 @@ class TelepathicEngine:
# Prevent tapping a search keyword instead of a media post
if "row search keyword title" in semantic.replace("_", " "):
return False
# Prevent tapping bottom tabs
if "tab" in semantic and "exclude bottom tabs" in intent:
return False
# 3.6 Post Author Username Header Guard
if "post author username header" in intent: