Skip to content

Commit a0af80e

Browse files
committed
fix(ios): write the simulator clipboard from the runner
Under Xcode 27, simctl pbcopy exits 0 but leaves the device pasteboard empty: it hands the simulator a promise of the data owned by the simctl process, which exits before anything reads it. clipboard write now sets UIPasteboard.general from the runner's own process through a new pasteboardWrite runner command. Reads keep simctl pbpaste, which works.
1 parent 8a62b37 commit a0af80e

19 files changed

Lines changed: 101 additions & 40 deletions

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,35 @@ extension RunnerTests {
6666
return Response(ok: true, data: DataPayload(applicationState: Self.applicationStateName(state)))
6767
}
6868

69+
/// Sets the device's general pasteboard to `text` from the runner's own process. A simulator's
70+
/// `simctl pbcopy` only promises the data from the short-lived `simctl` process, which exits before
71+
/// anything on the device reads it, so the runner is the writer that leaves the text in place.
72+
@MainActor
73+
func executePasteboardWrite(command: Command) -> Response {
74+
guard let text = command.text else {
75+
return Response(
76+
ok: false,
77+
error: ErrorPayload(
78+
code: "INVALID_ARGS",
79+
message: "pasteboardWrite requires text",
80+
hint: "Set text to the string the pasteboard should hold."
81+
)
82+
)
83+
}
84+
#if os(iOS) || os(visionOS)
85+
UIPasteboard.general.string = text
86+
return Response(ok: true, data: DataPayload(message: "pasteboard written"))
87+
#else
88+
return Response(
89+
ok: false,
90+
error: ErrorPayload(
91+
code: "UNSUPPORTED_OPERATION",
92+
message: "pasteboardWrite is supported on iOS and visionOS runners"
93+
)
94+
)
95+
#endif
96+
}
97+
6998
/// `XCUIApplication.State` by the names the TypeScript `AppleApplicationState` type declares, the
7099
/// same names the activation disclosure gives its prior state.
71100
static func applicationStateName(_ state: XCUIApplication.State) -> String {
@@ -423,6 +452,8 @@ extension RunnerTests {
423452
return executeUptime()
424453
case .appState:
425454
return executeAppState(command: command)
455+
case .pasteboardWrite:
456+
return executePasteboardWrite(command: command)
426457
case .activate:
427458
guard
428459
let bundleId = command.appBundleId?.trimmingCharacters(in: .whitespacesAndNewlines),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension RunnerTests {
1414
}
1515
switch command.command {
1616
case .status, .activate, .terminate, .targetReset, .shutdown, .recordStart, .recordStop, .uptime,
17-
.appState, .snapshot:
17+
.appState, .pasteboardWrite, .snapshot:
1818
return Response(
1919
ok: false,
2020
error: ErrorPayload(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ final class RunnerCommandJournal {
139139
.remotePress, .type, .swipe, .scroll, .desktopScroll, .findText, .querySelector, .readText,
140140
.backInApp, .backSystem, .home, .rotate, .appSwitcher, .actionButton, .keyboardDismiss, .keyboardReturn,
141141
.alert, .sequence, .gesture, .gestureViewport, .recordStart, .recordStop,
142-
.status, .uptime, .appState, .activate, .terminate, .targetReset, .shutdown:
142+
.status, .uptime, .appState, .pasteboardWrite, .activate, .terminate, .targetReset, .shutdown:
143143
return true
144144
}
145145
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ enum CommandType: String, Codable, CaseIterable {
3434
case status
3535
case uptime
3636
case appState
37+
case pasteboardWrite
3738
case activate
3839
case terminate
3940
case targetReset
@@ -149,6 +150,11 @@ fileprivate extension CommandTraits {
149150
/// The runner's own lifecycle: no session app is brought forward, and no mutation is proven.
150151
static let runnerLifecycle = CommandTraits(launchPolicy: .noApp)
151152

153+
/// Device state the runner sets from its own process, such as the pasteboard: no app is brought
154+
/// forward, since the state belongs to the device rather than to the session app, and no UI
155+
/// mutation is proven.
156+
static let deviceState = CommandTraits(launchPolicy: .noApp)
157+
152158
/// Commands hosted by the surface that already has focus, which no activation may cancel. A
153159
/// hardware press belongs to the system rather than to the session app, and an alert answers from
154160
/// the modal where it sits; both mutate.
@@ -253,6 +259,9 @@ extension Command {
253259
case .recordStop, .uptime, .terminate, .targetReset, .shutdown:
254260
return .runnerLifecycle
255261

262+
case .pasteboardWrite:
263+
return .deviceState
264+
256265
case .actionButton:
257266
return .presentedSurfaceMutation
258267

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+ModelsTests.swift‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,17 @@ extension RunnerTests {
198198
]
199199

200200
/// Commands that did not exist at the merge-base, so no classification of its is compared with
201-
/// theirs. `appState` arrived with #2929.
202-
private static let commandsNewerThanTheMergeBase: Set<CommandType> = [.appState]
201+
/// theirs. `appState` arrived with #2929, `pasteboardWrite` with the runner-written simulator
202+
/// clipboard.
203+
private static let commandsNewerThanTheMergeBase: Set<CommandType> = [.appState, .pasteboardWrite]
203204

204205
/// The commands production never runs through the prepared path's body: `executeOnMain` answers
205206
/// these before `executeOnMainPrepared` runs, and `executeDispatched` answers `snapshot` earlier
206207
/// still, on both this and the merge-base chain. The merge-base witness predicate was therefore
207208
/// never evaluated for them and neither is the derived one. If a command starts reaching the
208209
/// prepared path, removing it here is a claim the equivalence assertion below has to keep proving.
209210
private static let commandsAnsweredBeforeThePreparedPath: Set<CommandType> = [
210-
.status, .uptime, .appState, .activate, .terminate, .targetReset, .shutdown,
211+
.status, .uptime, .appState, .pasteboardWrite, .activate, .terminate, .targetReset, .shutdown,
211212
.recordStart, .recordStop, .snapshot,
212213
]
213214

@@ -286,6 +287,7 @@ extension RunnerTests {
286287
(.status, expectation(interaction: false, retry: true, launch: .noApp, converts: false)),
287288
(.uptime, expectation(interaction: false, retry: false, launch: .noApp, converts: false)),
288289
(.appState, expectation(interaction: false, retry: true, launch: .noApp, converts: false)),
290+
(.pasteboardWrite, expectation(interaction: false, retry: false, launch: .noApp, converts: false)),
289291
(.activate, expectation(interaction: false, retry: false, launch: .mayLaunch, converts: true)),
290292
(.terminate, expectation(interaction: false, retry: false, launch: .noApp, converts: false)),
291293
(.targetReset, expectation(interaction: false, retry: false, launch: .noApp, converts: false)),

‎contracts/fixtures/runner-requests.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
{"name": "ios-simulator.interactor-read-text.point", "producer": "packages/platform-apple/src/__tests__/runner-requests.test.ts", "request": {"command": "readText", "x": 10, "y": 20, "appBundleId": "com.example.app", "commandId": "<commandId>"}},
4444
{"name": "ios-simulator.interactor-set-orientation.rotate", "producer": "packages/platform-apple/src/__tests__/runner-requests.test.ts", "request": {"command": "rotate", "orientation": "landscape-left", "appBundleId": "com.example.app", "commandId": "<commandId>"}},
4545
{"name": "ios-simulator.interactor-snapshot.every-option", "producer": "packages/platform-apple/src/__tests__/runner-requests.test.ts", "request": {"command": "snapshot", "appBundleId": "com.example.app", "interactiveOnly": true, "preferredBackend": "tree", "customActions": true, "depth": 3, "scope": "Go", "raw": true, "commandId": "<commandId>"}},
46+
{"name": "ios-simulator.interactor-write-clipboard.text", "producer": "packages/platform-apple/src/__tests__/runner-requests.test.ts", "request": {"command": "pasteboardWrite", "text": "one-time code 246810", "commandId": "<commandId>"}},
4647
{"name": "ios-simulator.recording-clock-anchor.snapshot", "producer": "src/__tests__/screen-recording-runner-requests.test.ts", "request": {"command": "snapshot", "appBundleId": "com.example.app", "interactiveOnly": true, "depth": 1}},
4748
{"name": "ios-simulator.recording-start.default", "producer": "src/__tests__/screen-recording-runner-requests.test.ts", "request": {"command": "recordStart", "outPath": "capture.mp4", "appBundleId": "com.example.app"}},
4849
{"name": "ios-simulator.runner-adoption.uptime", "producer": "packages/platform-apple/src/runner/__tests__/runner-requests.test.ts", "request": {"command": "uptime", "commandId": "<commandId>"}},

‎packages/platform-apple/src/__tests__/interactor-runner-provider.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const RUNNER_TRANSPORT_METHODS: Record<
7878
tvRemote: { invoke: (i) => i.tvRemote!('select'), runnerCommand: 'remotePress' },
7979
keyboardDismiss: { invoke: (i) => i.keyboardDismiss!(), runnerCommand: 'keyboardDismiss' },
8080
keyboardEnter: { invoke: (i) => i.keyboardEnter!(), runnerCommand: 'keyboardReturn' },
81+
writeClipboard: { invoke: (i) => i.writeClipboard!('hi'), runnerCommand: 'pasteboardWrite' },
8182
// R59: same reading as `readTextAtPoint` — the macOS-helper branch is reachable only for a
8283
// local desktop surface, which a provider-owned mobile device never carries, so every
8384
// provider-backed alert leg rides the runner. Each spends one runner call when it succeeds.
@@ -93,7 +94,6 @@ const LOCAL_TOOL_METHODS: Record<string, (interactor: Interactor) => Promise<unk
9394
close: (i) => i.close('com.example.app'),
9495
screenshot: (i) => i.screenshot('/dev/null'),
9596
readClipboard: (i) => i.readClipboard!(),
96-
writeClipboard: (i) => i.writeClipboard!('hi'),
9797
setSetting: (i) => i.setSetting('wifi', 'on'),
9898
// `simctl ui ... content_size` is local Apple tooling like the write leg beside it, so a provider-owned
9999
// device has no way to answer a text-size read.

‎packages/platform-apple/src/__tests__/runner-requests.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ const INTERACTOR_SITES: Record<string, InteractorDrive> = {
154154
'tvos.interactor-tv-remote.hold': [TVOS_SIMULATOR, (i) => i.tvRemote!('select', 500)],
155155
'ios-simulator.interactor-keyboard-dismiss.dismiss': [IOS_SIMULATOR, (i) => i.keyboardDismiss!()],
156156
'ios-simulator.interactor-keyboard-enter.return': [IOS_SIMULATOR, (i) => i.keyboardEnter!()],
157+
'ios-simulator.interactor-write-clipboard.text': [
158+
IOS_SIMULATOR,
159+
(i) => i.writeClipboard!('one-time code 246810'),
160+
],
157161
'ios-simulator.interactor-snapshot.every-option': [
158162
IOS_SIMULATOR,
159163
(i) =>

‎packages/platform-apple/src/core/app-device-io.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { requireExecSuccess } from '@agent-device/host-kit/command';
33
import { ensureBootedSimulator, requireSimulatorDevice } from './simulator.ts';
44
import { readMacOsClipboardText, writeMacOsClipboardText } from '../os/macos/apps.ts';
55
import { runSimctlForDevice } from './simctl.ts';
6+
import { runAppleRunnerCommand } from './runner-client.ts';
7+
import type { AppleRunnerCommandOptions } from '../runner/index.ts';
68

79
export async function readIosClipboardText(device: DeviceInfo): Promise<string> {
810
if (isMacOs(device)) {
@@ -17,18 +19,20 @@ export async function readIosClipboardText(device: DeviceInfo): Promise<string>
1719
return result.stdout.replaceAll('\r\n', '\n').replace(/\n$/, '');
1820
}
1921

20-
export async function writeIosClipboardText(device: DeviceInfo, text: string): Promise<void> {
22+
/**
23+
* Writes the device's pasteboard: the macOS host's directly, a simulator's from the runner's own
24+
* process. `simctl pbcopy` hands the simulator only a promise of the data, owned by the `simctl`
25+
* process that exits before anything on the device reads it, so the pasteboard ends up empty.
26+
*/
27+
export async function writeIosClipboardText(
28+
device: DeviceInfo,
29+
text: string,
30+
runnerOptions?: AppleRunnerCommandOptions,
31+
): Promise<void> {
2132
if (isMacOs(device)) {
2233
await writeMacOsClipboardText(text);
2334
return;
2435
}
2536
requireSimulatorDevice(device, 'clipboard');
26-
await ensureBootedSimulator(device);
27-
requireExecSuccess(
28-
await runSimctlForDevice(device, ['pbcopy', device.id], {
29-
allowFailure: true,
30-
stdin: text,
31-
}),
32-
'Failed to write iOS simulator clipboard',
33-
);
37+
await runAppleRunnerCommand(device, { command: 'pasteboardWrite', text }, runnerOptions);
3438
}

‎packages/platform-apple/src/interactor.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export function createAppleInteractor(
192192
};
193193
},
194194
readClipboard: () => readIosClipboardText(device),
195-
writeClipboard: (text) => writeIosClipboardText(device, text),
195+
writeClipboard: (text) => writeIosClipboardText(device, text, runnerOpts),
196196
setSetting: (setting, state, appId, options) =>
197197
setIosSetting(device, setting, state, appId, options),
198198
readSetting: (setting) => readIosSetting(device, setting),
@@ -363,7 +363,6 @@ function withInjectedAppleRunnerTransport(
363363
close: async () => rejectLocalAppleToolMethod('close'),
364364
screenshot: async () => rejectLocalAppleToolMethod('screenshot'),
365365
readClipboard: async () => rejectLocalAppleToolMethod('readClipboard'),
366-
writeClipboard: async () => rejectLocalAppleToolMethod('writeClipboard'),
367366
setSetting: async () => rejectLocalAppleToolMethod('setSetting'),
368367
readSetting: async () => rejectLocalAppleToolMethod('readSetting'),
369368
};

0 commit comments

Comments
 (0)