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[] {
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,

View file

@ -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([]);
});
});