Skip to content

Commit e8f38d6

Browse files
committed
fix(web): compute fleet ownership once per scan and list only version directories
1 parent 932604e commit e8f38d6

4 files changed

Lines changed: 100 additions & 54 deletions

File tree

‎packages/platform-web/src/agent-browser-install-paths.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,18 @@ test('listing finds every installed version and tolerates a missing tools root',
3232
fs.rmSync(stateDir, { recursive: true, force: true });
3333
}
3434
});
35+
36+
test('listing skips the install lock file beside the version directories', () => {
37+
const stateDir = mkdtempForTestSync('agent-device-web-installs-');
38+
try {
39+
const { installDir } = managedInstallPaths(stateDir, '0.39.0');
40+
fs.mkdirSync(installDir, { recursive: true });
41+
fs.writeFileSync(path.join(installDir, '..', '.agent-browser-install.lock'), '');
42+
43+
expect(listManagedInstallPaths(stateDir).map((install) => install.installDir)).toEqual([
44+
installDir,
45+
]);
46+
} finally {
47+
fs.rmSync(stateDir, { recursive: true, force: true });
48+
}
49+
});

‎packages/platform-web/src/agent-browser-install-paths.ts‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import crypto from 'node:crypto';
22
import path from 'node:path';
3-
import { hostTemporaryDirectory, readHostDirectorySync } from '@agent-device/host-kit/host-file';
3+
import {
4+
hostFileStatSync,
5+
hostTemporaryDirectory,
6+
readHostDirectorySync,
7+
} from '@agent-device/host-kit/host-file';
48
import { hostPlatform } from '@agent-device/host-kit/process';
59

610
/** Everything that locates one version's managed install, its home, and its sockets. */
@@ -29,13 +33,17 @@ export function managedInstallPaths(stateDir: string, version: string): ManagedI
2933
* them rather than the one the running pin names.
3034
*/
3135
export function listManagedInstallPaths(stateDir: string): ManagedInstallPaths[] {
32-
let versions: string[];
36+
const root = managedToolsRoot(stateDir);
37+
let entries: string[];
3338
try {
34-
versions = readHostDirectorySync(managedToolsRoot(stateDir));
35-
} catch {
36-
return [];
39+
entries = readHostDirectorySync(root);
40+
} catch (error) {
41+
if (isNoEntryError(error)) return [];
42+
throw error;
3743
}
38-
return versions.map((version) => managedInstallPaths(stateDir, version));
44+
return entries
45+
.filter((entry) => isDirectory(path.join(root, entry)))
46+
.map((version) => managedInstallPaths(stateDir, version));
3947
}
4048

