Skip to content

Commit e2e4ad7

Browse files
committed
refactor(macos): share one bundle-id check and keep dotted display names resolvable
Booking.com matched the widened /i bundle-id test and was returned as a literal id. The shared isMacOsBundleId now requires a lowercase first segment, accepts mixed-case later segments and underscores, and is used by apps.ts, helper.ts and macos-app-lease.ts.
1 parent 8f9ca58 commit e2e4ad7

6 files changed

Lines changed: 22 additions & 10 deletions

File tree

‎packages/contracts/src/facades/session.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export type { SessionAction } from '../session-action.ts';
22
export type { SessionScope } from '../session-scope.ts';
33
export {
44
SESSION_SURFACES,
5+
isMacOsBundleId,
56
macOsHelperSurface,
67
macOsSurfaceBackend,
78
parseSessionSurface,

‎packages/contracts/src/session-surface.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ export function readMacOsAppBackend(
3737
return raw === undefined || raw.trim() === '' ? 'xctest' : MACOS_APP_BACKEND_ENUM.parse(raw);
3838
}
3939

40+
// The first segment is the lowercase TLD-style prefix, so a dotted display name like "Booking.com"
41+
// is not a bundle id.
42+
const MACOS_BUNDLE_ID_PATTERN = /^[a-z0-9-]+(?:\.[A-Za-z0-9_-]+)+$/;
43+
44+
export function isMacOsBundleId(value: string): boolean {
45+
return MACOS_BUNDLE_ID_PATTERN.test(value);
46+
}
47+
4048
const MACOS_HELPER_SURFACE_BACKENDS = {
4149
'frontmost-app': 'macos-helper',
4250
desktop: 'macos-helper',

‎packages/daemon/src/resources/macos-app-lease.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import os from 'node:os';
44
import { normalizeBatchCommandName } from '@agent-device/command-registry/batch-policy';
55
import { isRemoteTempArtifactPath } from '@agent-device/contracts/daemon-http';
66
import type { DeviceLease } from '@agent-device/contracts/device';
7-
import { readMacOsAppBackend } from '@agent-device/contracts/session';
7+
import { isMacOsBundleId, readMacOsAppBackend } from '@agent-device/contracts/session';
88
import { runCmd } from '@agent-device/host-kit/command';
99
import { isProcessAlive } from '@agent-device/host-kit/process';
1010
import { isMacOs } from '@agent-device/kernel/device';
@@ -20,15 +20,14 @@ import type { SessionState } from '../core/session-state.ts';
2020
*/
2121
export type MacOsAppLeaseKey = Readonly<{ bundleId: string; pid?: number }>;
2222

23-
const BUNDLE_ID = /^[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)+$/;
2423
const PID = /^[1-9][0-9]{0,9}$/;
2524

2625
export function parseMacOsAppLeaseKey(deviceKey: string | undefined): MacOsAppLeaseKey {
2726
const value = deviceKey?.trim() ?? '';
2827
const at = value.lastIndexOf('@');
2928
const bundleId = at < 0 ? value : value.slice(0, at);
3029
const pidText = at < 0 ? undefined : value.slice(at + 1);
31-
if (!BUNDLE_ID.test(bundleId) || (pidText !== undefined && !PID.test(pidText))) {
30+
if (!isMacOsBundleId(bundleId) || (pidText !== undefined && !PID.test(pidText))) {
3231
throw new AppError(
3332
'INVALID_ARGS',
3433
'A macos-app lease device key must be <bundleId> or <bundleId>@<pid>.',

‎packages/platform-apple/src/os/macos/apps.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ test('openMacOsApp resolves a display name to its canonical mixed-case bundle id
110110
expect(host.openBundle).toHaveBeenCalledWith('com.apple.TextEdit');
111111
});
112112

113+
test('openMacOsApp resolves a dotted mixed-case display name instead of treating it as a bundle id', async () => {
114+
const host = macOsHost([{ name: 'Booking.com', bundleId: 'com.booking.BookingApp' }]);
115+
116+
await host.run(() => openMacOsApp(MACOS_DEVICE, 'Booking.com'));
117+
118+
expect(host.openBundle).toHaveBeenCalledWith('com.booking.BookingApp');
119+
});
120+
113121
test('openMacOsApp keeps the typed error for an app that is not installed', async () => {
114122
const host = macOsHost([]);
115123

‎packages/platform-apple/src/os/macos/apps.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AppsFilter } from '@agent-device/contracts/device';
22
import { isDeepLinkTarget } from '@agent-device/contracts/command';
3+
import { isMacOsBundleId } from '@agent-device/contracts/session';
34
import { parseAppearanceAction } from '@agent-device/contracts/settings';
45
import type { DeviceInfo } from '@agent-device/kernel/device';
56
import type { EnvMap } from '@agent-device/kernel/source-value';
@@ -17,7 +18,6 @@ const MACOS_ALIASES: Record<string, string> = {
1718
settings: 'com.apple.systempreferences',
1819
};
1920

20-
const MACOS_BUNDLE_ID_PATTERN = /^[a-z0-9-]+(?:\.[a-z0-9-]+)+$/i;
2121
const MACOS_APP_TERMINATION_POLL_MS = 100;
2222
const MACOS_APP_TERMINATION_MAX_ATTEMPTS = 20;
2323

@@ -29,10 +29,6 @@ const MACOS_APP_RESOLUTION_CACHE_SCOPE = {
2929
} satisfies AppResolutionCacheScope;
3030
const macOsAppResolutionCache = createAppResolutionCache<string>();
3131

32-
function isMacOsBundleId(value: string): boolean {
33-
return MACOS_BUNDLE_ID_PATTERN.test(value);
34-
}
35-
3632
export async function resolveMacOsApp(app: string): Promise<string> {
3733
const trimmed = app.trim();
3834

‎packages/platform-apple/src/os/macos/helper.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
type ExecBackgroundResult,
2323
} from '@agent-device/host-kit/command';
2424
import type { EnvMap } from '@agent-device/kernel/source-value';
25+
import { isMacOsBundleId } from '@agent-device/contracts/session';
2526
import type { MacOsHelperSurface, SessionSurface } from '@agent-device/contracts/session';
2627
import {
2728
hasScopedAppleToolProvider,
@@ -83,11 +84,10 @@ const MACOS_HELPER_INSTALL_ROOT = path.join(
8384
'current',
8485
);
8586
const MACOS_HELPER_MANIFEST_PATH = path.join(MACOS_HELPER_INSTALL_ROOT, 'manifest.json');
86-
const MACOS_BUNDLE_ID_PATTERN = /^[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)+$/;
8787

8888
function assertMacOsBundleId(bundleId: string): string {
8989
const normalized = bundleId.trim();
90-
if (!MACOS_BUNDLE_ID_PATTERN.test(normalized)) {
90+
if (!isMacOsBundleId(normalized)) {
9191
throw new AppError(
9292
'INVALID_ARGS',
9393
'macOS bundle id must use reverse-DNS form like com.example.App',

0 commit comments

Comments
 (0)