fix(ingest): non-empty stack_trace for hub occurrences (BIT-796)

Accept stack_trace as an alias for stacktrace. When both are absent, store a
synthesized stack line derived from context so recent-errors always exposes
dedupe-friendly text. Fingerprints still ignore synthesized stacks so grouping
matches legacy omit-stack payloads.

PHP ingest client: fatal shutdown handler now sends a minimal stack line.

Paperclip-Agent: David Santos (id 0b1fcf92-5fe3-4ac0-9f29-df4ab72f5376)
Paperclip-Issue: BIT-796
Paperclip-Run: 7f4a8c69-7b5b-47f2-9fc9-c99e6d57f2d2
Made-with: Cursor
This commit is contained in:
David Santos committed 2026-04-04 18:26:15 +00:00
1 parent b41f17886a
commit bd6e3805ec
5 files changed
+105 -5

No files matched your search

+8 -3
View File
@@ -69,12 +69,17 @@ final class ErrorReporter
if (!in_array($err['type'], $fatal, true)) {
return;
}
$file = (string) ($err['file'] ?? '');
$line = (int) ($err['line'] ?? 0);
$syn = $file !== '' && $line > 0
? "[fatal shutdown] {$file}:{$line}"
: '[fatal shutdown] (location unknown)';
self::reportMessage(
(string) $err['message'],
'critical',
(string) ($err['file'] ?? ''),
(int) ($err['line'] ?? 0),
'',
$file,
$line,
$syn,
['shutdown' => true, 'errno' => $err['type']]
);
});
+1
View File
@@ -28,6 +28,7 @@ const _ingestEventSchema = {
message: { type: "string", minLength: 1, maxLength: 20000 },
fingerprint: { type: "string", minLength: 1, maxLength: 128 },
stacktrace: { type: "string", minLength: 1, maxLength: 500000 },
stack_trace: { type: "string", minLength: 1, maxLength: 500000 },
context: { type: "object", additionalProperties: true },
},
} as const;
@@ -0,0 +1,42 @@
import { describe, expect, it } from "vitest";
import { clientProvidedStack, synthesizeStackTraceForOccurrence } from "./ingestStacktrace.js";
describe("ingestStacktrace", () => {
it("prefers stacktrace over stack_trace", () => {
expect(
clientProvidedStack({
stacktrace: " a ",
stack_trace: "b",
}),
).toBe("a");
});
it("falls back to stack_trace", () => {
expect(
clientProvidedStack({
stack_trace: " trace ",
}),
).toBe("trace");
});
it("returns null when both empty or missing", () => {
expect(clientProvidedStack({})).toBeNull();
expect(clientProvidedStack({ stacktrace: " " })).toBeNull();
expect(clientProvidedStack({ stack_trace: "" })).toBeNull();
});
it("synthesizes from file and line in context", () => {
const s = synthesizeStackTraceForOccurrence("boom\nline2", {
file_path: "/app/x.php",
line_number: 42,
});
expect(s).toContain("[synthesized:");
expect(s).toContain("/app/x.php:42");
expect(s).toContain("boom");
expect(s).not.toContain("line2");
});
it("synthesizes with message only when no location", () => {
expect(synthesizeStackTraceForOccurrence("only msg", null)).toContain("only msg");
});
});
+44
View File
@@ -0,0 +1,44 @@
/**
* Resolve stack text for ingest: accept CRM-style `stack_trace` alias, and when
* clients omit stacks entirely, synthesize a non-empty line from context so
* recent-errors / dedupe tooling always have a stable string (BIT-796).
*
* Fingerprinting uses only client-provided stacks (not synthesized), so
* grouping stays stable for payloads that never sent a stack.
*/
export function clientProvidedStack(ev: {
stacktrace?: string;
stack_trace?: string;
}): string | null {
const a = typeof ev.stacktrace === "string" ? ev.stacktrace.trim() : "";
if (a !== "") {
return a;
}
const b = typeof ev.stack_trace === "string" ? ev.stack_trace.trim() : "";
if (b !== "") {
return b;
}
return null;
}
const _SYNTH_HEAD = "[synthesized: no stack trace in ingest payload]";
export function synthesizeStackTraceForOccurrence(
message: string,
context: Record<string, unknown> | null,
): string {
const firstLine = message.split(/\r?\n/)[0]?.trim() ?? message;
if (!context) {
return `${_SYNTH_HEAD}\n${firstLine}`;
}
const fp = String(context.file_path ?? context.filename ?? context.file ?? "").trim();
const ln = String(context.line_number ?? context.line ?? context.lineno ?? "").trim();
if (fp !== "" && ln !== "") {
return `${_SYNTH_HEAD}\n#0 ${fp}:${ln}: ${firstLine}`;
}
if (fp !== "") {
return `${_SYNTH_HEAD}\n#0 ${fp}: ${firstLine}`;
}
return `${_SYNTH_HEAD}\n${firstLine}`;
}
+10 -2
View File
@@ -12,6 +12,10 @@ import {
parseOccurredAt,
} from "../lib/normalize.js";
import { evaluateNoise, NOISE_RULESET_VERSION } from "../lib/noisePolicy.js";
import {
clientProvidedStack,
synthesizeStackTraceForOccurrence,
} from "../lib/ingestStacktrace.js";
export type IngestEventInput = {
schema_version: number;
@@ -20,6 +24,8 @@ export type IngestEventInput = {
message: string;
fingerprint?: string;
stacktrace?: string;
/** CRM / snake_case alias for `stacktrace` (same fingerprint rules as stacktrace). */
stack_trace?: string;
context?: Record<string, unknown>;
};
@@ -178,14 +184,16 @@ async function _ingestOneEvent(
fingerprint_policy_version: FINGERPRINT_POLICY_VERSION,
};
}
const _clientStack = clientProvidedStack(_ev);
const _fingerprint = computeFingerprint({
clientFingerprint: _ev.fingerprint,
message: _message,
stacktrace: _ev.stacktrace,
...(_clientStack !== null ? { stacktrace: _clientStack } : {}),
context: _context,
});
const _title = incidentTitleFromMessage(_message);
const _stack = typeof _ev.stacktrace === "string" ? _ev.stacktrace : null;
const _stack =
_clientStack !== null ? _clientStack : synthesizeStackTraceForOccurrence(_message, _context);
await client.execute(
`INSERT INTO er_incidents (