Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions packages/computer/src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,14 +828,7 @@ export class ComputerDevice implements AbstractInterface {
const element = opts?.target as LocateResultElement | undefined;

if (element) {
const [x, y] = element.center;
const target = this.toGlobalPoint({ x, y });
this.inputDriver.moveMouse(
Math.round(target.x),
Math.round(target.y),
);
this.inputDriver.mouseClick('left');
await this.inputDriver.delay(INPUT_FOCUS_DELAY);
await this.focusKeyboardTarget(element, INPUT_FOCUS_DELAY);

if (opts?.replace !== false) {
await this.selectAllAndDelete();
Expand All @@ -848,23 +841,14 @@ export class ComputerDevice implements AbstractInterface {
keyboardPress: async (keyName, opts) => {
const target = opts?.target as LocateResultElement | undefined;
if (target) {
const [x, y] = target.center;
const point = this.toGlobalPoint({ x, y });
this.inputDriver.moveMouse(Math.round(point.x), Math.round(point.y));
this.inputDriver.mouseClick('left');
await this.inputDriver.delay(50);
await this.focusKeyboardTarget(target, 50);
}

await this.pressKeyboardShortcut(keyName);
},
clearInput: async (target) => {
if (target) {
const element = target as LocateResultElement;
const [x, y] = element.center;
const point = this.toGlobalPoint({ x, y });
this.inputDriver.moveMouse(Math.round(point.x), Math.round(point.y));
this.inputDriver.mouseClick('left');
await this.inputDriver.delay(100);
await this.focusKeyboardTarget(target as LocateResultElement, 100);
}

await this.selectAllAndDelete();
Expand All @@ -885,6 +869,25 @@ export class ComputerDevice implements AbstractInterface {
process.platform === 'darwin' && options?.keyboardDriver !== 'libnut';
}

private async focusKeyboardTarget(
element: LocateResultElement,
delayMs: number,
): Promise<void> {
const [x, y] = element.center;
if (process.platform === 'darwin') {
// Reuse the macOS tap path, which holds mouse-down long enough for
// AppKit and follows up when the click changes the foreground process.
// A bare libnut mouseClick can be consumed solely as an activation click
// on hosted desktops, leaving subsequent AppleScript typing unfocused.
await this.inputPrimitives.pointer.tap({ x, y });
} else {
const point = this.toGlobalPoint({ x, y });
this.inputDriver.moveMouse(Math.round(point.x), Math.round(point.y));
this.inputDriver.mouseClick('left');
}
await this.inputDriver.delay(delayMs);
}

describe(): string {
return this.description || 'Computer Device';
}
Expand Down
73 changes: 55 additions & 18 deletions packages/computer/tests/ai/fixtures/macos-desktop-smoke-app.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,41 @@ final class FlippedDocumentView: NSView {
final class SmokeButton: NSButton {
var onMouseDown: (() -> Void)?

// AppKit normally consumes the first click while a programmatically
// activated application is still transitioning to the foreground. The
// hosted macOS runner can remain in that transition even after System
// Events says the process is frontmost. Accepting the activation click
// keeps this fixture focused on whether the global mouse event arrived.
override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
true
}

override func mouseDown(with event: NSEvent) {
onMouseDown?()
super.mouseDown(with: event)
}
}

@MainActor
final class SmokeTextField: NSTextField {
override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
true
}
}

@MainActor
final class FixtureController: NSObject, NSApplicationDelegate, NSTextFieldDelegate {
private let readyURL: URL
private let stateURL: URL

private var window: NSWindow!
private var button: SmokeButton!
private var textField: NSTextField!
private var textField: SmokeTextField!
private var scrollView: NSScrollView!
private var activationSource: DispatchSourceSignal?

private var activationCount = 0
private var inputReadyGeneration = 0
private var clickCount = 0
private var buttonActionCount = 0
private var lastKey = ""
Expand Down Expand Up @@ -80,7 +97,7 @@ final class FixtureController: NSObject, NSApplicationDelegate, NSTextFieldDeleg
self.writeState()
}

textField = NSTextField(frame: NSRect(x: 120, y: 275, width: 400, height: 44))
textField = SmokeTextField(frame: NSRect(x: 120, y: 275, width: 400, height: 44))
textField.placeholderString = "Type smoke text"
textField.delegate = self
textField.target = self
Expand Down Expand Up @@ -108,12 +125,7 @@ final class FixtureController: NSObject, NSApplicationDelegate, NSTextFieldDeleg

installActivationSignal()
activateFixture()
window.makeFirstResponder(textField)
writeState()

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.writeReadyMetadata()
}
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
Expand All @@ -140,22 +152,46 @@ final class FixtureController: NSObject, NSApplicationDelegate, NSTextFieldDeleg
}

private func activateFixture() {
focusFixture()
activationCount += 1
let activation = activationCount
NSApplication.shared.unhide(nil)
NSApplication.shared.activate(ignoringOtherApps: true)
NSRunningApplication.current.activate(options: [.activateAllWindows])
writeState()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { [weak self] in
self?.focusFixture()
self?.writeState()

// Present the window on the next AppKit turn, after the activation request
// has reached the application. A readiness generation is published only
// after AppKit itself reports a visible key window; callers synchronize on
// that generation instead of sleeping for an arbitrary duration.
DispatchQueue.main.async { [weak self] in
guard let self else { return }
self.window.orderFrontRegardless()
self.window.makeKeyAndOrderFront(nil)
self.window.makeFirstResponder(self.textField)
self.publishInputReady(for: activation, attemptsRemaining: 40)
}
}

private func focusFixture() {
NSApplication.shared.unhide(nil)
window.orderFrontRegardless()
window.makeKeyAndOrderFront(nil)
NSApplication.shared.activate(ignoringOtherApps: true)
NSRunningApplication.current.activate(options: [.activateAllWindows])
window.makeFirstResponder(textField)
private func publishInputReady(for activation: Int, attemptsRemaining: Int) {
guard activation == activationCount else { return }
if window.isVisible && window.isKeyWindow && NSApplication.shared.isActive {
inputReadyGeneration += 1
writeState()
if inputReadyGeneration == 1 {
writeReadyMetadata()
}
return
}
guard attemptsRemaining > 0 else {
writeState()
return
Comment on lines +185 to +187

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep polling until the fixture becomes ready

When hosted macOS takes more than about 2 seconds to complete AppKit activation, this branch stops the readiness loop permanently and only rewrites the current state, so a later isActive/isKeyWindow transition will never bump inputReadyGeneration or create the initial ready metadata. The TypeScript side waits 30s for launch and 3s per activation, but those timeouts cannot help once the fixture has stopped polling, which can turn a slow-but-successful activation into a smoke-test timeout.

Useful? React with 👍 / 👎.

}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { [weak self] in
self?.publishInputReady(
for: activation,
attemptsRemaining: attemptsRemaining - 1
)
}
}

private func installActivationSignal() {
Expand Down Expand Up @@ -255,6 +291,7 @@ final class FixtureController: NSObject, NSApplicationDelegate, NSTextFieldDeleg
"active": NSApplication.shared.isActive,
"keyWindow": window.isKeyWindow,
"activationCount": activationCount,
"inputReadyGeneration": inputReadyGeneration,
"clickCount": clickCount,
"buttonActionCount": buttonActionCount,
"text": textField.stringValue,
Expand Down
17 changes: 11 additions & 6 deletions packages/computer/tests/ai/macos-desktop-smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ interface FixtureState {
active: boolean;
keyWindow: boolean;
activationCount: number;
inputReadyGeneration: number;
clickCount: number;
buttonActionCount: number;
text: string;
Expand Down Expand Up @@ -161,6 +162,10 @@ function normalizeState(value: unknown): FixtureState {
raw.activationCount,
'state.activationCount',
),
inputReadyGeneration: asFiniteNumber(
raw.inputReadyGeneration,
'state.inputReadyGeneration',
),
clickCount: asFiniteNumber(raw.clickCount, 'state.clickCount'),
buttonActionCount: asFiniteNumber(
raw.buttonActionCount,
Expand Down Expand Up @@ -261,15 +266,15 @@ async function retryFixtureAction(options: {
await waitForJson(
options.stateFile,
normalizeState,
// GitHub's hosted macOS session reports the fixture as frontmost to
// System Events while AppKit still reports inactive/non-key. The
// fixture's activation signal is the reliable synchronization point;
// each caller subsequently verifies that the real input was received.
(state) => state.activationCount > beforeActivation.activationCount,
(state) =>
state.activationCount > beforeActivation.activationCount &&
state.inputReadyGeneration > beforeActivation.inputReadyGeneration &&
state.visible &&
state.active &&
state.keyWindow,
ACTIVATION_TIMEOUT_MS,
options.fixtureProcess,
);
await sleep(250);
await options.action();
const state = await waitForJson(
options.stateFile,
Expand Down
21 changes: 21 additions & 0 deletions packages/computer/tests/unit-test/device-security.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,4 +403,25 @@ describe('ComputerDevice pointer input', () => {
'left',
);
});

it('uses the held tap path when focusing a macOS keyboard target', async () => {
const device = await createConnectedDevice();

await device.inputPrimitives.keyboard.keyboardPress('Enter', {
target: { center: [100, 120] },
});

expect(mockState.libnut.mouseClick).not.toHaveBeenCalled();
expect(mockState.libnut.mouseToggle).toHaveBeenCalledTimes(2);
expect(mockState.libnut.mouseToggle).toHaveBeenNthCalledWith(
1,
'down',
'left',
);
expect(mockState.libnut.mouseToggle).toHaveBeenNthCalledWith(
2,
'up',
'left',
);
});
});
Loading