@bli-cockpit/telemetry-core 0.1.58 → 0.1.60
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/dist/log-digest/classify.d.ts +48 -0
- package/dist/log-digest/classify.js +81 -0
- package/dist/log-digest/digest-source.d.ts +101 -0
- package/dist/log-digest/digest-source.js +131 -0
- package/dist/log-digest/digest.d.ts +81 -0
- package/dist/log-digest/digest.js +262 -0
- package/dist/log-digest/index.d.ts +19 -0
- package/dist/log-digest/index.js +19 -0
- package/dist/log-digest/judge.d.ts +33 -0
- package/dist/log-digest/judge.js +198 -0
- package/dist/log-digest/log-projects.d.ts +78 -0
- package/dist/log-digest/log-projects.js +163 -0
- package/dist/log-digest/mask.d.ts +12 -0
- package/dist/log-digest/mask.js +106 -0
- package/dist/log-digest/merge.d.ts +12 -0
- package/dist/log-digest/merge.js +65 -0
- package/dist/log-digest/project-classes.d.ts +16 -0
- package/dist/log-digest/project-classes.js +31 -0
- package/dist/log-digest/render.d.ts +51 -0
- package/dist/log-digest/render.js +214 -0
- package/dist/log-digest/scrub.d.ts +11 -0
- package/dist/log-digest/scrub.js +73 -0
- package/dist/log-digest/sources/collector.d.ts +15 -0
- package/dist/log-digest/sources/collector.js +166 -0
- package/dist/log-digest/sources/process.d.ts +2 -0
- package/dist/log-digest/sources/process.js +78 -0
- package/dist/log-digest/sources/railway.d.ts +6 -0
- package/dist/log-digest/sources/railway.js +156 -0
- package/dist/log-digest/sources/stdin.d.ts +9 -0
- package/dist/log-digest/sources/stdin.js +56 -0
- package/dist/log-digest/sources/types.d.ts +67 -0
- package/dist/log-digest/sources/types.js +13 -0
- package/dist/log-digest/sources/vercel.d.ts +40 -0
- package/dist/log-digest/sources/vercel.js +194 -0
- package/dist/log-digest/stored.d.ts +98 -0
- package/dist/log-digest/stored.js +149 -0
- package/dist/log-digest/tagged.d.ts +37 -0
- package/dist/log-digest/tagged.js +114 -0
- package/dist/log-digest/tower-classes.d.ts +28 -0
- package/dist/log-digest/tower-classes.js +301 -0
- package/dist/log-digest/window.d.ts +21 -0
- package/dist/log-digest/window.js +39 -0
- package/package.json +6 -1
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The digest accumulator (port of Kaiben's `log_digest.Digest`).
|
|
3
|
+
*
|
|
4
|
+
* Keeps only the lines that can mean trouble, masks their variable parts into
|
|
5
|
+
* templates, groups equal templates with a count, first/last time and one
|
|
6
|
+
* example, and turns routine lines into counts per event type. Routine runs are
|
|
7
|
+
* collapsed into facts true of every collapsed line: a small value set is
|
|
8
|
+
* enumerated with exact counts, a large one becomes "N distinct" (Caveman).
|
|
9
|
+
*
|
|
10
|
+
* Output is deterministic and order-stable: the same input renders
|
|
11
|
+
* byte-identically.
|
|
12
|
+
*
|
|
13
|
+
* Not ported: the circuit-breaker facts (`breaker`) that read Kaiben's own
|
|
14
|
+
* deploy modules. The result still carries `breaker: {}` so the shape matches.
|
|
15
|
+
*/
|
|
16
|
+
import { DECLARED_WARNING, LONG_LINE_CHARS, judgeJson, jsonRecords, looksImportant, pythonJson, unitGroup } from "./judge.js";
|
|
17
|
+
import { EXAMPLE_CHARS, cut, mask, redact, squash, templateId } from "./mask.js";
|
|
18
|
+
import { TEMPLATE_CHARS } from "./mask.js";
|
|
19
|
+
import { judgeTagged, parseTagged, withoutZeroCounters } from "./tagged.js";
|
|
20
|
+
export const SUBJECTS_TRACKED = 6; // Caveman: enumerate a field only while its value set stays small
|
|
21
|
+
/** Units that only ever say "someone logged in" or "a package timer ran": dropped and counted as noise. */
|
|
22
|
+
export const DEFAULT_NOISE = [
|
|
23
|
+
"^ssh(d)?(\\.service)?$", "^sshd-session$", "^systemd-logind(\\.service)?$", "^session-\\d+\\.scope$",
|
|
24
|
+
"^user@\\d+\\.service$", "^user-runtime-dir@\\d+\\.service$", "^user-\\d+\\.slice$", "^run-user-\\d+\\.mount$",
|
|
25
|
+
"^(cron|CRON)(\\.service)?$", "^dbus(\\.service)?$", "^\\(sd-pam\\)$", "^packagekit(d)?(\\.service)?$",
|
|
26
|
+
"^(apt-news|esm-cache|apt-daily|apt-daily-upgrade|motd-news|man-db|logrotate|dpkg-db-backup|e2scrub_all|fstrim|"
|
|
27
|
+
+ "systemd-tmpfiles-clean|update-notifier-download|update-notifier-motd|fwupd-refresh|fwupd|snapd|"
|
|
28
|
+
+ "unattended-upgrades|systemd-timedated|sysstat-collect|sysstat-summary|polkit|systemd-hostnamed)(\\.service)?$",
|
|
29
|
+
];
|
|
30
|
+
// systemd's own routine words about a unit; the rest of such a line is the unit's description, which can say anything
|
|
31
|
+
const SYSTEMD_ROUTINE = /^(Starting|Started|Finished|Stopping|Stopped|Reloading|Reloaded|Created slice|Removed slice|Listening|Reached target|Closed|Queued|Activating|Deactivated|Consumed|\S+: (Deactivated|Consumed|Succeeded|Main process exited, code=exited, status=0\/SUCCESS))/;
|
|
32
|
+
const EXCEPTION_LINE = /^([A-Za-z_][\w.]*(?:Error|Exception|Interrupt|Exit|Warning|Failure))(?::\s?(.*))?$/;
|
|
33
|
+
// A thrown JavaScript error (`TypeError: ...`, `PostgresError: ...`): the word `error` is glued to its name, so the
|
|
34
|
+
// Python-ported word list (`\berror\b`) never sees it. Only consulted for Tower-shaped sources (`taggedLines`).
|
|
35
|
+
const JS_EXCEPTION = /\b[A-Z][A-Za-z]*(?:Error|Exception)\b/;
|
|
36
|
+
const FRAME_LINE = /^\s+File "([^"]+)", line \d+, in (\S+)/;
|
|
37
|
+
function compileAll(patterns) {
|
|
38
|
+
return patterns.map((pattern) => new RegExp(pattern));
|
|
39
|
+
}
|
|
40
|
+
function emptySubjects() {
|
|
41
|
+
return Object.create(null);
|
|
42
|
+
}
|
|
43
|
+
export class Digest {
|
|
44
|
+
groups = new Map();
|
|
45
|
+
routine = new Map();
|
|
46
|
+
lines = 0;
|
|
47
|
+
noiseLines = 0;
|
|
48
|
+
firstTs = null;
|
|
49
|
+
lastTs = null;
|
|
50
|
+
firstCursor = null;
|
|
51
|
+
lastCursor = null;
|
|
52
|
+
noise;
|
|
53
|
+
quiet;
|
|
54
|
+
routineUnits;
|
|
55
|
+
taggedLines;
|
|
56
|
+
openTracebacks = new Map();
|
|
57
|
+
constructor(options = {}) {
|
|
58
|
+
this.taggedLines = options.taggedLines ?? false;
|
|
59
|
+
this.noise = compileAll(options.noise ?? DEFAULT_NOISE);
|
|
60
|
+
this.quiet = compileAll(options.quietUnits ?? []);
|
|
61
|
+
this.routineUnits = new Set(options.routineUnits ?? []);
|
|
62
|
+
}
|
|
63
|
+
// -- input
|
|
64
|
+
isNoise(unit, identifier) {
|
|
65
|
+
return [unit, identifier].some((name) => name && this.noise.some((pattern) => pattern.test(name)));
|
|
66
|
+
}
|
|
67
|
+
/** One `journalctl -o json` entry. */
|
|
68
|
+
addEntry(entry) {
|
|
69
|
+
let message = entry.MESSAGE;
|
|
70
|
+
if (Array.isArray(message)) {
|
|
71
|
+
// journald stores non-UTF-8 messages as a byte list
|
|
72
|
+
const bytes = message.filter((value) => Number.isInteger(value) && value >= 0 && value < 256);
|
|
73
|
+
message = Buffer.from(bytes).toString("utf8");
|
|
74
|
+
}
|
|
75
|
+
if (typeof message !== "string")
|
|
76
|
+
return;
|
|
77
|
+
if (entry.__CURSOR) {
|
|
78
|
+
this.firstCursor = this.firstCursor ?? entry.__CURSOR;
|
|
79
|
+
this.lastCursor = entry.__CURSOR;
|
|
80
|
+
}
|
|
81
|
+
const parsed = Number.parseInt(String(entry.__REALTIME_TIMESTAMP ?? 0), 10);
|
|
82
|
+
const ts = Number.isFinite(parsed) ? parsed / 1e6 : 0;
|
|
83
|
+
const identifier = entry.SYSLOG_IDENTIFIER;
|
|
84
|
+
let unit = entry.UNIT || entry._SYSTEMD_UNIT;
|
|
85
|
+
if (!unit)
|
|
86
|
+
unit = entry._TRANSPORT === "kernel" ? "kernel" : (identifier || "?");
|
|
87
|
+
const ownUnit = entry._SYSTEMD_UNIT ?? "";
|
|
88
|
+
if (this.isNoise(unit, identifier)
|
|
89
|
+
|| this.isNoise(ownUnit, undefined)
|
|
90
|
+
|| (!entry.UNIT && this.quiet.some((pattern) => pattern.test(ownUnit)))) {
|
|
91
|
+
this.noiseLines += 1;
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.addLine(unit, message, ts, { manager: Boolean(entry.UNIT) });
|
|
95
|
+
}
|
|
96
|
+
/** One log line from any source. `ts` is epoch seconds (0 when the source has none). */
|
|
97
|
+
addLine(unit, message, ts, facts = {}) {
|
|
98
|
+
const manager = facts.manager ?? false;
|
|
99
|
+
this.lines += 1;
|
|
100
|
+
if (ts) {
|
|
101
|
+
this.firstTs = this.firstTs === null ? ts : Math.min(this.firstTs, ts);
|
|
102
|
+
this.lastTs = this.lastTs === null ? ts : Math.max(this.lastTs, ts);
|
|
103
|
+
}
|
|
104
|
+
const [group, instance] = unitGroup(unit);
|
|
105
|
+
const bare = unit.endsWith(".service") ? unit.slice(0, -".service".length) : unit;
|
|
106
|
+
if (!manager && this.routineUnits.has(bare) && (message.startsWith("FLAG ") || message.startsWith("NOTE "))) {
|
|
107
|
+
this.routineCount(group, "relay", null); // watchdog and breaker flags: ingested from their own log files
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (this.foldTraceback(unit, group, instance, message, ts))
|
|
111
|
+
return;
|
|
112
|
+
const stripped = message.trim();
|
|
113
|
+
if (stripped.startsWith("{") || Array.from(stripped).length > LONG_LINE_CHARS) {
|
|
114
|
+
const records = jsonRecords(stripped);
|
|
115
|
+
if (records === null) {
|
|
116
|
+
// journald splits a huge JSON line into pieces; a piece is not a sentence, and words like
|
|
117
|
+
// "archiveFailed" or "errors" inside it are key names, not failures.
|
|
118
|
+
this.routineCount(group, "json_fragment", null);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
for (const data of records) {
|
|
122
|
+
if (this.routineUnits.has(bare)) {
|
|
123
|
+
this.routineCount(group, "relay", null); // watchdog and breaker records: ingested from their own files
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const judged = judgeJson(data);
|
|
127
|
+
if (judged.important && judged.template !== null) {
|
|
128
|
+
this.addGroup(group, instance, judged.event, judged.template, ts, cut(pythonJson(data), EXAMPLE_CHARS * 2));
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
this.routineCount(group, judged.event, judged.subject);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (FRAME_LINE.test(message) || message.startsWith(" ")) {
|
|
137
|
+
this.routineCount(group, "traceback_frame", null); // a frame or code line whose traceback header was interleaved away
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (this.taggedLines && !manager) {
|
|
141
|
+
const parsed = parseTagged(message);
|
|
142
|
+
if (parsed) {
|
|
143
|
+
const judged = judgeTagged(parsed, facts.level);
|
|
144
|
+
if (!judged.important) {
|
|
145
|
+
this.routineCount(group, judged.template, null);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
const template = judged.declaredWarning ? cut(judged.template, TEMPLATE_CHARS - DECLARED_WARNING.length) + DECLARED_WARNING : judged.template;
|
|
149
|
+
this.addGroup(group, instance, "tagged", template, ts, message);
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const event = manager ? "systemd" : unit === "kernel" ? "kernel" : "log";
|
|
155
|
+
// The source's own level is NOT a verdict on a plain line: Tower writes ordinary output to stderr, which Vercel
|
|
156
|
+
// and Railway both mark "error" (measured 2026-10-07). Words and fields decide; see tagged.ts.
|
|
157
|
+
const judgedText = this.taggedLines ? withoutZeroCounters(message) : message;
|
|
158
|
+
if ((looksImportant(judgedText) || (this.taggedLines && JS_EXCEPTION.test(judgedText))) && !(manager && SYSTEMD_ROUTINE.test(message))) {
|
|
159
|
+
this.addGroup(group, instance, event, mask(message), ts, message);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const word = message.split(" ");
|
|
163
|
+
const routineEvent = manager
|
|
164
|
+
? `systemd ${cut((word.length > 1 && word[0].endsWith(":") ? word[1] : word[0]).replace(/^[.:]+|[.:]+$/g, ""), 20)}`
|
|
165
|
+
: "text";
|
|
166
|
+
this.routineCount(group, routineEvent, null);
|
|
167
|
+
}
|
|
168
|
+
// -- internals
|
|
169
|
+
routineCount(group, event, subject) {
|
|
170
|
+
let events = this.routine.get(group);
|
|
171
|
+
if (!events)
|
|
172
|
+
this.routine.set(group, (events = new Map()));
|
|
173
|
+
let record = events.get(event);
|
|
174
|
+
if (!record)
|
|
175
|
+
events.set(event, (record = [0, emptySubjects()]));
|
|
176
|
+
record[0] += 1;
|
|
177
|
+
if (subject !== null) {
|
|
178
|
+
const subjects = record[1];
|
|
179
|
+
if (subject in subjects || Object.keys(subjects).length < SUBJECTS_TRACKED)
|
|
180
|
+
subjects[subject] = (subjects[subject] ?? 0) + 1;
|
|
181
|
+
else
|
|
182
|
+
subjects["…"] = (subjects["…"] ?? 0) + 1;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
addGroup(group, instance, event, template, ts, message) {
|
|
186
|
+
const tid = templateId(group, event, template);
|
|
187
|
+
let record = this.groups.get(tid);
|
|
188
|
+
if (!record) {
|
|
189
|
+
record = {
|
|
190
|
+
tid, unit: group, event, template, count: 0,
|
|
191
|
+
first: ts || null, last: ts || null,
|
|
192
|
+
example: cut(redact(squash(message)), EXAMPLE_CHARS),
|
|
193
|
+
instances: [],
|
|
194
|
+
};
|
|
195
|
+
this.groups.set(tid, record);
|
|
196
|
+
}
|
|
197
|
+
record.count += 1;
|
|
198
|
+
if (ts) {
|
|
199
|
+
record.first = Math.min(record.first ?? ts, ts);
|
|
200
|
+
record.last = Math.max(record.last ?? ts, ts);
|
|
201
|
+
}
|
|
202
|
+
if (instance && !record.instances.includes(instance) && record.instances.length < 64)
|
|
203
|
+
record.instances.push(instance);
|
|
204
|
+
}
|
|
205
|
+
/** Python tracebacks span many lines: fold each into one group named by its exception and last frame. */
|
|
206
|
+
foldTraceback(unit, group, instance, message, ts) {
|
|
207
|
+
const state = this.openTracebacks.get(unit);
|
|
208
|
+
if (message.startsWith("Traceback (most recent call last)")) {
|
|
209
|
+
this.openTracebacks.set(unit, { frame: null });
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
if (!state)
|
|
213
|
+
return false;
|
|
214
|
+
const frame = FRAME_LINE.exec(message);
|
|
215
|
+
if (frame) {
|
|
216
|
+
state.frame = `${frame[1].split("/").pop()}:${frame[2]}`;
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
if (/^[ \t]/.test(message) || message.startsWith("During handling") || message.startsWith("The above exception") || !message.trim()) {
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
if (message.trimStart().startsWith("{"))
|
|
223
|
+
return false; // other threads keep logging JSON mid-traceback: judge it, keep it open
|
|
224
|
+
const exception = EXCEPTION_LINE.exec(message.trim());
|
|
225
|
+
if (exception) {
|
|
226
|
+
const where = state.frame ? ` @${state.frame}` : "";
|
|
227
|
+
const named = `${exception[1]}: ${exception[2] ?? ""}`.replace(/[: ]+$/, "");
|
|
228
|
+
const template = mask(named) + where;
|
|
229
|
+
this.addGroup(group, instance, "traceback", cut(template, TEMPLATE_CHARS), ts, message);
|
|
230
|
+
state.frame = null; // a chained exception may follow; its own frames come first
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
this.openTracebacks.delete(unit); // anything else ends the traceback; judge the line normally
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
// -- output
|
|
237
|
+
/** The result, sorted so equal input renders byte-identically: groups by count then tid, routine by name. */
|
|
238
|
+
result() {
|
|
239
|
+
const routine = Object.create(null);
|
|
240
|
+
for (const group of [...this.routine.keys()].sort()) {
|
|
241
|
+
const events = Object.create(null);
|
|
242
|
+
for (const event of [...this.routine.get(group).keys()].sort()) {
|
|
243
|
+
const [count, subjects] = this.routine.get(group).get(event);
|
|
244
|
+
const sorted = emptySubjects();
|
|
245
|
+
for (const name of Object.keys(subjects).sort())
|
|
246
|
+
sorted[name] = subjects[name];
|
|
247
|
+
events[event] = [count, sorted];
|
|
248
|
+
}
|
|
249
|
+
routine[group] = events;
|
|
250
|
+
}
|
|
251
|
+
const groups = [...this.groups.values()].sort((a, b) => b.count - a.count || (a.tid < b.tid ? -1 : a.tid > b.tid ? 1 : 0));
|
|
252
|
+
return {
|
|
253
|
+
lines: this.lines,
|
|
254
|
+
noise: this.noiseLines,
|
|
255
|
+
span: [this.firstTs, this.lastTs],
|
|
256
|
+
cursors: [this.firstCursor, this.lastCursor],
|
|
257
|
+
groups,
|
|
258
|
+
routine,
|
|
259
|
+
breaker: {},
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export { mask, redact, templateId, cut, squash, TEMPLATE_CHARS, EXAMPLE_CHARS } from "./mask.js";
|
|
2
|
+
export { DECLARED_WARNING, LONG_LINE_CHARS, judgeJson, jsonEvent, jsonRecords, looksImportant, pythonJson, unitGroup, type JsonJudgement, type JsonObject, } from "./judge.js";
|
|
3
|
+
export { DEFAULT_NOISE, Digest, SUBJECTS_TRACKED, type DigestGroup, type DigestOptions, type DigestResult, type JournalEntry, type LineFacts, type RoutineEvents, } from "./digest.js";
|
|
4
|
+
export { mergeResults, type MergeInput, type MergedResult } from "./merge.js";
|
|
5
|
+
export { CLASS_ORDER, ClassTable, type Classify, type ClassInfo, type ClassTableData, type Severity, type TablePattern, type Verdict, } from "./classify.js";
|
|
6
|
+
export { clock, fleetView, numberText, renderCapped, renderFleet, renderLocal, routineText, subjectsText, timeRange, type CappedOptions, type FleetView, } from "./render.js";
|
|
7
|
+
export { TOWER_CLASSES, TOWER_PATTERNS, TOWER_TABLE, towerClassTable } from "./tower-classes.js";
|
|
8
|
+
export { WindowError, isoOf, parseWindow, relativeSeconds, type LogWindow } from "./window.js";
|
|
9
|
+
export { DEFAULT_MAX_BYTES, SOURCE_NAMES, classifyTemplates, digestSource, sourceFamily, type DigestAnswer, type DigestEnvironment, type DigestOutcome, type DigestRequest, type DigestTemplate, type RawAnswer, type Refusal, } from "./digest-source.js";
|
|
10
|
+
export { SourceRefusal, type CliRun, type LogFn, type RunCli, type SourceContext, type SourceLine, type SourceRead } from "./sources/types.js";
|
|
11
|
+
export { nodeRunCli } from "./sources/process.js";
|
|
12
|
+
export { readCollector, receiptLine, streamLines, windowShare } from "./sources/collector.js";
|
|
13
|
+
export { RAILWAY_ENVIRONMENT, RAILWAY_PROJECT_ID, railwayRefusal, readRailway } from "./sources/railway.js";
|
|
14
|
+
export { VERCEL_PROJECT, VERCEL_SCOPE, readDashboard, routeTemplate, vercelRecordLines, vercelRefusal, type VercelClasses, type VercelRecord } from "./sources/vercel.js";
|
|
15
|
+
export { readStdin } from "./sources/stdin.js";
|
|
16
|
+
export { LOCAL_ACCESS_REFUSALS, STORED_MAX_TEMPLATES_PER_WINDOW, digestLines, mergeStored, scrubRow, storedRowBytes, toStoredRows, type DigestedLines, type StoredMerge, type StoredRow, type StoredWindow, } from "./stored.js";
|
|
17
|
+
export { PROJECT_TABLES, projectClassTable } from "./project-classes.js";
|
|
18
|
+
export { LOG_EXCLUDED, LOG_EXCLUDED_SUPABASE_REFS, LOG_PROJECTS, LOG_PROJECT_IDS, LOG_PROJECT_PATTERN, TOWER_RAILWAY, VERCEL_TEAM_ID, findLogProject, type LogProject, type LogSource, type NotWiredSource, type RailwaySource, type SupabaseSource, type SupabaseStream, type VercelSource, } from "./log-projects.js";
|
|
19
|
+
export { STORED_EXAMPLE_CHARS, STORED_TEMPLATE_CHARS, scrubForStorage, scrubLine, scrubUnit } from "./scrub.js";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export { mask, redact, templateId, cut, squash, TEMPLATE_CHARS, EXAMPLE_CHARS } from "./mask.js";
|
|
2
|
+
export { DECLARED_WARNING, LONG_LINE_CHARS, judgeJson, jsonEvent, jsonRecords, looksImportant, pythonJson, unitGroup, } from "./judge.js";
|
|
3
|
+
export { DEFAULT_NOISE, Digest, SUBJECTS_TRACKED, } from "./digest.js";
|
|
4
|
+
export { mergeResults } from "./merge.js";
|
|
5
|
+
export { CLASS_ORDER, ClassTable, } from "./classify.js";
|
|
6
|
+
export { clock, fleetView, numberText, renderCapped, renderFleet, renderLocal, routineText, subjectsText, timeRange, } from "./render.js";
|
|
7
|
+
export { TOWER_CLASSES, TOWER_PATTERNS, TOWER_TABLE, towerClassTable } from "./tower-classes.js";
|
|
8
|
+
export { WindowError, isoOf, parseWindow, relativeSeconds } from "./window.js";
|
|
9
|
+
export { DEFAULT_MAX_BYTES, SOURCE_NAMES, classifyTemplates, digestSource, sourceFamily, } from "./digest-source.js";
|
|
10
|
+
export { SourceRefusal } from "./sources/types.js";
|
|
11
|
+
export { nodeRunCli } from "./sources/process.js";
|
|
12
|
+
export { readCollector, receiptLine, streamLines, windowShare } from "./sources/collector.js";
|
|
13
|
+
export { RAILWAY_ENVIRONMENT, RAILWAY_PROJECT_ID, railwayRefusal, readRailway } from "./sources/railway.js";
|
|
14
|
+
export { VERCEL_PROJECT, VERCEL_SCOPE, readDashboard, routeTemplate, vercelRecordLines, vercelRefusal } from "./sources/vercel.js";
|
|
15
|
+
export { readStdin } from "./sources/stdin.js";
|
|
16
|
+
export { LOCAL_ACCESS_REFUSALS, STORED_MAX_TEMPLATES_PER_WINDOW, digestLines, mergeStored, scrubRow, storedRowBytes, toStoredRows, } from "./stored.js";
|
|
17
|
+
export { PROJECT_TABLES, projectClassTable } from "./project-classes.js";
|
|
18
|
+
export { LOG_EXCLUDED, LOG_EXCLUDED_SUPABASE_REFS, LOG_PROJECTS, LOG_PROJECT_IDS, LOG_PROJECT_PATTERN, TOWER_RAILWAY, VERCEL_TEAM_ID, findLogProject, } from "./log-projects.js";
|
|
19
|
+
export { STORED_EXAMPLE_CHARS, STORED_TEMPLATE_CHARS, scrubForStorage, scrubLine, scrubUnit } from "./scrub.js";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare const LONG_LINE_CHARS = 4000;
|
|
2
|
+
/**
|
|
3
|
+
* Appended to a JSON line's template when the line says `"level": "WARNING"`.
|
|
4
|
+
* The classifier strips it before matching patterns, and an otherwise unknown
|
|
5
|
+
* template with it is counted (WARNING), not escalated.
|
|
6
|
+
*/
|
|
7
|
+
export declare const DECLARED_WARNING = " level=WARNING";
|
|
8
|
+
export type JsonObject = Record<string, unknown>;
|
|
9
|
+
/** True when the line contains a trouble word (literal pre-filter, then the regex). */
|
|
10
|
+
export declare function looksImportant(text: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Every JSON object on one line: unbuffered writers sometimes glue two records
|
|
13
|
+
* together (`{...}{...}`). Null when the line is not entirely JSON objects.
|
|
14
|
+
*/
|
|
15
|
+
export declare function jsonRecords(text: string): JsonObject[] | null;
|
|
16
|
+
/** `rs-gap@caseys.service` becomes (`rs-gap@*`, `caseys`); `kaiben-shard.service` becomes (`kaiben-shard`, null). */
|
|
17
|
+
export declare function unitGroup(unit: string | null | undefined): [string, string | null];
|
|
18
|
+
/** The event name of one JSON log line: its "event" field, else its first key that is not a timestamp. */
|
|
19
|
+
export declare function jsonEvent(data: JsonObject): [string, boolean];
|
|
20
|
+
export interface JsonJudgement {
|
|
21
|
+
event: string;
|
|
22
|
+
important: boolean;
|
|
23
|
+
template: string | null;
|
|
24
|
+
subject: string | null;
|
|
25
|
+
}
|
|
26
|
+
/** (event, important, template, subject) for one JSON log line. */
|
|
27
|
+
export declare function judgeJson(data: JsonObject): JsonJudgement;
|
|
28
|
+
/**
|
|
29
|
+
* `json.dumps` as Python writes it (`", "` and `": "` separators, non-ASCII as
|
|
30
|
+
* `\uXXXX`), so a stored example matches the Python digest byte for byte. A
|
|
31
|
+
* float with a zero fraction prints as an integer here; JS cannot tell them apart.
|
|
32
|
+
*/
|
|
33
|
+
export declare function pythonJson(value: unknown): string;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which lines can mean trouble, and what a JSON log line says (port of the
|
|
3
|
+
* important-line half of Kaiben's `log_digest.py`).
|
|
4
|
+
*
|
|
5
|
+
* Caveman's cheap literal pre-filter first (most lines contain none of the
|
|
6
|
+
* literals, so the regex runs on few lines), then the regex; JSON lines are
|
|
7
|
+
* judged by their event name and state fields, not by words inside key names.
|
|
8
|
+
*/
|
|
9
|
+
import { TEMPLATE_CHARS, mask } from "./mask.js";
|
|
10
|
+
export const LONG_LINE_CHARS = 4000;
|
|
11
|
+
const IMPORTANT_LITERALS = [
|
|
12
|
+
"error", "fail", "traceback", "exception", "refused", "denied", "timeout", "timed out", "warn",
|
|
13
|
+
"fatal", "panic", "crit", "killed", "out of memory", "oom", "no space", "unreachable", "captcha",
|
|
14
|
+
"forbidden", "too many requests", "403", "429", "50", "scheduled restart", "abort", "blocked",
|
|
15
|
+
];
|
|
16
|
+
const IMPORTANT_TEXT = /\b(error|errors|fatal|panic|exception|traceback|fail|failed|failure|warn|warning|crit|critical|alert|emerg|refused|denied|timeout|timed out|unreachable|killed|oom-kill|out of memory|no space left|captcha|forbidden|too many requests|aborted|blocked)\b|\bHTTP(?: Error)?[ /:](?:403|429|5\d\d)\b|\bhttp[=: ](?:403|429|5\d\d)\b|Scheduled restart job|status=\d+\/[A-Z]+/i;
|
|
17
|
+
// Event names are judged by whole words (camelCase and snake_case split):
|
|
18
|
+
// "newsroom" must not match "oom".
|
|
19
|
+
const IMPORTANT_EVENT_WORDS = new Set([
|
|
20
|
+
"fail", "failed", "failure", "error", "errors", "exception", "traceback", "refused", "denied",
|
|
21
|
+
"blocked", "captcha", "timeout", "timed", "exhausted", "fatal", "panic", "crash", "crashed", "oom",
|
|
22
|
+
"killed", "abort", "aborted", "limited", "throttled", "warn", "warning", "retry", "stalled",
|
|
23
|
+
"skipped",
|
|
24
|
+
]);
|
|
25
|
+
const IMPORTANT_COMPOUND_EVENTS = new Set(["circuit_open", "needs_human", "stop_marker"]);
|
|
26
|
+
const EVENT_WORD = /[A-Z]?[a-z]+|[A-Z]+(?![a-z])|\d+/g;
|
|
27
|
+
const BAD_STATE = /^(fail|failed|failure|error|errored|fatal|blocked|refused|timeout|timed_out|crash|crashed|killed|aborted|needs_human|stopped)$/i;
|
|
28
|
+
const STATE_FIELDS = ["status", "outcome", "result", "level", "verdict", "state"];
|
|
29
|
+
/**
|
|
30
|
+
* Appended to a JSON line's template when the line says `"level": "WARNING"`.
|
|
31
|
+
* The classifier strips it before matching patterns, and an otherwise unknown
|
|
32
|
+
* template with it is counted (WARNING), not escalated.
|
|
33
|
+
*/
|
|
34
|
+
export const DECLARED_WARNING = " level=WARNING";
|
|
35
|
+
const EXPLAIN_FIELDS = [
|
|
36
|
+
"status", "outcome", "result", "http", "code", "step", "what", "reason", "error", "exception",
|
|
37
|
+
"message", "msg", "detail",
|
|
38
|
+
];
|
|
39
|
+
const META_KEYS = new Set(["ts", "at", "time", "timestamp", "level", "t"]);
|
|
40
|
+
/** True when the line contains a trouble word (literal pre-filter, then the regex). */
|
|
41
|
+
export function looksImportant(text) {
|
|
42
|
+
const lower = text.toLowerCase();
|
|
43
|
+
if (!IMPORTANT_LITERALS.some((literal) => lower.includes(literal)))
|
|
44
|
+
return false;
|
|
45
|
+
return IMPORTANT_TEXT.test(text);
|
|
46
|
+
}
|
|
47
|
+
/** The end index (exclusive) of the JSON object starting at `start`, or -1 when it never closes. */
|
|
48
|
+
function objectEnd(text, start) {
|
|
49
|
+
let depth = 0;
|
|
50
|
+
let inString = false;
|
|
51
|
+
for (let index = start; index < text.length; index += 1) {
|
|
52
|
+
const char = text[index];
|
|
53
|
+
if (inString) {
|
|
54
|
+
if (char === "\\")
|
|
55
|
+
index += 1;
|
|
56
|
+
else if (char === '"')
|
|
57
|
+
inString = false;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (char === '"')
|
|
61
|
+
inString = true;
|
|
62
|
+
else if (char === "{")
|
|
63
|
+
depth += 1;
|
|
64
|
+
else if (char === "}") {
|
|
65
|
+
depth -= 1;
|
|
66
|
+
if (depth === 0)
|
|
67
|
+
return index + 1;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return -1;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Every JSON object on one line: unbuffered writers sometimes glue two records
|
|
74
|
+
* together (`{...}{...}`). Null when the line is not entirely JSON objects.
|
|
75
|
+
*/
|
|
76
|
+
export function jsonRecords(text) {
|
|
77
|
+
const records = [];
|
|
78
|
+
let position = 0;
|
|
79
|
+
while (position < text.length) {
|
|
80
|
+
if (text[position] !== "{")
|
|
81
|
+
return null;
|
|
82
|
+
const end = objectEnd(text, position);
|
|
83
|
+
if (end < 0)
|
|
84
|
+
return null;
|
|
85
|
+
try {
|
|
86
|
+
const data = JSON.parse(text.slice(position, end));
|
|
87
|
+
if (data === null || typeof data !== "object" || Array.isArray(data))
|
|
88
|
+
return null;
|
|
89
|
+
records.push(data);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
position = end;
|
|
95
|
+
while (position < text.length && " \t,".includes(text[position]))
|
|
96
|
+
position += 1;
|
|
97
|
+
}
|
|
98
|
+
return records.length > 0 ? records : null;
|
|
99
|
+
}
|
|
100
|
+
/** `rs-gap@caseys.service` becomes (`rs-gap@*`, `caseys`); `kaiben-shard.service` becomes (`kaiben-shard`, null). */
|
|
101
|
+
export function unitGroup(unit) {
|
|
102
|
+
let name = unit || "?";
|
|
103
|
+
if (name.endsWith(".service"))
|
|
104
|
+
name = name.slice(0, -".service".length);
|
|
105
|
+
const at = name.indexOf("@");
|
|
106
|
+
if (at >= 0)
|
|
107
|
+
return [`${name.slice(0, at)}@*`, name.slice(at + 1)];
|
|
108
|
+
return [name, null];
|
|
109
|
+
}
|
|
110
|
+
/** The event name of one JSON log line: its "event" field, else its first key that is not a timestamp. */
|
|
111
|
+
export function jsonEvent(data) {
|
|
112
|
+
const event = data["event"];
|
|
113
|
+
if (typeof event === "string" && event)
|
|
114
|
+
return [event, false];
|
|
115
|
+
for (const key of Object.keys(data)) {
|
|
116
|
+
if (!META_KEYS.has(key))
|
|
117
|
+
return [key, true];
|
|
118
|
+
}
|
|
119
|
+
return ["json", false];
|
|
120
|
+
}
|
|
121
|
+
function shortValue(value) {
|
|
122
|
+
if (typeof value === "boolean" || value === null || value === undefined)
|
|
123
|
+
return JSON.stringify(value ?? null);
|
|
124
|
+
if (typeof value === "number") {
|
|
125
|
+
if (Number.isInteger(value) && value >= 100 && value <= 599)
|
|
126
|
+
return String(value); // an HTTP-like status
|
|
127
|
+
return Number.isInteger(value) && value >= -9 && value <= 9 ? String(value) : "#";
|
|
128
|
+
}
|
|
129
|
+
if (Array.isArray(value))
|
|
130
|
+
return "<list>";
|
|
131
|
+
if (typeof value === "object")
|
|
132
|
+
return "<dict>";
|
|
133
|
+
return mask(value);
|
|
134
|
+
}
|
|
135
|
+
function isEmptyError(value) {
|
|
136
|
+
if (value === null || value === undefined || value === "" || value === 0 || value === false)
|
|
137
|
+
return true;
|
|
138
|
+
if (Array.isArray(value))
|
|
139
|
+
return value.length === 0;
|
|
140
|
+
if (typeof value === "object")
|
|
141
|
+
return Object.keys(value).length === 0;
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
/** (event, important, template, subject) for one JSON log line. */
|
|
145
|
+
export function judgeJson(data) {
|
|
146
|
+
const [event, eventIsKey] = jsonEvent(data);
|
|
147
|
+
const subjectValue = eventIsKey ? data[event] : (data["kind"] || data["store"]);
|
|
148
|
+
const words = new Set((event.match(EVENT_WORD) ?? []).map((word) => word.toLowerCase()));
|
|
149
|
+
let important = [...words].some((word) => IMPORTANT_EVENT_WORDS.has(word)) || IMPORTANT_COMPOUND_EVENTS.has(event);
|
|
150
|
+
for (const field of STATE_FIELDS) {
|
|
151
|
+
const value = data[field];
|
|
152
|
+
if (typeof value === "string" && BAD_STATE.test(value))
|
|
153
|
+
important = true;
|
|
154
|
+
}
|
|
155
|
+
for (const field of ["http", "code", "status"]) {
|
|
156
|
+
const value = data[field];
|
|
157
|
+
if (typeof value === "number" && Number.isInteger(value) && (value === 403 || value === 429 || (value >= 500 && value <= 599))) {
|
|
158
|
+
important = true;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (!isEmptyError(data["error"]))
|
|
162
|
+
important = true;
|
|
163
|
+
if (!important) {
|
|
164
|
+
const subject = typeof subjectValue === "string" && Array.from(subjectValue).length <= 24 ? subjectValue : null;
|
|
165
|
+
return { event, important: false, template: null, subject };
|
|
166
|
+
}
|
|
167
|
+
const parts = [event];
|
|
168
|
+
if (eventIsKey && typeof subjectValue === "string" && Array.from(subjectValue).length <= 40)
|
|
169
|
+
parts.push(mask(subjectValue));
|
|
170
|
+
for (const field of EXPLAIN_FIELDS) {
|
|
171
|
+
const value = data[field];
|
|
172
|
+
if (field in data && field !== event && value !== null && value !== undefined && value !== "") {
|
|
173
|
+
parts.push(`${field}=${shortValue(value)}`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
let template = Array.from(parts.join(" ")).slice(0, TEMPLATE_CHARS - DECLARED_WARNING.length).join("");
|
|
177
|
+
const level = String(data["level"] ?? "").trim().toLowerCase();
|
|
178
|
+
if (level === "warning" || level === "warn")
|
|
179
|
+
template += DECLARED_WARNING;
|
|
180
|
+
return { event, important: true, template, subject: null };
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* `json.dumps` as Python writes it (`", "` and `": "` separators, non-ASCII as
|
|
184
|
+
* `\uXXXX`), so a stored example matches the Python digest byte for byte. A
|
|
185
|
+
* float with a zero fraction prints as an integer here; JS cannot tell them apart.
|
|
186
|
+
*/
|
|
187
|
+
export function pythonJson(value) {
|
|
188
|
+
if (value === null || value === undefined)
|
|
189
|
+
return "null";
|
|
190
|
+
if (typeof value === "string")
|
|
191
|
+
return JSON.stringify(value).replace(/[\u007f-\uffff]/g, (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}`);
|
|
192
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
193
|
+
return String(value);
|
|
194
|
+
if (Array.isArray(value))
|
|
195
|
+
return `[${value.map(pythonJson).join(", ")}]`;
|
|
196
|
+
const entries = Object.entries(value).map(([key, item]) => `${pythonJson(key)}: ${pythonJson(item)}`);
|
|
197
|
+
return `{${entries.join(", ")}}`;
|
|
198
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The project registry (BLI-12855): which Vercel project, Railway project and
|
|
3
|
+
* Supabase project each BLI product runs on, so the server-side cron knows what
|
|
4
|
+
* to read and `cockpit logs <project>` knows what to answer for.
|
|
5
|
+
*
|
|
6
|
+
* Ids and names only: no URL with a secret, no token, no env value. Every id
|
|
7
|
+
* below was discovered from the platforms' own CLIs on 2026-10-07
|
|
8
|
+
* (`vercel project ls --scope bli-inc`, `vercel api /v9/projects`,
|
|
9
|
+
* `railway list` and the Railway GraphQL API, `supabase projects list`), and the
|
|
10
|
+
* Vercel-to-Supabase pairing was read from each repo's own code
|
|
11
|
+
* (`gh search code supabase.co --repo BLI-INC/<repo>`), never guessed. A source
|
|
12
|
+
* that cannot be read is `not_wired` with the reason, so a gap is a line in this
|
|
13
|
+
* file and in `docs/runbooks/tower-logs.md`, not a silence.
|
|
14
|
+
*
|
|
15
|
+
* Adding a project: `docs/runbooks/tower-logs.md`, "Add a project".
|
|
16
|
+
*/
|
|
17
|
+
/** The Vercel team every BLI project lives in (slug `bli-inc`). */
|
|
18
|
+
export declare const VERCEL_TEAM_ID = "team_JPu5D09vtC8U3J7FyVxintpK";
|
|
19
|
+
/** Railway project `bli-cockpit-inference` (workspace "canakobli's Projects"), environment `production`. */
|
|
20
|
+
export declare const TOWER_RAILWAY: {
|
|
21
|
+
readonly project_id: "868238ce-d8fe-4629-8e05-fb7492baff1f";
|
|
22
|
+
readonly environment_id: "ec008563-7123-4f26-b425-fb6bb498a404";
|
|
23
|
+
readonly environment: "production";
|
|
24
|
+
};
|
|
25
|
+
export type SupabaseStream = "postgres" | "api" | "auth";
|
|
26
|
+
interface SourceBase {
|
|
27
|
+
/** A stable label stored with every row: `vercel:<name>`, `railway:<project>`, `supabase:<name>`. */
|
|
28
|
+
source: string;
|
|
29
|
+
/** What the class table's `source` regex sees: `dashboard`, `railway`, `vercel`, `supabase`. */
|
|
30
|
+
family: string;
|
|
31
|
+
}
|
|
32
|
+
export interface VercelSource extends SourceBase {
|
|
33
|
+
kind: "vercel";
|
|
34
|
+
/** `prj_...` */
|
|
35
|
+
project_id: string;
|
|
36
|
+
name: string;
|
|
37
|
+
/** The unit prefix of every line: `dashboard` keeps Tower's own class table matching. */
|
|
38
|
+
unit_prefix: string;
|
|
39
|
+
}
|
|
40
|
+
export interface RailwaySource extends SourceBase {
|
|
41
|
+
kind: "railway";
|
|
42
|
+
project_id: string;
|
|
43
|
+
environment_id: string;
|
|
44
|
+
environment: string;
|
|
45
|
+
}
|
|
46
|
+
export interface SupabaseSource extends SourceBase {
|
|
47
|
+
kind: "supabase";
|
|
48
|
+
ref: string;
|
|
49
|
+
name: string;
|
|
50
|
+
streams: readonly SupabaseStream[];
|
|
51
|
+
}
|
|
52
|
+
export interface NotWiredSource extends SourceBase {
|
|
53
|
+
kind: "not_wired";
|
|
54
|
+
/** The platform it would be read from. */
|
|
55
|
+
platform: string;
|
|
56
|
+
/** Plain words: why nothing reads it, and what would. */
|
|
57
|
+
reason: string;
|
|
58
|
+
}
|
|
59
|
+
export type LogSource = VercelSource | RailwaySource | SupabaseSource | NotWiredSource;
|
|
60
|
+
export interface LogProject {
|
|
61
|
+
/** What `cockpit logs <project>` takes. */
|
|
62
|
+
id: string;
|
|
63
|
+
name: string;
|
|
64
|
+
sources: readonly LogSource[];
|
|
65
|
+
}
|
|
66
|
+
export declare const LOG_PROJECTS: readonly LogProject[];
|
|
67
|
+
/** Not Tower, so not here: Veetesh's own Vercel project. */
|
|
68
|
+
export declare const LOG_EXCLUDED: readonly ["bli-cockpit-oversight"];
|
|
69
|
+
/**
|
|
70
|
+
* Supabase refs that must NEVER be read, from the project sessions' handoffs on ticket 12860: Kolm's frozen v14 and its
|
|
71
|
+
* empty project, and Pearl Portal's unused staging. `stored.test.ts` pins that none is in the registry.
|
|
72
|
+
*/
|
|
73
|
+
export declare const LOG_EXCLUDED_SUPABASE_REFS: Readonly<Record<string, string>>;
|
|
74
|
+
export declare function findLogProject(id: string): LogProject | null;
|
|
75
|
+
export declare const LOG_PROJECT_IDS: readonly string[];
|
|
76
|
+
/** A project id as `cockpit logs <project>` spells it: lowercase letters, digits and dashes. */
|
|
77
|
+
export declare const LOG_PROJECT_PATTERN: RegExp;
|
|
78
|
+
export {};
|