Skip to content

fix(extension): resolve link element IDs in plain string fields - #3363

Open
lhy-dcf wants to merge 1 commit into
browserbase:mainfrom
lhy-dcf:fix/resolve-plain-string-url-ids
Open

lhy-dcf wants to merge 1 commit into
browserbase:mainfrom
lhy-dcf:fix/resolve-plain-string-url-ids

Conversation

@lhy-dcf

@lhy-dcf lhy-dcf commented Nov 1, 2026 •

Copy link
Copy Markdown

Closes #3234

Problem

extract() can return accessibility-tree element IDs instead of URLs when a
schema uses a plain z.string() field for a link.

The extract system prompt (packages/extension/prompt.ts) asks the model to
return link element IDs rather than hrefs, but injectUrls in utils.ts only
resolves IDs for schema paths that are marked as URL fields
(z.string().url()). A plain z.string() field carrying a link therefore
leaks the raw ID ("0-74", or "[0-74]\n[0-112]" for multi-link strings) to
the caller.

Change

After the existing .url() resolution, walk the extracted value with a new
resolveUrlIds helper and replace any bare or bracketed element ID that is
actually present in the snapshot's URL map (idToUrlMapping). IDs that are not
real links are left untouched, so a value like a score of "3-2" is preserved
exactly as requested in the issue.

Verification

  • Added a mock unit test in extract.test.ts covering plain-string resolution,
    .url() fields, bracketed multi-ID strings, and the "3-2" non-link case.
  • The resolveUrlIds helper was exercised in an isolated vitest run (8 cases
    pass) plus a strict tsc --noEmit before submission.
  • Note on scope: the repo-wide extract.test.ts could not be executed in this
    environment (no full monorepo checkout is possible here), but it is a
    pure-mock test (vi.fn stubs for captureSnapshot/generate, no real
    browser) and is covered by CI's unit-test job.

Known trade-off

If a plain string field legitimately contains a value that happens to equal a
real link element ID from the page's accessibility tree, it will be resolved to
that link's href. This is the intended behaviour described in the issue (the
discriminator is "the ID is present in the snapshot's URL map"), but it is a
semantic edge case worth surfacing to maintainers.

AI use

Drafted with an AI coding agent; the isolated helper test and strict typecheck
were run locally before submission.


Summary by cubic

Fixes extract() returning raw accessibility-tree element IDs like "0-74" instead of URLs when a link lands in a plain z.string() field. Link IDs are now resolved to hrefs via the snapshot's URL map, while non-link values like a score of "3-2" stay unchanged.

Details

  • The extract prompt asks the model to return link IDs for plain string fields, but only z.string().url() paths were resolved beforehand.
  • A new resolveUrlIds helper recursively walks the extracted value and replaces bare or bracketed IDs present in the URL map.
  • Adds a mock test covering plain strings, .url() fields, bracketed multi-ID strings, and the "3-2" non-link case.

Written for commit ac375bd. Summary will update on new commits.

View guided diff

The extract prompt asks the model to return link element IDs, but
`injectUrls` only resolves IDs for `z.string().url()` schema paths. Plain
`z.string()` fields carrying a link leak the raw accessibility-tree ID
(e.g. "0-74" or "[0-74]\n[0-112]") to the caller.

Add `resolveUrlIds`, which walks the extracted value after the existing
`.url()` resolution and replaces any bare or bracketed ID that is actually
present in the snapshot's URL map. IDs that are not real links (a score of
"3-2") are left unchanged.

Closes browserbase#3234
@changeset-bot

changeset-bot Bot commented Nov 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ac375bd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@browserbasehq/stagehand-extension Patch

Not sure what this means? Click here to learn what changesets are.

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

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run.
Approving the latest commit mirrors it into an internal PR owned by the approver.
If new commits are pushed later, the internal PR stays open but is marked stale until someone approves the latest external commit and refreshes it.

@github-actions github-actions Bot added external-contributor Tracks PRs mirrored from external contributor forks. external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. labels Nov 1, 2026
@lhy-dcf

lhy-dcf commented Nov 13, 2026

Copy link
Copy Markdown
Author

Checking in on this one — the failing link-ID lookup is still present on main, and the branch is green. Happy to add a regression test or narrow the diff if that helps it get reviewed; no action needed if it is already on your list.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. external-contributor Tracks PRs mirrored from external contributor forks.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

extract() can return element IDs instead of URLs in plain string fields

1 participant