-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(agent): fillForm tool drops caller-provided values for LLM-hallucinated ones #1807
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
Open
shrey150
wants to merge
8
commits into
main
Choose a base branch
from
fix/fillform-value-hallucination
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6b1bd6a
fix: use caller-provided values in fillForm instead of LLM-hallucinat…
shrey150 ca9e6ab
fix: skip extra fills from observe and log warning
shrey150 b223c55
style: format test file with prettier
shrey150 8fcaa8e
style: simplify test file comment to match repo conventions
shrey150 c36ccd0
style: drop issue ref from test describe block
shrey150 61f963b
test: replace PII in test fixtures with generic values
shrey150 118fc02
refactor: remove redundant undefined guard on required field value
shrey150 86eba36
fix: warn when observe returns fewer fills than provided fields
shrey150 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 fillForm tool using LLM-hallucinated values instead of caller-provided values |
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 |
|---|---|---|
|
|
@@ -56,7 +56,22 @@ export const fillFormTool = ( | |
|
|
||
| const completed = [] as unknown[]; | ||
| const replayableActions: Action[] = []; | ||
| let fillIndex = 0; | ||
| for (const res of observeResults) { | ||
| if (res.method === "fill") { | ||
| if (fillIndex < fields.length) { | ||
| res.arguments = [fields[fillIndex].value]; | ||
| fillIndex++; | ||
| } else { | ||
| v3.logger({ | ||
| category: "agent", | ||
| message: `fillForm: observe returned more fill actions than provided fields (${fields.length}); skipping extra fill`, | ||
| level: 1, | ||
| }); | ||
| continue; | ||
| } | ||
| } | ||
shrey150 marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
60
to
+73
Collaborator
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 assumes agent will provide the values in same order as the observe results are returned in .This likely will not be consistently true. |
||
|
|
||
| const actOptions = variables | ||
| ? { variables, timeout: toolTimeout } | ||
| : { timeout: toolTimeout }; | ||
|
|
@@ -66,6 +81,13 @@ export const fillFormTool = ( | |
| replayableActions.push(...(actResult.actions as Action[])); | ||
| } | ||
| } | ||
| if (fillIndex < fields.length) { | ||
| v3.logger({ | ||
| category: "agent", | ||
| message: `fillForm: observe returned fewer fill actions (${fillIndex}) than provided fields (${fields.length}); ${fields.length - fillIndex} field value(s) were not applied`, | ||
| level: 1, | ||
| }); | ||
| } | ||
| v3.recordAgentReplayStep({ | ||
| type: "fillForm", | ||
| fields, | ||
|
|
||
191 changes: 191 additions & 0 deletions
191
packages/core/tests/unit/fillform-value-override.test.ts
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,191 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { fillFormTool } from "../../lib/v3/agent/tools/fillform.js"; | ||
| import type { V3 } from "../../lib/v3/v3.js"; | ||
|
|
||
| /** | ||
| * Minimal mock of V3 that captures arguments passed to act(). | ||
| */ | ||
| function createMockV3( | ||
| observeResults: Array<{ | ||
| method: string; | ||
| arguments: string[]; | ||
| elementId?: string; | ||
| description?: string; | ||
| }>, | ||
| ) { | ||
| const actCalls: Array<{ | ||
| method: string; | ||
| arguments: string[]; | ||
| }> = []; | ||
|
|
||
| const mock = { | ||
| logger: vi.fn(), | ||
| recordAgentReplayStep: vi.fn(), | ||
| act: vi.fn(async (res: { method: string; arguments: string[] }) => { | ||
| actCalls.push({ method: res.method, arguments: [...res.arguments] }); | ||
| return { | ||
| success: true, | ||
| message: "ok", | ||
| actionDescription: "done", | ||
| actions: [], | ||
| }; | ||
| }), | ||
| observe: vi.fn(async () => observeResults), | ||
| actCalls, | ||
| }; | ||
|
|
||
| return mock as unknown as V3 & { actCalls: typeof actCalls }; | ||
| } | ||
|
|
||
| const toolCtx = { | ||
| toolCallId: "t1", | ||
| messages: [] as never[], | ||
| abortSignal: new AbortController().signal, | ||
| }; | ||
|
|
||
| describe("fillForm value override", () => { | ||
| it("uses caller-provided values instead of LLM-hallucinated values", async () => { | ||
| // observe() returns hallucinated placeholder values | ||
| const v3 = createMockV3([ | ||
| { | ||
| method: "fill", | ||
| arguments: ["test@example.com"], // hallucinated! | ||
| elementId: "0-395", | ||
| description: "email field", | ||
| }, | ||
| { | ||
| method: "fill", | ||
| arguments: ["password123"], // hallucinated! | ||
| elementId: "0-396", | ||
| description: "password field", | ||
| }, | ||
| ]); | ||
|
|
||
| const tool = fillFormTool(v3); | ||
| await tool.execute!( | ||
| { | ||
| fields: [ | ||
| { | ||
| action: "type email into email field", | ||
| value: "user@example.org", | ||
| }, | ||
| { | ||
| action: "type password into password field", | ||
| value: "s3cret!", | ||
| }, | ||
| ], | ||
| }, | ||
| toolCtx, | ||
| ); | ||
|
|
||
| // With the fix applied, act() should receive the REAL values | ||
| expect(v3.actCalls[0].arguments).toEqual(["user@example.org"]); | ||
| expect(v3.actCalls[1].arguments).toEqual(["s3cret!"]); | ||
| }); | ||
|
|
||
| it("does not override non-fill methods (e.g. click)", async () => { | ||
| const v3 = createMockV3([ | ||
| { | ||
| method: "click", | ||
| arguments: [], | ||
| elementId: "0-100", | ||
| description: "click submit", | ||
| }, | ||
| ]); | ||
|
|
||
| const tool = fillFormTool(v3); | ||
| await tool.execute!( | ||
| { | ||
| fields: [{ action: "click submit button", value: "ignored" }], | ||
| }, | ||
| toolCtx, | ||
| ); | ||
|
|
||
| // click method should NOT have its arguments overridden | ||
| expect(v3.actCalls[0].arguments).toEqual([]); | ||
| }); | ||
|
|
||
| it("handles interleaved non-fill actions (fillIndex alignment)", async () => { | ||
| // observe() returns a click between two fills | ||
| const v3 = createMockV3([ | ||
| { method: "click", arguments: [], description: "focus email" }, | ||
| { method: "fill", arguments: ["placeholder1"], description: "email" }, | ||
| { method: "fill", arguments: ["placeholder2"], description: "password" }, | ||
| ]); | ||
|
|
||
| const tool = fillFormTool(v3); | ||
| await tool.execute!( | ||
| { | ||
| fields: [ | ||
| { action: "type email", value: "real@email.com" }, | ||
| { action: "type password", value: "realPass" }, | ||
| ], | ||
| }, | ||
| toolCtx, | ||
| ); | ||
|
|
||
| // The fix should use a fillIndex counter so: | ||
| // - click at i=0 is skipped | ||
| // - first fill maps to fields[0].value | ||
| // - second fill maps to fields[1].value | ||
| expect(v3.actCalls[0].arguments).toEqual([]); // click unchanged | ||
| expect(v3.actCalls[1].arguments).toEqual(["real@email.com"]); | ||
| expect(v3.actCalls[2].arguments).toEqual(["realPass"]); | ||
| }); | ||
|
|
||
| it("handles empty string value (clearing a field)", async () => { | ||
| const v3 = createMockV3([ | ||
| { | ||
| method: "fill", | ||
| arguments: ["hallucinated"], | ||
| description: "search box", | ||
| }, | ||
| ]); | ||
|
|
||
| const tool = fillFormTool(v3); | ||
| await tool.execute!( | ||
| { | ||
| fields: [{ action: "clear the search box", value: "" }], | ||
| }, | ||
| toolCtx, | ||
| ); | ||
|
|
||
| // Empty string is a valid value (clearing a field) — | ||
| // it should NOT fall back to the hallucinated value | ||
| expect(v3.actCalls[0].arguments).toEqual([""]); | ||
| }); | ||
|
|
||
| it("skips extra fills when observe returns more fills than fields", async () => { | ||
| const v3 = createMockV3([ | ||
| { method: "fill", arguments: ["hal1"], description: "email" }, | ||
| { method: "fill", arguments: ["hal2"], description: "password" }, | ||
| { method: "fill", arguments: ["hal3"], description: "confirm password" }, | ||
| ]); | ||
|
|
||
| const tool = fillFormTool(v3); | ||
| await tool.execute!( | ||
| { | ||
| fields: [ | ||
| { action: "type email", value: "real@email.com" }, | ||
| { action: "type password", value: "realPass" }, | ||
| ], | ||
| }, | ||
| toolCtx, | ||
| ); | ||
|
|
||
| // Only the two matched fills should be acted on | ||
| expect(v3.actCalls).toHaveLength(2); | ||
| expect(v3.actCalls[0].arguments).toEqual(["real@email.com"]); | ||
| expect(v3.actCalls[1].arguments).toEqual(["realPass"]); | ||
|
|
||
| // Warning should be logged for the skipped fill | ||
| expect(v3.logger).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| category: "agent", | ||
| message: expect.stringContaining( | ||
| "more fill actions than provided fields", | ||
| ), | ||
| }), | ||
| ); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.