Skip to content

Commit 43ea03c

Browse files
committed
refactor(ios-runner): charge the text-entry budget the plan the runner posts
The delivery budget added in #2904 was a parallel projection: projectedSeconds re-derived the spaced split, the warmup peel and the warmup wait, all decided elsewhere, and two pre-merge fix commits were drift this shape caused. It also under-charged the plan it modeled: a post that replaces selects with its own synthesize record (RunnerSynthesizedTextEntry.m), which the estimate counted as one call. SynthesizedTextPlan now carries the posts a command makes (characters, whether the post selects first, the wait after it), the budget charges the sum over those steps, and both synthesized routes execute that same array — the loop sleeps the plan's own pauses instead of re-deriving delaySeconds > 0 twice more. The type route decides the budget once, outside the per-chunk closure, and its budget refusal and synthesizer .fallback arm share one application-wide typing path. The pace moved from the ObjC bridge (whose getter Swift read back) into TextEntryTiming and is passed to the bridge, so no call site can type at a pace its own budget never charged. The test-only acknowledge window moved into the tests, and the fixture's never-asserted edits= counter is gone. fill's admitted length at --delay-ms 0 moves 214 -> 212 (honesty about the select-all record, which the old hint overpromised); the recovery hint now fits the host's 400-character diagnostic bound it previously overflowed by 60, losing its last sentence on the wire.
1 parent 78c0ca4 commit 43ea03c

8 files changed

Lines changed: 438 additions & 217 deletions

