Speed optimizations: Economy element tree and TOTP context parsing skip (#3936)

This commit is contained in:
pedrohsdb
2025-11-06 21:56:52 -08:00
committed by GitHub
parent 44528cbd38
commit d8631151ba
4 changed files with 131 additions and 11 deletions

View File

@@ -2066,6 +2066,27 @@ class ForgeAgent:
# If we don't have pre-scraped data, scrape normally
if scraped_page is None:
# Check PostHog for speed optimizations BEFORE scraping
# This decision will be used in both:
# 1. SVG conversion skip (in agent_functions.py cleanup)
# 2. Tree selection (economy vs regular tree)
# By checking once and storing in context, we ensure perfect coordination
if context:
try:
distinct_id = task.workflow_run_id if task.workflow_run_id else task.task_id
context.enable_speed_optimizations = await app.EXPERIMENTATION_PROVIDER.is_feature_enabled_cached(
"ENABLE_SPEED_OPTIMIZATIONS",
distinct_id,
properties={"organization_id": task.organization_id},
)
except Exception:
LOG.warning(
"Failed to check ENABLE_SPEED_OPTIMIZATIONS feature flag",
exc_info=True,
task_id=task.task_id,
)
context.enable_speed_optimizations = False
# start the async tasks while running scrape_website
if engine not in CUA_ENGINES:
self.async_operation_pool.run_operation(task.task_id, AgentPhase.scrape)
@@ -2113,7 +2134,51 @@ class ForgeAgent:
)
# TODO: we only use HTML element for now, introduce a way to switch in the future
element_tree_format = ElementTreeFormat.HTML
element_tree_in_prompt: str = scraped_page.build_element_tree(element_tree_format)
# OPTIMIZATION: Use economy tree (skip SVGs) when ENABLE_SPEED_OPTIMIZATIONS is enabled
# Economy tree removes all SVG elements from the DOM tree sent to LLM
# - SVGs are decorative (icons, logos, graphics) - not needed for action planning
# - Even for charts/graphs: LLM sees them in screenshots, not SVG code
# - Saves ~8s per SVG x ~15 SVGs = ~120s per workflow (30% speedup!)
#
# RETRY STRATEGY: Use economy tree on first attempt only
# - retry_index 0: Use economy tree (fast, no SVGs)
# - retry_index 1+: Use regular tree (SVGs loaded from existing 4-week cache)
# Note: SVG conversions are already cached globally with 4-week TTL, so retries are fast
#
# COORDINATION: The enable_speed_optimizations decision is made ONCE before scraping
# and stored in context. Both SVG conversion skip (agent_functions.py) and tree
# selection (here) use the SAME value, ensuring perfect coordination.
element_tree_in_prompt: str = ""
# Use the speed optimization decision from context (set before scraping)
enable_speed_optimizations = context.enable_speed_optimizations if context else False
if not enable_speed_optimizations:
# Optimization disabled - use regular tree always
element_tree_in_prompt = scraped_page.build_element_tree(element_tree_format)
elif step.retry_index == 0:
# First attempt - use economy tree (fast, no SVG conversion)
# Note: SVG conversion was already skipped in cleanup_element_tree_func
# based on the same context.enable_speed_optimizations value
element_tree_in_prompt = scraped_page.build_economy_elements_tree(element_tree_format)
LOG.info(
"Speed optimization: Using economy element tree (skipping SVGs)",
step_order=step.order,
step_retry=step.retry_index,
task_id=task.task_id,
workflow_run_id=task.workflow_run_id,
)
else:
# Retry 1+ - use regular tree (SVGs will be loaded from existing 4-week cache)
element_tree_in_prompt = scraped_page.build_element_tree(element_tree_format)
LOG.info(
"Speed optimization: Using regular tree on retry (SVGs from global cache)",
step_order=step.order,
step_retry=step.retry_index,
task_id=task.task_id,
workflow_run_id=task.workflow_run_id,
)
extract_action_prompt = ""
if engine not in CUA_ENGINES:
extract_action_prompt, use_caching = await self._build_extract_action_prompt(