4149
function managedToolsRoot(stateDir: string): string {
@@ -54,3 +62,16 @@ function resolveManagedSocketDir(installDir: string): string {
5462
function installHash(installDir: string): string {
5563
return crypto.createHash('sha1').update(installDir).digest('hex').slice(0, 12);
5664
}
65+
66+
function isDirectory(entryPath: string): boolean {
67+
try {
68+
return hostFileStatSync(entryPath).isDirectory();
69+
} catch (error) {
70+
if (isNoEntryError(error)) return false;
71+
throw error;
72+
}
73+
}
74+
75+
function isNoEntryError(error: unknown): boolean {
76+
return typeof error === 'object' && error !== null && 'code' in error && error.code === 'ENOENT';
77+
}

‎packages/platform-web/src/agent-browser-process-record.ts‎

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -62,51 +62,31 @@ export function matchAgentBrowserChromeProcess(
6262
processInfo: HostProcessInfo,
6363
status: AgentBrowserToolStatus,
6464
): AgentBrowserProcessMatch | undefined {
65-
if (processInfo.pid === hostProcessId() || !isChromeLikeCommand(processInfo.command))
66-
return undefined;
67-
const installs = managedInstalls(status);
68-
if (
69-
installs.some((install) =>
70-
processInfo.command.includes(agentBrowserChromeLaunchMarker(install)),
71-
)
72-
) {
73-
return { process: processInfo, reason: 'launch-marker' };
74-
}
75-
if (
76-
installs
77-
.flatMap(managedBrowserHomeMarkers)
78-
.some((marker) => isCommandUnderPath(processInfo, marker))
79-
) {
80-
return { process: processInfo, reason: 'managed-browser-home' };
81-
}
82-
return undefined;
65+
return matchChromeProcess(processInfo, managedFleetOwnership(status));
8366
}
8467

8568
export function summarizeAgentBrowserProcesses(
8669
processes: HostProcessInfo[],
8770
status: AgentBrowserToolStatus,
8871
): AgentBrowserProcessSummary {
89-
return summarizeMatches(processes, (processInfo) =>
90-
matchAgentBrowserChromeProcess(processInfo, status),
91-
);
72+
const ownership = managedFleetOwnership(status);
73+
return summarizeMatches(processes, (processInfo) => matchChromeProcess(processInfo, ownership));
9274
}
9375

9476
function matchManagedAgentBrowserProcess(
9577
processInfo: HostProcessInfo,
96-
status: AgentBrowserToolStatus,
78+
ownership: ManagedFleetOwnership,
9779
): AgentBrowserProcessMatch | undefined {
98-
return (
99-
matchAgentBrowserChromeProcess(processInfo, status) ??
100-
matchAgentBrowserDaemonProcess(processInfo, status)
101-
);
80+
return matchChromeProcess(processInfo, ownership) ?? matchDaemonProcess(processInfo, ownership);
10281
}
10382

10483
export function summarizeManagedAgentBrowserProcesses(
10584
processes: HostProcessInfo[],
10685
status: AgentBrowserToolStatus,
10786
): AgentBrowserProcessSummary {
87+
const ownership = managedFleetOwnership(status);
10888
return summarizeMatches(processes, (processInfo) =>
109-
matchManagedAgentBrowserProcess(processInfo, status),
89+
matchManagedAgentBrowserProcess(processInfo, ownership),
11090
);
11191
}
11292

@@ -121,14 +101,17 @@ export async function recordManagedAgentBrowserProcesses(
121101
store: OwnedProcessRecordStore,
122102
): Promise<AgentBrowserProcessSummary> {
123103
const processes = await listRealHostProcesses();
124-
const roots = summarizeManagedAgentBrowserProcesses(processes, status);
104+
const ownership = managedFleetOwnership(status);
105+
const roots = summarizeMatches(processes, (processInfo) =>
106+
matchManagedAgentBrowserProcess(processInfo, ownership),
107+
);
125108
const ownedProcesses = expandProcessTree(roots.pids, processes);
126109
const summary: AgentBrowserProcessSummary = {
127110
count: ownedProcesses.length,
128111
pids: ownedProcesses.map(({ pid }) => pid),
129112
processes: ownedProcesses.map((process) => ({
130113
process,
131-
reason: matchManagedAgentBrowserProcess(process, status)?.reason ?? 'descendant',
114+
reason: matchManagedAgentBrowserProcess(process, ownership)?.reason ?? 'descendant',
132115
})),
133116
};
134117
const records: OwnedProcessRecord[] = summary.processes.flatMap(({ process: processInfo }) => {
@@ -222,38 +205,65 @@ function isChromeLikeCommand(command: string): boolean {
222205
);
223206
}
224207

225-
function matchAgentBrowserDaemonProcess(
208+
/**
209+
* What marks a process as part of the managed fleet: the current install plus every sibling
210+
* version directory under the same state dir, so a fleet left running by a previous pin still
211+
* matches after an upgrade moves the current install.
212+
*/
213+
type ManagedFleetOwnership = {
214+
launchMarkers: string[];
215+
browserHomeRoots: string[];
216+
installRoots: string[];
217+
};
218+
219+
function managedFleetOwnership(status: AgentBrowserToolStatus): ManagedFleetOwnership {
220+
const siblings = listManagedInstallPaths(status.stateDir).filter(
221+
(install) => path.resolve(install.installDir) !== path.resolve(status.installDir),
222+
);
223+
const installs: ManagedInstallPaths[] = [status, ...siblings];
224+
return {
225+
launchMarkers: installs.map(agentBrowserChromeLaunchMarker),
226+
browserHomeRoots: uniqueStrings(installs.flatMap(managedBrowserHomeMarkers)),
227+
installRoots: installs.map(
228+
(install) => `${normalizePathSeparators(path.resolve(install.installDir))}/`,
229+
),
230+
};
231+
}
232+
233+
function matchChromeProcess(
226234
processInfo: HostProcessInfo,
227-
status: AgentBrowserToolStatus,
235+
ownership: ManagedFleetOwnership,
236+
): AgentBrowserProcessMatch | undefined {
237+
if (processInfo.pid === hostProcessId() || !isChromeLikeCommand(processInfo.command))
238+
return undefined;
239+
if (ownership.launchMarkers.some((marker) => processInfo.command.includes(marker))) {
240+
return { process: processInfo, reason: 'launch-marker' };
241+
}
242+
if (ownership.browserHomeRoots.some((root) => isCommandUnderPath(processInfo, root))) {
243+
return { process: processInfo, reason: 'managed-browser-home' };
244+
}
245+
return undefined;
246+
}
247+
248+
function matchDaemonProcess(
249+
processInfo: HostProcessInfo,
250+
ownership: ManagedFleetOwnership,
228251
): AgentBrowserProcessMatch | undefined {
229252
if (processInfo.pid === hostProcessId() || isChromeLikeCommand(processInfo.command))
230253
return undefined;
231254
const command = normalizePathSeparators(processInfo.command);
232-
return managedInstalls(status).some((install) =>
233-
command.includes(`${normalizePathSeparators(path.resolve(install.installDir))}/`),
234-
)
255+
return ownership.installRoots.some((root) => command.includes(root))
235256
? { process: processInfo, reason: 'agent-browser-daemon' }
236257
: undefined;
237258
}
238259

239-
/**
240-
* The current install plus every sibling version directory under the same state dir, so a fleet
241-
* left running by a previous pin still matches after an upgrade moves the current install.
242-
*/
243-
function managedInstalls(status: AgentBrowserToolStatus): ManagedInstallPaths[] {
244-
const siblings = listManagedInstallPaths(status.stateDir).filter(
245-
(install) => path.resolve(install.installDir) !== path.resolve(status.installDir),
246-
);
247-
return [status, ...siblings];
248-
}
249-
250260
function managedBrowserHomeMarkers(
251261
status: Pick<ManagedInstallPaths, 'homeDir' | 'runtimeHomeDir'>,
252262
): string[] {
253-
return uniqueStrings([
263+
return [
254264
path.join(status.homeDir, '.agent-browser', 'browsers'),
255265
path.join(status.runtimeHomeDir, '.agent-browser', 'browsers'),
256-
]);
266+
];
257267
}
258268

259269
function isCommandUnderPath(processInfo: HostProcessInfo, rootPath: string): boolean {

‎packages/platform-web/src/agent-browser-tool.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ function hasManifest(installDir: string): boolean {
215215
/**
216216
* The backend's declared `bin` entry. Read from the installed manifest rather
217217
* than hard-coded, because the path inside the package is the package's to
218-
* choose (`bin/agent-browser.js` in 0.27.1, a native binary in 0.39.0).
218+
* choose (`bin/agent-browser.js` in 0.27.1).
219219
*/
220220
function resolveManagedEntryScript(packageDir: string): string | undefined {
221221
let bin: unknown;

0 commit comments

Comments
 (0)