@hamzaahmadaslam/wp-debuglog-triage 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +205 -2
- package/SECURITY.md +20 -0
- package/package.json +43 -4
- package/src/clean.mjs +133 -0
- package/src/cli.mjs +17 -0
- package/src/group.mjs +98 -0
- package/src/jev.mjs +92 -0
- package/src/main.mjs +144 -0
- package/src/parse.mjs +214 -0
- package/src/questions.mjs +114 -0
- package/src/report.mjs +206 -0
- package/src/source.mjs +166 -0
- package/src/triage.mjs +66 -0
package/src/source.mjs
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// Works out which part of a WordPress site a message comes from, using the file paths in the cleaned message
|
|
2
|
+
// and its stack trace: WordPress core, a plugin, a must-use plugin, a theme, a drop-in, a PHP file in uploads
|
|
3
|
+
// (which should never run), wp-config.php, or another file.
|
|
4
|
+
|
|
5
|
+
// A relative path from cleanText(), optionally followed by a line: "file.php:12", "on line 12", "file.php(12)".
|
|
6
|
+
const PATH = /(?<![\w./@+~%-])((?:\.\.\.\/)?(?:[\w.@+~%-]+\/)*[\w.@+~%-]+\.php)(?:\((\d+)\) : eval\(\)'d code)?(?::(\d+)| on line (\d+)|\((\d+)\))?/g;
|
|
7
|
+
const OUTPUT_STARTED = /output started at ((?:[\w.@+~%-]+\/)*[\w.@+~%-]+\.php)(?::(\d+))?/;
|
|
8
|
+
// Notices that WordPress core raises about someone else's code (_doing_it_wrong(), _deprecated_function() and
|
|
9
|
+
// friends). The file in the message is core's; the caller is not in the log.
|
|
10
|
+
const CORE_NOTICE = /was called incorrectly|deprecated since version/i;
|
|
11
|
+
const TEXT_DOMAIN = /Translation loading for the ([\w-]+) domain/;
|
|
12
|
+
const OWNED = new Set(["plugin", "mu-plugin", "theme", "drop-in", "uploads"]);
|
|
13
|
+
|
|
14
|
+
/** Which part of WordPress a relative path belongs to. */
|
|
15
|
+
export function attributeFile(path) {
|
|
16
|
+
const file = path;
|
|
17
|
+
let m;
|
|
18
|
+
if ((m = /^(?:wp-content|app)\/plugins\/([^/]+)\//.exec(path))) return { type: "plugin", slug: m[1], file };
|
|
19
|
+
if ((m = /^(?:wp-content|app)\/plugins\/([^/]+)\.php$/.exec(path))) return { type: "plugin", slug: m[1], file };
|
|
20
|
+
if ((m = /^(?:wp-content|app)\/mu-plugins\/([^/]+?)(?:\.php)?(?:\/|$)/.exec(path))) return { type: "mu-plugin", slug: m[1], file };
|
|
21
|
+
if ((m = /^(?:wp-content|app)\/themes\/([^/]+)\//.exec(path))) return { type: "theme", slug: m[1], file };
|
|
22
|
+
if (/^(?:wp-content|app)\/uploads\//.test(path)) return { type: "uploads", slug: null, file };
|
|
23
|
+
if ((m = /^wp-content\/([^/]+\.php)$/.exec(path))) return { type: "drop-in", slug: m[1], file };
|
|
24
|
+
if ((m = /^wp-content\/([^/]+)\//.exec(path))) return { type: "other", slug: `wp-content/${m[1]}`, file };
|
|
25
|
+
if (/^(?:wp-includes|wp-admin)\//.test(path)) return { type: "core", slug: null, file };
|
|
26
|
+
if (path === "wp-config.php") return { type: "config", slug: null, file };
|
|
27
|
+
if (/^(?:wp-[a-z-]+|index|xmlrpc)\.php$/.test(path)) return { type: "core", slug: null, file };
|
|
28
|
+
return { type: "other", slug: null, file };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Every path in a cleaned message, in order, with the line number after it when there is one. */
|
|
32
|
+
export function findPaths(message) {
|
|
33
|
+
// For eval()'d code, m[2] is the line of the file that called eval(); the later number is inside the eval'd text.
|
|
34
|
+
return [...message.matchAll(PATH)].map((m) => ({
|
|
35
|
+
path: m[1],
|
|
36
|
+
line: Number(m[2] ?? m[3] ?? m[4] ?? m[5]) || null,
|
|
37
|
+
hasLine: Boolean(m[2] || m[3] || m[4] || m[5]),
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The path that best names the code responsible: the "output started at" file for "headers already sent",
|
|
43
|
+
* otherwise the last path with a line number (PHP puts the error location at the end of the message).
|
|
44
|
+
*/
|
|
45
|
+
export function primaryPath(message) {
|
|
46
|
+
const started = OUTPUT_STARTED.exec(message);
|
|
47
|
+
if (started) return { path: started[1], line: started[2] ? Number(started[2]) : null, via: "output started at" };
|
|
48
|
+
const paths = findPaths(message);
|
|
49
|
+
if (!paths.length) return null;
|
|
50
|
+
const located = paths.filter((p) => p.hasLine);
|
|
51
|
+
const pick = (located.length ? located : paths).at(-1);
|
|
52
|
+
return { path: pick.path, line: pick.line, via: "path" };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const UNKNOWN = { type: "unknown", slug: null, file: null, line: null, via: null };
|
|
56
|
+
|
|
57
|
+
// A database error has no location of its own. Its caller list can include the plugin or theme file that ran the
|
|
58
|
+
// query; the innermost one (last in the list) is the closest to it. Core files in the list say nothing.
|
|
59
|
+
function callerSource(message) {
|
|
60
|
+
const madeBy = / made by (.*)$/.exec(message);
|
|
61
|
+
if (!madeBy) return { ...UNKNOWN };
|
|
62
|
+
const owned = findPaths(madeBy[1])
|
|
63
|
+
.map((p) => attributeFile(p.path))
|
|
64
|
+
.filter((s) => OWNED.has(s.type));
|
|
65
|
+
return owned.length ? { ...owned.at(-1), line: null, via: "caller list" } : { ...UNKNOWN };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The source of one cleaned message. When the message points at core or at no file, the first stack frame in a
|
|
70
|
+
* plugin, theme, drop-in or uploads file names the code that called it.
|
|
71
|
+
*/
|
|
72
|
+
export function sourceOf(message, frames = [], level = null) {
|
|
73
|
+
if (level === "database") return callerSource(message);
|
|
74
|
+
const primary = primaryPath(message);
|
|
75
|
+
let source = primary ? { ...attributeFile(primary.path), line: primary.line, via: primary.via } : { ...UNKNOWN };
|
|
76
|
+
if (!OWNED.has(source.type) && source.type !== "config") {
|
|
77
|
+
const frame = frames.find((f) => f.file && OWNED.has(attributeFile(f.file).type));
|
|
78
|
+
if (frame) return { ...attributeFile(frame.file), line: frame.line, via: "stack trace" };
|
|
79
|
+
}
|
|
80
|
+
if (source.type === "core" && CORE_NOTICE.test(message)) {
|
|
81
|
+
source = { ...source, via: "core notice" };
|
|
82
|
+
const domain = TEXT_DOMAIN.exec(message);
|
|
83
|
+
if (domain) source.textDomain = domain[1];
|
|
84
|
+
}
|
|
85
|
+
return source;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Names a message offers when it has no path: the callers of a failing query (innermost last in WordPress's
|
|
89
|
+
// "made by" list) or the prefix of a custom log line ("example-importer: ...").
|
|
90
|
+
function namesIn(group) {
|
|
91
|
+
if (group.level === "database") {
|
|
92
|
+
const madeBy = / made by (.*)$/.exec(group.message);
|
|
93
|
+
if (!madeBy) return [];
|
|
94
|
+
return madeBy[1]
|
|
95
|
+
.split(", ")
|
|
96
|
+
.reverse()
|
|
97
|
+
.map((call) => call.split(/->|::|\(/)[0].trim())
|
|
98
|
+
.filter(Boolean)
|
|
99
|
+
.map((name) => ({ name, via: "function name" }));
|
|
100
|
+
}
|
|
101
|
+
if (group.level === "log") {
|
|
102
|
+
const prefix = /^\[?([A-Za-z][\w-]*)\]?:? /.exec(group.message);
|
|
103
|
+
return prefix ? [{ name: prefix[1], via: "line prefix" }] : [];
|
|
104
|
+
}
|
|
105
|
+
return [];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* For groups without a file path, guesses the plugin or theme from a function name or line prefix that
|
|
110
|
+
* matches the slug of a plugin or theme seen elsewhere in the same log. Guesses are marked as such.
|
|
111
|
+
*/
|
|
112
|
+
export function guessOwners(groups) {
|
|
113
|
+
const known = new Map();
|
|
114
|
+
for (const group of groups) {
|
|
115
|
+
const { type, slug } = group.source;
|
|
116
|
+
if ((type === "plugin" || type === "mu-plugin" || type === "theme") && slug) known.set(slug.toLowerCase(), { type, slug });
|
|
117
|
+
}
|
|
118
|
+
if (!known.size) return;
|
|
119
|
+
for (const group of groups) {
|
|
120
|
+
if (group.source.type !== "unknown") continue;
|
|
121
|
+
for (const { name, via } of namesIn(group)) {
|
|
122
|
+
const normal = name.toLowerCase().replace(/_/g, "-");
|
|
123
|
+
let best = null;
|
|
124
|
+
for (const [slug, owner] of known) {
|
|
125
|
+
if ((normal === slug || normal.startsWith(`${slug}-`)) && (!best || slug.length > best.slug.length)) best = owner;
|
|
126
|
+
}
|
|
127
|
+
if (best) {
|
|
128
|
+
group.source = { type: best.type, slug: best.slug, file: null, line: null, via };
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** A short phrase for the report and for the state sent to Jev. */
|
|
136
|
+
export function describeSource(source) {
|
|
137
|
+
const base = {
|
|
138
|
+
plugin: `plugin ${source.slug}`,
|
|
139
|
+
"mu-plugin": `must-use plugin ${source.slug}`,
|
|
140
|
+
theme: `theme ${source.slug}`,
|
|
141
|
+
"drop-in": `drop-in ${source.slug}`,
|
|
142
|
+
uploads: `PHP file in the uploads folder (${source.file})`,
|
|
143
|
+
config: "wp-config.php",
|
|
144
|
+
core: "WordPress core",
|
|
145
|
+
other: source.slug ? `other file in ${source.slug}` : `other file ${source.file ?? ""}`.trim(),
|
|
146
|
+
unknown: "unknown (no file path in the message)",
|
|
147
|
+
}[source.type];
|
|
148
|
+
switch (source.via) {
|
|
149
|
+
case "stack trace":
|
|
150
|
+
return `${base} (from the stack trace)`;
|
|
151
|
+
case "caller list":
|
|
152
|
+
return `${base} (from the list of callers)`;
|
|
153
|
+
case "output started at":
|
|
154
|
+
return `${base} (where output started)`;
|
|
155
|
+
case "function name":
|
|
156
|
+
return `${base} (guessed from a function name)`;
|
|
157
|
+
case "line prefix":
|
|
158
|
+
return `${base} (guessed from the line prefix)`;
|
|
159
|
+
case "core notice":
|
|
160
|
+
return source.textDomain
|
|
161
|
+
? `${base}, reporting code that uses the text domain ${source.textDomain}`
|
|
162
|
+
: `${base}, reporting a call from code the log does not name`;
|
|
163
|
+
default:
|
|
164
|
+
return base;
|
|
165
|
+
}
|
|
166
|
+
}
|
package/src/triage.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Sends the planned requests to Jev one after another, reads each answer back onto its group, and decides which
|
|
2
|
+
// groups are clear enough to report as triaged. A group goes to review when either answer's confidence is below
|
|
3
|
+
// the threshold or an answer is missing; nothing is dropped.
|
|
4
|
+
import { askJev } from "./jev.mjs";
|
|
5
|
+
import { KINDS, URGENCY_LABELS } from "./questions.mjs";
|
|
6
|
+
|
|
7
|
+
function readKind(answer) {
|
|
8
|
+
if (!answer || answer.type !== "choice" || !Object.hasOwn(KINDS, answer.choice)) return null;
|
|
9
|
+
if (!Number.isFinite(answer.confidence) || typeof answer.probabilities !== "object") return null;
|
|
10
|
+
return { choice: answer.choice, confidence: answer.confidence, probabilities: answer.probabilities };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function readUrgency(answer) {
|
|
14
|
+
if (!answer || answer.type !== "score" || !Number.isFinite(answer.score) || !Number.isFinite(answer.confidence)) return null;
|
|
15
|
+
// The score is a probability-weighted position from 0 to 3; rounding it gives the level it is closest to.
|
|
16
|
+
const level = Math.min(URGENCY_LABELS.length - 1, Math.max(0, Math.round(answer.score)));
|
|
17
|
+
return {
|
|
18
|
+
label: URGENCY_LABELS[level],
|
|
19
|
+
level,
|
|
20
|
+
score: answer.score,
|
|
21
|
+
confidence: answer.confidence,
|
|
22
|
+
probabilities: typeof answer.probabilities === "object" && answer.probabilities ? answer.probabilities : {},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Marks a group "triaged" when both answers exist and reach the threshold, otherwise "review" with reasons. */
|
|
27
|
+
export function decide(group, threshold) {
|
|
28
|
+
const reasons = [];
|
|
29
|
+
if (!group.kind) reasons.push("no kind answer from Jev");
|
|
30
|
+
else if (group.kind.confidence < threshold) reasons.push(`kind confidence ${group.kind.confidence.toFixed(2)}`);
|
|
31
|
+
if (!group.urgency) reasons.push("no urgency answer from Jev");
|
|
32
|
+
else if (group.urgency.confidence < threshold) reasons.push(`urgency confidence ${group.urgency.confidence.toFixed(2)}`);
|
|
33
|
+
group.decision = reasons.length ? "review" : "triaged";
|
|
34
|
+
group.reviewReasons = reasons;
|
|
35
|
+
return group;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Copies the answers for one group out of a response's `answers` map. */
|
|
39
|
+
export function applyAnswers(group, answers, threshold) {
|
|
40
|
+
group.kind = readKind(answers?.[`${group.id}_kind`]);
|
|
41
|
+
group.urgency = readUrgency(answers?.[`${group.id}_urgency`]);
|
|
42
|
+
return decide(group, threshold);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Asks Jev every planned request. Options other than `threshold` go to askJev (key, model, fetch, retries). */
|
|
46
|
+
export async function triage(groups, requests, { threshold, ...jevOptions }) {
|
|
47
|
+
const byId = new Map(groups.map((group) => [group.id, group]));
|
|
48
|
+
const usage = { input_tokens: 0, output_tokens: 0 };
|
|
49
|
+
let model = null;
|
|
50
|
+
for (const request of requests) {
|
|
51
|
+
const response = await askJev(request.state, request.questions, jevOptions);
|
|
52
|
+
model = typeof response.model === "string" ? response.model : model;
|
|
53
|
+
usage.input_tokens += Number(response.usage?.input_tokens) || 0;
|
|
54
|
+
usage.output_tokens += Number(response.usage?.output_tokens) || 0;
|
|
55
|
+
for (const id of request.ids) applyAnswers(byId.get(id), response.answers, threshold);
|
|
56
|
+
}
|
|
57
|
+
return { model, usage, requests: requests.length };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Most urgent first; ties go to the group seen more often, then to the one that appeared first in the log. */
|
|
61
|
+
export function rank(groups) {
|
|
62
|
+
return [...groups].sort(
|
|
63
|
+
(a, b) =>
|
|
64
|
+
(b.urgency?.score ?? -1) - (a.urgency?.score ?? -1) || b.count - a.count || a.firstLogLine - b.firstLogLine,
|
|
65
|
+
);
|
|
66
|
+
}
|