Skip to content

Commit 5f73cd9

Browse files
nik13claude
andcommitted
fix(typescript): make stripStackTrace linear, mask link attributes
dropRepeatedStackHeader sliced and regex-matched the rest of the message for every blank-line separator (quadratic: about 165 s at 900 KB). It now walks the text once with forward-only indexes and compares the message only when its length can fit. The Exception Group regex, which was quadratic on long whitespace runs, is a suffix check, and the frame search walks lines with indexOf instead of splitting. Link attributes (SpanOptions.links, addLink, addLinks) go through maskAttributes(). Adds 1 MB performance tests (CPU time) for each message shape and a link masking test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 353c0d2 commit 5f73cd9

5 files changed

Lines changed: 267 additions & 25 deletions

File tree

‎typescript/packages/fi-core/src/__tests__/creationAttributes.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ describe("FITracer creation-time attributes", () => {
116116
expect(attributes).toEqual({ "input.value": REDACTED_VALUE });
117117
});
118118

119+
it("link attributes are masked at creation and on addLink(s) with hideInputs", () => {
120+
const linked = trace.getTracer("t").startSpan("other").spanContext();
121+
const link = { context: linked, attributes: { "input.value": PROMPT, "error.message": PROMPT, keep: "ok" } };
122+
const span = tracerWith({ hideInputs: true }).startSpan("x", { links: [link] });
123+
span.addLink({ ...link });
124+
span.addLinks([{ ...link }]);
125+
span.end();
126+
const { links } = onlySpan();
127+
expect(links).toHaveLength(3);
128+
for (const l of links) {
129+
expect(l.attributes).toEqual({ "input.value": REDACTED_VALUE, keep: "ok" });
130+
}
131+
expect(exported(onlySpan())).not.toContain(PROMPT);
132+
});
133+
119134
describe.each([
120135
["hideInputs", { hideInputs: true }],
121136
["hideInputs, env-style explicit hideErrorDetails", { hideInputs: true, hideErrorDetails: true }],
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { describe, it, expect } from "@jest/globals";
2+
import { SpanStatusCode } from "@opentelemetry/api";
3+
import {
4+
BasicTracerProvider,
5+
InMemorySpanExporter,
6+
SimpleSpanProcessor,
7+
} from "@opentelemetry/sdk-trace-base";
8+
import { FITracer } from "../trace/trace-config/FITracer";
9+
import { stripStackTrace } from "../trace/trace-config/errorDetails";
10+
11+
const MB = 1024 * 1024;
12+
/** Generous: the linear code needs a few ms per MB, the quadratic one seconds to minutes */
13+
const LIMIT_MS = 200;
14+
// The tests run on shared CI machines: five runs, best one counts (see time())
15+
const FRAME = " at f (/a.js:1:1)";
16+
17+
function repeatTo(unit: string, size: number): string {
18+
return unit.repeat(Math.ceil(size / unit.length));
19+
}
20+
21+
/**
22+
* The best of five runs, in CPU time (not wall-clock time), so neither a busy machine nor other
23+
* test workers fail a linear algorithm. GC time in this process is included.
24+
*/
25+
function time(fn: () => void): number {
26+
let best = Infinity;
27+
for (let run = 0; run < 5; run++) {
28+
const before = process.cpuUsage();
29+
fn();
30+
const used = process.cpuUsage(before);
31+
best = Math.min(best, (used.user + used.system) / 1000);
32+
}
33+
return best;
34+
}
35+
36+
const SHAPES: Array<[string, () => string]> = [
37+
["a repeated header paragraph then a frame", () => repeatTo("Note: m\n\n", MB) + FRAME],
38+
["repeated paragraphs of different text then a frame", () => {
39+
let text = "";
40+
for (let i = 0; text.length < MB; i++) text += `Note: m${i}\n\n`;
41+
return text + FRAME;
42+
}],
43+
["blank lines then a frame", () => `msg\n${"\n".repeat(MB)}${FRAME}`],
44+
["whitespace-only blank lines then a frame", () => `msg\n${repeatTo(" \n\n", MB)}${FRAME}`],
45+
["a long single line", () => repeatTo("x", MB)],
46+
["a long single line then a frame", () => `${repeatTo("x", MB)}\n${FRAME}`],
47+
["many colons", () => repeatTo(":", MB) + "\n" + FRAME],
48+
["many colons in an at-line", () => ` at ${repeatTo(":1", MB)}`],
49+
["many tracebacks", () => repeatTo("Traceback (most recent call last):\n File \"a.py\", line 1\nValueError: boom\n", MB)],
50+
["many tracebacks on one line", () => repeatTo("Traceback (most recent call last): ", MB)],
51+
["long indented traceback bodies", () =>
52+
`Traceback (most recent call last):\n${repeatTo(" indented source line\n", MB)}ValueError: boom`],
53+
["a long whitespace run before a traceback header", () => `${" ".repeat(MB)}Traceback (most recent call last):\nValueError: boom`],
54+
["many exception-group prefixes", () => repeatTo(" + | Exception Group Traceback (most recent call last):\n", MB)],
55+
];
56+
57+
describe("stripStackTrace is linear in the message size", () => {
58+
it.each(SHAPES)("%s, 1 MB", (_name, build) => {
59+
const input = build();
60+
let result = "";
61+
const elapsed = time(() => {
62+
result = stripStackTrace(input);
63+
});
64+
expect(typeof result).toBe("string");
65+
expect(elapsed).toBeLessThan(LIMIT_MS);
66+
});
67+
68+
it("still strips the frame and keeps the whole message on big inputs", () => {
69+
// Only the frame goes; the repeated paragraphs are not the message followed by its header
70+
expect(stripStackTrace(`${"Note: m\n\n".repeat(51)}${FRAME}`)).toBe("Note: m\n\n".repeat(50) + "Note: m");
71+
expect(stripStackTrace(`m\n\nError: m\n${FRAME}`)).toBe("m");
72+
expect(stripStackTrace(`line one\nline two\n\nError: line one\nline two\n${FRAME}`)).toBe(
73+
"line one\nline two",
74+
);
75+
});
76+
});
77+
78+
describe("a 1 MB message through the span API", () => {
79+
const exporter = new InMemorySpanExporter();
80+
const provider = new BasicTracerProvider({
81+
spanProcessors: [new SimpleSpanProcessor(exporter)],
82+
});
83+
const tracer = new FITracer({ tracer: provider.getTracer("perf"), traceConfig: {} });
84+
85+
it("setStatus in default mode", () => {
86+
const message = repeatTo("Note: m\n\n", MB) + FRAME;
87+
const span = tracer.startSpan("s");
88+
expect(time(() => span.setStatus({ code: SpanStatusCode.ERROR, message }))).toBeLessThan(LIMIT_MS);
89+
span.end();
90+
});
91+
92+
it("setStatus in default mode, plain message", () => {
93+
const span = tracer.startSpan("s");
94+
const message = repeatTo("x", MB);
95+
expect(time(() => span.setStatus({ code: SpanStatusCode.ERROR, message }))).toBeLessThan(LIMIT_MS);
96+
span.end();
97+
});
98+
99+
it("recordException", () => {
100+
const error = new Error(repeatTo("Note: m\n\n", MB));
101+
const span = tracer.startSpan("s");
102+
expect(time(() => span.recordException(error))).toBeLessThan(LIMIT_MS);
103+
span.end();
104+
});
105+
});

‎typescript/packages/fi-core/src/trace/trace-config/FISpan.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,22 @@ import {
210210
return this.span.spanContext();
211211
}
212212

213+
/** Link attributes get the same masking as span attributes */
214+
private maskLink(link: Link): Link {
215+
return link.attributes != null
216+
? { ...link, attributes: maskAttributes({ config: this.config, attributes: link.attributes }) }
217+
: link;
218+
}
219+
213220
addLink(link: Link): this {
214221
if (this.isEnded) return this;
215-
this.span.addLink(link);
222+
this.span.addLink(this.maskLink(link));
216223
return this;
217224
}
218225

219226
addLinks(links: Link[]): this {
220227
if (this.isEnded) return this;
221-
this.span.addLinks(links);
228+
this.span.addLinks(links.map((link) => this.maskLink(link)));
222229
return this;
223230
}
224231
}

‎typescript/packages/fi-core/src/trace/trace-config/FITracer.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,17 @@ import {
152152
attributes != null
153153
? maskAttributes({ config: this.config, attributes })
154154
: undefined;
155+
// Link attributes are span attributes too: same masking
156+
const links = options?.links?.map((link) =>
157+
link.attributes != null
158+
? { ...link, attributes: maskAttributes({ config: this.config, attributes: link.attributes }) }
159+
: link,
160+
);
155161
const span = tracer.startSpan(
156162
name,
157-
{ ...options, attributes: startAttributes },
163+
links != null
164+
? { ...options, attributes: startAttributes, links }
165+
: { ...options, attributes: startAttributes },
158166
context,
159167
);
160168
return new FISpan({ span, config: this.config, startAttributes });

‎typescript/packages/fi-core/src/trace/trace-config/errorDetails.ts‎

Lines changed: 129 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,28 @@ export function getErrorMessage(exception: unknown): string | undefined {
189189
* such as " at least one item is required" from matching.
190190
*/
191191
const JS_STACK_FRAME = /^\s+at\s.*(?::\d+:\d+\)?|\(native\)|<anonymous>\)?)\s*$/;
192+
/**
193+
* The offset of the first stack frame line (see {@link JS_STACK_FRAME}), or -1. Walks the lines
194+
* with indexOf, so no array of lines is built and no line is looked at twice.
195+
*/
196+
function indexOfFirstFrameLine(text: string): number {
197+
let lineStart = 0;
198+
while (lineStart <= text.length) {
199+
let lineEnd = text.indexOf("\n", lineStart);
200+
if (lineEnd === -1) {
201+
lineEnd = text.length;
202+
}
203+
// A frame line starts with whitespace; the cheap check skips the regex for all other lines
204+
if (lineEnd > lineStart && isWhitespaceCode(text.charCodeAt(lineStart))) {
205+
if (JS_STACK_FRAME.test(text.slice(lineStart, lineEnd))) {
206+
return lineStart;
207+
}
208+
}
209+
lineStart = lineEnd + 1;
210+
}
211+
return -1;
212+
}
213+
192214
/** The header of a Python traceback relayed inside an error message */
193215
const PYTHON_TRACEBACK = "Traceback (most recent call last):";
194216
/** The lines Python prints between chained tracebacks */
@@ -198,8 +220,20 @@ const PYTHON_CHAINING_LINES: ReadonlySet<string> = new Set([
198220
]);
199221
/** An error name on its own, followed by ":" ("Error:"), left in front of a relayed traceback */
200222
const BARE_ERROR_NAME = /^[A-Za-z_$][\w$.]*:$/;
201-
/** "<Name>: <rest>": the header line that opens a JavaScript stack */
202-
const NAMED_HEADER = /^([A-Za-z_$][\w$.]*): ([\s\S]*)$/;
223+
224+
/** " + Exception Group" at the end of the text removed, then trimmed (linear, no regex) */
225+
function dropExceptionGroupSuffix(text: string): string {
226+
const trimmed = text.trimEnd();
227+
const suffix = "Exception Group";
228+
if (!trimmed.endsWith(suffix)) {
229+
return text.trim();
230+
}
231+
let end = trimmed.length - suffix.length;
232+
while (end > 0 && /[\s+|]/.test(trimmed[end - 1])) {
233+
end--;
234+
}
235+
return trimmed.slice(0, end).trim();
236+
}
203237

204238
/**
205239
* Removes relayed Python tracebacks: the "Traceback (most recent call last):" header and the
@@ -215,10 +249,7 @@ function stripPythonTracebacks(lines: string[]): { lines: string[]; stripped: bo
215249
if (header !== -1) {
216250
// e.g. "Error: Traceback (most recent call last):" when the traceback is an Error's message,
217251
// or " + Exception Group Traceback (most recent call last):"
218-
const before = line
219-
.slice(0, header)
220-
.replace(/[\s+|]*Exception Group\s*$/, "")
221-
.trim();
252+
const before = dropExceptionGroupSuffix(line.slice(0, header));
222253
if (before.length > 0 && !BARE_ERROR_NAME.test(before)) {
223254
kept.push(before);
224255
}
@@ -241,19 +272,94 @@ function stripPythonTracebacks(lines: string[]): { lines: string[]; stripped: bo
241272
return { lines: kept, stripped };
242273
}
243274

275+
/** Same characters as the regex `\s` and `String.prototype.trim` */
276+
function isWhitespaceCode(code: number): boolean {
277+
return (
278+
(code >= 9 && code <= 13) ||
279+
code === 32 ||
280+
code === 160 ||
281+
code === 0x1680 ||
282+
(code >= 0x2000 && code <= 0x200a) ||
283+
code === 0x2028 ||
284+
code === 0x2029 ||
285+
code === 0x202f ||
286+
code === 0x205f ||
287+
code === 0x3000 ||
288+
code === 0xfeff
289+
);
290+
}
291+
292+
function isNameStart(code: number): boolean {
293+
return (
294+
(code >= 65 && code <= 90) || (code >= 97 && code <= 122) || code === 95 || code === 36
295+
);
296+
}
297+
298+
function isNamePart(code: number): boolean {
299+
return isNameStart(code) || (code >= 48 && code <= 57) || code === 46;
300+
}
301+
244302
/**
245303
* LangChain's `run.error` is `message + "\n\n" + stack`, and a stack opens with
246304
* `"<Name>: <message>"`. When the text is the message followed by that header, returns the
247305
* message, so it is not repeated.
306+
*
307+
* Linear in the text size: every index only moves forward, no tail is sliced or matched by a
308+
* regex per separator, and the message is compared only when its length can fit the tail.
248309
*/
249310
function dropRepeatedStackHeader(text: string): string {
311+
const length = text.length;
250312
let separator = text.indexOf("\n\n");
313+
if (separator === -1) {
314+
return text;
315+
}
316+
// The text trimmed: [start, end)
317+
let start = 0;
318+
while (start < length && isWhitespaceCode(text.charCodeAt(start))) {
319+
start++;
320+
}
321+
let end = length;
322+
while (end > start && isWhitespaceCode(text.charCodeAt(end - 1))) {
323+
end--;
324+
}
325+
let scanned = start; // text[start, scanned) was scanned for the last non-whitespace
326+
let messageEnd = start; // end of the trimmed text before the separator
327+
let tail = 0; // the first non-whitespace at or after the last separator + 2
251328
while (separator !== -1) {
252-
const message = text.slice(0, separator).trim();
253-
const rest = text.slice(separator + 2).trim();
254-
const header = NAMED_HEADER.exec(rest);
255-
if (message.length > 0 && (rest === message || header?.[2].trim() === message)) {
256-
return message;
329+
for (; scanned < separator; scanned++) {
330+
if (!isWhitespaceCode(text.charCodeAt(scanned))) {
331+
messageEnd = scanned + 1;
332+
}
333+
}
334+
const messageLength = messageEnd - start;
335+
if (messageLength > 0) {
336+
tail = Math.max(tail, separator + 2);
337+
while (tail < end && isWhitespaceCode(text.charCodeAt(tail))) {
338+
tail++;
339+
}
340+
// rest === message
341+
if (tail + messageLength === end && text.startsWith(text.slice(start, messageEnd), tail)) {
342+
return text.slice(start, messageEnd);
343+
}
344+
// rest is "<Name>: <message>"
345+
if (tail < end && isNameStart(text.charCodeAt(tail))) {
346+
let cursor = tail + 1;
347+
while (cursor < end && isNamePart(text.charCodeAt(cursor))) {
348+
cursor++;
349+
}
350+
if (text.charCodeAt(cursor) === 58 && text.charCodeAt(cursor + 1) === 32) {
351+
cursor += 2;
352+
while (cursor < end && isWhitespaceCode(text.charCodeAt(cursor))) {
353+
cursor++;
354+
}
355+
if (
356+
cursor + messageLength === end &&
357+
text.startsWith(text.slice(start, messageEnd), cursor)
358+
) {
359+
return text.slice(start, messageEnd);
360+
}
361+
}
362+
}
257363
}
258364
separator = text.indexOf("\n\n", separator + 2);
259365
}
@@ -268,20 +374,21 @@ function dropRepeatedStackHeader(text: string): string {
268374
* final "Type: message" line is kept. Text without a stack is returned unchanged.
269375
*/
270376
export function stripStackTrace(description: string): string {
271-
let lines = description.split("\n");
272-
// A JS stack: everything from the first frame on is the stack.
273-
const firstFrame = lines.findIndex((line) => JS_STACK_FRAME.test(line));
274-
let stripped = firstFrame !== -1;
275-
if (stripped) {
276-
lines = lines.slice(0, firstFrame);
277-
}
278-
const python = stripPythonTracebacks(lines);
279-
stripped = stripped || python.stripped;
377+
// A JS stack: everything from the first frame line on is the stack.
378+
const frame = indexOfFirstFrameLine(description);
379+
let stripped = frame !== -1;
380+
let text = frame === -1 ? description : description.slice(0, Math.max(frame - 1, 0));
381+
if (text.includes(PYTHON_TRACEBACK)) {
382+
// Runs of blank lines end up as one blank line anyway; collapsing them first keeps a message
383+
// of a million newlines from becoming a million-element array.
384+
const python = stripPythonTracebacks(text.replace(/\n{3,}/g, "\n\n").split("\n"));
385+
stripped = stripped || python.stripped;
386+
text = python.lines.join("\n");
387+
}
280388
if (!stripped) {
281389
return description;
282390
}
283-
const text = python.lines.join("\n").replace(/\n{3,}/g, "\n\n").trim();
284-
return dropRepeatedStackHeader(text);
391+
return dropRepeatedStackHeader(text.replace(/\n{3,}/g, "\n\n").trim());
285392
}
286393

287394
/**

0 commit comments

Comments
 (0)