Skip to content

fix(find): reject ambiguous find get text and get attrs - #4699

Open
xiatiandeairen wants to merge 4 commits into
callstack:mainfrom
xiatiandeairen:fix/find-read-ambiguity
Open

xiatiandeairen wants to merge 4 commits into
callstack:mainfrom
xiatiandeairen:fix/find-read-ambiguity

Conversation

@xiatiandeairen

@xiatiandeairen xiatiandeairen commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

find <q> get text|attrs silently took the first of several matches. It now follows mutating find's #1625 contract:

To make that work:

  • The read capture is now unscoped, because a scoped capture holds only the first match (CONSERVATIVE:).
  • The daemon now forwards --first and --last to the read-only runtime.
  • The printed candidates follow the strict-read issuance rule, so they come out pinned.
  • Help, the selectors docs and the policy comments are updated.

16 files.

Fixes #4685

Blocked by #4698. Merge it first: an unscoped read publishes a full frame, which would let a plain ref from an earlier snapshot -i resolve to the wrong node. #4698 refuses that read. With both branches combined, tests pass (30/30), and help workflow is 9,099 bytes against the 9,100 budget.

Validation

Head 363f5eb9d on base aa600e980.

  • check:quick passed. Focused vitest: 2,656 tests pass. The new policy, --last and candidate-issuance tests fail on base.
  • check:affected --run --head 363f5eb9d: format, lint, typecheck, layering, fallow, build and vitest-related passed. unit failed only in android-sdk-setup-action.test.ts, which fails the same way on base on this host. docs-links and docs-prose passed when run directly.
  • iOS 27.2 simulator, MultiTodo:
  • Android was not run.

🤖 Generated with Claude Code


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

View guided diff

@thymikee

thymikee commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

I reviewed 363f5eb and found one problem that needs a fix before merge. CI is green: the one reported check passes. There are no conflicts.

Value reads and mutating find count different candidate sets. findValueReadMatch counts and narrows the raw list from captureFindMatches. Mutating find (find-match-resolution.ts:39-60) first removes root interaction containers, then applies preferOnscreenMatches, and only then counts matches or applies --first/--last. On the same tree the two routes disagree in two cases. First, when the query also matches the Application node label (for example find "<app name>" get text, or any locator on an iOS app root), click resolves the one real element, but get text refuses with the app root as a candidate, and get text --first reads the root. Second, with one onscreen and one offscreen match, find X click acts on the onscreen one, while find X get text refuses, and --first/--last pick by document order instead of onscreen ranking.

The PR and the issue promise the same contract as mutating find. As it stands, an agent can run find X get text --first, then find X click --first, and read one element while tapping another. It can also get AMBIGUOUS_MATCH on a read where the click on the same query is unambiguous. The rule to satisfy: every find action that binds one node counts and narrows the same candidate set. The root-container filter and preferOnscreenMatches ordering should live in @agent-device/selectors, beside narrowFindMatches, and both resolveFindMatch and findValueReadMatch should call them. preferOnscreenMatches only depends on selectors/interaction-targeting and kernel, so the move should be small. Please add a test with an Application-labelled root or an offscreen duplicate that fails on the current head.

Would one selectors helper, findOneNodeCandidates(nodes, locator, query, selectorExpression, platform), be enough? It would return the rooted, onscreen-preferred, ranked list that resolveFindMatch builds today, and both callers would then pass it to narrowFindMatches. That would keep a single contract. The rest of the PR is already minimal, and each added line is used.

Not blocking, and fine to take or leave: merge #4698 first, then rebase and re-run the combined suite as the PR body says, because value reads now store an unscoped full capture with one ref and a plain @ref from an earlier snapshot -i would resolve against the newer tree until then; the daemon issuance test in selector-runtime-ambiguity-issuance.test.ts stubs selectors.find with a hand-written AppError, so it would be stronger driven through the real find command or built with elementMatchCandidateDetails(nodes); every find get text|attrs now pays an unscoped full-tree capture, including unique-match reads, so please state that cost in the PR or show a timing on a large tree; and the readAny comments in resolution-policy.ts and selector-pipeline-policy.ts should say find <q> exists|wait, since find wait still resolves each poll through readAny.

Evidence limits: the iOS live run comes from the PR body. Android and web value reads were not run by anyone, and the unscoped capture cost on Android is unmeasured. The mismatch above is traced from code, not observed live. findFirst/findLast are declared recorded: false, as before,. Whether a recorded find X get text --first replays with the flag is not checked yet. The combined-branch claim (30/30 tests, help budget 9,099/9,100) is unverified while #4698 is open. Once the shared candidate set and its failing-on-head test land, this can merge after #4698.

A find value read binds one node, so it now shares the mutating find
contract from callstack#1625: several matches fail with AMBIGUOUS_MATCH and the
candidate list unless --first or --last picks one. The capture is no
longer scoped to the first match, the daemon forwards --first/--last to
the read-only runtime, and the printed candidates follow the strict-read
issuance rule. exists, wait, and list keep their semantics.

Fixes callstack#4685
find <q> get text|attrs counted the raw match list, while mutating find
first drops the root interaction container and prefers on-screen
matches. On the same tree the two could disagree: a read refused or
read the app root where a click bound one element, and --first/--last
picked by document order instead of find's ranking.

findOneNodeCandidates in @agent-device/selectors now builds the rooted,
on-screen-preferred, ranked candidate list, and both resolveFindMatch
and findValueReadMatch count and narrow it. preferOnscreenMatches moves
from the daemon into the same module.
@xiatiandeairen
xiatiandeairen force-pushed the fix/find-read-ambiguity branch from 363f5eb to 1953103 Compare November 19, 2026 06:10
@xiatiandeairen

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Pushed 1953103, rebased on main after #4698 merged.

The blocking issue: one candidate set. I added findOneNodeCandidates({ nodes, locator, query, selectorExpression, platform }) in a new @agent-device/selectors/find-candidates subpath, as you suggested. It returns the rooted, on-screen-preferred, ranked list that resolveFindMatch built before. resolveFindMatch and findValueReadMatch both count it and narrow it with narrowFindMatches. preferOnscreenMatches moved into the same module from the daemon, and its test moved with it. find <q> list still enumerates the whole readList set.

Tests that fail on the old head. selector-read-find-list.test.ts has two new cases:

  • An Application root with the same label as a button: get text reads the button. At 363f5eb it fails.
  • An on-screen and an off-screen duplicate: get text reads the on-screen node, and --last cannot reach the off-screen one. At 363f5eb it fails.

I checked both by swapping the old selector-read.ts back in.

Two behavior changes:

  1. --first/--last on a value read now pick the ends of find's ranked list, not document order. In selector-read-policy.test.ts, --first on the ambiguous fixture now returns the smaller tiebreak winner (@e3), so I swapped that test's expectations.
  2. Value reads now use the findAct row (requireRect: true, occlusion: 'refuse'). A match without a rect, or an occluded match, is no longer a read candidate. A click could not bind it either, so this follows the rule that both routes count the same set. It does mean that a read of a rectless text-only node now reports no match. If you want to keep those readable, tell me and I will handle that in the shared helper rather than in one caller.

Also in this push: the readAny comments now say find <q> exists|wait, the policy comments point value reads at findAct, and package-boundaries.test.ts lists the new subpath.

Checks run locally: command-runtime, selectors and daemon/execution unit tests pass (1769). The layering tests pass (483/483), and so do lint and the format check. I did not run the full check:affected. Android and web value reads are still not run live. I have not done the other non-blocking items yet: driving the daemon issuance test through the real find command, and timing the unscoped capture.

@thymikee

Copy link
Copy Markdown
Member

I reviewed 1953103 against the earlier findings at 363f5eb (#4699 (comment)). The ambiguity refusal for find get text and get attrs is now in place, but the change adds a regression on web, and the value-read route has no live proof at this head.

findOneNodeCandidates hard-codes the findAct row with requireRect: true (find-candidates.ts:33), and findValueReadMatch now uses it for get text and get attrs (selector-read.ts:281). On web, agent-browser gives static text lines no ref and no rect (agent-browser-snapshot.ts:83-88), so find "Order total" get text is dropped by the rect check in findBestMatchesByLocator. A selector-shaped query is cut by the hasSnapshotRef filter. On main (6e18ea3) the read path used requireRect: false and read the node, and refAddress still exists only to serve such matches. The result is a false COMMAND_FAILED "find did not match any element" for on-screen non-interactive text, while find ... list, wait text and snapshot all show the node. Other platforms are affected the same way for any rectless node. This is traced from the code, not observed live. The rule: a find action that binds one node must count every match that action can bind. Root-container exclusion and on-screen ranking stay shared, and addressability comes from the action's row. Could findOneNodeCandidates take the pipeline row, with findAct for click, fill, focus and type, and a new read row with requireRect: false and occlusion ignore for get text and get attrs? Please add a command-runtime test with a ref-less, rectless text node from the backend-issued fixture. find <text> get text must read it, and the test must fail on this head.

This delta also changes the live value-read route: which nodes are candidates, their order, and what --first and --last pick (selector-read.ts:272). The only live run in the PR body is iOS from an earlier head, and Android and web value reads were not run. After the fix above, please run the branch-built CLI and paste the JSON for two checks. First, on iOS or Android, find <text with two on-screen matches> get text must return AMBIGUOUS_MATCH with candidate refs and refsGeneration, and ... get text --last must return the same ref that find <same> click --last would tap. Second, on web, find "<static text>" get text must return the text of a non-interactive node.

On CI, Smoke Tests failed in live-automation-scenario.ts:50. The snapshot after click id="automation-continue-catalog" still shows the Automation lab screen, where wait text Catalog already matches the "Continue to catalog" button, so the snapshot raced the navigation. That route does not go through resolveFindMatch, findOneNodeCandidates or the find value read, so it looks unrelated to this diff. Please re-run it. The Coverage job log is cut off before any failing test or threshold line, so its cause is unclear. Please post the failure summary, or fix it if it is a changed-file or changed-line threshold.

One earlier question is still open: does a recorded find X get text --first replay with the flag, given that findFirst and findLast are recorded as false?

There are no conflicts. Before merge, value reads must keep reading rectless and ref-less matches with a test that fails on this head. Then we need the live run at the new head, and the Coverage failure either fixed or shown to be unrelated.

The eager-closure budget rejected find-candidates.ts: as a new subpath
it added a module to command-runtime's closure, and its static edge to
the selectors entry made it evaluate 67 modules where the ranking code
it replaced evaluated 15.

preferOnscreenMatches and its ranking helpers now live in
interaction-targeting, whose functions they already called, and
findOneNodeCandidates lives in selector-pipeline, which both callers
already import. Callers pass the findAct row, typed to that row, so
selector-pipeline does not pull in the policy table. The ranking test
is renamed for its module.

Rollback: reverting this commit restores find-candidates.ts, its
./find-candidates export and the package-boundaries entry; the eager
closure budget then fails again until the module is re-homed.
@xiatiandeairen

Copy link
Copy Markdown
Contributor Author

I pushed b7ec11e. It fixes the Coverage failure on 1953103.

Cause. eager-closure-budgets.test.ts rejected the new find-candidates.ts for two reasons:

  • As a new subpath, it added one module to command-runtime/src/index.ts's closure (173 → 174).
  • Its static edges to the selectors entry and the policy table made it evaluate 67 modules. The daemon ranking file it replaced evaluated 15.

Change. The module is gone, and the same code moved into modules that both callers already evaluate:

  • preferOnscreenMatches and its ranking helpers now live in interaction-targeting.ts, whose functions they already called. Its test is renamed to interaction-targeting-find-ranking.test.ts.
  • findOneNodeCandidates now lives in selector-pipeline.ts, which both resolveFindMatch and findValueReadMatch already import.
  • The callers now pass the row: pipeline: SELECTOR_PIPELINE_POLICIES.findAct. Reading the policy table from inside selector-pipeline.ts took that module from 26 to 66 evaluated modules. The parameter type is (typeof SELECTOR_PIPELINE_POLICIES)['findAct'], and the import is type-only, so a caller cannot pass another row: passing readList fails tsc with TS2739. Both routes still count the same set.
  • I also removed the ./find-candidates export and its package-boundaries.test.ts entry.

Checks run locally:

  • eager-closure budgets: 909/909 pass. They failed 2 tests before this push.
  • pnpm typecheck passes.
  • The command-runtime, selectors and daemon/execution unit tests pass (1769).
  • Lint and format pass.
  • The layering tests (483/483) and scan pass.

Android smoke. It failed on 1953103 in live-automation-scenario.ts:50: the catalog snapshot had no Settings tab node. That step runs snapshot -i, not find, and the same workflow also failed on #4696, which changes only apple-runner. So I think it is unrelated, but I have not proved that. I will check the run on this head.

@thymikee

Copy link
Copy Markdown
Member

The CI checks are green at b7ec11e (12 checks, none failing), and the eager-closure Coverage failure from the earlier review is fixed. The main finding from that review is still open, and one test and one live run are missing.

findOneNodeCandidates now takes only the findAct row, and findValueReadMatch passes findAct, which requires a rect and a snapshot ref. On web, agent-browser lines without a backend ref get no rect, so find "Order total" get text on static text now fails with "find did not match any element". On main (6e18ea3) the read used requireRect: false and returned the node. Value reads of on-screen non-interactive text regress to a false no-match, while find list, wait text and snapshot still show the node. This is traced from the code, not observed live. The rule: a find action that binds one node must count every match that action can bind, with root exclusion and on-screen ranking shared, and addressability taken from the action's row. Could findOneNodeCandidates accept a one-node pipeline row, using findAct for click/fill/focus/type and a new read row with requireRect: false and occlusion ignore for get text and get attrs? Please pass the read row from findValueReadMatch and keep the policy import type-only so the closure budget is unaffected.

No test in selector-read-find-list.test.ts reads a ref-less, rectless text node through find get text. All five tests use nodes with refs and rects, so this regression passes CI. Please add a command-runtime test with a backend-issued ref-less, rectless text node where find <text> get text returns its text. It must fail on b7ec11e.

The value-read route changed candidate set, order and --first/--last behavior since the only live run (iOS at 363f5eb), and no live JSON is posted for this head. After the fix, please run the branch-built CLI and paste JSON for two cases. First, on iOS or Android, find <text with two on-screen matches> get text should return AMBIGUOUS_MATCH with candidate refs and refsGeneration, and get text --last should return the same ref that find <same> click --last taps. Second, on web, find "<static text>" get text should return the text of a non-interactive, ref-less node.

One earlier question is still open: does a recorded find X get text --first replay with the flag, given that findFirst/findLast are recorded as false?

No conflicts are known. Before merge, value reads need a read row with requireRect: false, plus a test that fails on b7ec11e and live JSON on both platforms at the new head.

findValueReadMatch passed the findAct row, which requires a rect and a ref,
so on web `find "<static text>" get text` refused a node that `find list`,
`wait text` and `snapshot` all show. Add a findRead row (reject-candidates,
requireRect: false, occlusion ignore) and let findOneNodeCandidates take
either row, so reads and taps still share root exclusion and on-screen
ranking while addressability comes from the action's row.
@xiatiandeairen

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Status at 4efbb210c:

Rectless / ref-less reads. Added a findRead row (reject-candidates, requireRect: false, occlusion ignore); findOneNodeCandidates takes findAct | findRead (type-only import) and findValueReadMatch passes findRead. New test runtime find get text reads a ref-less, rectless text node fails on b7ec11e with find did not match any element and passes here.

Validation: check:affected --run --base 6e18ea334 --head 4efbb210c passed format, lint, typecheck, layering, fallow, build and vitest-related. unit failed only in test/repo-guards/android-sdk-setup-action.test.ts (3 tests read this host's real Android SDK path), which this PR does not touch, as before. Two host notes: with the default base (current main, 127 commits ahead) fallow sat at 0% CPU for 29 min, so I passed the merge base; and layering's fixture commits were blocked by my global commit-msg hook, so the run set core.hooksPath=/dev/null.

Live run, iOS 27.2 simulator, MultiTodo with two "dup" rows, CLI and daemon built from 4efbb210c.

find dup get text:

{"code":"AMBIGUOUS_MATCH","details":{"matches":2,"candidates":["@e15 [text] \"dup\"","@e11 [text] \"dup\""],"refsGeneration":421699,"dispatched":"no"}}

--last does not bind the same row yet:

  • find dup get text --last → @e11, rect.y 214.8 (first row)
  • find dup click --last → Tapped @e13 (201, 277) (second row)

Cause: rankInteractiveMatches orders equal-score matches by rect area, then input order (packages/selectors/src/interaction-targeting.ts:493). The two text nodes have heights 20.333333333333343 and 20.333333333333314, so their areas are 603.2222222222223 and 603.2222222222215; the second row sorts first and --last lands on the first. The click capture matched the two cells, whose areas are exactly equal, so it fell through to input order. The same comparison exists on main (daemon/.../find-match-ranking.ts:50), used only by mutating find; routing reads through it exposed it. I'd treat areas within a small epsilon as equal so same-size siblings keep tree order, with a test. Should that go in this PR or separately?

Replay question. It does not keep the flag. Recorded with open --save-script, then find dup get text --first (returned @e15):

open "dev.agentverify.MultiTodo"
find "dup" "get" "text"
close

Replaying it fails: REPLAY_DIVERGENCE at step 2, find matched 2 elements for any "dup". findFirst/findLast are recorded: false (command-registry/src/flag-definitions-workflow.ts:298-305), so a recorded ambiguous read that used to replay as first-match now refuses. Marking them recorded: true would fix it but changes recording for every find action; happy to do it here if you want it.

Web not run yet: this host has no managed web backend (TOOL_MISSING, needs agent-device web setup). I'll post the static-text JSON once it runs.

@xiatiandeairen

Copy link
Copy Markdown
Contributor Author

Web value read at 4efbb210c, CLI and daemon built from that commit, managed agent-browser 0.39.0, a local page with <p>Order total $42.00</p> next to a heading and a button.

The snapshot gives the line no ref, and find "Order total" list shows it with ref: null and rect: null. find "Order total" get text --json reads it:

{"success":true,"data":{"text":"Order total $42.00","node":{"index":1,"type":"StaticText","role":"StaticText","value":"Order total $42.00","depth":1,"parentIndex":0,"kind":"text"}}}

Exit 0. I did not re-run this page at b7ec11e; the failing-on-head evidence for this case is the new command-runtime test.

@thymikee

Copy link
Copy Markdown
Member

Thanks for the update. The earlier findings from b7ec11e are addressed at 4efbb21, and one regression remains.

Recorded find ... get text|attrs --first/--last does not replay with its flag. findFirst and findLast are declared recorded:false, while this PR now makes value reads honor those flags and refuse when they are absent. A session that records find dup get text --first saves find "dup" "get" "text". Replay then hits AMBIGUOUS_MATCH, which surfaces as REPLAY_DIVERGENCE at that step. The live iOS simulator run confirmed this at 4efbb21. On the base, the same recording replays with a first-match read, so this is a regression on the replay route, and the issue's --first completion condition fails there.

The rule to satisfy: a positional choice that resolved a find action at record time must be replayed with that choice. Mark findFirst and findLast as recorded:true in the flag definition, which is the one place that enumerates every recorded find action. Then add a record-then-replay test for find X get text --first. This also fixes the same lost choice for mutating find. If the wider recording change does not belong in this PR, should it move to a separate issue? That needs explicit maintainer agreement.

Not blocking, and these can be taken or left: rankInteractiveMatches in interaction-targeting.ts breaks ties on exact float area, so iOS rows of 20.333333333333343 and 20.333333333333314 can reorder (a follow-up could compare areas within an epsilon, with a test for heights about 1e-14 apart, and until then the two "bind the same node" comments in selector-read.ts and selector-pipeline.ts could say "count the same candidate set"), and findRead in selector-pipeline-policy.ts declares offscreen, promotion and poll stages that its only consumer never runs, so should it be typed as a SelectorListPolicy?

The code needs the fix above before merge. The only failing check is Body rules, which reads only the PR body (258 words over the 250 limit, and no '## Before / after' section), so it is likely unrelated to the diff. The other 14 checks pass, and there are no conflicts. Not verified here: the new command-runtime test was not run on b7ec11e or at head, the live iOS and web output is the author's paste and was not reproduced, and Android was not run. After the replay fix lands, please refresh the PR body with validation at 4efbb21, a Before / after section and under 250 words so Body rules passes.

@thymikee

Copy link
Copy Markdown
Member

4efbb21 now conflicts with main. Please rebase onto main and include the open item from the last review (recorded find ... get text|attrs --first/--last replaying with its flag) in the same push.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

find <q> get text / get attrs still take the first of several matches (follow-up to #1625)

2 participants