manager.Focus pairs a focusout with the focusin that follows it into one focus move, and it decides the pairing by App-Worker wall-clock time. When the worker is busy for longer than maxFocusInOutGap (50 ms) between handling the focusout and handling its focusin — a heavy render in between is enough — the move is misread: the focusin becomes a fresh focusEnter, and the focusout's pending leave is then suppressed because a focusin "came after" it. No focusLeave is ever delivered, and every component on the old path keeps containsFocus: true until its own next focus event.
Observed twice, independently.
#15536 (2026-07-19, @neo-opus-vega, FM cockpit): a heavier card re-render pushed a same-app focus move to 140 ms, "classified as a fresh focusEnter, which never clears the PRIOR card's containsFocus — so two cards read as focused at once". Worked around at app level in commit 5a9bf47; the engine was left alone because clearing prior paths on enter would be wrong in multi-window apps. That workaround lived in the since-replaced FleetGrid — apps/agentos/view/fleet/roster/** reads no containsFocus today.
2026-09-19 (this ticket), a deterministic unit probe on dev@7db03e862c: focus on A → onFocusout(A) → the thread busy 80 ms → onFocusin(B) → wait out the timer. Idle control: A false, B true. Busy: A true, B true.
Found while writing up @neo-opus-ada's [KB_GAP] on PR #18938: the grid editor's reprojection rule reads view.containsFocus, and under this defect a click outside the grid while the worker is busy would leave the flag true and let the editor take focus back.
The Problem
src/manager/Focus.mjs:
onFocusin stamps lastFocusInDate = new Date() and calls focusMove only if lastFocusInDate - lastFocusOutDate < maxFocusInOutGap, else focusEnter.
onFocusout stamps lastFocusOutDate = new Date() and schedules focusLeave after the gap, delivered only if lastFocusOutDate === date && date > lastFocusInDate.
Both stamps are processing times. The browser fires the pair in one main-thread task, so the real gap is ~0 ms. But if the worker handles the focusout, then runs something for more than 50 ms, then handles the focusin before the timer's callback runs, the two tests become complementary in the wrong way: the move test fails (gap too long) and the leave test fails (a focusin is newer). Neither branch fires for the old path.
The DOM events already carry their own timeStamp (main/DomEvents#getEventData), and manager.Focus ignores it — but see Avoided Traps for why that is not the fix.
The Architectural Reality
manager.Focus (singleton, App Worker) receives focusin / focusout from main.DomEvents#onFocusIn / #onFocusOut with componentPath (ids upwards) and data (path, relatedTarget, timeStamp, and removedNodeId for an engine removal).
setComponentFocus(opts, flag) writes containsFocus on every component of a path and fires focusEnter / focusLeave / focusChange; focusMove diffs the old path (history[0]) against the new one at their closest common component.
The pending leave's guards stay: identity (a newer focusout supersedes an older one's leave) and isRemovedFloatingShownAgain (a removed floating component shown again keeps its focus).
component.Base#containsFocus_'s JSDoc says only "manager.Focus will change this flag on focusin & out dom events". It never says what the flag answers — DOM focus inside this component's subtree, never which descendant — nor when it changes. Three #18762 leaves misread it within a day (#18863's same-tick read, a retracted defect-note that read it before the round trip, and #18938's in-View gesture).
The Fix
Pair by pending state, not by clock:
onFocusout records its opts as the pending focusout and schedules the leave after maxFocusInOutGap, as today.
onFocusin: a pending focusout makes the pair a move (and clears the pending one); none makes it an enter.
The timer delivers the leave only while its focusout is still the pending one, and not for a removed floating component shown again — the existing identity guard, keyed on the pending opts instead of a Date.
lastFocusInDate / lastFocusOutDate retire. Only test/playwright/unit/manager/Focus.spec.mjs's afterEach reads them, and lastFocusOutDate's docblock names the wrong member.
component.Base#containsFocus_ gets a JSDoc that states what it answers and when it changes.
maxFocusInOutGap's JSDoc says what the gap now bounds: how long a focusout waits for its focusin before it counts as a leave.
In every idle case this is behaviour-identical: a focusin inside the gap was a move and stays one, and a focusout alone still leaves after the gap. Under load the worker never saw an intermediate state, and "a move" is the faithful account of what it knows.
Accretion: no consumer line is deleted — the one workaround (#15536) is gone with the grid it lived in. This is a correctness fix for every reader of the flag.
Contract Ledger
Target surface
Source of authority
Proposed behaviour
Fallback
Docs
Evidence
component.Base#containsFocus_
src/component/Base.mjs:92-98, written by manager.Focus#setComponentFocus
meaning unchanged; also correct when the worker is busy between a focusout and its focusin
A unit arm in test/playwright/unit/manager/Focus.spec.mjs: focus on A, a focusout, the thread busy past maxFocusInOutGap, a focusin on B, the timer run out → A false, B true, and A received its leave. RED on dev.
Every existing Focus.spec.mjs arm passes unchanged (superseded focusout, removed floating menu shown again, the three dismissals, the closest-common-component move).
Reverting the classification to the clock comparison turns the new arm red.
containsFocus_ states: DOM focus is inside this component's subtree — not which descendant, so focus moving between two descendants that share one focused node (a grid View's cells) never changes it; App-Worker state, set when the worker handles the focusin and cleared one maxFocusInOutGap after a focusout unless a focusin pairs it into a move.
Unit tier green; component tier green; e2e/grid/CellEditingReprojectionFocus.spec.mjs green.
Out of Scope
Per-window focus state for multi-window apps. A focusout in one window and a focusin in another can pair today inside the gap, and still can.
The DOM events' own timeStamp (see Avoided Traps).
Any Neural Link or Brain surface.
Avoided Traps
Compare the DOM events' timeStamp. Each window's event.timeStamp counts from its own performance.timeOrigin, so two windows of one SharedWorker app stamp on unrelated clocks. It would need an absolute clock in the payload, and it still answers "how far apart" when the question is "are these a pair".
Clear every prior path on focusEnter. Rejected in #15536 and still wrong: in a multi-window app a second window's element legitimately holds focus.
Raise maxFocusInOutGap. Moves the threshold only. A render longer than any gap reopens the hole, and a longer gap delays every genuine leave.
Related
#15536 — first occurrence, app-level workaround.
#18927 / PR #18938 — the grid editor's reprojection reads view.containsFocus at the mount.
#18762 — the epic whose three leaves misread the flag.
archived #6129 — kept maxFocusInOutGap "unless a regression is proven".
Sweeps
Live latest-open sweep: checked the latest 20 open issues at 2026-09-19T11:04:18Z; no equivalent.
Memory Core rationale sweep ("containsFocus … focus flag lags one manager.Focus gap"): #15536's workaround and the retracted 09-18 defect-note (a read before the round trip, not this defect); no decision against an engine fix. Own open assignments: none on focus.
Context
manager.Focuspairs a focusout with the focusin that follows it into one focus move, and it decides the pairing by App-Worker wall-clock time. When the worker is busy for longer thanmaxFocusInOutGap(50 ms) between handling the focusout and handling its focusin — a heavy render in between is enough — the move is misread: the focusin becomes a freshfocusEnter, and the focusout's pending leave is then suppressed because a focusin "came after" it. NofocusLeaveis ever delivered, and every component on the old path keepscontainsFocus: trueuntil its own next focus event.Observed twice, independently.
#15536(2026-07-19, @neo-opus-vega, FM cockpit): a heavier card re-render pushed a same-app focus move to 140 ms, "classified as a fresh focusEnter, which never clears the PRIOR card's containsFocus — so two cards read as focused at once". Worked around at app level in commit5a9bf47; the engine was left alone because clearing prior paths on enter would be wrong in multi-window apps. That workaround lived in the since-replaced FleetGrid —apps/agentos/view/fleet/roster/**reads nocontainsFocustoday.dev@7db03e862c: focus on A →onFocusout(A)→ the thread busy 80 ms →onFocusin(B)→ wait out the timer. Idle control: Afalse, Btrue. Busy: Atrue, Btrue.Found while writing up @neo-opus-ada's
[KB_GAP]on PR #18938: the grid editor's reprojection rule readsview.containsFocus, and under this defect a click outside the grid while the worker is busy would leave the flag true and let the editor take focus back.The Problem
src/manager/Focus.mjs:onFocusinstampslastFocusInDate = new Date()and callsfocusMoveonly iflastFocusInDate - lastFocusOutDate < maxFocusInOutGap, elsefocusEnter.onFocusoutstampslastFocusOutDate = new Date()and schedulesfocusLeaveafter the gap, delivered only iflastFocusOutDate === date && date > lastFocusInDate.Both stamps are processing times. The browser fires the pair in one main-thread task, so the real gap is ~0 ms. But if the worker handles the focusout, then runs something for more than 50 ms, then handles the focusin before the timer's callback runs, the two tests become complementary in the wrong way: the move test fails (gap too long) and the leave test fails (a focusin is newer). Neither branch fires for the old path.
The DOM events already carry their own
timeStamp(main/DomEvents#getEventData), andmanager.Focusignores it — but see Avoided Traps for why that is not the fix.The Architectural Reality
manager.Focus(singleton, App Worker) receivesfocusin/focusoutfrommain.DomEvents#onFocusIn/#onFocusOutwithcomponentPath(ids upwards) anddata(path,relatedTarget,timeStamp, andremovedNodeIdfor an engine removal).setComponentFocus(opts, flag)writescontainsFocuson every component of a path and firesfocusEnter/focusLeave/focusChange;focusMovediffs the old path (history[0]) against the new one at their closest common component.isRemovedFloatingShownAgain(a removed floating component shown again keeps its focus).component.Base#containsFocus_'s JSDoc says only "manager.Focus will change this flag on focusin & out dom events". It never says what the flag answers — DOM focus inside this component's subtree, never which descendant — nor when it changes. Three #18762 leaves misread it within a day (#18863's same-tick read, a retracted defect-note that read it before the round trip, and #18938's in-View gesture).The Fix
Pair by pending state, not by clock:
onFocusoutrecords itsoptsas the pending focusout and schedules the leave aftermaxFocusInOutGap, as today.onFocusin: a pending focusout makes the pair a move (and clears the pending one); none makes it an enter.optsinstead of aDate.lastFocusInDate/lastFocusOutDateretire. Onlytest/playwright/unit/manager/Focus.spec.mjs'safterEachreads them, andlastFocusOutDate's docblock names the wrong member.component.Base#containsFocus_gets a JSDoc that states what it answers and when it changes.maxFocusInOutGap's JSDoc says what the gap now bounds: how long a focusout waits for its focusin before it counts as a leave.In every idle case this is behaviour-identical: a focusin inside the gap was a move and stays one, and a focusout alone still leaves after the gap. Under load the worker never saw an intermediate state, and "a move" is the faithful account of what it knows.
Accretion: no consumer line is deleted — the one workaround (
#15536) is gone with the grid it lived in. This is a correctness fix for every reader of the flag.Contract Ledger
component.Base#containsFocus_src/component/Base.mjs:92-98, written bymanager.Focus#setComponentFocusfocusEnter/focusMove/focusLeaveclassificationmanager.Focus#onFocusin/#onFocusoutFocus.spec.mjsarms unchangedmanager.Focus#lastFocusInDate,#lastFocusOutDate(@protected)src/manager/Focus.mjsconfigsgrep: no reader outside the spec'safterEachAcceptance Criteria
test/playwright/unit/manager/Focus.spec.mjs: focus on A, a focusout, the thread busy pastmaxFocusInOutGap, a focusin on B, the timer run out → Afalse, Btrue, and A received its leave. RED ondev.Focus.spec.mjsarm passes unchanged (superseded focusout, removed floating menu shown again, the three dismissals, the closest-common-component move).containsFocus_states: DOM focus is inside this component's subtree — not which descendant, so focus moving between two descendants that share one focused node (a grid View's cells) never changes it; App-Worker state, set when the worker handles the focusin and cleared onemaxFocusInOutGapafter a focusout unless a focusin pairs it into a move.e2e/grid/CellEditingReprojectionFocus.spec.mjsgreen.Out of Scope
timeStamp(see Avoided Traps).Avoided Traps
timeStamp. Each window'sevent.timeStampcounts from its ownperformance.timeOrigin, so two windows of one SharedWorker app stamp on unrelated clocks. It would need an absolute clock in the payload, and it still answers "how far apart" when the question is "are these a pair".focusEnter. Rejected in#15536and still wrong: in a multi-window app a second window's element legitimately holds focus.maxFocusInOutGap. Moves the threshold only. A render longer than any gap reopens the hole, and a longer gap delays every genuine leave.Related
#15536— first occurrence, app-level workaround.view.containsFocusat the mount.#6129— keptmaxFocusInOutGap"unless a regression is proven".Sweeps
manager.Focus.#15536's workaround and the retracted 09-18 defect-note (a read before the round trip, not this defect); no decision against an engine fix. Own open assignments: none on focus.Origin Session ID: bd178faf-66b1-4f57-8c53-8224751e92c2
Retrieval Hint: "manager.Focus focusout focusin gap busy worker stale containsFocus pending focusout"