‎apple/runner/AgentDeviceRunner/AgentDeviceRunner/AgentDeviceRunnerApp.m‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ @interface AgentDeviceRunnerViewController : UIViewController
6666
@property(nonatomic, assign) NSUInteger firstAlertActions;
6767
@property(nonatomic, assign) NSUInteger replacementAlertActions;
6868
@property(nonatomic, strong) UILabel *textEntryWriteBackStatus;
69-
@property(nonatomic, assign) NSUInteger textEntryRenderedEdits;
7069
@property(nonatomic, assign) NSUInteger textEntryWriteBacks;
7170
@property(nonatomic, copy, nullable) NSString *textEntryRenderedValue;
7271
@property(nonatomic, assign) NSTimeInterval textEntryLastEditTime;
@@ -187,8 +186,7 @@ - (void)updateAlertActionStatus {
187186
- (void)updateTextEntryWriteBackStatus {
188187
NSTimeInterval burstSpan = self.textEntryLastEditTime - self.textEntryBurstStartTime;
189188
self.textEntryWriteBackStatus.text = [NSString
190-
stringWithFormat:@"edits=%lu write-backs=%lu burst-edits=%lu burst-ms=%lu min-gap-ms=%lu",
191-
(unsigned long)self.textEntryRenderedEdits,
189+
stringWithFormat:@"write-backs=%lu burst-edits=%lu burst-ms=%lu min-gap-ms=%lu",
192190
(unsigned long)self.textEntryWriteBacks,
193191
(unsigned long)self.textEntryBurstEdits,
194192
(unsigned long)llround(burstSpan * 1000),
@@ -292,7 +290,6 @@ - (void)agentDeviceTextEntryDidChange:(UITextField *)textField {
292290
}
293291
} else {
294292
self.textEntryRenderedValue = [textField.text copy];
295-
self.textEntryRenderedEdits += 1;
296293
}
297294
[self updateTextEntryWriteBackStatus];
298295
}

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerSynthesizedTextEntry.h‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ typedef NS_ENUM(NSInteger, RunnerSynthesizedTextEntryStatus) {
1717

1818
@interface RunnerSynthesizedTextEntry : NSObject
1919

20-
// Characters per second the synthesized text-input records are typed at. Declared here, where the
21-
// typing happens, so the delivery budget that bounds a burst is charged the same pace the app sees.
22-
// The edit-acknowledge window that pace is sized for is a separate assumption about the app
23-
// (TextEntryTiming.synthesizedAcknowledgeWindowSeconds), not a value derived from this one.
24-
+ (NSUInteger)typingSpeedCharactersPerSecond;
25-
2620
// Synthesizes keyboard input for the current first responder without resolving an
2721
// XCUIElement or serializing the application's accessibility tree.
22+
//
23+
// `charactersPerSecond` is XCTest's `typingSpeed:` argument. The caller declares it because the
24+
// same number is what the caller's delivery budget charges a burst (#2955): a pace owned here and
25+
// read back from Swift would let a call site type at a speed its own budget never charged.
2826
+ (RunnerSynthesizedTextEntryResult *)synthesizeTextWithApplication:(id)application
29-
text:(NSString *)text;
27+
text:(NSString *)text
28+
charactersPerSecond:(NSUInteger)charactersPerSecond;
3029

3130
// Replaces the current first responder's contents with one synthesized Command-A record
32-
// followed by a text-input record, typed at the bounded pace declared in the implementation.
31+
// followed by a text-input record, typed at the pace the caller declares.
3332
+ (RunnerSynthesizedTextEntryResult *)replaceTextWithApplication:(id)application
34-
text:(NSString *)text;
33+
text:(NSString *)text
34+
charactersPerSecond:(NSUInteger)charactersPerSecond;
3535

3636
@end
3737

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerSynthesizedTextEntry.m‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,10 @@
55

66
static NSString *const RunnerTextSynthesisSurface = @"text";
77

8-
// XCTest's `typingSpeed:` argument is characters per second. At 60 the 11 characters of a `fill`
9-
// arrived at a fixture field across 131 ms (~13 ms per gap), which is faster than an app that owns
10-
// its field's value and re-applies it after the edit (a controlled React Native `TextInput`, an
11-
// async validator) can acknowledge: such a write lands between two characters of the burst and
12-
// erases what was typed while it was in flight, leaving a value that is stable short of the
13-
// request. 12 characters/second spaces them ~83 ms apart on average, which reduces that loss but
14-
// does not remove it: XCTest does not space the characters evenly, and two of them can reach the
15-
// app a few milliseconds apart. Against a fixture app that acknowledges each edit within 40 ms, 60
16-
// characters/second left 1 of 11 characters in 20 of 20 bursts, and this pace left 10 or 11. The
17-
// command refuses a field left short; back-pressure from the field (#2906) is what would prevent
18-
// it. The app-owned-value lane test pins the average spacing the app sees, and the delivery budget
19-
// in TextEntryTiming bounds what the pace costs a long text.
20-
static const NSUInteger RunnerTextEntryTypingSpeedCharactersPerSecond = 12;
8+
// The pace (`typingSpeed:`) is a caller argument, not a constant here: TextEntryTiming owns it so
9+
// the synthesized delivery budget charges the pace the app actually sees (#2955). XCTest's
10+
// `typingSpeed:` is characters per second, and it spaces them unevenly — two characters of a paced
11+
// burst can reach the app a few milliseconds apart at any pace.
2112

2213
typedef id (*RunnerTextMsgSendInit)(id, SEL, NSString *);
2314
typedef id (*RunnerTextMsgSendInitPath)(id, SEL);
@@ -49,6 +40,7 @@
4940
static RunnerSynthesizedTextEntryResult *RunnerSynthesizeTextWithMode(
5041
id application,
5142
NSString *text,
43+
NSUInteger charactersPerSecond,
5244
BOOL replace
5345
);
5446

@@ -65,25 +57,24 @@ @implementation RunnerSynthesizedTextEntryResult
6557

6658
@implementation RunnerSynthesizedTextEntry
6759

68-
+ (NSUInteger)typingSpeedCharactersPerSecond {
69-
return RunnerTextEntryTypingSpeedCharactersPerSecond;
70-
}
71-
7260
+ (RunnerSynthesizedTextEntryResult *)synthesizeTextWithApplication:(id)application
73-
text:(NSString *)text {
74-
return RunnerSynthesizeTextWithMode(application, text, NO);
61+
text:(NSString *)text
62+
charactersPerSecond:(NSUInteger)charactersPerSecond {
63+
return RunnerSynthesizeTextWithMode(application, text, charactersPerSecond, NO);
7564
}
7665

7766
+ (RunnerSynthesizedTextEntryResult *)replaceTextWithApplication:(id)application
78-
text:(NSString *)text {
79-
return RunnerSynthesizeTextWithMode(application, text, YES);
67+
text:(NSString *)text
68+
charactersPerSecond:(NSUInteger)charactersPerSecond {
69+
return RunnerSynthesizeTextWithMode(application, text, charactersPerSecond, YES);
8070
}
8171

8272
@end
8373

8474
static RunnerSynthesizedTextEntryResult *RunnerSynthesizeTextWithMode(
8575
id application,
8676
NSString *text,
77+
NSUInteger charactersPerSecond,
8778
BOOL replace
8879
) {
8980
@try {
@@ -151,7 +142,7 @@ + (RunnerSynthesizedTextEntryResult *)replaceTextWithApplication:(id)application
151142
bridge.typeTextSelector,
152143
text,
153144
0.0,
154-
RunnerTextEntryTypingSpeedCharactersPerSecond,
145+
charactersPerSecond,
155146
YES
156147
);
157148
((RunnerMsgSendAddPath)objc_msgSend)(record, bridge.core.addPathSelector, path);

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SynthesizedTextEntry.swift‎

Lines changed: 147 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)