@@ -47,17 +47,17 @@ struct ComputerActionExecutionAuthority {
4747/// execution-local, and invalidated when the native lifecycle generation moves.
4848@ MainActor
4949final class ComputerActionServiceV2 {
50- private struct WindowTarget {
50+ struct WindowTarget {
5151 let app : ServiceApplicationInfo
5252 let window : ServiceWindowInfo
5353 }
5454
55- private struct ElementTarget {
55+ struct ElementTarget {
5656 let id : String
5757 let bounds : CGRect
5858 }
5959
60- private struct ObservationState {
60+ struct ObservationState {
6161 let id : String
6262 let windowRef : String
6363 let snapshotId : String
@@ -190,7 +190,6 @@ final class ComputerActionServiceV2 {
190190 let appOutput = try await self . withExecutionAuthority {
191191 try await self . applications . listApplications ( )
192192 }
193- self . windowRefs . removeAll ( keepingCapacity : true )
194193 var rows : [ [ String : Any ] ] = [ ]
195194 var warnings = appOutput . metadata . warnings
196195 outer: for app in appOutput . data . applications
@@ -204,9 +203,8 @@ final class ComputerActionServiceV2 {
204203 }
205204 warnings . append ( contentsOf : output . metadata . warnings )
206205 for window in output . data . windows where window . layer == 0 {
207- let ref = self . issueWindowRef ( app : app , window : window )
208206 rows . append ( [
209- " windowRef " : ref ,
207+ " windowRef " : self . issueWindowRef ( app : app , window : window ) ,
210208 " appName " : app . name ,
211209 " title " : window . title ,
212210 " bounds " : Self . boundsDictionary ( window . bounds ) ,
@@ -298,28 +296,23 @@ final class ComputerActionServiceV2 {
298296 let detected = result . elements ? . elements . all ?? [ ]
299297 let filtered = Self . filterElements ( detected , query : params . query )
300298 let bounded = Array ( filtered . prefix ( limits . maxElements ) )
301- let observationID = self . issueRef ( " observation " )
302- var elementTargets : [ String : ElementTarget ] = [ : ]
303- let elements = bounded . map { element in
304- let ref = self . issueRef ( " element " )
305- elementTargets [ ref ] = ElementTarget ( id : element . id , bounds : element . bounds )
306- return OpenClawComputerObservationElement (
307- elementRef : ref ,
308- role : element . type . rawValue ,
309- label : element . label ,
310- value : element . value ,
311- bounds : Self . bounds ( element . bounds ) )
312- }
313299 let snapshotID = result . elements ? . snapshotId ?? " "
314300 guard ! snapshotID . isEmpty else {
315301 throw ComputerActionService . ComputerActionError . refused (
316302 " Peekaboo observation returned no snapshot receipt " )
317303 }
318- self . observation = ObservationState (
319- id : observationID ,
304+ let issued = self . issueObservation (
320305 windowRef : windowRef ,
321306 snapshotId : snapshotID ,
322- elements : elementTargets )
307+ elements : bounded . map { ElementTarget ( id : $0 . id , bounds : $0 . bounds ) } )
308+ let elements = zip ( bounded , issued . elementRefs ) . map { element , ref in
309+ OpenClawComputerObservationElement (
310+ elementRef : ref ,
311+ role : element . type . rawValue ,
312+ label : element . label ,
313+ value : element . value ,
314+ bounds : Self . bounds ( element . bounds ) )
315+ }
323316 var details : [ String : AnyCodable ] = [
324317 " totalElementCount " : AnyCodable ( detected . count ) ,
325318 " coordinateSpace " : AnyCodable ( " global-logical-points " ) ,
@@ -343,7 +336,7 @@ final class ComputerActionServiceV2 {
343336 format : " png " ,
344337 width : Int ( size . width ) ,
345338 height : Int ( size . height ) ,
346- observationId : observationID ,
339+ observationId : issued . id ,
347340 elements : elements . isEmpty ? nil : elements ) ,
348341 details : details )
349342 }
@@ -603,72 +596,96 @@ final class ComputerActionServiceV2 {
603596
604597 // MARK: - Reference and target helpers
605598
606- private func adoptLifecycleGeneration ( _ generation : UInt64 ) {
599+ func adoptLifecycleGeneration ( _ generation : UInt64 ) {
607600 guard self . lifecycleGeneration != generation else { return }
608601 self . lifecycleGeneration = generation
609602 self . appRefs . removeAll ( )
610603 self . windowRefs . removeAll ( )
611604 self . observation = nil
612605 }
613606
614- private func issueRef ( _ kind : String ) -> String {
615- " peekaboo:v2: \( kind ) : \( self . executionID ) : \( self . lifecycleGeneration ?? 0 ) : " +
616- UUID ( ) . uuidString . lowercased ( )
617- }
618-
619- private func issueWindowRef ( app : ServiceApplicationInfo , window : ServiceWindowInfo ) -> String {
620- if let existing = self . windowRefs . first ( where : {
621- $0 . value . app . processIdentifier == app . processIdentifier &&
622- $0 . value . window . windowID == window . windowID &&
623- $0 . value . window . mutationIdentity == window . mutationIdentity
624- } ) ? . key {
625- return existing
626- }
627- let ref = self . issueRef ( " window " )
607+ /// A window ref names one live window for the whole lifecycle generation and
608+ /// keys on stable identity only: WindowServer id plus the owner process
609+ /// generation that guards pid reuse. Bounds and minimized state stay out —
610+ /// they belong to the per-action expected-identity check — so a window that
611+ /// moves, resizes, or minimizes keeps its ref and has its stored target
612+ /// refreshed here, and later checks compare against the live window.
613+ func issueWindowRef ( app : ServiceApplicationInfo , window : ServiceWindowInfo ) -> String {
614+ let ref = self . windowRefs . first { Self . sameWindow ( $0 . value . window , window ) } ? . key
615+ ?? self . issueRef ( " window " )
628616 self . windowRefs [ ref ] = WindowTarget ( app : app , window : window )
629617 return ref
630618 }
631619
632- private func resolveWindow ( _ ref : String ) throws -> WindowTarget {
620+ func resolveWindow ( _ ref : String ) throws -> WindowTarget {
633621 guard let target = self . windowRefs [ ref ] else {
634622 throw ComputerActionService . ComputerActionError . staleObservation
635623 }
636624 return target
637625 }
638626
639- private func requiredWindow ( _ params : OpenClawComputerActParams ) throws -> WindowTarget {
640- try self . resolveWindow ( Self . require ( params . windowRef , field : " windowRef " ) )
627+ /// Only the newest observation may authorize element work, so issuing one
628+ /// supersedes every element ref handed out by the previous observation.
629+ func issueObservation (
630+ windowRef : String ,
631+ snapshotId : String ,
632+ elements : [ ElementTarget ] ) -> ( id : String , elementRefs : [ String ] )
633+ {
634+ let id = self . issueRef ( " observation " )
635+ let elementRefs = elements . map { _ in self . issueRef ( " element " ) }
636+ self . observation = ObservationState (
637+ id : id ,
638+ windowRef : windowRef ,
639+ snapshotId : snapshotId ,
640+ elements : Dictionary ( uniqueKeysWithValues : zip ( elementRefs , elements ) ) )
641+ return ( id , elementRefs )
641642 }
642643
643- private func requiredObservation ( _ params : OpenClawComputerActParams , windowRef : String ) throws
644- -> ObservationState
645- {
644+ func resolveObservation ( _ id : String ? , windowRef : String ) throws -> ObservationState {
646645 guard let observation = self . observation ,
647- observation . id == params . observationId ,
646+ observation . id == id ,
648647 observation . windowRef == windowRef
649648 else {
650649 throw ComputerActionService . ComputerActionError . staleObservation
651650 }
652651 return observation
653652 }
654653
654+ func resolveElement ( _ ref : String , observation : ObservationState ) throws -> ElementTarget {
655+ guard let element = observation . elements [ ref ] else {
656+ throw ComputerActionService . ComputerActionError . staleObservation
657+ }
658+ return element
659+ }
660+
661+ private func issueRef ( _ kind : String ) -> String {
662+ " peekaboo:v2: \( kind ) : \( self . executionID ) : \( self . lifecycleGeneration ?? 0 ) : " +
663+ UUID ( ) . uuidString . lowercased ( )
664+ }
665+
666+ private static func sameWindow ( _ lhs : ServiceWindowInfo , _ rhs : ServiceWindowInfo ) -> Bool {
667+ guard let left = lhs . mutationIdentity , let right = rhs . mutationIdentity else { return false }
668+ return left . windowID == right . windowID && left . processIdentity == right . processIdentity
669+ }
670+
671+ private func requiredWindow ( _ params : OpenClawComputerActParams ) throws -> WindowTarget {
672+ try self . resolveWindow ( Self . require ( params . windowRef , field : " windowRef " ) )
673+ }
674+
655675 private func requiredElement (
656676 _ params : OpenClawComputerActParams ,
657677 windowRef : String ) throws -> ElementTarget
658678 {
659679 let elementRef = try Self . require ( params . elementRef , field : " elementRef " )
660- let observation = try self . requiredObservation ( params , windowRef : windowRef )
661- guard let element = observation . elements [ elementRef ] else {
662- throw ComputerActionService . ComputerActionError . staleObservation
663- }
664- return element
680+ let observation = try self . resolveObservation ( params . observationId , windowRef : windowRef )
681+ return try self . resolveElement ( elementRef , observation : observation )
665682 }
666683
667684 private func clickTarget (
668685 _ params : OpenClawComputerActParams ,
669686 windowRef : String ) throws -> ( target : ClickTarget , point : CGPoint , snapshotId : String )
670687 {
671- let observation = try self . requiredObservation ( params , windowRef : windowRef )
688+ let observation = try self . resolveObservation ( params . observationId , windowRef : windowRef )
672689 if params . elementRef != nil {
673690 let element = try self . requiredElement ( params , windowRef : windowRef )
674691 return ( . elementId ( element . id ) , element . bounds . centerPoint , observation . snapshotId )
@@ -701,7 +718,7 @@ final class ComputerActionServiceV2 {
701718 throw ComputerActionService . ComputerActionError . invalidV2Request (
702719 " coordinates must be nonnegative " )
703720 }
704- _ = try self . requiredObservation ( params , windowRef : windowRef )
721+ _ = try self . resolveObservation ( params . observationId , windowRef : windowRef )
705722 return CGPoint ( x : x , y : y )
706723 }
707724
@@ -712,10 +729,8 @@ final class ComputerActionServiceV2 {
712729 foreground : Bool ) async throws -> OpenClawComputerActResult
713730 {
714731 let windowRef = try Self . require ( params . windowRef , field : " windowRef " )
715- let observation = try self . requiredObservation ( params , windowRef : windowRef )
716- guard let element = observation . elements [ elementRef ] else {
717- throw ComputerActionService . ComputerActionError . staleObservation
718- }
732+ let observation = try self . resolveObservation ( params . observationId , windowRef : windowRef )
733+ let element = try self . resolveElement ( elementRef , observation : observation )
719734 if foreground {
720735 try await self . focus ( target )
721736 try Self . postForegroundClick ( at : element . bounds . centerPoint , action : . leftClick , modifiers : nil )
0 commit comments