Skip to content

Conversation

@seanmcguire12
Copy link
Member

@seanmcguire12 seanmcguire12 commented Dec 1, 2025

why


Summary by cubic

Refactors actHandler to centralize LLM action parsing and execution, reduce duplication, and improve metrics reporting. Behavior stays the same, with clearer naming and more reliable two-step and fallback flows.

Why:

  • Reduce duplicated LLM calls and normalization logic.
  • Improve readability and maintainability.
  • Ensure consistent metrics and variable substitution.
  • Make the self-heal/fallback path more robust.

What:

  • Renamed actFromObserveResult to takeDeterministicAction and updated all call sites (ActCache, AgentCache, v3).
  • Added getActionFromLLM for inference, metrics, normalization, and variable substitution.
  • Added recordActMetrics to centralize ACT metrics reporting.
  • Extracted normalizeActInferenceElement and substituteVariablesInArguments helpers.
  • Simplified two-step act flow and fallback retry using shared helpers.
  • Kept existing behavior (selector normalization, variable substitution, retries).

Test Plan:

  • Run unit tests for actHandler to confirm no regressions.
  • Verify single-step actions execute as before.
  • Verify two-step flow triggers when LLM returns twoStep and executes the second action.
  • Confirm fallback self-heal path updates selector and retries successfully.
  • Check metrics are recorded once per inference call in both steps and fallback.
  • Validate variable substitution replaces %key% tokens in action arguments.
  • Exercise AgentCache and ActCache paths to ensure takeDeterministicAction works end-to-end.
  • Build passes and type checks for all renamed method references.

Written for commit 08d8454. Summary will update automatically on new commits.

@changeset-bot
Copy link

changeset-bot bot commented Dec 1, 2025

⚠️ No Changeset found

Latest commit: 08d8454

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@seanmcguire12 seanmcguire12 force-pushed the seanmcguire/stg-1020-refactor-acthandlerts branch from 8d8e569 to 08d8454 Compare December 5, 2025 00:35
@seanmcguire12 seanmcguire12 marked this pull request as ready for review December 5, 2025 01:27
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 5, 2025

Greptile Overview

Greptile Summary

Refactors the ActHandler class to centralize LLM action parsing and execution logic, reducing code duplication and improving maintainability.

  • Renamed actFromObserveResult to takeDeterministicAction and updated all call sites (ActCache, AgentCache, v3.ts)
  • Extracted getActionFromLLM helper that handles inference, metrics recording, normalization, and variable substitution in one place
  • Added recordActMetrics to centralize ACT metrics reporting
  • Created normalizeActInferenceElement and substituteVariablesInArguments as pure utility functions
  • Simplified two-step act flow and self-heal/fallback retry paths to use shared helpers
  • Existing behavior is preserved, including selector normalization, variable substitution, and retry logic

Confidence Score: 5/5

  • This is a pure refactoring PR with no functional changes - safe to merge
  • The changes are purely structural refactoring that centralizes duplicated logic into helper functions. The renamed method takeDeterministicAction is updated at all call sites. The new helper functions (getActionFromLLM, normalizeActInferenceElement, substituteVariablesInArguments) extract existing inline logic without modifying behavior. Variable substitution, selector normalization, metrics reporting, and self-heal retry logic all remain functionally identical to the previous implementation.
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
packages/core/lib/v3/handlers/actHandler.ts 5/5 Refactored to centralize LLM action parsing via getActionFromLLM, recordActMetrics, normalizeActInferenceElement, and substituteVariablesInArguments helpers. Renamed actFromObserveResult to takeDeterministicAction. No functional changes, cleaner code.
packages/core/lib/v3/cache/ActCache.ts 5/5 Updated method call from actFromObserveResult to takeDeterministicAction to match the renamed method in ActHandler.
packages/core/lib/v3/cache/AgentCache.ts 5/5 Updated two method calls from actFromObserveResult to takeDeterministicAction to match the renamed method in ActHandler.
packages/core/lib/v3/v3.ts 5/5 Updated method call from actFromObserveResult to takeDeterministicAction to match the renamed method in ActHandler.

Sequence Diagram

sequenceDiagram
    participant V3 as V3.act()
    participant AH as ActHandler
    participant LLM as getActionFromLLM
    participant TDA as takeDeterministicAction
    participant Page as Page/DOM

    V3->>AH: act(instruction)
    AH->>Page: waitForDomNetworkQuiet()
    AH->>Page: captureHybridSnapshot()
    Page-->>AH: combinedTree, combinedXpathMap
    AH->>LLM: getActionFromLLM(instruction, domElements)
    LLM->>LLM: actInference()
    LLM->>LLM: recordActMetrics()
    LLM->>LLM: normalizeActInferenceElement()
    LLM->>LLM: substituteVariablesInArguments()
    LLM-->>AH: {action, response}
    AH->>TDA: takeDeterministicAction(action)
    TDA->>Page: performUnderstudyMethod()
    
    alt twoStep === true
        AH->>Page: captureHybridSnapshot()
        AH->>AH: diffCombinedTrees()
        AH->>LLM: getActionFromLLM(stepTwoInstructions)
        LLM-->>AH: {secondAction}
        AH->>TDA: takeDeterministicAction(secondAction)
        TDA->>Page: performUnderstudyMethod()
    end

    alt Self-heal on error
        TDA->>Page: captureHybridSnapshot()
        TDA->>LLM: getActionFromLLM(fallback)
        LLM-->>TDA: {fallbackAction}
        TDA->>Page: performUnderstudyMethod(newSelector)
    end

    AH-->>V3: ActResult
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

Copy link
Collaborator

@miguelg719 miguelg719 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@miguelg719 miguelg719 merged commit 0e24d23 into main Dec 5, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants