@@ -42,8 +42,16 @@ extension RunnerTests {
4242#if os( iOS )
4343 let postStartedAt = Date ( )
4444 let result = replacingExistingText
45- ? RunnerSynthesizedTextEntry . replaceText ( withApplication : app , text : text )
46- : RunnerSynthesizedTextEntry . synthesizeText ( withApplication : app , text : text )
45+ ? RunnerSynthesizedTextEntry . replaceText (
46+ withApplication : app ,
47+ text : text ,
48+ charactersPerSecond : TextEntryTiming . synthesizedCharactersPerSecond
49+ )
50+ : RunnerSynthesizedTextEntry . synthesizeText (
51+ withApplication : app ,
52+ text : text ,
53+ charactersPerSecond : TextEntryTiming . synthesizedCharactersPerSecond
54+ )
4755 NSLog (
4856 " [DEBUG-1874] synthesize posted %d chars status=%d tookMs=%.0f " ,
4957 text . count , result . status . rawValue , postStartedAt . timeIntervalSinceNow * - 1000
@@ -73,74 +81,144 @@ extension RunnerTests {
7381#endif
7482 }
7583
76- struct SynthesizedReplacementStep : Equatable {
77- let text : String
78- let replacesExistingText : Bool
79- }
84+ /// One synthesized text-entry plan: the posts a command makes, in the order it makes them, each
85+ /// with the wait that follows it. The delivery budget charges this array and the dispatch loop
86+ /// posts this same array, so the ceiling a command is refused against can never be a projection
87+ /// of a plan the runner did not run (#2955).
88+ struct SynthesizedTextPlan : Equatable {
89+ /// One post. `characterCount` characters of the text, taken in order, are what it types.
90+ struct Step : Equatable {
91+ let characterCount : Int
92+ /// True when this post selects the field's existing value away first. That selection is its
93+ /// own synthesize record, so the post costs one call more than typing characters.
94+ var replacesExistingText = false
95+ /// Seconds charged for what follows this post: the `--delay-ms` gap before the next post, or
96+ /// the warmup read-back after a peeled first character. The read-back costs one poll, because
97+ /// the route that needs a budget has no element to read the character back from and its wait
98+ /// cannot be longer than one poll of a value nobody can observe.
99+ var pauseAfterSeconds : TimeInterval = 0
100+ /// Set on the post whose value the loop waits for before the rest is posted.
101+ var warmsUpField = false
80102
81- static func synthesizedReplacementSteps (
82- text : String ,
83- delaySeconds : Double
84- ) -> [ SynthesizedReplacementStep ] {
85- guard synthesizedReplacementIsSpaced ( characterCount : text . count , delaySeconds : delaySeconds )
86- else {
87- return [ SynthesizedReplacementStep ( text : text , replacesExistingText : true ) ]
103+ /// How many private synthesize records this post runs.
104+ var synthesizeCallCount : Int {
105+ replacesExistingText ? 2 : 1
106+ }
88107 }
89- return Array ( text ) . enumerated ( ) . map { index , character in
90- SynthesizedReplacementStep (
91- text : String ( character ) ,
92- replacesExistingText : index == 0
93- )
108+
109+ let steps : [ Step ]
110+ /// Recorded by the builder so the dispatch loop never re-decides the shape from `delaySeconds`.
111+ let isSpaced : Bool
112+
113+ /// One post per character, `delaySeconds` charged after every post but the last. A replacement
114+ /// selects once, on its first post; an append never selects.
115+ static func spacedSteps (
116+ characterCount : Int ,
117+ delaySeconds : Double ,
118+ replacesExistingTextOnFirstPost : Bool = false
119+ ) -> [ Step ] {
120+ ( 0 ..< characterCount ) . map { index in
121+ Step (
122+ characterCount : 1 ,
123+ replacesExistingText : replacesExistingTextOnFirstPost && index == 0 ,
124+ pauseAfterSeconds : index + 1 < characterCount ? delaySeconds : 0
125+ )
126+ }
127+ }
128+
129+ /// Wall clock this plan spends posting: each post types its characters at the pace and pays for
130+ /// every synthesize record it runs, and each wait in front of the next post is charged once.
131+ var seconds : TimeInterval {
132+ steps . reduce ( 0 ) { total , step in
133+ total
134+ + Double ( step . characterCount ) * TextEntryTiming . synthesizedCharacterInterval
135+ + Double ( step . synthesizeCallCount ) * TextEntryTiming . synthesizeCallOverhead
136+ + step . pauseAfterSeconds
137+ }
138+ }
139+
140+ /// The peeled first character and the rest, when the plan warms the field up before posting it.
141+ var warmupSplit : ( first : Step , rest : Step ) ? {
142+ guard steps . count == 2 , let first = steps . first , first . warmsUpField else {
143+ return nil
144+ }
145+ return ( first , steps [ 1 ] )
94146 }
95147 }
96148
97- /// Whether a replacement is posted one character per synthesize call, `delaySeconds` apart,
98- /// rather than as one burst.
99- static func synthesizedReplacementIsSpaced ( characterCount : Int , delaySeconds : Double ) -> Bool {
149+ /// Whether a text is posted one character per synthesize call, `delaySeconds` apart, rather than
150+ /// as one burst. The one place this is decided for every synthesized route .
151+ static func synthesizedDeliveryIsSpaced ( characterCount : Int , delaySeconds : Double ) -> Bool {
100152 delaySeconds > 0 && characterCount > 1
101153 }
102154
103- /// What a synthesized burst costs in wall clock, and the ceiling it has to fit inside before the
104- /// first character is posted. `synthesizedReplacementSteps` decides how a text is posted; this
105- /// decides whether the runner may start posting it at all.
106- enum SynthesizedDeliveryBudget {
107- /// Seconds between two characters of one synthesized burst.
108- static var characterInterval : TimeInterval {
109- 1.0 / Double ( RunnerSynthesizedTextEntry . typingSpeedCharactersPerSecond ( ) )
155+ /// The posts a synthesized command makes.
156+ ///
157+ /// - A spaced request posts one character per call with the requested gap between calls; a
158+ /// `fill` selects once, on its first post, and a `type` never selects.
159+ /// - A burst is one post, which a `fill` selects away first.
160+ /// - A burst from a command that repairs peels one character instead, because a field whose app
161+ /// owns its value can reject the whole burst on the first edit's write-back; the rest follows
162+ /// after the warmup read-back.
163+ static func synthesizedTextPlan (
164+ characterCount : Int ,
165+ delaySeconds : Double ,
166+ selectsExistingText : Bool ,
167+ peelsWarmupCharacter : Bool = false
168+ ) -> SynthesizedTextPlan {
169+ if synthesizedDeliveryIsSpaced ( characterCount : characterCount , delaySeconds : delaySeconds ) {
170+ return SynthesizedTextPlan (
171+ steps : SynthesizedTextPlan . spacedSteps (
172+ characterCount : characterCount ,
173+ delaySeconds : delaySeconds ,
174+ replacesExistingTextOnFirstPost : selectsExistingText
175+ ) ,
176+ isSpaced : true
177+ )
110178 }
111-
112- /// Seconds the plan spends posting: each synthesize call types its characters at the pace and
113- /// pays its overhead, a spaced plan sleeps `delaySeconds` between two calls, and a plan that
114- /// peels one character as a warmup (`typeWarmup`) pays one more call and the wait before the
115- /// rest is posted. That wait is one poll here because the caller that asks has no element to
116- /// read the warmup character back from, so `waitForWarmupValue` has no value to wait for.
117- static func projectedSeconds (
118- textLength : Int ,
119- delaySeconds : TimeInterval ,
120- typeWarmup : Bool = false
121- ) -> TimeInterval {
122- let spaced = synthesizedReplacementIsSpaced ( characterCount : textLength , delaySeconds : delaySeconds )
123- let warmupSplit = typeWarmup && textLength > 1 && ! spaced
124- let calls = spaced ? textLength : ( warmupSplit ? 2 : 1 )
125- return Double ( textLength ) * characterInterval
126- + Double ( calls ) * TextEntryTiming . synthesizeCallOverhead
127- + Double ( calls - 1 ) * delaySeconds
128- + ( warmupSplit ? TextEntryTiming . pollInterval : 0 )
179+ guard peelsWarmupCharacter && characterCount > 1 else {
180+ return SynthesizedTextPlan (
181+ steps : [
182+ SynthesizedTextPlan . Step (
183+ characterCount : characterCount ,
184+ replacesExistingText : selectsExistingText
185+ ) ,
186+ ] ,
187+ isSpaced : false
188+ )
129189 }
190+ return SynthesizedTextPlan (
191+ steps : [
192+ SynthesizedTextPlan . Step (
193+ characterCount : 1 ,
194+ pauseAfterSeconds : TextEntryTiming . pollInterval ,
195+ warmsUpField : true
196+ ) ,
197+ SynthesizedTextPlan . Step ( characterCount : characterCount - 1 ) ,
198+ ] ,
199+ isSpaced : false
200+ )
201+ }
130202
131- static func exceeds (
132- textLength : Int ,
133- delaySeconds : TimeInterval ,
134- typeWarmup : Bool = false
135- ) -> Bool {
136- projectedSeconds ( textLength : textLength , delaySeconds : delaySeconds , typeWarmup : typeWarmup )
137- > TextEntryTiming . synthesizedDeliveryCeiling
203+ /// What a plan may not cost: more wall clock than the command has left to post characters. It is
204+ /// asked before the first character is dispatched, from the array about to be posted.
205+ enum SynthesizedDeliveryBudget {
206+ static func exceeds ( _ plan : SynthesizedTextPlan ) -> Bool {
207+ plan . seconds > TextEntryTiming . synthesizedDeliveryCeiling
138208 }
139209
140- /// Longest text `exceeds` admits at `delaySeconds`, which is what the refusal tells the caller.
210+ /// Longest text a replacement at `delaySeconds` still admits, which is the number the refusal
211+ /// tells the caller. It asks the same plan admission judges, so the recovery hint can never
212+ /// promise a length the route then refuses.
141213 static func maxTextLength ( delaySeconds : TimeInterval ) -> Int {
142214 var length = 1
143- while ! exceeds ( textLength : length + 1 , delaySeconds : delaySeconds ) {
215+ while ! exceeds (
216+ synthesizedTextPlan (
217+ characterCount : length + 1 ,
218+ delaySeconds : delaySeconds ,
219+ selectsExistingText : true
220+ )
221+ ) {
144222 length += 1
145223 }
146224 return length
@@ -153,10 +231,12 @@ extension RunnerTests {
153231 ) -> SynthesizedReplacementRouteOutcome {
154232#if os( iOS )
155233 NSLog ( " AGENT_DEVICE_RUNNER_TEXT_ENTRY_ROUTE route=synthesized-first-responder-replacement " )
156- if SynthesizedDeliveryBudget . exceeds (
157- textLength : request . text . count ,
158- delaySeconds : request . delaySeconds
159- ) {
234+ let plan = Self . synthesizedTextPlan (
235+ characterCount : request . text . count ,
236+ delaySeconds : request . delaySeconds ,
237+ selectsExistingText : true
238+ )
239+ if SynthesizedDeliveryBudget . exceeds ( plan ) {
160240 NSLog (
161241 " AGENT_DEVICE_RUNNER_TEXT_ENTRY_ROUTE route=synthesized-first-responder-replacement "
162242 + " reason=delivery-budget-refused chars=%d budgetChars=%d " ,
@@ -174,14 +254,13 @@ extension RunnerTests {
174254 )
175255 )
176256 }
177- let steps = Self . synthesizedReplacementSteps (
178- text : request . text ,
179- delaySeconds : request . delaySeconds
180- )
181- for ( index , step ) in steps . enumerated ( ) {
257+ var postedCount = 0
258+ let characters = Array ( request . text )
259+ for step in plan . steps {
260+ let nextCount = postedCount + step . characterCount
182261 switch request . synthesizer . enterText (
183262 app : request . app ,
184- text : step . text ,
263+ text : String ( characters [ postedCount ..< nextCount ] ) ,
185264 replacingExistingText : step . replacesExistingText
186265 ) {
187266 case . fallback :
@@ -198,8 +277,9 @@ extension RunnerTests {
198277 case . continueTyping :
199278 break
200279 }
201- if index + 1 < steps . count {
202- sleepFor ( request . delaySeconds )
280+ postedCount = nextCount
281+ if step . pauseAfterSeconds > 0 {
282+ sleepFor ( step . pauseAfterSeconds )
203283 }
204284 }
205285 // The private synthesize call returns at post time, not commit time, and this route never
0 commit comments