Compare commits
4 Commits
0ed12303ac
...
refactor/g
| Author | SHA1 | Date | |
|---|---|---|---|
| effb1f5ae1 | |||
| 0e43996ccd | |||
| b6846ab0fe | |||
| 6db579f45b |
@@ -445,56 +445,68 @@ def start_bot(**kwargs):
|
||||
has_scanned_own_profile = True
|
||||
|
||||
while not dopamine.is_app_session_over():
|
||||
# 1. Ask the Growth Brain for a Strategic Objective
|
||||
success_rates = getattr(session_state, "successfulInteractions", {})
|
||||
current_goal = growth_brain.get_current_goal(
|
||||
dopamine, getattr(configs.args, "goals", []), success_rates=success_rates
|
||||
# ── 1. Generate available tasks from mission + plugins ──
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
decomposer = GoalDecomposer(
|
||||
plugins=configs.config.get("plugins", {}) if configs.config else {},
|
||||
actions={
|
||||
k: getattr(configs.args, k, None)
|
||||
for k in ("feed", "explore", "reels")
|
||||
if getattr(configs.args, k, None)
|
||||
},
|
||||
mission=configs.config.get("mission", {}) if configs.config else {},
|
||||
)
|
||||
available_tasks = decomposer.generate_tasks()
|
||||
|
||||
if current_goal == "ShiftContext":
|
||||
logger.info("🧠 [Free Will] Boredom critical. Forcing app restart to clear context.")
|
||||
device.app_stop(device.app_id)
|
||||
random_sleep(2.0, 4.0)
|
||||
device.app_start(device.app_id, use_monkey=True)
|
||||
random_sleep(4.0, 6.0)
|
||||
dopamine.boredom = max(0.0, dopamine.boredom * 0.2)
|
||||
continue
|
||||
if not available_tasks:
|
||||
# No plugins enabled = nothing to do. Fall back to legacy desire system.
|
||||
current_desire = growth_brain.get_current_desire(dopamine)
|
||||
if current_desire == "ShiftContext":
|
||||
logger.info("🧠 [Free Will] Boredom critical. Forcing app restart.")
|
||||
device.app_stop(device.app_id)
|
||||
random_sleep(2.0, 4.0)
|
||||
device.app_start(device.app_id, use_monkey=True)
|
||||
random_sleep(4.0, 6.0)
|
||||
dopamine.boredom = max(0.0, dopamine.boredom * 0.2)
|
||||
continue
|
||||
|
||||
# 2. Execution: GOAP Plan & Execute (Autonomous Mode)
|
||||
if getattr(configs.args, "goals", None):
|
||||
logger.info(f"🤖 Autonomous Mode Active. Delegating to GoalExecutor for: {current_goal}")
|
||||
# Legacy desire → target mapping (kept for backward compatibility)
|
||||
target_map = {
|
||||
"DiscoverNewContent": ["ExploreFeed", "ReelsFeed"],
|
||||
"NurtureCommunity": ["HomeFeed", "StoriesFeed"],
|
||||
"SocialReciprocity": ["FollowingList"],
|
||||
}
|
||||
|
||||
goal_executor = GoalExecutor(device=device, bot_username=getattr(configs.args, "username", ""))
|
||||
dm_config = configs.get_plugin_config("dm_reply")
|
||||
if dm_config.get("enabled", False):
|
||||
target_map["SocialReciprocity"].append("MessageInbox")
|
||||
|
||||
result = goal_executor.achieve(current_goal)
|
||||
import secrets
|
||||
|
||||
if result:
|
||||
logger.info("✅ Goal achieved autonomously!")
|
||||
else:
|
||||
logger.warning(f"⚠️ Goal execution failed for: {current_goal}")
|
||||
options = target_map.get(current_desire, ["HomeFeed"])
|
||||
current_target = secrets.choice(options)
|
||||
else:
|
||||
# ── 2. Select a concrete Task ──
|
||||
selected_task = growth_brain.select_task(dopamine, available_tasks)
|
||||
|
||||
continue # The GoalExecutor handles navigation internally
|
||||
if selected_task is None:
|
||||
# ShiftContext signal from high boredom
|
||||
logger.info("🧠 [Free Will] Boredom critical. Forcing app restart to clear context.")
|
||||
device.app_stop(device.app_id)
|
||||
random_sleep(2.0, 4.0)
|
||||
device.app_start(device.app_id, use_monkey=True)
|
||||
random_sleep(4.0, 6.0)
|
||||
dopamine.boredom = max(0.0, dopamine.boredom * 0.2)
|
||||
continue
|
||||
|
||||
# --- LEGACY PROCEDURAL FALLBACK (For config without goals) ---
|
||||
current_desire = current_goal
|
||||
current_target = selected_task.target_screen
|
||||
logger.info(
|
||||
f"🎯 [GoalDecomposer] Task: {selected_task.intent} "
|
||||
f"→ {current_target} (budget={selected_task.budget_posts})"
|
||||
)
|
||||
|
||||
# 2. Map Desire to Sub-Feed
|
||||
target_map = {
|
||||
"DiscoverNewContent": ["ExploreFeed", "ReelsFeed"],
|
||||
"NurtureCommunity": ["HomeFeed", "StoriesFeed"],
|
||||
"SocialReciprocity": ["FollowingList"],
|
||||
}
|
||||
|
||||
dm_config = configs.get_plugin_config("dm_reply")
|
||||
if dm_config.get("enabled", False):
|
||||
target_map["SocialReciprocity"].append("MessageInbox")
|
||||
|
||||
import secrets
|
||||
|
||||
options = target_map.get(current_desire, ["HomeFeed"])
|
||||
current_target = secrets.choice(options)
|
||||
|
||||
logger.info(f"🧠 [Agent Orchestrator] Desire '{current_desire}' -> Routed to {current_target}")
|
||||
logger.info(f"🧠 [Agent Orchestrator] Routed to {current_target}")
|
||||
|
||||
logger.info(f"⚡ Navigating to {current_target}")
|
||||
success = nav_graph.navigate_to(current_target, zero_engine)
|
||||
|
||||
@@ -86,8 +86,8 @@ class Config:
|
||||
self.debug = self.config.get("debug", False)
|
||||
self.app_id = self.config.get("app_id", "com.instagram.android")
|
||||
|
||||
# Autonomous Agent Goals
|
||||
self.goals = self.config.get("goals", [])
|
||||
# Autonomous goals removed — the bot now derives tasks from mission + plugins
|
||||
# via GoalDecomposer. See GramAddict/core/goal_decomposer.py.
|
||||
else:
|
||||
if "--debug" in self.args:
|
||||
self.debug = True
|
||||
|
||||
276
GramAddict/core/goal_decomposer.py
Normal file
276
GramAddict/core/goal_decomposer.py
Normal file
@@ -0,0 +1,276 @@
|
||||
"""
|
||||
GoalDecomposer — Mission-Driven Task Planning
|
||||
|
||||
Translates the bot's `mission` config + `plugins` capabilities into
|
||||
concrete, weighted Task objects. Pure logic — no LLM, no device,
|
||||
no network, no side effects.
|
||||
|
||||
This is the bridge between:
|
||||
- "What does the user WANT?" (mission.strategy)
|
||||
- "What CAN the bot DO?" (enabled plugins + actions)
|
||||
- "What SHOULD it do NOW?" (weighted Task selection)
|
||||
|
||||
Tesla analogy: FSD doesn't have a "goal: drive safely" config.
|
||||
It derives behavior from destination + road rules + sensor capabilities.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import random
|
||||
from dataclasses import dataclass
|
||||
from typing import Dict, List
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# ── Strategy Weight Tables ──
|
||||
# Each strategy defines relative weights for screen targets.
|
||||
# Higher weight = more likely to be selected by GrowthBrain.
|
||||
STRATEGY_WEIGHTS: Dict[str, Dict[str, float]] = {
|
||||
"aggressive_growth": {
|
||||
"HomeFeed": 0.15,
|
||||
"ExploreFeed": 0.45,
|
||||
"ReelsFeed": 0.15,
|
||||
"StoriesFeed": 0.10,
|
||||
"MessageInbox": 0.10,
|
||||
"FollowingList": 0.05,
|
||||
},
|
||||
"community_builder": {
|
||||
"HomeFeed": 0.40,
|
||||
"ExploreFeed": 0.10,
|
||||
"ReelsFeed": 0.05,
|
||||
"StoriesFeed": 0.25,
|
||||
"MessageInbox": 0.15,
|
||||
"FollowingList": 0.05,
|
||||
},
|
||||
"passive_learning": {
|
||||
"HomeFeed": 0.20,
|
||||
"ExploreFeed": 0.50,
|
||||
"ReelsFeed": 0.20,
|
||||
"StoriesFeed": 0.05,
|
||||
"MessageInbox": 0.00,
|
||||
"FollowingList": 0.05,
|
||||
},
|
||||
"stealth_lurker": {
|
||||
"HomeFeed": 0.35,
|
||||
"ExploreFeed": 0.25,
|
||||
"ReelsFeed": 0.15,
|
||||
"StoriesFeed": 0.15,
|
||||
"MessageInbox": 0.05,
|
||||
"FollowingList": 0.05,
|
||||
},
|
||||
}
|
||||
|
||||
# ── Plugin → Screen Mapping ──
|
||||
# Which plugins enable which screen targets.
|
||||
# A screen is only viable if at least one enabling plugin is active.
|
||||
# Some plugins work on MULTIPLE screens (likes work on home, explore, reels).
|
||||
PLUGIN_SCREENS_MAP: Dict[str, set] = {
|
||||
"likes": {"HomeFeed", "ExploreFeed", "ReelsFeed"},
|
||||
"comment": {"HomeFeed", "ExploreFeed"},
|
||||
"follow": {"HomeFeed", "ExploreFeed"},
|
||||
"repost": {"HomeFeed", "ExploreFeed"},
|
||||
"profile_visit": {"HomeFeed", "ExploreFeed"},
|
||||
"grid_like": {"HomeFeed"},
|
||||
"carousel_browsing": {"HomeFeed"},
|
||||
"rabbit_hole": {"HomeFeed", "ExploreFeed"},
|
||||
"story_view": {"StoriesFeed"},
|
||||
"dm_reply": {"MessageInbox"},
|
||||
}
|
||||
|
||||
# ── Action → Screen Mapping ──
|
||||
# The `actions:` config section maps directly to screens.
|
||||
ACTION_SCREEN_MAP: Dict[str, str] = {
|
||||
"feed": "HomeFeed",
|
||||
"explore": "ExploreFeed",
|
||||
"reels": "ReelsFeed",
|
||||
}
|
||||
|
||||
# ── Screen → Verb Mapping ──
|
||||
SCREEN_VERB_MAP: Dict[str, str] = {
|
||||
"HomeFeed": "browse_feed",
|
||||
"ExploreFeed": "browse_explore",
|
||||
"ReelsFeed": "browse_reels",
|
||||
"StoriesFeed": "view_stories",
|
||||
"MessageInbox": "check_messages",
|
||||
"FollowingList": "manage_following",
|
||||
}
|
||||
|
||||
# ── Screen → Human Intent ──
|
||||
SCREEN_INTENT_MAP: Dict[str, str] = {
|
||||
"HomeFeed": "Interact with posts in the home feed",
|
||||
"ExploreFeed": "Discover and engage with new content",
|
||||
"ReelsFeed": "Browse and interact with reels",
|
||||
"StoriesFeed": "View and react to stories",
|
||||
"MessageInbox": "Reply to unread direct messages",
|
||||
"FollowingList": "Review and manage following list",
|
||||
}
|
||||
|
||||
DEFAULT_BUDGET = 5
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Task:
|
||||
"""A concrete, executable unit of work for the bot.
|
||||
|
||||
Unlike abstract goals ("nurture community"), a Task has:
|
||||
- A specific screen to navigate to
|
||||
- A measurable budget (how many posts/items to process)
|
||||
- A weight for probabilistic selection
|
||||
- A human-readable intent for logging
|
||||
"""
|
||||
|
||||
verb: str
|
||||
target_screen: str
|
||||
intent: str
|
||||
budget_posts: int
|
||||
weight: float
|
||||
|
||||
|
||||
class GoalDecomposer:
|
||||
"""Translates mission + plugins → weighted Task list.
|
||||
|
||||
Pure logic, zero side effects. Call generate_tasks() to get
|
||||
the bot's action menu for the current session.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
plugins: Dict[str, dict],
|
||||
actions: Dict[str, str],
|
||||
mission: Dict[str, str],
|
||||
):
|
||||
self._plugins = plugins
|
||||
self._actions = actions
|
||||
self._strategy = mission.get("strategy", "aggressive_growth")
|
||||
|
||||
def generate_tasks(self) -> List[Task]:
|
||||
"""Generate weighted tasks from config.
|
||||
|
||||
Returns an empty list if no plugins are enabled —
|
||||
the bot literally has nothing to do.
|
||||
"""
|
||||
viable_screens = self._discover_viable_screens()
|
||||
if not viable_screens:
|
||||
return []
|
||||
|
||||
strategy_weights = STRATEGY_WEIGHTS.get(self._strategy, STRATEGY_WEIGHTS["aggressive_growth"])
|
||||
|
||||
tasks = []
|
||||
for screen in viable_screens:
|
||||
weight = strategy_weights.get(screen, 0.1)
|
||||
if weight <= 0:
|
||||
continue
|
||||
|
||||
budget = self._budget_for_screen(screen)
|
||||
verb = SCREEN_VERB_MAP.get(screen, "browse")
|
||||
intent = SCREEN_INTENT_MAP.get(screen, f"Interact on {screen}")
|
||||
|
||||
tasks.append(
|
||||
Task(
|
||||
verb=verb,
|
||||
target_screen=screen,
|
||||
intent=intent,
|
||||
budget_posts=budget,
|
||||
weight=weight,
|
||||
)
|
||||
)
|
||||
|
||||
return tasks
|
||||
|
||||
def _discover_viable_screens(self) -> set:
|
||||
"""Determine which screens the bot can meaningfully interact on.
|
||||
|
||||
A screen is viable if it has BOTH:
|
||||
1. A route (action config or plugin-implied), AND
|
||||
2. At least one active plugin that can DO something there.
|
||||
|
||||
Without an active plugin, navigating to a screen is pointless —
|
||||
the bot would just scroll with nothing to interact on.
|
||||
"""
|
||||
# 1. Collect screens with active plugins
|
||||
plugin_screens: set = set()
|
||||
for plugin_name, screens in PLUGIN_SCREENS_MAP.items():
|
||||
plugin_cfg = self._plugins.get(plugin_name, {})
|
||||
if not plugin_cfg:
|
||||
continue
|
||||
if not self._is_plugin_active(plugin_cfg):
|
||||
continue
|
||||
plugin_screens.update(screens)
|
||||
|
||||
# 2. Screens from actions are only viable if plugins exist for them
|
||||
action_screens: set = set()
|
||||
for action_key, screen in ACTION_SCREEN_MAP.items():
|
||||
if action_key in self._actions and self._actions[action_key]:
|
||||
action_screens.add(screen)
|
||||
|
||||
# 3. A screen must have plugin coverage to be viable
|
||||
# Action-enabled screens need at least one active plugin
|
||||
viable = action_screens & plugin_screens
|
||||
|
||||
# 4. Plugin-only screens (story_view, dm_reply) are viable
|
||||
# even without an explicit action config
|
||||
viable |= plugin_screens
|
||||
|
||||
return viable
|
||||
|
||||
def _is_plugin_active(self, plugin_cfg: dict) -> bool:
|
||||
"""Check if a plugin config represents an active plugin.
|
||||
|
||||
A plugin is active if:
|
||||
- It has `enabled: true` (explicit), OR
|
||||
- It has `percentage` > 0 (implicit enable), OR
|
||||
- It has any config keys and `enabled` is not explicitly False
|
||||
"""
|
||||
# Explicit disable
|
||||
if plugin_cfg.get("enabled") is False:
|
||||
return False
|
||||
|
||||
# Explicit enable
|
||||
if plugin_cfg.get("enabled") is True:
|
||||
return True
|
||||
|
||||
# Percentage-based: 0% means disabled
|
||||
pct = plugin_cfg.get("percentage")
|
||||
if pct is not None:
|
||||
try:
|
||||
return float(pct) > 0
|
||||
except (ValueError, TypeError):
|
||||
return False
|
||||
|
||||
# Has config keys but no explicit enabled/percentage = active
|
||||
return bool(plugin_cfg)
|
||||
|
||||
def _budget_for_screen(self, screen: str) -> int:
|
||||
"""Determine the post budget for a screen.
|
||||
|
||||
Reads from actions config (e.g. feed: "5-10") and parses
|
||||
the range string into a random integer within bounds.
|
||||
"""
|
||||
# Map screen back to action key
|
||||
reverse_map = {v: k for k, v in ACTION_SCREEN_MAP.items()}
|
||||
action_key = reverse_map.get(screen)
|
||||
|
||||
if action_key and action_key in self._actions:
|
||||
return _parse_range(self._actions[action_key])
|
||||
|
||||
# Special screens get fixed budgets from plugin config
|
||||
if screen == "StoriesFeed":
|
||||
story_cfg = self._plugins.get("story_view", {})
|
||||
count_str = story_cfg.get("count", "1-3")
|
||||
return _parse_range(str(count_str))
|
||||
|
||||
if screen == "MessageInbox":
|
||||
return DEFAULT_BUDGET
|
||||
|
||||
return DEFAULT_BUDGET
|
||||
|
||||
|
||||
def _parse_range(range_str: str) -> int:
|
||||
"""Parse a range string like '5-10' into a random int within bounds."""
|
||||
try:
|
||||
if "-" in str(range_str):
|
||||
parts = str(range_str).split("-")
|
||||
low, high = int(parts[0]), int(parts[1])
|
||||
return random.randint(low, high)
|
||||
return int(range_str)
|
||||
except (ValueError, IndexError):
|
||||
return DEFAULT_BUDGET
|
||||
@@ -99,6 +99,9 @@ class GrowthBrain:
|
||||
Autonomously selects the next strategic goal.
|
||||
If no goals are configured, falls back to legacy desires.
|
||||
Weights goals based on session success rates if provided.
|
||||
|
||||
.. deprecated::
|
||||
Use select_task() instead for concrete, plugin-linked task selection.
|
||||
"""
|
||||
import random
|
||||
|
||||
@@ -121,6 +124,33 @@ class GrowthBrain:
|
||||
|
||||
return random.choices(available_goals, weights=weights, k=1)[0]
|
||||
|
||||
def select_task(self, dopamine_engine, available_tasks: list) -> "Optional[Task]":
|
||||
"""Select the next concrete Task using weighted random selection.
|
||||
|
||||
This is the primary interface for the orchestrator. Unlike get_current_goal()
|
||||
which returns abstract strings, this returns a Task object with a specific
|
||||
target_screen, budget, and success metric.
|
||||
|
||||
Returns:
|
||||
Task: The selected task to execute.
|
||||
None: If no tasks available or boredom is too high (ShiftContext signal).
|
||||
"""
|
||||
if not available_tasks:
|
||||
return None
|
||||
|
||||
# High boredom = ShiftContext (take a break, switch feed)
|
||||
if dopamine_engine.boredom > 85.0:
|
||||
logger.info("🧠 [GrowthBrain] Boredom too high for task selection. ShiftContext.")
|
||||
return None
|
||||
|
||||
weights = [task.weight for task in available_tasks]
|
||||
selected = random.choices(available_tasks, weights=weights, k=1)[0]
|
||||
logger.info(
|
||||
f"🧠 [GrowthBrain] Selected task: {selected.verb} → {selected.target_screen} "
|
||||
f"(weight={selected.weight:.2f}, budget={selected.budget_posts})"
|
||||
)
|
||||
return selected
|
||||
|
||||
def get_circadian_pacing(self) -> float:
|
||||
"""
|
||||
Adjusts activity levels based on the current local time
|
||||
|
||||
@@ -21,6 +21,7 @@ If Instagram updates its app and moves a button, GramPilot doesn't crash. It fal
|
||||
## ✨ Core Features
|
||||
|
||||
* 🚫 **Zero Limits Configuration**: Forget about configuring "max_likes" or "delays". GramPilot uses a **Dopamine Pacing Engine** to simulate human boredom. If the content isn't interesting, it skips it or ends the session early.
|
||||
* 🎯 **Mission-Driven Navigation**: Say goodbye to abstract goal configurations. Define a `strategy` (like `aggressive_growth` or `nurture_community`) in `config.yml`, and the **Goal Decomposer Engine** automatically orchestrates the optimal routing and task allocation using enabled plugins.
|
||||
* ⚖️ **Active Inference (Shadow Mode)**: The bot continuously predicts the outcome of its clicks. If it lands on a popup instead of a profile, it registers a "Prediction Error", presses back, and dynamically recalibrates without panicking.
|
||||
* ⛩️ **Telepathic Engine**: A strictly tiered resolution cascade (Keyword -> Vectors -> LLM) that ensures 90% of navigation happens at 0-token cost while maintaining fallback AI resilience.
|
||||
* 🧬 **Resonance Oracle**: The bot only interacts with content that matches a pre-defined persona aesthetic, completely bypassing spam or low-quality content.
|
||||
|
||||
102
tests/e2e/test_goal_decomposer_e2e.py
Normal file
102
tests/e2e/test_goal_decomposer_e2e.py
Normal file
@@ -0,0 +1,102 @@
|
||||
"""
|
||||
E2E Test: Goal Decomposition and Autonomous Orchestration
|
||||
==========================================================
|
||||
|
||||
This test proves that the bot's core autonomy pipeline (Task Generation -> Selection -> Navigation)
|
||||
works end-to-end using real configuration objects and real UI XML dumps.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
from GramAddict.core.growth_brain import GrowthBrain
|
||||
from GramAddict.core.q_nav_graph import QNavGraph
|
||||
from GramAddict.core.telepathic_engine import TelepathicEngine
|
||||
|
||||
FIXTURE_DIR = os.path.join(os.path.dirname(__file__), "fixtures")
|
||||
|
||||
|
||||
def _load_fixture(name: str) -> str:
|
||||
path = os.path.join(FIXTURE_DIR, name)
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
class MockZeroEngine:
|
||||
"""Mock ZeroEngine needed by nav_graph to run actions without full active_inference logic."""
|
||||
|
||||
def __init__(self, device):
|
||||
self.device = device
|
||||
self.telepathic = TelepathicEngine()
|
||||
|
||||
def do(self, intent: str):
|
||||
# Simplistic execution for navigation
|
||||
xml = self.device.dump_hierarchy()
|
||||
node = self.telepathic.find_best_node(xml, intent, self.device)
|
||||
if node:
|
||||
# Just pretend we clicked
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@pytest.mark.live_llm
|
||||
class TestAutonomousOrchestrationE2E:
|
||||
"""End-to-End pipeline: Config -> GoalDecomposer -> GrowthBrain -> NavGraph -> UI XML"""
|
||||
|
||||
def test_e2e_mission_to_explore_feed_navigation(self, make_real_device_with_xml, monkeypatch):
|
||||
"""
|
||||
Simulates the bot_flow.py autonomous loop:
|
||||
1. Decomposer parses aggressive_growth mission.
|
||||
2. Brain selects ExploreFeed task.
|
||||
3. Orchestrator uses nav_graph to reach ExploreFeed.
|
||||
"""
|
||||
# 1. Setup simulated device with XML sequence
|
||||
home_xml = _load_fixture("home_feed_real.xml")
|
||||
explore_xml = _load_fixture("explore_grid_real.xml")
|
||||
|
||||
# Sequence for nav_graph.navigate_to("ExploreFeed")
|
||||
# We provide a long sequence of explore_xml at the end to simulate the app remaining on the Explore screen
|
||||
# after the transition.
|
||||
xml_sequence = [
|
||||
home_xml, # Initial perception (HomeFeed)
|
||||
home_xml, # finding explore button
|
||||
explore_xml, # post-click state (ExploreFeed)
|
||||
explore_xml, # Goal validation check
|
||||
] + [explore_xml] * 20
|
||||
|
||||
device = make_real_device_with_xml(xml_sequence)
|
||||
|
||||
# 2. Fake Config inputs
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
plugins = {"likes": {"percentage": 100}}
|
||||
actions = {"explore": "1-3"}
|
||||
|
||||
# 3. Generate Tasks
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
assert len(tasks) > 0, "Decomposer must generate tasks"
|
||||
|
||||
# Force selection of ExploreFeed for deterministic test
|
||||
monkeypatch.setattr(
|
||||
"random.choices", lambda population, weights, k: [t for t in population if t.target_screen == "ExploreFeed"]
|
||||
)
|
||||
|
||||
# 4. Brain Selection
|
||||
brain = GrowthBrain(username="testuser")
|
||||
|
||||
class MockDopamine:
|
||||
boredom = 0.0
|
||||
|
||||
selected_task = brain.select_task(MockDopamine(), tasks)
|
||||
assert selected_task is not None
|
||||
assert selected_task.target_screen == "ExploreFeed"
|
||||
|
||||
# 5. Execute Navigation (mimicking bot_flow.py)
|
||||
nav_graph = QNavGraph(device=device)
|
||||
zero_engine = MockZeroEngine(device)
|
||||
|
||||
success = nav_graph.navigate_to(selected_task.target_screen, zero_engine)
|
||||
|
||||
assert success is True, "Orchestrator failed to navigate to the decomposed Task's target screen!"
|
||||
402
tests/tdd/test_goal_decomposer.py
Normal file
402
tests/tdd/test_goal_decomposer.py
Normal file
@@ -0,0 +1,402 @@
|
||||
"""
|
||||
GoalDecomposer TDD Tests
|
||||
=========================
|
||||
|
||||
The GoalDecomposer takes mission config + enabled plugins and produces
|
||||
a weighted list of concrete Tasks. No LLM, no device, pure logic.
|
||||
|
||||
This is the bridge between "what do I want?" (mission) and
|
||||
"what can I do?" (plugins) → "what should I do now?" (Task).
|
||||
"""
|
||||
|
||||
|
||||
class TestGoalDecomposerGeneratesTasks:
|
||||
"""Tests that GoalDecomposer produces correct tasks from plugin config."""
|
||||
|
||||
def test_generates_feed_task_when_likes_enabled(self):
|
||||
"""If likes plugin is enabled and feed action is configured,
|
||||
the decomposer MUST produce a HomeFeed browsing task."""
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
plugins = {
|
||||
"likes": {"percentage": 100, "count": "2-3"},
|
||||
}
|
||||
actions = {"feed": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
feed_tasks = [t for t in tasks if t.target_screen == "HomeFeed"]
|
||||
assert len(feed_tasks) >= 1, "Must generate at least one HomeFeed task when likes + feed are enabled"
|
||||
assert feed_tasks[0].budget_posts > 0
|
||||
assert feed_tasks[0].weight > 0
|
||||
|
||||
def test_generates_explore_task_when_explore_configured(self):
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
plugins = {"likes": {"percentage": 100}}
|
||||
actions = {"explore": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
explore_tasks = [t for t in tasks if t.target_screen == "ExploreFeed"]
|
||||
assert len(explore_tasks) >= 1
|
||||
|
||||
def test_generates_story_task_when_story_view_enabled(self):
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
plugins = {"story_view": {"percentage": 80, "count": "1-3"}}
|
||||
actions = {"feed": "5-10"}
|
||||
mission = {"strategy": "community_builder"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
story_tasks = [t for t in tasks if t.target_screen == "StoriesFeed"]
|
||||
assert len(story_tasks) >= 1
|
||||
|
||||
def test_generates_no_tasks_when_no_plugins_enabled(self):
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
plugins = {}
|
||||
actions = {}
|
||||
mission = {"strategy": "passive_learning"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
assert len(tasks) == 0, "No enabled plugins = no tasks"
|
||||
|
||||
def test_task_weights_reflect_aggressive_growth_strategy(self):
|
||||
"""aggressive_growth should weight explore higher than home feed."""
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
plugins = {
|
||||
"likes": {"percentage": 100},
|
||||
"follow": {"percentage": 100},
|
||||
"story_view": {"percentage": 80},
|
||||
}
|
||||
actions = {"feed": "5-10", "explore": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
explore_weight = sum(t.weight for t in tasks if t.target_screen == "ExploreFeed")
|
||||
home_weight = sum(t.weight for t in tasks if t.target_screen == "HomeFeed")
|
||||
|
||||
assert (
|
||||
explore_weight > home_weight
|
||||
), f"aggressive_growth must weight explore ({explore_weight}) > home ({home_weight})"
|
||||
|
||||
def test_community_builder_weights_home_higher(self):
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
plugins = {
|
||||
"likes": {"percentage": 100},
|
||||
"story_view": {"percentage": 80},
|
||||
}
|
||||
actions = {"feed": "5-10", "explore": "5-10"}
|
||||
mission = {"strategy": "community_builder"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
home_weight = sum(t.weight for t in tasks if t.target_screen == "HomeFeed")
|
||||
explore_weight = sum(t.weight for t in tasks if t.target_screen == "ExploreFeed")
|
||||
|
||||
assert (
|
||||
home_weight > explore_weight
|
||||
), f"community_builder must weight home ({home_weight}) > explore ({explore_weight})"
|
||||
|
||||
def test_budget_parsed_from_range_string(self):
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
plugins = {"likes": {"percentage": 100}}
|
||||
actions = {"feed": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
feed_task = [t for t in tasks if t.target_screen == "HomeFeed"][0]
|
||||
assert 5 <= feed_task.budget_posts <= 10
|
||||
|
||||
def test_dm_task_generated_when_dm_reply_enabled(self):
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
plugins = {"dm_reply": {"enabled": True}}
|
||||
actions = {"feed": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
dm_tasks = [t for t in tasks if t.target_screen == "MessageInbox"]
|
||||
assert len(dm_tasks) >= 1
|
||||
|
||||
def test_task_has_required_fields(self):
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer, Task
|
||||
|
||||
plugins = {"likes": {"percentage": 100}}
|
||||
actions = {"feed": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
for task in tasks:
|
||||
assert isinstance(task, Task)
|
||||
assert task.verb, "Task must have a verb"
|
||||
assert task.target_screen, "Task must have a target_screen"
|
||||
assert task.intent, "Task must have a human-readable intent"
|
||||
assert task.weight > 0, "Task must have positive weight"
|
||||
|
||||
def test_disabled_plugin_produces_no_task(self):
|
||||
"""A plugin with enabled: false must not generate tasks."""
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
plugins = {
|
||||
"likes": {"percentage": 100},
|
||||
"dm_reply": {"enabled": False},
|
||||
}
|
||||
actions = {"feed": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
dm_tasks = [t for t in tasks if t.target_screen == "MessageInbox"]
|
||||
assert len(dm_tasks) == 0, "Disabled dm_reply must NOT produce MessageInbox task"
|
||||
|
||||
def test_zero_percentage_plugin_produces_no_task(self):
|
||||
"""A plugin with percentage: 0 must not generate tasks."""
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
plugins = {"follow": {"percentage": 0}}
|
||||
actions = {"feed": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
# follow with 0% should not create tasks on its own
|
||||
# but likes are not enabled either, so no tasks at all
|
||||
assert len(tasks) == 0
|
||||
|
||||
def test_all_task_target_screens_are_routable(self):
|
||||
"""Every target_screen a Task references must exist in ScreenTopology."""
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
from GramAddict.core.screen_topology import ScreenTopology
|
||||
|
||||
plugins = {
|
||||
"likes": {"percentage": 100},
|
||||
"follow": {"percentage": 100},
|
||||
"comment": {"percentage": 40},
|
||||
"story_view": {"percentage": 80},
|
||||
"dm_reply": {"enabled": True},
|
||||
}
|
||||
actions = {"feed": "5-10", "explore": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
for task in tasks:
|
||||
assert task.target_screen in ScreenTopology.SCREEN_NAME_MAP, (
|
||||
f"Task target_screen '{task.target_screen}' is not in ScreenTopology! "
|
||||
f"The bot cannot navigate there."
|
||||
)
|
||||
|
||||
|
||||
class TestGoalDecomposerFromConfig:
|
||||
"""Tests that GoalDecomposer correctly derives tasks from config-like dicts.
|
||||
Validates that the legacy `goals:` config is not needed."""
|
||||
|
||||
def test_decomposer_produces_tasks_without_goals(self):
|
||||
"""Tasks come from plugins + actions, NOT from goals list."""
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
plugins = {"likes": {"percentage": 100}}
|
||||
actions = {"feed": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
assert len(tasks) > 0, "Tasks must come from plugins, not goals"
|
||||
|
||||
def test_decomposer_accepts_empty_mission(self):
|
||||
"""If no mission is provided, default to aggressive_growth."""
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
|
||||
plugins = {"likes": {"percentage": 100}}
|
||||
actions = {"feed": "5-10"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission={})
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
assert len(tasks) > 0, "Empty mission must fall back to aggressive_growth"
|
||||
|
||||
|
||||
class TestGrowthBrainTaskSelection:
|
||||
"""Tests that GrowthBrain.select_task() picks from concrete Task objects."""
|
||||
|
||||
def test_select_task_returns_task_object(self):
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer, Task
|
||||
from GramAddict.core.growth_brain import GrowthBrain
|
||||
from GramAddict.core.dopamine_engine import DopamineEngine
|
||||
|
||||
plugins = {"likes": {"percentage": 100}, "story_view": {"percentage": 80}}
|
||||
actions = {"feed": "5-10", "explore": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
dopamine = DopamineEngine()
|
||||
|
||||
brain = GrowthBrain(username="test", persona_interests=[])
|
||||
selected = brain.select_task(dopamine, tasks)
|
||||
|
||||
assert isinstance(selected, Task), f"Expected Task, got {type(selected)}"
|
||||
assert selected in tasks
|
||||
|
||||
def test_select_task_returns_none_on_empty_tasks(self):
|
||||
from GramAddict.core.growth_brain import GrowthBrain
|
||||
from GramAddict.core.dopamine_engine import DopamineEngine
|
||||
|
||||
dopamine = DopamineEngine()
|
||||
brain = GrowthBrain(username="test", persona_interests=[])
|
||||
selected = brain.select_task(dopamine, [])
|
||||
|
||||
assert selected is None, "Empty task list must return None"
|
||||
|
||||
def test_select_task_returns_none_on_high_boredom(self):
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
from GramAddict.core.growth_brain import GrowthBrain
|
||||
from GramAddict.core.dopamine_engine import DopamineEngine
|
||||
|
||||
plugins = {"likes": {"percentage": 100}}
|
||||
actions = {"feed": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
dopamine = DopamineEngine()
|
||||
dopamine.boredom = 95.0
|
||||
|
||||
brain = GrowthBrain(username="test", persona_interests=[])
|
||||
selected = brain.select_task(dopamine, tasks)
|
||||
|
||||
assert selected is None, "High boredom must return None (= ShiftContext signal)"
|
||||
|
||||
def test_select_task_uses_weights(self):
|
||||
"""Over many selections, higher-weighted tasks should appear more often."""
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer, Task
|
||||
from GramAddict.core.growth_brain import GrowthBrain
|
||||
from GramAddict.core.dopamine_engine import DopamineEngine
|
||||
|
||||
plugins = {"likes": {"percentage": 100}, "story_view": {"percentage": 80}}
|
||||
actions = {"feed": "5-10", "explore": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
dopamine = DopamineEngine()
|
||||
brain = GrowthBrain(username="test", persona_interests=[])
|
||||
|
||||
# Run 100 selections and count
|
||||
counts: dict = {}
|
||||
for _ in range(100):
|
||||
selected = brain.select_task(dopamine, tasks)
|
||||
if selected:
|
||||
counts[selected.target_screen] = counts.get(selected.target_screen, 0) + 1
|
||||
|
||||
# With aggressive_growth, ExploreFeed (weight 0.45) should dominate
|
||||
assert len(counts) > 1, "Selection must pick from multiple screens"
|
||||
|
||||
|
||||
class TestOrchestratorTaskRouting:
|
||||
"""Tests that every Task produced by GoalDecomposer is routable by ScreenTopology."""
|
||||
|
||||
def test_task_to_screen_topology_mapping(self):
|
||||
"""Each Task.target_screen MUST exist in ScreenTopology.SCREEN_NAME_MAP."""
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
from GramAddict.core.screen_topology import ScreenTopology
|
||||
|
||||
plugins = {
|
||||
"likes": {"percentage": 100},
|
||||
"follow": {"percentage": 100},
|
||||
"comment": {"percentage": 40},
|
||||
"story_view": {"percentage": 80},
|
||||
"dm_reply": {"enabled": True},
|
||||
}
|
||||
actions = {"feed": "5-10", "explore": "5-10", "reels": "3-5"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
assert len(tasks) > 0
|
||||
|
||||
for task in tasks:
|
||||
assert task.target_screen in ScreenTopology.SCREEN_NAME_MAP, (
|
||||
f"Task '{task.verb}' → '{task.target_screen}' has no ScreenTopology mapping!"
|
||||
)
|
||||
|
||||
def test_all_task_screens_reachable_from_home(self):
|
||||
"""Every Task target must be reachable via BFS from HOME_FEED."""
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
from GramAddict.core.screen_topology import ScreenTopology
|
||||
from GramAddict.core.perception.screen_identity import ScreenType
|
||||
|
||||
plugins = {
|
||||
"likes": {"percentage": 100},
|
||||
"story_view": {"percentage": 80},
|
||||
"dm_reply": {"enabled": True},
|
||||
}
|
||||
actions = {"feed": "5-10", "explore": "5-10"}
|
||||
mission = {"strategy": "community_builder"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
for task in tasks:
|
||||
target_type = ScreenTopology.SCREEN_NAME_MAP.get(task.target_screen)
|
||||
assert target_type is not None
|
||||
|
||||
if target_type == ScreenType.HOME_FEED:
|
||||
continue # Already there
|
||||
|
||||
route = ScreenTopology.find_route(ScreenType.HOME_FEED, target_type)
|
||||
assert route is not None, (
|
||||
f"No HD Map route from HOME_FEED to {task.target_screen}! "
|
||||
f"The bot cannot navigate there."
|
||||
)
|
||||
|
||||
def test_task_target_screen_to_goal_string_conversion(self):
|
||||
"""Every task target_screen must convert to a valid GOAP goal string."""
|
||||
from GramAddict.core.goal_decomposer import GoalDecomposer
|
||||
from GramAddict.core.screen_topology import ScreenTopology
|
||||
|
||||
plugins = {"likes": {"percentage": 100}, "dm_reply": {"enabled": True}}
|
||||
actions = {"feed": "5-10", "explore": "5-10"}
|
||||
mission = {"strategy": "aggressive_growth"}
|
||||
|
||||
decomposer = GoalDecomposer(plugins=plugins, actions=actions, mission=mission)
|
||||
tasks = decomposer.generate_tasks()
|
||||
|
||||
for task in tasks:
|
||||
goal_str = ScreenTopology.screen_name_to_goal(task.target_screen)
|
||||
assert goal_str, (
|
||||
f"Task '{task.target_screen}' cannot be converted to GOAP goal!"
|
||||
)
|
||||
# The goal string should resolve back to a target screen
|
||||
target = ScreenTopology.goal_to_target_screen(goal_str)
|
||||
assert target is not None, (
|
||||
f"Goal string '{goal_str}' doesn't resolve to any ScreenType!"
|
||||
)
|
||||
|
||||
@@ -1,23 +1,30 @@
|
||||
def test_bot_flow_prioritizes_goals_over_desires():
|
||||
def test_bot_flow_uses_goal_decomposer_not_abstract_goals():
|
||||
"""
|
||||
Test that when goals are present in config, the bot uses GoalExecutor
|
||||
instead of the legacy desire mapping.
|
||||
This should fail (RED) before we refactor bot_flow.py.
|
||||
Test that bot_flow.py uses GoalDecomposer for task-based navigation
|
||||
instead of the abstract goals string system.
|
||||
|
||||
The old system passed abstract strings like "Nurture my community"
|
||||
to GoalExecutor.achieve() — which caused endless scrolling because
|
||||
the LLM had no semantic bridge to concrete plugin actions.
|
||||
|
||||
The new system:
|
||||
1. GoalDecomposer reads mission + plugins → generates concrete Tasks
|
||||
2. GrowthBrain.select_task() picks a Task with weighted random
|
||||
3. The Task's target_screen routes through nav_graph to feed loops
|
||||
"""
|
||||
|
||||
# We won't run the whole start_bot (it's massive),
|
||||
# we'll just test the core orchestrator loop extraction if we can,
|
||||
# or we can test the behavior by mocking the device and config.
|
||||
|
||||
# Actually, a better way is to test that the goal string is passed to achieve.
|
||||
# Since we can't easily mock the massive `start_bot`, we will test the
|
||||
# conceptual behavior by just ensuring the code in bot_flow contains
|
||||
# GoalExecutor.achieve logic.
|
||||
|
||||
# Let's import the file and check for GoalExecutor usage
|
||||
with open("GramAddict/core/bot_flow.py", "r") as f:
|
||||
content = f.read()
|
||||
|
||||
# This assertion will fail (RED) because GoalExecutor is not in the original bot_flow.py
|
||||
assert "GoalExecutor" in content, "bot_flow.py does not use GoalExecutor for autonomous goals"
|
||||
assert "goal_executor.achieve(current_goal)" in content, "bot_flow.py does not execute goals autonomously"
|
||||
# New system MUST be present
|
||||
assert "GoalDecomposer" in content, "bot_flow.py must use GoalDecomposer"
|
||||
assert "select_task" in content, "bot_flow.py must use GrowthBrain.select_task()"
|
||||
assert "available_tasks" in content, "bot_flow.py must generate available_tasks"
|
||||
|
||||
# Old abstract goals path MUST be gone
|
||||
assert "goal_executor.achieve(current_goal)" not in content, (
|
||||
"bot_flow.py still uses old abstract goal_executor.achieve()! "
|
||||
"This causes endless scrolling."
|
||||
)
|
||||
assert 'getattr(configs.args, "goals"' not in content, (
|
||||
"bot_flow.py still reads abstract goals from config!"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user