refactor: remove abandoned test-only analysis scaffolding
This commit is contained in:
@@ -1,112 +0,0 @@
|
||||
import type { ProcessNode, ProcessTreeEvent } from "./eventProcessTree.js";
|
||||
|
||||
/**
|
||||
* Defaults shared by every fixture event. Tests vary only the fields they are
|
||||
* actually asserting on, so a tree reads as the sequence of transitions under
|
||||
* test rather than thirteen repeated literal fields per event.
|
||||
*/
|
||||
const eventDefaults: {
|
||||
arguments: string[];
|
||||
previous_pid: number | null;
|
||||
previous_ppid: number | null;
|
||||
exit_code: number | null;
|
||||
signal: number | null;
|
||||
tid: number | null;
|
||||
} = {
|
||||
arguments: [],
|
||||
previous_pid: null,
|
||||
previous_ppid: null,
|
||||
exit_code: null,
|
||||
signal: null,
|
||||
tid: null,
|
||||
};
|
||||
|
||||
/** One `spawn` event, numbering itself from the sequence given. */
|
||||
export const spawnEvent = (event: {
|
||||
sequence: number;
|
||||
at_ms?: number;
|
||||
pid: number;
|
||||
ppid: number;
|
||||
process_name: string;
|
||||
executable?: string | null;
|
||||
arguments?: string[];
|
||||
}): ProcessTreeEvent => ({
|
||||
...eventDefaults,
|
||||
at_ms: event.at_ms ?? event.sequence * 10,
|
||||
type: "spawn",
|
||||
executable: event.executable ?? `/${event.process_name}`,
|
||||
...event,
|
||||
});
|
||||
|
||||
/** One `exit` event. */
|
||||
export const exitEvent = (event: {
|
||||
sequence: number;
|
||||
at_ms?: number;
|
||||
pid: number;
|
||||
ppid: number | null;
|
||||
process_name: string | null;
|
||||
exit_code?: number;
|
||||
}): ProcessTreeEvent => ({
|
||||
...eventDefaults,
|
||||
at_ms: event.at_ms ?? event.sequence * 10,
|
||||
type: "exit",
|
||||
executable: null,
|
||||
exit_code: 0,
|
||||
...event,
|
||||
});
|
||||
|
||||
/** One `re_exec` event, which keeps the pid and replaces the executable. */
|
||||
export const reExecEvent = (event: {
|
||||
sequence: number;
|
||||
at_ms?: number;
|
||||
pid: number;
|
||||
ppid: number;
|
||||
process_name: string;
|
||||
executable: string;
|
||||
arguments?: string[];
|
||||
}): ProcessTreeEvent => ({
|
||||
...eventDefaults,
|
||||
at_ms: event.at_ms ?? event.sequence * 10,
|
||||
type: "re_exec",
|
||||
previous_pid: null,
|
||||
...event,
|
||||
});
|
||||
|
||||
/** One `reparent` event, which records the pid the process was adopted from. */
|
||||
export const reparentEvent = (event: {
|
||||
sequence: number;
|
||||
at_ms?: number;
|
||||
pid: number;
|
||||
ppid: number;
|
||||
process_name: string;
|
||||
previous_ppid: number;
|
||||
}): ProcessTreeEvent => ({
|
||||
...eventDefaults,
|
||||
at_ms: event.at_ms ?? event.sequence * 10,
|
||||
type: "reparent",
|
||||
executable: null,
|
||||
...event,
|
||||
});
|
||||
|
||||
/** A reconstructed node, defaulting to a childless process with no lifecycle. */
|
||||
export const processNode = (node: {
|
||||
pid: number;
|
||||
ppid: number | null;
|
||||
process_name: string;
|
||||
children?: number[];
|
||||
}): ProcessNode => ({
|
||||
executable: null,
|
||||
arguments: [],
|
||||
spawn_time_ms: null,
|
||||
exit_time_ms: null,
|
||||
exit_code: null,
|
||||
is_re_exec: false,
|
||||
is_reparented: false,
|
||||
previous_ppid: null,
|
||||
...node,
|
||||
children: node.children ?? [],
|
||||
});
|
||||
|
||||
/** The common `spawn` of a root process at pid 1. */
|
||||
export const rootSpawn = (sequence = 0): ProcessTreeEvent =>
|
||||
spawnEvent({ sequence, pid: 1, ppid: 0, process_name: "root" });
|
||||
@@ -1,214 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
containsProcess,
|
||||
getDescendants,
|
||||
maxTreeDepth,
|
||||
reconstructProcessTree,
|
||||
type ProcessTreeEvent,
|
||||
} from "./eventProcessTree.js";
|
||||
import {
|
||||
exitEvent,
|
||||
processNode,
|
||||
reExecEvent,
|
||||
reparentEvent,
|
||||
rootSpawn,
|
||||
spawnEvent,
|
||||
} from "./eventProcessTree.fixture.js";
|
||||
|
||||
describe("event-backed process tree reconstruction", () => {
|
||||
it("walks trees larger than ten thousand nodes", () => {
|
||||
const childPids = Array.from({ length: 10_001 }, (_, index) => index + 2);
|
||||
const root = processNode({
|
||||
pid: 1,
|
||||
ppid: null,
|
||||
process_name: "root",
|
||||
children: childPids,
|
||||
});
|
||||
const children = childPids.map((pid) =>
|
||||
processNode({ pid, ppid: 1, process_name: "root" }),
|
||||
);
|
||||
|
||||
expect(maxTreeDepth([root, ...children], 1)).toBe(1);
|
||||
expect(getDescendants([root, ...children], 1)).toHaveLength(
|
||||
childPids.length,
|
||||
);
|
||||
});
|
||||
|
||||
it("reconstructs a simple process tree", () => {
|
||||
const events: ProcessTreeEvent[] = [
|
||||
rootSpawn(0),
|
||||
spawnEvent({
|
||||
sequence: 1,
|
||||
pid: 2,
|
||||
ppid: 1,
|
||||
process_name: "child",
|
||||
executable: "/child",
|
||||
}),
|
||||
exitEvent({
|
||||
sequence: 2,
|
||||
at_ms: 20,
|
||||
pid: 2,
|
||||
ppid: 1,
|
||||
process_name: "child",
|
||||
}),
|
||||
exitEvent({
|
||||
sequence: 3,
|
||||
at_ms: 30,
|
||||
pid: 1,
|
||||
ppid: 0,
|
||||
process_name: "root",
|
||||
}),
|
||||
];
|
||||
const result = reconstructProcessTree(events, 1);
|
||||
expect(result.processes).toHaveLength(2);
|
||||
expect(result.events_consumed).toBe(4);
|
||||
expect(result.events_unmatched).toBe(0);
|
||||
expect(result.short_lived_descendants).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("short-lived process descendants", () => {
|
||||
it("detects short-lived descendants", () => {
|
||||
const events: ProcessTreeEvent[] = [
|
||||
rootSpawn(0),
|
||||
spawnEvent({
|
||||
sequence: 1,
|
||||
pid: 2,
|
||||
ppid: 1,
|
||||
process_name: "short",
|
||||
executable: "/short",
|
||||
}),
|
||||
exitEvent({
|
||||
sequence: 2,
|
||||
at_ms: 11,
|
||||
pid: 2,
|
||||
ppid: 1,
|
||||
process_name: "short",
|
||||
}),
|
||||
];
|
||||
const result = reconstructProcessTree(events, 1);
|
||||
expect(result.short_lived_descendants).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("event-backed process tree transitions", () => {
|
||||
it("detects re-exec", () => {
|
||||
const events: ProcessTreeEvent[] = [
|
||||
rootSpawn(0),
|
||||
reExecEvent({
|
||||
sequence: 1,
|
||||
pid: 1,
|
||||
ppid: 0,
|
||||
process_name: "root",
|
||||
executable: "/newroot",
|
||||
arguments: ["--new"],
|
||||
}),
|
||||
];
|
||||
const result = reconstructProcessTree(events, 1);
|
||||
expect(result.has_re_exec).toBe(true);
|
||||
const root = result.processes.find((p) => p.pid === 1);
|
||||
expect(root?.is_re_exec).toBe(true);
|
||||
expect(root?.executable).toBe("/newroot");
|
||||
});
|
||||
|
||||
it("detects reparenting", () => {
|
||||
const events: ProcessTreeEvent[] = [
|
||||
rootSpawn(0),
|
||||
spawnEvent({
|
||||
sequence: 1,
|
||||
pid: 2,
|
||||
ppid: 1,
|
||||
process_name: "child",
|
||||
executable: "/child",
|
||||
}),
|
||||
reparentEvent({
|
||||
sequence: 2,
|
||||
pid: 2,
|
||||
ppid: 0,
|
||||
process_name: "child",
|
||||
previous_ppid: 1,
|
||||
}),
|
||||
];
|
||||
const result = reconstructProcessTree(events, 1);
|
||||
expect(result.has_reparenting).toBe(true);
|
||||
const child = result.processes.find((p) => p.pid === 2);
|
||||
expect(child?.is_reparented).toBe(true);
|
||||
expect(child?.previous_ppid).toBe(1);
|
||||
expect(child?.ppid).toBe(0);
|
||||
});
|
||||
|
||||
it("reports unmatched events for unknown exits", () => {
|
||||
const events: ProcessTreeEvent[] = [
|
||||
exitEvent({
|
||||
sequence: 0,
|
||||
at_ms: 0,
|
||||
pid: 999,
|
||||
ppid: null,
|
||||
process_name: null,
|
||||
exit_code: 1,
|
||||
}),
|
||||
];
|
||||
const result = reconstructProcessTree(events, 1);
|
||||
expect(result.events_unmatched).toBe(1);
|
||||
expect(result.processes).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("event-backed process tree queries", () => {
|
||||
it("gets descendants", () => {
|
||||
const events: ProcessTreeEvent[] = [
|
||||
rootSpawn(0),
|
||||
spawnEvent({
|
||||
sequence: 1,
|
||||
pid: 2,
|
||||
ppid: 1,
|
||||
process_name: "child1",
|
||||
executable: "/c1",
|
||||
}),
|
||||
spawnEvent({
|
||||
sequence: 2,
|
||||
pid: 3,
|
||||
ppid: 2,
|
||||
process_name: "grandchild",
|
||||
executable: "/gc",
|
||||
}),
|
||||
];
|
||||
const result = reconstructProcessTree(events, 1);
|
||||
const descendants = getDescendants(result.processes, 1);
|
||||
expect(descendants).toHaveLength(2);
|
||||
expect(descendants).toContain(2);
|
||||
expect(descendants).toContain(3);
|
||||
});
|
||||
|
||||
it("checks process containment", () => {
|
||||
const events: ProcessTreeEvent[] = [rootSpawn(0)];
|
||||
const result = reconstructProcessTree(events, 1);
|
||||
expect(containsProcess(result.processes, 1)).toBe(true);
|
||||
expect(containsProcess(result.processes, 999)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("event-backed process tree depth and types", () => {
|
||||
it("computes max tree depth", () => {
|
||||
const events: ProcessTreeEvent[] = [
|
||||
rootSpawn(0),
|
||||
spawnEvent({
|
||||
sequence: 1,
|
||||
pid: 2,
|
||||
ppid: 1,
|
||||
process_name: "child",
|
||||
executable: "/c",
|
||||
}),
|
||||
spawnEvent({
|
||||
sequence: 2,
|
||||
pid: 3,
|
||||
ppid: 2,
|
||||
process_name: "gc",
|
||||
executable: "/gc",
|
||||
}),
|
||||
];
|
||||
const result = reconstructProcessTree(events, 1);
|
||||
expect(maxTreeDepth(result.processes, 1)).toBe(2);
|
||||
});
|
||||
});
|
||||
@@ -1,262 +0,0 @@
|
||||
import { z } from "zod";
|
||||
|
||||
/** Event types for event-backed process-tree capture. */
|
||||
export const processEventTypeSchema = z.enum([
|
||||
"spawn",
|
||||
"exit",
|
||||
"re_exec",
|
||||
"reparent",
|
||||
"signal",
|
||||
"name_change",
|
||||
"thread_create",
|
||||
"thread_exit",
|
||||
]);
|
||||
|
||||
/** A single event-backed process-tree event. */
|
||||
export const processTreeEventSchema = z.strictObject({
|
||||
/** Monotonic event sequence number. */
|
||||
sequence: z.number().int().nonnegative(),
|
||||
/** Event timestamp in milliseconds. */
|
||||
at_ms: z.number().int().nonnegative(),
|
||||
/** Event type. */
|
||||
type: processEventTypeSchema,
|
||||
/** Process ID. */
|
||||
pid: z.number().int().positive(),
|
||||
/** Parent process ID. */
|
||||
ppid: z.number().int().nullable(),
|
||||
/** Process name if known. */
|
||||
process_name: z.string().nullable(),
|
||||
/** Executable path if known. */
|
||||
executable: z.string().nullable(),
|
||||
/** Command line arguments. */
|
||||
arguments: z.array(z.string()).default([]),
|
||||
/** Previous PID (for reparent events). */
|
||||
previous_pid: z.number().int().nullable(),
|
||||
/** Previous PPID (for reparent events). */
|
||||
previous_ppid: z.number().int().nullable(),
|
||||
/** Exit code (for exit events). */
|
||||
exit_code: z.number().int().nullable(),
|
||||
/** Signal number (for signal events). */
|
||||
signal: z.number().int().nullable(),
|
||||
/** Thread ID (for thread events). */
|
||||
tid: z.number().int().nullable(),
|
||||
});
|
||||
export type ProcessTreeEvent = z.infer<typeof processTreeEventSchema>;
|
||||
|
||||
/** Process node in a reconstructed tree. */
|
||||
export const processNodeSchema = z.strictObject({
|
||||
pid: z.number().int().positive(),
|
||||
ppid: z.number().int().nullable(),
|
||||
process_name: z.string().nullable(),
|
||||
executable: z.string().nullable(),
|
||||
arguments: z.array(z.string()).default([]),
|
||||
children: z.array(z.number().int()).default([]),
|
||||
spawn_time_ms: z.number().int().nullable(),
|
||||
exit_time_ms: z.number().int().nullable(),
|
||||
exit_code: z.number().int().nullable(),
|
||||
is_re_exec: z.boolean().default(false),
|
||||
is_reparented: z.boolean().default(false),
|
||||
previous_ppid: z.number().int().nullable(),
|
||||
});
|
||||
export type ProcessNode = z.infer<typeof processNodeSchema>;
|
||||
|
||||
/** Result of event-backed process-tree reconstruction. */
|
||||
export const processTreeReconstructionSchema = z.strictObject({
|
||||
/** Root PID of the process tree. */
|
||||
root_pid: z.number().int().positive(),
|
||||
/** All reconstructed process nodes. */
|
||||
processes: z.array(processNodeSchema).min(0),
|
||||
/** Events that were consumed. */
|
||||
events_consumed: z.number().int().nonnegative(),
|
||||
/** Events that could not be matched. */
|
||||
events_unmatched: z.number().int().nonnegative(),
|
||||
/** Short-lived descendants that were missed by sampling. */
|
||||
short_lived_descendants: z.number().int().nonnegative(),
|
||||
/** Whether any re-exec was detected. */
|
||||
has_re_exec: z.boolean().default(false),
|
||||
/** Whether any reparenting was detected. */
|
||||
has_reparenting: z.boolean().default(false),
|
||||
});
|
||||
export type ProcessTreeReconstruction = z.infer<
|
||||
typeof processTreeReconstructionSchema
|
||||
>;
|
||||
|
||||
type ProcessTreeAccumulator = {
|
||||
readonly nodes: Map<number, ProcessNode>;
|
||||
readonly children: Map<number, number[]>;
|
||||
eventsUnmatched: number;
|
||||
hasReExec: boolean;
|
||||
hasReparenting: boolean;
|
||||
shortLivedDescendants: number;
|
||||
};
|
||||
|
||||
const applyProcessTreeEvent = (
|
||||
event: ProcessTreeEvent,
|
||||
state: ProcessTreeAccumulator,
|
||||
): void => {
|
||||
let node = state.nodes.get(event.pid);
|
||||
switch (event.type) {
|
||||
case "spawn": {
|
||||
if (!node) {
|
||||
node = {
|
||||
pid: event.pid,
|
||||
ppid: event.ppid,
|
||||
process_name: event.process_name,
|
||||
executable: event.executable,
|
||||
arguments: event.arguments,
|
||||
children: [],
|
||||
spawn_time_ms: event.at_ms,
|
||||
exit_time_ms: null,
|
||||
exit_code: null,
|
||||
is_re_exec: false,
|
||||
is_reparented: false,
|
||||
previous_ppid: null,
|
||||
};
|
||||
state.nodes.set(event.pid, node);
|
||||
}
|
||||
if (event.ppid !== null) {
|
||||
const parentChildren = state.children.get(event.ppid) ?? [];
|
||||
parentChildren.push(event.pid);
|
||||
state.children.set(event.ppid, parentChildren);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "exit":
|
||||
if (node) {
|
||||
node.exit_time_ms = event.at_ms;
|
||||
node.exit_code = event.exit_code;
|
||||
if (
|
||||
node.spawn_time_ms !== null &&
|
||||
event.at_ms - node.spawn_time_ms < 100
|
||||
)
|
||||
state.shortLivedDescendants++;
|
||||
} else state.eventsUnmatched++;
|
||||
break;
|
||||
case "re_exec":
|
||||
if (node) {
|
||||
node.is_re_exec = true;
|
||||
node.executable = event.executable ?? node.executable;
|
||||
node.arguments = event.arguments;
|
||||
state.hasReExec = true;
|
||||
} else state.eventsUnmatched++;
|
||||
break;
|
||||
case "reparent":
|
||||
if (node) {
|
||||
node.previous_ppid = node.ppid;
|
||||
node.ppid = event.ppid;
|
||||
node.is_reparented = true;
|
||||
state.hasReparenting = true;
|
||||
} else state.eventsUnmatched++;
|
||||
break;
|
||||
case "signal":
|
||||
case "name_change":
|
||||
case "thread_create":
|
||||
case "thread_exit":
|
||||
// These events are recorded but don't modify the tree structure.
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
/** Reconstruct a process tree from event-backed capture. */
|
||||
export function reconstructProcessTree(
|
||||
events: readonly ProcessTreeEvent[],
|
||||
rootPid: number,
|
||||
): ProcessTreeReconstruction {
|
||||
const nodes = new Map<number, ProcessNode>();
|
||||
const children = new Map<number, number[]>();
|
||||
let eventsConsumed = 0;
|
||||
const state: ProcessTreeAccumulator = {
|
||||
nodes,
|
||||
children,
|
||||
eventsUnmatched: 0,
|
||||
hasReExec: false,
|
||||
hasReparenting: false,
|
||||
shortLivedDescendants: 0,
|
||||
};
|
||||
|
||||
const sorted = [...events].sort((a, b) => a.at_ms - b.at_ms);
|
||||
|
||||
for (const event of sorted) {
|
||||
eventsConsumed++;
|
||||
applyProcessTreeEvent(event, state);
|
||||
}
|
||||
|
||||
// Attach children to nodes
|
||||
for (const [pid, childPids] of children) {
|
||||
const node = nodes.get(pid);
|
||||
if (node) node.children = childPids;
|
||||
}
|
||||
|
||||
return {
|
||||
root_pid: rootPid,
|
||||
processes: [...nodes.values()],
|
||||
events_consumed: eventsConsumed,
|
||||
events_unmatched: state.eventsUnmatched,
|
||||
short_lived_descendants: state.shortLivedDescendants,
|
||||
has_re_exec: state.hasReExec,
|
||||
has_reparenting: state.hasReparenting,
|
||||
};
|
||||
}
|
||||
|
||||
/** Get all descendant PIDs of a given root PID. */
|
||||
export function getDescendants(
|
||||
processes: readonly ProcessNode[],
|
||||
rootPid: number,
|
||||
): number[] {
|
||||
const byPid = new Map(processes.map((p) => [p.pid, p]));
|
||||
const visited = new Set<number>();
|
||||
const result: number[] = [];
|
||||
const queue: number[] = [rootPid];
|
||||
for (let head = 0; head < queue.length; head += 1) {
|
||||
const pid = queue[head];
|
||||
if (pid === undefined) continue;
|
||||
if (visited.has(pid)) continue;
|
||||
visited.add(pid);
|
||||
const node = byPid.get(pid);
|
||||
if (node) {
|
||||
for (const child of node.children) {
|
||||
if (!visited.has(child)) {
|
||||
result.push(child);
|
||||
queue.push(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Check if a process tree contains a specific PID. */
|
||||
export function containsProcess(
|
||||
processes: readonly ProcessNode[],
|
||||
pid: number,
|
||||
): boolean {
|
||||
return processes.some((p) => p.pid === pid);
|
||||
}
|
||||
|
||||
/** Get the maximum tree depth from the root. */
|
||||
export function maxTreeDepth(
|
||||
processes: readonly ProcessNode[],
|
||||
rootPid: number,
|
||||
): number {
|
||||
const byPid = new Map(processes.map((p) => [p.pid, p]));
|
||||
const pending = [{ pid: rootPid, depth: 0 }];
|
||||
const visited = new Set<number>();
|
||||
let maximumDepth = 0;
|
||||
|
||||
while (pending.length > 0) {
|
||||
const current = pending.pop();
|
||||
if (!current || visited.has(current.pid)) continue;
|
||||
visited.add(current.pid);
|
||||
|
||||
const node = byPid.get(current.pid);
|
||||
if (!node || node.children.length === 0) continue;
|
||||
|
||||
const childDepth = current.depth + 1;
|
||||
maximumDepth = Math.max(maximumDepth, childDepth);
|
||||
for (const childPid of node.children)
|
||||
if (!visited.has(childPid))
|
||||
pending.push({ pid: childPid, depth: childDepth });
|
||||
}
|
||||
|
||||
return maximumDepth;
|
||||
}
|
||||
Reference in New Issue
Block a user