From 1f46a14a79b708ef355938e5c6fba8c59b0029cd Mon Sep 17 00:00:00 2001 From: 777genius Date: Sat, 30 May 2026 22:14:52 +0300 Subject: [PATCH] perf(main): trim runtime process liveness scans --- .../team/TeamRuntimeLivenessResolver.ts | 47 +++++++++++++++---- .../team/TeamRuntimeLivenessResolver.test.ts | 37 +++++++++++++++ 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/src/main/services/team/TeamRuntimeLivenessResolver.ts b/src/main/services/team/TeamRuntimeLivenessResolver.ts index 0d47b74b..8529ed27 100644 --- a/src/main/services/team/TeamRuntimeLivenessResolver.ts +++ b/src/main/services/team/TeamRuntimeLivenessResolver.ts @@ -71,6 +71,10 @@ function escapeRegexLiteral(value: string): string { } export function extractCliArgValues(command: string, argName: string): string[] { + if (!command.includes(argName)) { + return []; + } + const cachedByArg = cliArgValuesCache.get(command); const cachedValues = cachedByArg?.get(argName); if (cachedValues) { @@ -111,6 +115,7 @@ export function commandArgEquals( ): boolean { const normalizedExpected = expected?.trim(); if (!normalizedExpected) return false; + if (!command.includes(argName)) return false; return extractCliArgValues(command, argName).some((value) => value === normalizedExpected); } @@ -149,6 +154,28 @@ function isVerifiedRuntimeProcess(params: { ); } +function findNewestVerifiedRuntimeProcess(params: { + rows: readonly RuntimeProcessTableRow[]; + teamName: string; + agentId?: string; +}): RuntimeProcessTableRow | undefined { + const agentId = params.agentId?.trim(); + if (!agentId) { + return undefined; + } + + let newest: RuntimeProcessTableRow | undefined; + for (const row of params.rows) { + if (!isVerifiedRuntimeProcess({ row, teamName: params.teamName, agentId })) { + continue; + } + if (!newest || row.pid > newest.pid) { + newest = row; + } + } + return newest; +} + function isOpenCodeRuntimeProcess(command: string | undefined): boolean { return (command ?? '').toLowerCase().includes('opencode'); } @@ -227,11 +254,11 @@ export function resolveTeamMemberRuntimeLiveness( }); } - const verifiedProcess = input.processRows - .filter((row) => - isVerifiedRuntimeProcess({ row, teamName: input.teamName, agentId: input.agentId }) - ) - .sort((left, right) => right.pid - left.pid)[0]; + const verifiedProcess = findNewestVerifiedRuntimeProcess({ + rows: input.processRows, + teamName: input.teamName, + agentId: input.agentId, + }); if (verifiedProcess) { return result({ alive: true, @@ -325,11 +352,11 @@ export function resolveTeamMemberRuntimeLiveness( const pane = input.pane; if (pane) { const descendants = collectDescendants(input.processRows, pane.panePid); - const verifiedDescendant = descendants - .filter((row) => - isVerifiedRuntimeProcess({ row, teamName: input.teamName, agentId: input.agentId }) - ) - .sort((left, right) => right.pid - left.pid)[0]; + const verifiedDescendant = findNewestVerifiedRuntimeProcess({ + rows: descendants, + teamName: input.teamName, + agentId: input.agentId, + }); if (verifiedDescendant) { return result({ alive: true, diff --git a/test/main/services/team/TeamRuntimeLivenessResolver.test.ts b/test/main/services/team/TeamRuntimeLivenessResolver.test.ts index 81cbc776..2bb3704f 100644 --- a/test/main/services/team/TeamRuntimeLivenessResolver.test.ts +++ b/test/main/services/team/TeamRuntimeLivenessResolver.test.ts @@ -50,6 +50,39 @@ describe('resolveTeamMemberRuntimeLiveness', () => { expect(result.pid).toBe(222); }); + it('uses the newest verified team and agent process without requiring sorted rows', () => { + const result = resolveTeamMemberRuntimeLiveness({ + teamName: 'demo', + memberName: 'alice', + agentId: 'agent-alice', + backendType: 'tmux', + processRows: [ + { + pid: 222, + ppid: 1, + command: 'node runtime --team-name demo --agent-id agent-alice', + }, + { + pid: 111, + ppid: 1, + command: 'node runtime --team-name demo --agent-id agent-alice', + }, + { + pid: 333, + ppid: 1, + command: 'node runtime --team-name other --agent-id agent-alice', + }, + ], + processTableAvailable: true, + nowIso: NOW, + }); + + expect(result.alive).toBe(true); + expect(result.livenessKind).toBe('runtime_process'); + expect(result.pidSource).toBe('agent_process_table'); + expect(result.pid).toBe(222); + }); + it('keeps a verified process pid visible after bootstrap is confirmed', () => { const result = resolveTeamMemberRuntimeLiveness({ teamName: 'demo', @@ -248,4 +281,8 @@ describe('resolveTeamMemberRuntimeLiveness', () => { expect(extractCliArgValues(command, '--agent-id')).toEqual(['agent alice', 'agent-bob']); expect(extractCliArgValues(command, '--team-name')).toEqual(['demo']); }); + + it('returns no CLI arg values when the flag is absent', () => { + expect(extractCliArgValues('node runtime --other value', '--agent-id')).toEqual([]); + }); });