perf(main): trim runtime process liveness scans

This commit is contained in:
777genius 2026-05-30 22:14:52 +03:00
parent 4575255c28
commit 1f46a14a79
2 changed files with 74 additions and 10 deletions

View file

@ -71,6 +71,10 @@ function escapeRegexLiteral(value: string): string {
} }
export function extractCliArgValues(command: string, argName: string): string[] { export function extractCliArgValues(command: string, argName: string): string[] {
if (!command.includes(argName)) {
return [];
}
const cachedByArg = cliArgValuesCache.get(command); const cachedByArg = cliArgValuesCache.get(command);
const cachedValues = cachedByArg?.get(argName); const cachedValues = cachedByArg?.get(argName);
if (cachedValues) { if (cachedValues) {
@ -111,6 +115,7 @@ export function commandArgEquals(
): boolean { ): boolean {
const normalizedExpected = expected?.trim(); const normalizedExpected = expected?.trim();
if (!normalizedExpected) return false; if (!normalizedExpected) return false;
if (!command.includes(argName)) return false;
return extractCliArgValues(command, argName).some((value) => value === normalizedExpected); 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 { function isOpenCodeRuntimeProcess(command: string | undefined): boolean {
return (command ?? '').toLowerCase().includes('opencode'); return (command ?? '').toLowerCase().includes('opencode');
} }
@ -227,11 +254,11 @@ export function resolveTeamMemberRuntimeLiveness(
}); });
} }
const verifiedProcess = input.processRows const verifiedProcess = findNewestVerifiedRuntimeProcess({
.filter((row) => rows: input.processRows,
isVerifiedRuntimeProcess({ row, teamName: input.teamName, agentId: input.agentId }) teamName: input.teamName,
) agentId: input.agentId,
.sort((left, right) => right.pid - left.pid)[0]; });
if (verifiedProcess) { if (verifiedProcess) {
return result({ return result({
alive: true, alive: true,
@ -325,11 +352,11 @@ export function resolveTeamMemberRuntimeLiveness(
const pane = input.pane; const pane = input.pane;
if (pane) { if (pane) {
const descendants = collectDescendants(input.processRows, pane.panePid); const descendants = collectDescendants(input.processRows, pane.panePid);
const verifiedDescendant = descendants const verifiedDescendant = findNewestVerifiedRuntimeProcess({
.filter((row) => rows: descendants,
isVerifiedRuntimeProcess({ row, teamName: input.teamName, agentId: input.agentId }) teamName: input.teamName,
) agentId: input.agentId,
.sort((left, right) => right.pid - left.pid)[0]; });
if (verifiedDescendant) { if (verifiedDescendant) {
return result({ return result({
alive: true, alive: true,

View file

@ -50,6 +50,39 @@ describe('resolveTeamMemberRuntimeLiveness', () => {
expect(result.pid).toBe(222); 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', () => { it('keeps a verified process pid visible after bootstrap is confirmed', () => {
const result = resolveTeamMemberRuntimeLiveness({ const result = resolveTeamMemberRuntimeLiveness({
teamName: 'demo', teamName: 'demo',
@ -248,4 +281,8 @@ describe('resolveTeamMemberRuntimeLiveness', () => {
expect(extractCliArgValues(command, '--agent-id')).toEqual(['agent alice', 'agent-bob']); expect(extractCliArgValues(command, '--agent-id')).toEqual(['agent alice', 'agent-bob']);
expect(extractCliArgValues(command, '--team-name')).toEqual(['demo']); 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([]);
});
}); });