@@ -307,14 +307,14 @@ final class Snapshot {
307307 var truncated = false
308308 /// Every element this app session has been given an id, so the same element keeps its id across observations.
309309 private( set) var index = ElementIndex ( )
310- private( set) var nextOrdinal = 0
311- /// Carries ids over from `previous` when it observed the same process instance.
312- init ( app : NSRunningApplication , previous : Snapshot ? = nil ) {
313- pid = app . processIdentifier ; launchDate = app . launchDate
314- if let previous , previous . pid == pid , previous . launchDate == launchDate {
315- // Ids never repeat within a session ; past the cap the next read starts a fresh index so it stays bounded .
316- nextOrdinal = previous . nextOrdinal
317- if previous . index . count < 20_000 { index = previous . index }
310+ private( set) var nextOrdinal : Int
311+ /// Reuses `previous`'s ids when it observed the same process instance and new ids continue from
312+ /// `nextOrdinal`, which the driver keeps per process so ids never repeat while it runs.
313+ init ( app : NSRunningApplication , previous : Snapshot ? = nil , nextOrdinal : Int = 0 ) {
314+ pid = app . processIdentifier ; launchDate = app . launchDate ; self . nextOrdinal = nextOrdinal
315+ // One read adds at most 2,000 elements, so the carried index stays within 20,000 ; past that the next read starts fresh.
316+ if let previous , previous . pid == pid , previous . launchDate == launchDate , previous . index . count <= 18_000 {
317+ index = previous . index
318318 }
319319 }
320320 func read ( preferredWindowFrame : CGRect ? = nil ) async throws {
@@ -706,6 +706,8 @@ final class Driver {
706706 var snapshots : [ String : Snapshot ] = [ : ]
707707 /// Each app's latest observation, kept after actions consume `snapshots` so the next read reuses its element ids.
708708 var lastObserved : [ String : Snapshot ] = [ : ]
709+ /// Each app's next element id, kept apart from the evictable history so ids never repeat while that process runs.
710+ var nextOrdinals : [ String : ( pid : pid_t , launchDate : Date ? , next : Int ) ] = [ : ]
709711 func point ( _ p : Parameters , app : NSRunningApplication , snapshot : Snapshot , end : Bool = false ) throws -> CGPoint {
710712 if let elementId = p . elementId , ! end {
711713 guard let frame = elementRect ( try snapshot . element ( elementId ) ) else { throw ComputerError ( " element_unavailable " , " Element has no usable frame. " ) }
@@ -759,7 +761,9 @@ final class Driver {
759761 return [ " kind " : " action " , " action " : " activate_app " , " bundleId " : app . bundleIdentifier ! , " dispatched " : true , " verified " : true ]
760762 case " get_app_state " :
761763 try requireAccessibility ( )
762- let app = try await appFor ( p . bundleId , launch : true ) ; let snapshot = Snapshot ( app : app , previous : lastObserved [ app . bundleIdentifier ! ] )
764+ let app = try await appFor ( p . bundleId , launch : true ) ; let bundle = app . bundleIdentifier !
765+ let ordinal = nextOrdinals [ bundle ] . flatMap { $0 . pid == app . processIdentifier && $0 . launchDate == app . launchDate ? $0 . next : nil } ?? 0
766+ let snapshot = Snapshot ( app : app , previous : lastObserved [ bundle ] , nextOrdinal : ordinal )
763767 let preferredFrame = try p . windowId . map { try windowRect ( $0 , pid : app . processIdentifier ) }
764768 try await snapshot . read ( preferredWindowFrame : preferredFrame )
765769 let axWindows = snapshot . elements . values . filter { attribute ( $0 , kAXRoleAttribute ) as? String == kAXWindowRole }
@@ -783,9 +787,10 @@ final class Driver {
783787 result . merge ( rectJSON ( rect ) ) { _ , new in new } ; return result
784788 }
785789 if snapshots . count >= 16 { snapshots . removeAll ( ) }
786- if lastObserved . count >= 16 { lastObserved . removeAll ( ) }
787- snapshots [ app . bundleIdentifier ! ] = snapshot
788- lastObserved [ app . bundleIdentifier ! ] = snapshot
790+ if lastObserved [ bundle ] == nil , lastObserved . count >= 16 , let evicted = lastObserved . keys . first { lastObserved . removeValue ( forKey : evicted ) }
791+ snapshots [ bundle ] = snapshot
792+ lastObserved [ bundle ] = snapshot
793+ nextOrdinals [ bundle ] = ( app . processIdentifier , app . launchDate , snapshot . nextOrdinal )
789794 let root = AXUIElementCreateApplication ( app . processIdentifier )
790795 let focusedID = attribute ( root , kAXFocusedWindowAttribute ) . flatMap { CFGetTypeID ( $0 ) == AXUIElementGetTypeID ( ) ? snapshot . windowId ( holding : unsafeBitCast ( $0 , to : AXUIElement . self ) ) : nil }
791796 for index in snapshot . nodes . indices {
0 commit comments