-
Notifications
You must be signed in to change notification settings - Fork 893
[fix] ensure selfHeal respects arguments #897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
miguelg719
merged 6 commits into
main
from
miguel/stg-562-fix-bug-with-self-heal-forgetting-arguments
Jul 25, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
837b889
ensure selfHeal respects arguments
miguelg719 b691e41
added evals
miguelg719 048c119
changeset
miguelg719 c0d2c99
add % signs to arg in eval
seanmcguire12 47b15ee
throw error if element node not found by xpath
seanmcguire12 31e36c4
trim prepended 'xpath=' before downstream method helpers
seanmcguire12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@browserbasehq/stagehand": patch | ||
--- | ||
|
||
Fix selfHeal to remember intially received arguments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ const StagehandConfig = { | |
}, | ||
}, | ||
}, | ||
selfHeal: true, | ||
}; | ||
|
||
/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { EvalFunction } from "@/types/evals"; | ||
|
||
export const heal_custom_dropdown: EvalFunction = async ({ | ||
debugUrl, | ||
sessionUrl, | ||
stagehand, | ||
logger, | ||
}) => { | ||
/** | ||
* This eval is meant to test whether we do not incorrectly attempt | ||
* the selectOptionFromDropdown method (defined in actHandlerUtils.ts) on a | ||
* 'dropdown' that is not a <select> element. | ||
* | ||
* This kind of dropdown must be clicked to be expanded before being interacted | ||
* with. | ||
*/ | ||
|
||
try { | ||
const page = stagehand.page; | ||
await page.goto( | ||
"https://browserbase.github.io/stagehand-eval-sites/sites/expand-dropdown/", | ||
); | ||
|
||
await page.act({ | ||
description: "The dropdown", | ||
selector: "/html/not-a-dropdown", | ||
arguments: [], | ||
method: "click", | ||
}); | ||
|
||
// we are expecting stagehand to click the dropdown to expand it, | ||
// and therefore the available options should now be contained in the full | ||
// a11y tree. | ||
|
||
// to test, we'll grab the full a11y tree, and make sure it contains 'Canada' | ||
const extraction = await page.extract(); | ||
const fullTree = extraction.page_text; | ||
|
||
if (fullTree.includes("Canada")) { | ||
return { | ||
_success: true, | ||
debugUrl, | ||
sessionUrl, | ||
logs: logger.getLogs(), | ||
}; | ||
} | ||
return { | ||
_success: false, | ||
message: "unable to expand the dropdown", | ||
debugUrl, | ||
sessionUrl, | ||
logs: logger.getLogs(), | ||
}; | ||
} catch (error) { | ||
return { | ||
_success: false, | ||
message: `error attempting to select an option from the dropdown: ${error.message}`, | ||
debugUrl, | ||
sessionUrl, | ||
logs: logger.getLogs(), | ||
}; | ||
} finally { | ||
await stagehand.close(); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { EvalFunction } from "@/types/evals"; | ||
|
||
export const heal_scroll_50: EvalFunction = async ({ | ||
debugUrl, | ||
sessionUrl, | ||
stagehand, | ||
logger, | ||
}) => { | ||
try { | ||
await stagehand.page.goto( | ||
"https://browserbase.github.io/stagehand-eval-sites/sites/aigrant/", | ||
); | ||
await stagehand.page.act({ | ||
description: "the element to scroll on", | ||
selector: "/html/body/div/div/button", | ||
arguments: ["50%"], | ||
method: "scrollTo", | ||
}); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 5000)); | ||
|
||
// Get the current scroll position and total scroll height | ||
const scrollInfo = await stagehand.page.evaluate(() => { | ||
return { | ||
scrollTop: window.scrollY + window.innerHeight / 2, | ||
scrollHeight: document.documentElement.scrollHeight, | ||
}; | ||
}); | ||
|
||
const halfwayScroll = scrollInfo.scrollHeight / 2; | ||
const halfwayReached = | ||
Math.abs(scrollInfo.scrollTop - halfwayScroll) <= 200; | ||
const evaluationResult = halfwayReached | ||
? { | ||
_success: true, | ||
logs: logger.getLogs(), | ||
debugUrl, | ||
sessionUrl, | ||
} | ||
: { | ||
_success: false, | ||
logs: logger.getLogs(), | ||
debugUrl, | ||
sessionUrl, | ||
message: `Scroll position (${scrollInfo.scrollTop}px) is not halfway down the page (${halfwayScroll}px).`, | ||
}; | ||
|
||
return evaluationResult; | ||
} catch (error) { | ||
return { | ||
_success: false, | ||
error: error, | ||
logs: logger.getLogs(), | ||
debugUrl, | ||
sessionUrl, | ||
}; | ||
} finally { | ||
await stagehand.close(); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { EvalFunction } from "@/types/evals"; | ||
|
||
export const heal_simple_google_search: EvalFunction = async ({ | ||
debugUrl, | ||
sessionUrl, | ||
stagehand, | ||
logger, | ||
}) => { | ||
try { | ||
await stagehand.page.goto( | ||
"https://browserbase.github.io/stagehand-eval-sites/sites/google/", | ||
); | ||
|
||
await stagehand.page.act({ | ||
description: "The search bar", | ||
selector: "/html/not-the-search-bar", | ||
arguments: ["OpenAI"], | ||
method: "fill", | ||
}); | ||
|
||
await stagehand.page.act("click the google search button"); | ||
|
||
const expectedUrl = | ||
"https://browserbase.github.io/stagehand-eval-sites/sites/google/openai.html"; | ||
const currentUrl = stagehand.page.url(); | ||
|
||
return { | ||
_success: currentUrl.startsWith(expectedUrl), | ||
currentUrl, | ||
debugUrl, | ||
sessionUrl, | ||
logs: logger.getLogs(), | ||
}; | ||
} catch (error) { | ||
return { | ||
_success: false, | ||
error: error, | ||
debugUrl, | ||
sessionUrl, | ||
logs: logger.getLogs(), | ||
}; | ||
} finally { | ||
await stagehand.close(); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,10 +147,35 @@ export class StagehandActHandler { | |
: method | ||
? `${method} ${observe.description}` | ||
: observe.description; | ||
// Call act with the ObserveResult description | ||
return await this.stagehandPage.act({ | ||
action: actCommand, | ||
const instruction = buildActObservePrompt( | ||
actCommand, | ||
Object.values(SupportedPlaywrightAction), | ||
{}, | ||
); | ||
miguelg719 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const observeResults = await this.stagehandPage.observe({ | ||
instruction, | ||
}); | ||
if (observeResults.length === 0) { | ||
return { | ||
success: false, | ||
message: `Failed to self heal act: No observe results found for action`, | ||
action: actCommand, | ||
}; | ||
} | ||
const element: ObserveResult = observeResults[0]; | ||
await this._performPlaywrightMethod( | ||
// override previously provided method and arguments | ||
observe.method, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will also be overriden |
||
observe.arguments, | ||
// only update selector | ||
element.selector, | ||
domSettleTimeoutMs, | ||
); | ||
return { | ||
success: true, | ||
message: `Action [${element.method}] performed successfully on selector: ${element.selector}`, | ||
action: observe.description || `ObserveResult action (${method})`, | ||
miguelg719 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} catch (err) { | ||
this.logger({ | ||
category: "action", | ||
|
@@ -282,9 +307,10 @@ export class StagehandActHandler { | |
private async _performPlaywrightMethod( | ||
method: string, | ||
args: unknown[], | ||
xpath: string, | ||
rawXPath: string, | ||
domSettleTimeoutMs?: number, | ||
) { | ||
const xpath = rawXPath.replace(/^xpath=/i, "").trim(); | ||
const locator = deepLocator(this.stagehandPage.page, xpath).first(); | ||
const initialUrl = this.stagehandPage.page.url(); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seanmcguire12 👀, 👍 / 👎 ?