Hardening: Centralized zero-trust CognitiveStack validation and removed synthetic e2e test mocks

This commit is contained in:
2026-05-03 20:30:30 +02:00
parent c7c7ce29f8
commit 4b645c6fb2
3 changed files with 57 additions and 29 deletions

View File

@@ -685,6 +685,7 @@ def _interact_with_profile(device, configs, username, session_state, sleep_mod,
if cognitive_stack is None:
cognitive_stack = {}
_validate_cognitive_stack(cognitive_stack, "ProfileInteraction")
if hasattr(session_state, "my_username") and username == session_state.my_username:
logger.info(f"🤝 [Deep Interaction] Skipping own profile @{username} to prevent self-interactions.")
@@ -824,6 +825,7 @@ def _run_zero_latency_stories_loop(device, configs, session_state, cognitive_sta
from colorama import Fore
_validate_cognitive_stack(cognitive_stack, "StoriesLoop")
logger.info("🎬 [StoriesFeed] Starting native story binging loop...", extra={"color": f"{Fore.CYAN}"})
dopamine = cognitive_stack.get("dopamine")
@@ -893,6 +895,36 @@ def _run_zero_latency_stories_loop(device, configs, session_state, cognitive_sta
return "FEED_EXHAUSTED"
def _validate_cognitive_stack(cognitive_stack, context_name):
"""
Validates that the cognitive stack has all required engine dependencies
injected properly. This is the ultimate zero-trust guard for plugins.
"""
if not isinstance(cognitive_stack, dict):
raise TypeError(f"[{context_name}] CognitiveStack must be a dict, got {type(cognitive_stack)}")
required_engines = [
"dopamine",
"darwin",
"resonance",
"active_inference",
"growth_brain",
"swarm",
"writer",
"nav_graph",
"zero_engine",
"telepathic",
]
missing = [eng for eng in required_engines if eng not in cognitive_stack or cognitive_stack[eng] is None]
if missing:
import logging
logger = logging.getLogger(__name__)
logger.error(f"🚨 [{context_name}] CognitiveStack missing required engines: {missing}")
raise ValueError(f"[{context_name}] CognitiveStack missing required engines: {missing}")
def _run_zero_latency_feed_loop(
device, zero_engine, nav_graph, configs, session_state, job_target, cognitive_stack, is_reels=False
):
@@ -906,6 +938,7 @@ def _run_zero_latency_feed_loop(
- Darwin is the SOLE dwell controller → no duplicate sleep calls
- SwarmProtocol emits pheromones after successful interactions
"""
_validate_cognitive_stack(cognitive_stack, "FeedLoop")
logger.info(f"🔄 Entering Zero-Latency Interaction Pool. Feed: {job_target}")
dopamine = cognitive_stack.get("dopamine")
@@ -1055,6 +1088,7 @@ def _run_zero_latency_search_loop(
"""
Executes the autonomous Search & Interact logic.
"""
_validate_cognitive_stack(cognitive_stack, "SearchLoop")
logger.info("🧠 [Search Engine] Initiating keyword discovery...", extra={"color": f"{Style.BRIGHT}{Fore.CYAN}"})
import random
@@ -1080,6 +1114,16 @@ def _run_zero_latency_search_loop(
xml = device.dump_hierarchy()
telepathic = cognitive_stack.get("telepathic")
# ── Perimeter Guard: Verify we're still inside Instagram ──
if xml:
search_packages = set(re.findall(r'package="([^"]+)"', xml))
search_app_id = getattr(device, "app_id", "com.instagram.android")
if search_packages and search_app_id not in search_packages:
logger.error(f"🚨 [SearchLoop] FOREIGN APP DETECTED! Packages: {search_packages}. Aborting loop.")
device.press("back")
sleep(1.5)
return "CONTEXT_LOST"
# Find search bar
search_bar = telepathic.find_best_node(xml, "Search edit text box or magnifying glass input", device=device)
if search_bar: