Recover browse navigation when the focused object dies unseen

When a page removed the focused object before any navigation adopted it
(Steam's loading spinner after a popup closes), every browse key failed
with "Could not find current location" until focus moved natively. The
dead focus was never a saved place, so the existing anchor fallback did
not apply. Recapture through the most recent retained documents of the
same provider (at most two) and keep that document's reading place.

Document start and end now choose again once when their target died
before placement; they are idempotent, unlike relative moves.

Verified live in Steam Big Picture and ChatGPT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Storm DragonandClaude Opus 5.5 committed 2026-12-01 02:52:44 -05:00
1 parent 87b6aa49da
commit 7b4fca03bf
5 files changed
+234 -12

No files matched your search

+12 -4
View File
@@ -99,7 +99,15 @@ items spoke in focus mode. Automated: `TestSteamOverlayFocusMatchesForegroundPro
`TestToastDocument`, and the native `TestNativeWebScriptNotificationCapture`. `TestToastDocument`, and the native `TestNativeWebScriptNotificationCapture`.
The toast restriction has not yet been checked against a real toast. The toast restriction has not yet been checked against a real toast.
Known remaining issue: when Steam replaces page content (for example the Popup documents are announced by name ("Quick Access", "Main Menu") rather
RECOMMENDED tab), browse-mode navigation can fail with "Could not find than Steam's internal `QuickAccess_uid2`, and Quick Access tabs without an
current location" because the remembered native focus no longer exists and aria-label (Friends) are named from their `quickaccess_tab_<n>` id.
`DocumentCursor.syncFocus` does not fall back to the reading place.
When Steam replaces page content, the remembered native focus can die before
any navigation adopts it (its loading spinner after a popup closes). The review
worker then recaptures through the most recent retained document of the same
provider and keeps that document's reading place, instead of failing with
"Could not find current location". Document start and end choose again once
if their target died before placement. Verified live on 2026-12-01 in Steam
and ChatGPT; covered by `TestDeadFocusNeverSavedRecoversThroughRetainedDocument`
and `TestDocumentStartChoosesAgainWhenFirstObjectDies`.
+14
View File
@@ -150,6 +150,20 @@ func (cursor *DocumentCursor) captureAnchor(focus atspi.ObjectID) (atspi.ObjectI
return document, true return document, true
} }
// savedDocument reports the document of the most recent saved place.
func (cursor *DocumentCursor) savedDocument() (atspi.ObjectID, bool) {
if cursor == nil {
return atspi.ObjectID{}, false
}
cursor.mu.Lock()
defer cursor.mu.Unlock()
if len(cursor.places) == 0 {
return atspi.ObjectID{}, false
}
document := cursor.places[len(cursor.places)-1].document.ID()
return document, document.Valid()
}
func NewDocumentCursor(tree *atspi.AccessibleTree) (*DocumentCursor, error) { func NewDocumentCursor(tree *atspi.AccessibleTree) (*DocumentCursor, error) {
if tree == nil { if tree == nil {
return nil, errors.New("create document cursor: nil accessible tree") return nil, errors.New("create document cursor: nil accessible tree")
+1 -1
View File
@@ -252,7 +252,7 @@ func TestDocumentRecoveryKeepsVirtualPlaceWhenNativeAnchorDisappears(t *testing.
t.Fatal(err) t.Fatal(err)
} }
result := Result{DocumentFocus: webID("/text")} result := Result{DocumentFocus: webID("/text")}
if !worker.recoverCapturedDocumentFocus(cursor, cursor.tree, &result) { if !worker.recoverCapturedDocumentFocus(cursor, cursor.tree, &result, false) {
t.Fatal("removed native anchor was not recovered") t.Fatal("removed native anchor was not recovered")
} }
if _, err := cursor.SyncFocus(ctx, result.DocumentFocus); err != nil { if _, err := cursor.SyncFocus(ctx, result.DocumentFocus); err != nil {
+84 -4
View File
@@ -198,6 +198,40 @@ func (worker *Worker) captureDocumentForMove(ctx context.Context, cursor *Docume
return fallbackTree, duration + fallbackDuration, nil return fallbackTree, duration + fallbackDuration, nil
} }
// maxDeadFocusDocuments bounds the documents tried for one dead focus, so an
// unresponsive provider cannot multiply the interactive capture budget.
const maxDeadFocusDocuments = 2
// captureRetainedDocumentForDeadFocus handles a focus that died before any
// navigation adopted it, such as Steam's loading spinner after a popup
// closes. captureAnchor cannot help because the dead focus was never a saved
// place. Try the most recently used retained documents of the same provider;
// the first one that still exists becomes the anchor. A live focus never gets
// here: capture keyed on it succeeds or fails with another error.
func (worker *Worker) captureRetainedDocumentForDeadFocus(ctx context.Context, current *DocumentCursor, focus atspi.ObjectID, loadEpoch uint64) (*atspi.AccessibleTree, *DocumentCursor, time.Duration, bool) {
var total time.Duration
tried := 0
for _, retained := range worker.retainedCursorsExcept(current, loadEpoch) {
document, ok := retained.savedDocument()
if !ok || document.BusName != focus.BusName || document == focus {
continue
}
if tried == maxDeadFocusDocuments {
break
}
tried++
captureContext, cancel := context.WithTimeout(ctx, worker.documentCaptureTimeout)
started := time.Now()
tree, err := worker.capturer.CaptureDocument(captureContext, document)
total += time.Since(started)
cancel()
if err == nil && tree != nil {
return tree, retained, total, true
}
}
return nil, nil, total, false
}
func (worker *Worker) moveDocument(ctx context.Context, cursor *DocumentCursor, target DocumentTarget, level int, backwards bool, epoch, loadEpoch uint64) (Result, *DocumentCursor) { func (worker *Worker) moveDocument(ctx context.Context, cursor *DocumentCursor, target DocumentTarget, level int, backwards bool, epoch, loadEpoch uint64) (Result, *DocumentCursor) {
return worker.moveDocumentRepeated(ctx, cursor, target, level, backwards, epoch, loadEpoch, 1) return worker.moveDocumentRepeated(ctx, cursor, target, level, backwards, epoch, loadEpoch, 1)
} }
@@ -261,6 +295,15 @@ func (worker *Worker) moveDocumentRepeated(ctx context.Context, cursor *Document
result.CaptureReason = result.Err result.CaptureReason = result.Err
result.DocumentDiagnostics.CaptureAttempts++ result.DocumentDiagnostics.CaptureAttempts++
tree, captureDuration, err := worker.captureDocumentForMove(ctx, cursor, result.DocumentFocus) tree, captureDuration, err := worker.captureDocumentForMove(ctx, cursor, result.DocumentFocus)
adopted := false
if errors.Is(err, atspi.ErrObjectNotFound) {
if retainedTree, retained, retainedDuration, ok := worker.captureRetainedDocumentForDeadFocus(ctx, cursor, result.DocumentFocus, loadEpoch); ok {
tree, err, cursor, adopted = retainedTree, nil, retained, true
captureDuration += retainedDuration
} else {
captureDuration += retainedDuration
}
}
result.CaptureDuration = captureDuration result.CaptureDuration = captureDuration
if err != nil { if err != nil {
result.Err = err result.Err = err
@@ -270,7 +313,7 @@ func (worker *Worker) moveDocumentRepeated(ctx context.Context, cursor *Document
result.Err = errors.New("capture document tree: nil tree") result.Err = errors.New("capture document tree: nil tree")
return result, cursor return result, cursor
} }
recovered := worker.recoverCapturedDocumentFocus(cursor, tree, &result) recovered := worker.recoverCapturedDocumentFocus(cursor, tree, &result, adopted)
if cursor == nil || cursor.tree != tree { if cursor == nil || cursor.tree != tree {
retained, created, _ := worker.cursorForTree(tree, loadEpoch) retained, created, _ := worker.cursorForTree(tree, loadEpoch)
result.DocumentDiagnostics.CursorRecreated = created result.DocumentDiagnostics.CursorRecreated = created
@@ -395,6 +438,31 @@ func documentCaretNeedsRecapture(err error) bool {
} }
func (worker *Worker) moveDocumentCaret(ctx context.Context, cursor *DocumentCursor, command DocumentCaretCommand, epoch, loadEpoch uint64) (Result, *DocumentCursor) { func (worker *Worker) moveDocumentCaret(ctx context.Context, cursor *DocumentCursor, command DocumentCaretCommand, epoch, loadEpoch uint64) (Result, *DocumentCursor) {
result, cursor := worker.moveDocumentCaretOnce(ctx, cursor, command, epoch, loadEpoch)
if command != CaretDocumentStart && command != CaretDocumentEnd || !errors.Is(result.Err, atspi.ErrDeadObject) ||
!worker.CurrentDocumentResult(result) || worker.focus() != result.DocumentFocus {
return result, cursor
}
// The selected first or last object died before placement and is absent
// from the fresh tree. Unlike relative moves, start and end name the same
// place however often they run, so choosing again cannot skip content.
tree, captureDuration, err := worker.captureDocumentForMove(ctx, cursor, result.DocumentFocus)
if err != nil || tree == nil {
return result, cursor
}
if cursor == nil || cursor.tree != tree {
cursor, _, err = worker.cursorForTree(tree, loadEpoch)
if err != nil {
return result, cursor
}
}
retry, cursor := worker.moveDocumentCaretOnce(ctx, cursor, command, epoch, loadEpoch)
retry.CaptureDuration += result.CaptureDuration + captureDuration
retry.DocumentDiagnostics.CaptureAttempts += result.DocumentDiagnostics.CaptureAttempts + 1
return retry, cursor
}
func (worker *Worker) moveDocumentCaretOnce(ctx context.Context, cursor *DocumentCursor, command DocumentCaretCommand, epoch, loadEpoch uint64) (Result, *DocumentCursor) {
result := Result{DocumentEpoch: epoch, DocumentCaretCommand: command, DocumentFocus: worker.focus()} result := Result{DocumentEpoch: epoch, DocumentCaretCommand: command, DocumentFocus: worker.focus()}
if !result.DocumentFocus.Valid() { if !result.DocumentFocus.Valid() {
result.Err = ErrNoCurrent result.Err = ErrNoCurrent
@@ -456,6 +524,15 @@ func (worker *Worker) moveDocumentCaret(ctx context.Context, cursor *DocumentCur
result.CaptureReason = result.Err result.CaptureReason = result.Err
result.DocumentDiagnostics.CaptureAttempts++ result.DocumentDiagnostics.CaptureAttempts++
tree, captureDuration, err := worker.captureDocumentForMove(ctx, cursor, result.DocumentFocus) tree, captureDuration, err := worker.captureDocumentForMove(ctx, cursor, result.DocumentFocus)
adopted := false
if errors.Is(err, atspi.ErrObjectNotFound) {
if retainedTree, retained, retainedDuration, ok := worker.captureRetainedDocumentForDeadFocus(ctx, cursor, result.DocumentFocus, loadEpoch); ok {
tree, err, cursor, adopted = retainedTree, nil, retained, true
captureDuration += retainedDuration
} else {
captureDuration += retainedDuration
}
}
result.CaptureDuration = captureDuration result.CaptureDuration = captureDuration
if err != nil { if err != nil {
result.Err = err result.Err = err
@@ -465,7 +542,7 @@ func (worker *Worker) moveDocumentCaret(ctx context.Context, cursor *DocumentCur
result.Err = errors.New("capture document tree: nil tree") result.Err = errors.New("capture document tree: nil tree")
return result, cursor return result, cursor
} }
recovered := worker.recoverCapturedDocumentFocus(cursor, tree, &result) recovered := worker.recoverCapturedDocumentFocus(cursor, tree, &result, adopted)
if cursor == nil || cursor.tree != tree { if cursor == nil || cursor.tree != tree {
retained, created, _ := worker.cursorForTree(tree, loadEpoch) retained, created, _ := worker.cursorForTree(tree, loadEpoch)
result.DocumentDiagnostics.CursorRecreated = created result.DocumentDiagnostics.CursorRecreated = created
@@ -665,7 +742,10 @@ func (worker *Worker) refreshDocumentCursorWithRecovery(ctx context.Context, cur
return fresh, nil return fresh, nil
} }
func (worker *Worker) recoverCapturedDocumentFocus(cursor *DocumentCursor, tree *atspi.AccessibleTree, result *Result) bool { // adopted reports that the dead focus was never part of cursor's saved place
// and cursor's document was chosen by captureRetainedDocumentForDeadFocus. The
// saved place then becomes the anchor, so its reading position is kept.
func (worker *Worker) recoverCapturedDocumentFocus(cursor *DocumentCursor, tree *atspi.AccessibleTree, result *Result, adopted bool) bool {
if cursor == nil || len(cursor.places) == 0 { if cursor == nil || len(cursor.places) == 0 {
return false return false
} }
@@ -688,7 +768,7 @@ func (worker *Worker) recoverCapturedDocumentFocus(cursor *DocumentCursor, tree
if cursor.tree == tree { if cursor.tree == tree {
cursor.mu.Lock() cursor.mu.Lock()
place := &cursor.places[len(cursor.places)-1] place := &cursor.places[len(cursor.places)-1]
if place.document == root && place.focus.ID() == result.DocumentFocus { if place.document == root && (adopted || place.focus.ID() == result.DocumentFocus) {
place.focus = root place.focus = root
} }
cursor.mu.Unlock() cursor.mu.Unlock()
+123 -3
View File
@@ -604,12 +604,12 @@ func TestRecoverRemovedDocumentFocusStaysInActiveDocument(t *testing.T) {
} }
worker.SyncFocus(webID("/first")) worker.SyncFocus(webID("/first"))
result := Result{DocumentFocus: webID("/first"), CaptureReason: atspi.ErrObjectNotFound} result := Result{DocumentFocus: webID("/first"), CaptureReason: atspi.ErrObjectNotFound}
if !worker.recoverCapturedDocumentFocus(cursor, tree, &result) || result.DocumentFocus != webID("/doc") || worker.focus() != webID("/doc") { if !worker.recoverCapturedDocumentFocus(cursor, tree, &result, false) || result.DocumentFocus != webID("/doc") || worker.focus() != webID("/doc") {
t.Fatalf("same-document recovery failed: %+v, focus %v", result, worker.focus()) t.Fatalf("same-document recovery failed: %+v, focus %v", result, worker.focus())
} }
worker.SyncFocus(webID("/chrome")) worker.SyncFocus(webID("/chrome"))
result.DocumentFocus = webID("/first") result.DocumentFocus = webID("/first")
if worker.recoverCapturedDocumentFocus(cursor, tree, &result) || worker.focus() != webID("/chrome") { if worker.recoverCapturedDocumentFocus(cursor, tree, &result, false) || worker.focus() != webID("/chrome") {
t.Fatal("recovery displaced browser chrome focus") t.Fatal("recovery displaced browser chrome focus")
} }
worker.SyncFocus(webID("/first")) worker.SyncFocus(webID("/first"))
@@ -619,7 +619,7 @@ func TestRecoverRemovedDocumentFocusStaysInActiveDocument(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if worker.recoverCapturedDocumentFocus(cursor, otherTree, &result) || worker.focus() != webID("/first") { if worker.recoverCapturedDocumentFocus(cursor, otherTree, &result, false) || worker.focus() != webID("/first") {
t.Fatal("recovery crossed to another document") t.Fatal("recovery crossed to another document")
} }
} }
@@ -1433,3 +1433,123 @@ func TestCaptureAnchorRequiresSavedPlaceIdentity(t *testing.T) {
t.Fatal("capture anchor fired for an unrelated same-bus object") t.Fatal("capture anchor fired for an unrelated same-bus object")
} }
} }
// Steam shows a loading spinner when a popup closes, focus lands on it, and
// the spinner is removed before the next key. That dead focus was never a
// saved place, so captureAnchor cannot recover it. The move must continue
// from the reading place in the surviving document instead of reporting
// "Could not find current location" on every key.
func TestDeadFocusNeverSavedRecoversThroughRetainedDocument(t *testing.T) {
treeA := retainedDocumentTree(t, "/docA", "/a1", "/a2")
treeB := retainedDocumentTree(t, "/docB", "/b1", "/b2", "/b3")
cursorA, _ := NewDocumentCursor(treeA)
cursorB, _ := NewDocumentCursor(treeB)
ctx := context.Background()
if _, err := cursorB.SyncFocus(ctx, webID("/b2")); err != nil {
t.Fatal(err)
}
if _, err := cursorA.SyncFocus(ctx, webID("/a1")); err != nil {
t.Fatal(err)
}
// The popup document is gone; the main document survives unchanged.
treeA.Invalidate(webID("/a1"))
treeA.Invalidate(webID("/docA"))
capturer := &retainedDocumentCapturer{trees: map[atspi.ObjectID]*atspi.AccessibleTree{webID("/docB"): treeB}}
worker, err := NewWorker(capturer, time.Second)
if err != nil {
t.Fatal(err)
}
worker.rememberDocumentCursor(cursorB, 0)
worker.rememberDocumentCursor(cursorA, 0)
worker.SyncFocus(webID("/spinner"))
result, _ := worker.moveDocument(ctx, cursorA, DocumentHeading, 0, false, 0, 0)
if result.Err != nil || !result.Moved || result.DocumentPosition.Object.ID() != webID("/b3") {
t.Fatalf("dead unsaved focus: object=%v moved=%v error=%v captures=%v",
result.DocumentPosition.Object.ID(), result.Moved, result.Err, capturer.captures)
}
if worker.focus() != webID("/docB") {
t.Fatalf("anchor = %v, want surviving document /docB", worker.focus())
}
result, _ = worker.moveDocument(ctx, cursorB, DocumentHeading, 0, true, 0, 0)
if result.Err != nil || result.DocumentPosition.Object.ID() != webID("/b2") {
t.Fatalf("next key after recovery: object=%v error=%v", result.DocumentPosition.Object.ID(), result.Err)
}
}
// A dead focus on another provider must not be rebound into an unrelated
// application's retained document.
func TestDeadFocusRecoveryStaysOnProviderBus(t *testing.T) {
treeB := retainedDocumentTree(t, "/docB", "/b1", "/b2")
cursorB, _ := NewDocumentCursor(treeB)
if _, err := cursorB.SyncFocus(context.Background(), webID("/b1")); err != nil {
t.Fatal(err)
}
capturer := &retainedDocumentCapturer{trees: map[atspi.ObjectID]*atspi.AccessibleTree{webID("/docB"): treeB}}
worker, err := NewWorker(capturer, time.Second)
if err != nil {
t.Fatal(err)
}
worker.rememberDocumentCursor(cursorB, 0)
other := atspi.NewObjectID(":9.9", "/spinner")
if _, _, _, ok := worker.captureRetainedDocumentForDeadFocus(context.Background(), nil, other, 0); ok {
t.Fatal("dead focus adopted another provider's document")
}
}
type deadStartNavigator struct {
*readAllCapturer
caretTextReader
placements []atspi.ObjectID
replace func()
}
func (navigator *deadStartNavigator) SetDocumentCaretPosition(_ context.Context, position atspi.ObjectReference, _ atspi.ObjectID, _ int) error {
navigator.placements = append(navigator.placements, position.ID())
if position.ID() == webID("/text") {
if navigator.replace != nil {
navigator.replace()
navigator.replace = nil
}
return atspi.ErrDeadObject
}
return nil
}
// Steam replaced the top of its page between capture and placement, so the
// first object chosen for document start no longer existed. Start must
// choose again from the fresh tree instead of reporting no location.
func TestDocumentStartChoosesAgainWhenFirstObjectDies(t *testing.T) {
text := caretTextReader{webID("/text"): "Alpha", webID("/next"): "Next line"}
cursor := newCaretTextCursor(t, text)
ctx := context.Background()
if _, err := cursor.SyncFocus(ctx, webID("/next")); err != nil {
t.Fatal(err)
}
if move, err := cursor.MoveCaret(ctx, text, CaretNextCharacter); err != nil || move.Position.Offset != 1 {
t.Fatalf("setup move = %+v, %v", move, err)
}
tree := cursor.tree
navigator := &deadStartNavigator{readAllCapturer: &readAllCapturer{tree: tree}, caretTextReader: text}
navigator.replace = func() {
// The provider retires the dead object; the page keeps /next.
tree.Invalidate(webID("/text"))
snapshot := atspi.NewSnapshot("/doc", "", "document web", "", "")
snapshot.BusName = webID("/doc").BusName
next := atspi.NewSnapshot("/next", "", "static", "", "")
next.BusName = webID("/next").BusName
next.CharacterCount = len("Next line")
if err := tree.Replace(webID("/doc"), []atspi.TreeNode{{Snapshot: snapshot, Children: []atspi.ObjectID{webID("/next")}}, {Snapshot: next}}); err != nil {
t.Fatal(err)
}
}
worker, err := NewWorker(navigator, time.Second)
if err != nil {
t.Fatal(err)
}
worker.SyncFocus(webID("/next"))
result, _ := worker.moveDocumentCaret(ctx, cursor, CaretDocumentStart, 0, 0)
if result.Err != nil || !result.Moved || result.DocumentPosition.Object.ID() != webID("/next") || result.DocumentPosition.Offset != 0 {
t.Fatalf("document start after dead first object: object=%v offset=%d moved=%v error=%v placements=%v",
result.DocumentPosition.Object.ID(), result.DocumentPosition.Offset, result.Moved, result.Err, navigator.placements)
}
}