fix(navigation): harden autonomous perimeter guards against Play Store trap

This commit is contained in:
2026-05-03 18:29:25 +02:00
parent 93b2140844
commit b36dde77d8
12 changed files with 709 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
import logging
import os
import random
import re
try:
import psutil
@@ -857,6 +858,20 @@ def _run_zero_latency_stories_loop(device, configs, session_state, cognitive_sta
logger.warning("Failed to dump UI hierarchy in StoriesFeed.")
return "CONTEXT_LOST"
# ── Perimeter Guard: Verify we're still inside Instagram ──
# Production bug 2026-05-03: A story's swipe-up link opened the Play Store,
# and the loop kept tapping blindly on com.android.vending for 5+ iterations.
packages = set(re.findall(r'package="([^"]+)"', xml_dump))
app_id = getattr(device, "app_id", "com.instagram.android")
if packages and app_id not in packages:
logger.error(
f"🚨 [StoriesFeed] FOREIGN APP DETECTED! Packages: {packages}. "
f"A story link likely opened an external app. Aborting loop."
)
device.press("back")
sleep(1.5)
return "CONTEXT_LOST"
if getattr(configs.args, "ignore_close_friends", False):
if "enge freunde" in xml_dump.lower() or "close friend" in xml_dump.lower():
logger.info(
@@ -970,6 +985,17 @@ def _run_zero_latency_feed_loop(
if cognitive_stack.get("radome"):
context_xml = cognitive_stack.get("radome").sanitize_xml(context_xml)
# ── Perimeter Guard: Verify we're still inside Instagram ──
# Parity with story loop guard (production bug 2026-05-03).
if context_xml:
feed_packages = set(re.findall(r'package="([^"]+)"', context_xml))
feed_app_id = getattr(device, "app_id", "com.instagram.android")
if feed_packages and feed_app_id not in feed_packages:
logger.error(f"🚨 [FeedLoop] FOREIGN APP DETECTED! Packages: {feed_packages}. Aborting loop.")
device.press("back")
sleep(1.5)
return "CONTEXT_LOST"
# ── Execute Plugin Registry Behaviors (Feed Level) ──
from GramAddict.core.behaviors import BehaviorContext, PluginRegistry

View File

@@ -247,34 +247,37 @@ class GoalExecutor:
for k in keys_to_clear:
del self.action_failures[k]
# ── Back-Press Circuit Breaker ──
# ── Back-Press Circuit Breaker → Escalation ──
if action == "press back":
consecutive_back_presses += 1
if consecutive_back_presses >= MAX_CONSECUTIVE_BACK:
logger.error(
logger.warning(
f"🛑 [GOAP] Back-pressed {MAX_CONSECUTIVE_BACK} times with no screen transition. "
f"Aborting goal '{goal}' to prevent app exit."
f"Escalating to force restart."
)
self.path_memory.learn_path(goal, start_screen, steps_taken, False)
# Phase 3 GREEN: Unlearn the trap path
# Unlearn the trap path
from GramAddict.core.qdrant_memory import NavigationMemoryDB
# We don't know the exact action that got us here easily without analyzing steps_taken,
# but we can grab the first action taken from start_screen in this chain if available.
if len(steps_taken) > consecutive_back_presses:
last_real_action = steps_taken[-consecutive_back_presses - 1]["action"]
logger.debug(
f"[GOAP Unlearn] last_real_action={last_real_action}, " f"start_screen={start_screen}"
)
NavigationMemoryDB().unlearn_transition(start_screen, last_real_action)
else:
logger.debug(
f"[GOAP Unlearn] No real action to unlearn. "
f"steps={len(steps_taken)}, back_presses={consecutive_back_presses}"
)
return False
# ── ESCALATION: Force restart instead of aborting ──
app_id = getattr(self.device, "app_id", "com.instagram.android")
self.device.app_start(app_id, use_monkey=True)
random_sleep(2.0, 3.5)
steps_taken.append({"action": "force start instagram"})
logger.info("🔄 [GOAP Escalation] App restarted. Purging all failure state for fresh attempt.")
self.action_failures.clear()
explored_nav_actions.clear()
visited_screens.clear()
consecutive_back_presses = 0
continue
else:
consecutive_back_presses = 0
else:
@@ -388,6 +391,7 @@ class GoalExecutor:
# Determine if this was a navigation or an interaction
from GramAddict.core.screen_topology import ScreenTopology
is_navigation = any(k in action.lower() for k in ["tab", "open", "go to", "navigate", "following list"])
if not is_navigation:
is_navigation = ScreenTopology.is_structural_action(pre_action_screen_type, action)

View File

@@ -350,8 +350,31 @@ class SituationalAwarenessEngine:
is_foreign = True
if is_foreign:
# We explicitly ask the TelepathicEngine to classify this to avoid writing brittle substring hacks
# for Android System UI variations across different device manufacturers.
# ── Tier 1: Known Foreign Packages (O(1) — ZERO LLM) ──
# Production bug 2026-05-03: Play Store was detected via slow LLM path.
# For these well-known packages, a set lookup is instant and infallible.
KNOWN_FOREIGN_PACKAGES = {
"com.android.vending", # Play Store
"com.android.chrome", # Chrome
"com.google.android.chrome", # Chrome (Google build)
"com.google.android.youtube", # YouTube
"org.mozilla.firefox", # Firefox
"com.opera.browser", # Opera
"com.brave.browser", # Brave
"com.microsoft.emmx", # Edge
"com.sec.android.app.sbrowser", # Samsung Browser
}
dominant_pkgs = packages - {"com.android.systemui"}
fast_match = dominant_pkgs & KNOWN_FOREIGN_PACKAGES
if fast_match:
logger.info(
f"🚨 [SAE Perceive] Known foreign package: {fast_match}"
f"OBSTACLE_FOREIGN_APP (O(1) fast-path, no LLM needed)"
)
return SituationType.OBSTACLE_FOREIGN_APP
# ── Tier 2: Unknown/Ambiguous Packages → LLM Classification ──
# Only SystemUI-only or rare custom packages reach this path.
try:
from GramAddict.core.config import Config
from GramAddict.core.llm_provider import query_telepathic_llm