fix(process): preserve numeric output and original PTY evidence

This commit is contained in:
N0zoM1z0
2026-10-09 17:55:28 +08:00
parent aa5750c263
commit 00d8e44cbf
10 changed files with 168 additions and 7 deletions
@@ -828,6 +828,7 @@ export const captureTerminalFrames = (options: {
sequence,
at_ms: atMs,
data: normalized,
...(normalized === data ? {} : { raw_data: data }),
});
options.renderer.write(data, atMs);
options.recordEvent("frames", sequence);
@@ -1,6 +1,9 @@
import { expect, it } from "vitest";
import { normalizeProcessSamples } from "./ProcessNormalization.js";
import {
normalizeProcessSamples,
normalizeProcessText,
} from "./ProcessNormalization.js";
import { parseProcessScenario } from "../../domain/process/processCapture.js";
const rootPid = 41_010;
@@ -68,6 +71,34 @@ const scenario = (normalization: {
},
});
it("preserves compact JSON, counters, line numbers, and ambiguous endpoint spellings", () => {
const input = [
'{"pid":32,"size":108,"height":480,"width":640,"score":20}',
"score=20 size:108 elapsed=3600 file.js:480 server:8080",
"port=65536 port:8080ms localhost:70000 999.1.2.3:8080",
"https://user:32@example.test/path http://user:20@localhost/path",
].join("\n");
expect(normalizeProcessText(input, scenario({}), "/temporary", rootPid)).toBe(
input,
);
});
it("normalizes only contextual ports before overlapping PID values and honors disabled rules", () => {
const input =
'http://example.test:8080/x https://user:password@example.test:443/x 127.0.0.1:9 [::1]:65535 localhost:41010 "port":80 tcp_port=123 udp_port:0 listen:8080';
expect(normalizeProcessText(input, scenario({}), "/temporary", rootPid)).toBe(
'http://example.test:<port>/x https://user:password@example.test:<port>/x 127.0.0.1:<port> [::1]:<port> localhost:<port> "port":<port> tcp_port=<port> udp_port:<port> listen:<port>',
);
expect(
normalizeProcessText(
input,
scenario({ ports: false, pids: false }),
"/temporary",
rootPid,
),
).toBe(input);
});
it("normalizes representative process identities and preserves null, zero, and unrelated numbers", () => {
const before = JSON.stringify(samples);
const normalized = normalizeProcessSamples(samples, scenario({}), rootPid);
+23 -2
View File
@@ -1,3 +1,5 @@
import { isIP } from "node:net";
import type {
ProcessSample,
ProcessScenario,
@@ -30,15 +32,34 @@ export const normalizeProcessText = (
`<filesystem-root-${String(index)}>`,
);
}
if (scenario.normalization.ports)
normalized = normalizePortTokens(normalized);
if (scenario.normalization.pids)
normalized = normalizePidTokens(normalized, [pid]);
if (scenario.normalization.ports)
normalized = normalized.replaceAll(/(?<=[:=])\d{2,5}\b/g, "<port>");
for (const pattern of scenario.normalization.patterns)
normalized = normalized.replaceAll(pattern.pattern, pattern.replacement);
return normalized;
};
const normalizePortTokens = (value: string): string => {
const endpointNormalized = value.replaceAll(
/(\b[a-z][a-z0-9+.-]*:\/\/[^\s/?#"'<>]+|\blocalhost|\b(?:\d{1,3}\.){3}\d{1,3}|\[[\da-f:.%]+\]):(\d{1,5})(?![\w.@:])/giu,
(match: string, endpoint: string, port: string) => {
if (Number(port) > 65_535) return match;
const isEndpoint = endpoint.includes("://")
? URL.canParse(`${endpoint}:${port}`)
: endpoint.toLowerCase() === "localhost" ||
isIP(endpoint.replace(/^\[|\]$/gu, "")) !== 0;
return isEndpoint ? `${endpoint}:<port>` : match;
},
);
return endpointNormalized.replaceAll(
/(\b(?:port|tcp_port|udp_port|listen)\b["']?\s*[:=]\s*)(\d{1,5})(?![\w.])/giu,
(match: string, prefix: string, port: string) =>
Number(port) <= 65_535 ? `${prefix}<port>` : match,
);
};
/** Project sampled observations under the caller-selected normalization rules. */
export const normalizeProcessSamples = (
samples: readonly ProcessSample[],