@bli-cockpit/telemetry-core 0.1.72 → 0.1.75
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/image-descriptions.d.ts +12 -4
- package/dist/image-descriptions.js +14 -4
- package/dist/image-marker-resolver.d.ts +88 -0
- package/dist/image-marker-resolver.js +195 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/log-digest/index.d.ts +1 -0
- package/dist/log-digest/index.js +1 -0
- package/dist/log-digest/mask.js +25 -4
- package/dist/log-digest/remask.d.ts +6 -0
- package/dist/log-digest/remask.js +37 -0
- package/package.json +1 -1
|
@@ -26,10 +26,14 @@ import { z } from "zod";
|
|
|
26
26
|
*/
|
|
27
27
|
export type ImageHandlingMode = "describe_and_keep" | "describe_and_drop";
|
|
28
28
|
/**
|
|
29
|
-
* The
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
29
|
+
* The FLEET DEFAULT and the fallback: the mode a machine uses when Tower has no setting for it and it
|
|
30
|
+
* has never heard from Tower. Default is keep (images stay in the transcript, uploaded as on disk).
|
|
31
|
+
*
|
|
32
|
+
* Since BLI-17273 the mode is decided PER MACHINE. The `collector_image_mode:<device_id>` row in
|
|
33
|
+
* `public.system_settings` (`cockpit settings switches set collector_image_mode:<device_id> describe_and_drop`)
|
|
34
|
+
* rides the reply to the sync-start receipt, the collector caches the last value it saw, and that
|
|
35
|
+
* value overrides this constant for that machine only. Flipping one machine needs a CLI release that
|
|
36
|
+
* carries the per-machine path (`gh workflow run release.yml -f tag=latest`); no other file changes.
|
|
33
37
|
*/
|
|
34
38
|
export declare const IMAGE_HANDLING_MODE: ImageHandlingMode;
|
|
35
39
|
/**
|
|
@@ -49,12 +53,16 @@ export declare function sniffImageMediaType(bytes: Uint8Array): ImageMediaType |
|
|
|
49
53
|
/** Hex characters of the sha256 a marker carries. 64 bits is far past any one person's image count. */
|
|
50
54
|
export declare const IMAGE_MARKER_HASH_CHARS = 16;
|
|
51
55
|
export declare function imageSizeLabel(byteSize: number): string;
|
|
56
|
+
/** What every marker starts with: a cheap `includes` check before anyone runs the marker regex. */
|
|
57
|
+
export declare const IMAGE_MARKER_START = "[image sha256:";
|
|
52
58
|
/** `[image sha256:ab12cd34ef567890… png 412KB]`: what replaces the base64 in an uploaded transcript. */
|
|
53
59
|
export declare function formatImageMarker(image: {
|
|
54
60
|
sha256: string;
|
|
55
61
|
mediaType: ImageMediaType;
|
|
56
62
|
byteSize: number;
|
|
57
63
|
}): string;
|
|
64
|
+
/** A fresh global regex for one marker: group 1 the 16-hex prefix, 2 the type extension, 3 the size label. */
|
|
65
|
+
export declare function imageMarkerPattern(): RegExp;
|
|
58
66
|
export interface ParsedImageMarker {
|
|
59
67
|
sha256Prefix: string;
|
|
60
68
|
mediaType: ImageMediaType;
|
|
@@ -14,10 +14,14 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import { z } from "zod";
|
|
16
16
|
/**
|
|
17
|
-
* The
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
17
|
+
* The FLEET DEFAULT and the fallback: the mode a machine uses when Tower has no setting for it and it
|
|
18
|
+
* has never heard from Tower. Default is keep (images stay in the transcript, uploaded as on disk).
|
|
19
|
+
*
|
|
20
|
+
* Since BLI-17273 the mode is decided PER MACHINE. The `collector_image_mode:<device_id>` row in
|
|
21
|
+
* `public.system_settings` (`cockpit settings switches set collector_image_mode:<device_id> describe_and_drop`)
|
|
22
|
+
* rides the reply to the sync-start receipt, the collector caches the last value it saw, and that
|
|
23
|
+
* value overrides this constant for that machine only. Flipping one machine needs a CLI release that
|
|
24
|
+
* carries the per-machine path (`gh workflow run release.yml -f tag=latest`); no other file changes.
|
|
21
25
|
*/
|
|
22
26
|
export const IMAGE_HANDLING_MODE = "describe_and_keep";
|
|
23
27
|
// ---------------------------------------------------------------------------
|
|
@@ -71,12 +75,18 @@ export function imageSizeLabel(byteSize) {
|
|
|
71
75
|
return `${Math.round(byteSize / 1_000)}KB`;
|
|
72
76
|
return `${byteSize}B`;
|
|
73
77
|
}
|
|
78
|
+
/** What every marker starts with: a cheap `includes` check before anyone runs the marker regex. */
|
|
79
|
+
export const IMAGE_MARKER_START = "[image sha256:";
|
|
74
80
|
/** `[image sha256:ab12cd34ef567890… png 412KB]`: what replaces the base64 in an uploaded transcript. */
|
|
75
81
|
export function formatImageMarker(image) {
|
|
76
82
|
const hash = image.sha256.slice(0, IMAGE_MARKER_HASH_CHARS);
|
|
77
83
|
return `[image sha256:${hash}… ${EXTENSION_BY_TYPE[image.mediaType]} ${imageSizeLabel(image.byteSize)}]`;
|
|
78
84
|
}
|
|
79
85
|
const MARKER_SOURCE = String.raw `\[image sha256:([0-9a-f]{${IMAGE_MARKER_HASH_CHARS}})… (png|jpg|gif|webp) (\d+(?:\.\d)?(?:B|KB|MB))\]`;
|
|
86
|
+
/** A fresh global regex for one marker: group 1 the 16-hex prefix, 2 the type extension, 3 the size label. */
|
|
87
|
+
export function imageMarkerPattern() {
|
|
88
|
+
return new RegExp(MARKER_SOURCE, "g");
|
|
89
|
+
}
|
|
80
90
|
/** Every marker in a piece of text, in order. A fresh regex per call, so no shared `lastIndex`. */
|
|
81
91
|
export function parseImageMarkers(text) {
|
|
82
92
|
const found = [];
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/** Hash prefix (16 hex) to the description of that image. A prefix with no row is simply absent. */
|
|
2
|
+
export type ImageDescriptionLookup = (hashPrefixes: string[]) => Promise<ReadonlyMap<string, string>>;
|
|
3
|
+
export interface InlineImageDescriptionsResult {
|
|
4
|
+
text: string;
|
|
5
|
+
/** Markers in the text (a marker repeated three times counts three). */
|
|
6
|
+
found: number;
|
|
7
|
+
/** Markers swapped for their description. */
|
|
8
|
+
resolved: number;
|
|
9
|
+
/** Markers left as they were: `found - resolved`. */
|
|
10
|
+
missing: number;
|
|
11
|
+
/** Why markers stayed, by reason label. */
|
|
12
|
+
missing_reasons: Record<string, number>;
|
|
13
|
+
/** Database calls this one call made (0 when everything was already remembered). */
|
|
14
|
+
lookups: number;
|
|
15
|
+
}
|
|
16
|
+
/** One described image found in a text: the marker as written, and its words. */
|
|
17
|
+
export interface ImageNote {
|
|
18
|
+
/** `sha256:ab12cd34ef567890` */
|
|
19
|
+
image: string;
|
|
20
|
+
description: string;
|
|
21
|
+
}
|
|
22
|
+
export interface ImageNotesResult {
|
|
23
|
+
notes: ImageNote[];
|
|
24
|
+
found: number;
|
|
25
|
+
resolved: number;
|
|
26
|
+
missing: number;
|
|
27
|
+
missing_reasons: Record<string, number>;
|
|
28
|
+
lookups: number;
|
|
29
|
+
}
|
|
30
|
+
export interface ImageDescriptionResolver {
|
|
31
|
+
/**
|
|
32
|
+
* `escape: "json"` (default) writes the description so it is safe INSIDE a JSON string, because
|
|
33
|
+
* the marker sits inside one in a JSONL transcript: quotes, backslashes and line breaks are
|
|
34
|
+
* escaped and every line stays one valid JSON line. `"plain"` is for text that is not JSON.
|
|
35
|
+
*/
|
|
36
|
+
inline(text: string, options?: {
|
|
37
|
+
escape?: "json" | "plain";
|
|
38
|
+
}): Promise<InlineImageDescriptionsResult>;
|
|
39
|
+
/**
|
|
40
|
+
* The same lookup without touching the text, for a reader that must keep the bytes it was handed
|
|
41
|
+
* exactly (a quote whose hash is bound to its bytes). Distinct images only, in order of first sight.
|
|
42
|
+
*/
|
|
43
|
+
notes(text: string): Promise<ImageNotesResult>;
|
|
44
|
+
}
|
|
45
|
+
export interface ImageDescriptionResolverOptions {
|
|
46
|
+
/** Who is asking, for the log line: `harvest_extract`, `judge_quotes`, `digest`, ... */
|
|
47
|
+
reader: string;
|
|
48
|
+
/** Prefixes asked per database call. */
|
|
49
|
+
batchSize?: number;
|
|
50
|
+
/** Descriptions remembered for the run before the oldest are forgotten. */
|
|
51
|
+
maxRemembered?: number;
|
|
52
|
+
/** Tests only. */
|
|
53
|
+
log?: (message: string, fields: Record<string, unknown>) => void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Swap each marker in `text` for `[image sha256:... png 412KB: <words>]` using notes a resolver
|
|
57
|
+
* already fetched. Pure and synchronous, so a prompt builder can apply it to a quote it must keep
|
|
58
|
+
* byte-exact elsewhere. A marker with no note stays as it is.
|
|
59
|
+
*/
|
|
60
|
+
export declare function applyImageNotes(text: string, notes: readonly ImageNote[], escape?: "json" | "plain"): string;
|
|
61
|
+
export declare function createImageDescriptionResolver(lookup: ImageDescriptionLookup, options: ImageDescriptionResolverOptions): ImageDescriptionResolver;
|
|
62
|
+
/**
|
|
63
|
+
* The smallest slice of a Supabase client the lookup needs. A reader passes the client it already
|
|
64
|
+
* uses: the caller's RLS client (the database decides which rows that person sees) or, for a cron
|
|
65
|
+
* that reads every person's sessions, the service role. This file never builds a client.
|
|
66
|
+
*/
|
|
67
|
+
export interface ImageDescriptionRowsClient {
|
|
68
|
+
from(table: "ambient_image_descriptions"): {
|
|
69
|
+
select(columns: string): {
|
|
70
|
+
or(filter: string): {
|
|
71
|
+
limit(count: number): PromiseLike<{
|
|
72
|
+
data: Array<{
|
|
73
|
+
sha256: string;
|
|
74
|
+
description: string;
|
|
75
|
+
}> | null;
|
|
76
|
+
error: {
|
|
77
|
+
code?: string | null;
|
|
78
|
+
message?: string;
|
|
79
|
+
} | null;
|
|
80
|
+
}>;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export declare function imageDescriptionLookupFor(client: ImageDescriptionRowsClient, options: {
|
|
86
|
+
reader: string;
|
|
87
|
+
log?: (message: string, fields: Record<string, unknown>) => void;
|
|
88
|
+
}): ImageDescriptionLookup;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn an image marker back into the words that describe the image (BLI-16979).
|
|
3
|
+
*
|
|
4
|
+
* In slim mode a stored transcript carries only `[image sha256:ab12cd34ef567890… png 412KB]` where
|
|
5
|
+
* a screenshot used to be. The describe door keeps the words for each image in
|
|
6
|
+
* `ambient_image_descriptions`. A model that is asked about a screenshot and sees only the sticker
|
|
7
|
+
* guesses (blind test on 15 real sessions: originals 12 right, descriptions inlined 10, stickers
|
|
8
|
+
* only 5). So every reader that hands transcript text to a model runs it through this one resolver
|
|
9
|
+
* first.
|
|
10
|
+
*
|
|
11
|
+
* One resolver per run. It looks descriptions up in ONE batch per call (all the markers in the text
|
|
12
|
+
* at once, not one query per marker), remembers every answer for the rest of the run, and leaves a
|
|
13
|
+
* marker with no description exactly as it was. The lookup itself is passed in: a reader hands in
|
|
14
|
+
* the Supabase client it already uses (the caller's RLS client for a person's request, the service
|
|
15
|
+
* role for a cron), so this file never widens who can read what.
|
|
16
|
+
*
|
|
17
|
+
* Logs counts only: markers found, resolved, missing, and one reason label per kind of miss. Never
|
|
18
|
+
* a description, never an image.
|
|
19
|
+
*/
|
|
20
|
+
import { IMAGE_MARKER_HASH_CHARS, IMAGE_MARKER_START, imageMarkerPattern, parseImageMarkers } from "./image-descriptions.js";
|
|
21
|
+
const TAG = "[image-markers]";
|
|
22
|
+
/** Characters of the sha256 a marker carries. */
|
|
23
|
+
const PREFIX_CHARS = IMAGE_MARKER_HASH_CHARS;
|
|
24
|
+
const DEFAULT_BATCH = 25;
|
|
25
|
+
const DEFAULT_MAX_REMEMBERED = 20_000;
|
|
26
|
+
function escapeForJsonString(description) {
|
|
27
|
+
return JSON.stringify(description).slice(1, -1);
|
|
28
|
+
}
|
|
29
|
+
/** `[image sha256:ab12cd34ef567890… png 412KB: the words]`. The id stays so a quote can still point at the image. */
|
|
30
|
+
function describedMarker(marker, description, escape) {
|
|
31
|
+
const safe = escape === "json" ? escapeForJsonString(oneLine(description)) : oneLine(description);
|
|
32
|
+
return `${marker.slice(0, -1)}: ${safe}]`;
|
|
33
|
+
}
|
|
34
|
+
function oneLine(description) {
|
|
35
|
+
return description.replace(/\s+/g, " ").trim();
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Swap each marker in `text` for `[image sha256:... png 412KB: <words>]` using notes a resolver
|
|
39
|
+
* already fetched. Pure and synchronous, so a prompt builder can apply it to a quote it must keep
|
|
40
|
+
* byte-exact elsewhere. A marker with no note stays as it is.
|
|
41
|
+
*/
|
|
42
|
+
export function applyImageNotes(text, notes, escape = "json") {
|
|
43
|
+
if (notes.length === 0 || !text.includes(IMAGE_MARKER_START))
|
|
44
|
+
return text;
|
|
45
|
+
const byImage = new Map(notes.map((note) => [note.image, note.description]));
|
|
46
|
+
return text.replace(imageMarkerPattern(), (whole, prefix) => {
|
|
47
|
+
const description = byImage.get(`sha256:${prefix}`);
|
|
48
|
+
return description ? describedMarker(whole, description, escape) : whole;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
export function createImageDescriptionResolver(lookup, options) {
|
|
52
|
+
const log = options.log ?? ((message, fields) => console.info(message, JSON.stringify(fields)));
|
|
53
|
+
const batchSize = Math.max(1, options.batchSize ?? DEFAULT_BATCH);
|
|
54
|
+
const maxRemembered = Math.max(1, options.maxRemembered ?? DEFAULT_MAX_REMEMBERED);
|
|
55
|
+
/** description, or null for "asked and there is none". A failed lookup is never remembered. */
|
|
56
|
+
const remembered = new Map();
|
|
57
|
+
let totalFound = 0;
|
|
58
|
+
let totalResolved = 0;
|
|
59
|
+
let totalLookups = 0;
|
|
60
|
+
function remember(prefix, description) {
|
|
61
|
+
if (remembered.size >= maxRemembered) {
|
|
62
|
+
const oldest = remembered.keys().next();
|
|
63
|
+
if (!oldest.done)
|
|
64
|
+
remembered.delete(oldest.value);
|
|
65
|
+
}
|
|
66
|
+
remembered.set(prefix, description);
|
|
67
|
+
}
|
|
68
|
+
async function fetchMissing(prefixes) {
|
|
69
|
+
const failed = new Set();
|
|
70
|
+
let lookups = 0;
|
|
71
|
+
for (let start = 0; start < prefixes.length; start += batchSize) {
|
|
72
|
+
const batch = prefixes.slice(start, start + batchSize);
|
|
73
|
+
lookups += 1;
|
|
74
|
+
try {
|
|
75
|
+
const answers = await lookup(batch);
|
|
76
|
+
for (const prefix of batch)
|
|
77
|
+
remember(prefix, answers.get(prefix) ?? null);
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
for (const prefix of batch)
|
|
81
|
+
failed.add(prefix);
|
|
82
|
+
log(`${TAG} lookup failed, markers stay as they are`, {
|
|
83
|
+
reader: options.reader,
|
|
84
|
+
reason: "lookup_failed",
|
|
85
|
+
markers: batch.length,
|
|
86
|
+
error_name: error instanceof Error ? error.name : typeof error,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return { lookups, failed };
|
|
91
|
+
}
|
|
92
|
+
/** Looks up every distinct marker in `text` that is not already remembered. */
|
|
93
|
+
async function ask(text) {
|
|
94
|
+
const markers = parseImageMarkers(text);
|
|
95
|
+
if (markers.length === 0)
|
|
96
|
+
return null;
|
|
97
|
+
const distinct = [...new Set(markers.map((marker) => marker.sha256Prefix))];
|
|
98
|
+
const toAsk = distinct.filter((prefix) => !remembered.has(prefix));
|
|
99
|
+
const { lookups, failed } = toAsk.length ? await fetchMissing(toAsk) : { lookups: 0, failed: new Set() };
|
|
100
|
+
return { found: markers.length, distinct, failed, lookups, askedFor: toAsk.length };
|
|
101
|
+
}
|
|
102
|
+
function logSummary(summary) {
|
|
103
|
+
totalFound += summary.found;
|
|
104
|
+
totalResolved += summary.resolved;
|
|
105
|
+
totalLookups += summary.lookups;
|
|
106
|
+
log(`${TAG} markers resolved`, {
|
|
107
|
+
reader: options.reader,
|
|
108
|
+
found: summary.found,
|
|
109
|
+
resolved: summary.resolved,
|
|
110
|
+
missing: summary.found - summary.resolved,
|
|
111
|
+
missing_reasons: summary.reasons,
|
|
112
|
+
distinct: summary.distinct,
|
|
113
|
+
looked_up: summary.askedFor,
|
|
114
|
+
lookups: summary.lookups,
|
|
115
|
+
run_found: totalFound,
|
|
116
|
+
run_resolved: totalResolved,
|
|
117
|
+
run_lookups: totalLookups,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const reasonFor = (prefix, failed) => (failed.has(prefix) ? "lookup_failed" : "no_description_row");
|
|
121
|
+
return {
|
|
122
|
+
async inline(text, callOptions) {
|
|
123
|
+
const asked = await ask(text);
|
|
124
|
+
if (!asked)
|
|
125
|
+
return { text, found: 0, resolved: 0, missing: 0, missing_reasons: {}, lookups: 0 };
|
|
126
|
+
const escape = callOptions?.escape ?? "json";
|
|
127
|
+
const reasons = {};
|
|
128
|
+
let resolved = 0;
|
|
129
|
+
const output = text.replace(imageMarkerPattern(), (whole, prefix) => {
|
|
130
|
+
const description = remembered.get(prefix);
|
|
131
|
+
if (description) {
|
|
132
|
+
resolved += 1;
|
|
133
|
+
return describedMarker(whole, description, escape);
|
|
134
|
+
}
|
|
135
|
+
const reason = reasonFor(prefix, asked.failed);
|
|
136
|
+
reasons[reason] = (reasons[reason] ?? 0) + 1;
|
|
137
|
+
return whole;
|
|
138
|
+
});
|
|
139
|
+
logSummary({ found: asked.found, resolved, reasons, distinct: asked.distinct.length, askedFor: asked.askedFor, lookups: asked.lookups });
|
|
140
|
+
return { text: output, found: asked.found, resolved, missing: asked.found - resolved, missing_reasons: reasons, lookups: asked.lookups };
|
|
141
|
+
},
|
|
142
|
+
async notes(text) {
|
|
143
|
+
const asked = await ask(text);
|
|
144
|
+
if (!asked)
|
|
145
|
+
return { notes: [], found: 0, resolved: 0, missing: 0, missing_reasons: {}, lookups: 0 };
|
|
146
|
+
const reasons = {};
|
|
147
|
+
let resolved = 0;
|
|
148
|
+
const notes = [];
|
|
149
|
+
const seen = new Set();
|
|
150
|
+
for (const marker of parseImageMarkers(text)) {
|
|
151
|
+
const description = remembered.get(marker.sha256Prefix);
|
|
152
|
+
if (description) {
|
|
153
|
+
resolved += 1;
|
|
154
|
+
if (!seen.has(marker.sha256Prefix)) {
|
|
155
|
+
seen.add(marker.sha256Prefix);
|
|
156
|
+
notes.push({ image: `sha256:${marker.sha256Prefix}`, description: oneLine(description) });
|
|
157
|
+
}
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const reason = reasonFor(marker.sha256Prefix, asked.failed);
|
|
161
|
+
reasons[reason] = (reasons[reason] ?? 0) + 1;
|
|
162
|
+
}
|
|
163
|
+
logSummary({ found: asked.found, resolved, reasons, distinct: asked.distinct.length, askedFor: asked.askedFor, lookups: asked.lookups });
|
|
164
|
+
return { notes, found: asked.found, resolved, missing: asked.found - resolved, missing_reasons: reasons, lookups: asked.lookups };
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/** PostgREST caps a request at 1,000 rows; a batch cannot reach it (25 prefixes) but a cap is said out loud. */
|
|
169
|
+
const ROW_CAP = 1_000;
|
|
170
|
+
export function imageDescriptionLookupFor(client, options) {
|
|
171
|
+
const log = options.log ?? ((message, fields) => console.warn(message, JSON.stringify(fields)));
|
|
172
|
+
return async (prefixes) => {
|
|
173
|
+
const filter = prefixes.map((prefix) => `sha256.like.${prefix}*`).join(",");
|
|
174
|
+
const { data, error } = await client
|
|
175
|
+
.from("ambient_image_descriptions")
|
|
176
|
+
.select("sha256,description")
|
|
177
|
+
.or(filter)
|
|
178
|
+
.limit(ROW_CAP);
|
|
179
|
+
if (error) {
|
|
180
|
+
log(`${TAG} description read failed`, { reader: options.reader, reason: "description_read_failed", code: error.code ?? null, prefixes: prefixes.length });
|
|
181
|
+
throw new Error("description_read_failed");
|
|
182
|
+
}
|
|
183
|
+
const rows = data ?? [];
|
|
184
|
+
if (rows.length >= ROW_CAP) {
|
|
185
|
+
log(`${TAG} description read hit the row cap`, { reader: options.reader, reason: "description_read_capped", rows: rows.length, prefixes: prefixes.length });
|
|
186
|
+
}
|
|
187
|
+
const byPrefix = new Map();
|
|
188
|
+
for (const row of rows) {
|
|
189
|
+
const prefix = row.sha256.slice(0, PREFIX_CHARS);
|
|
190
|
+
if (row.description && !byPrefix.has(prefix))
|
|
191
|
+
byPrefix.set(prefix, row.description);
|
|
192
|
+
}
|
|
193
|
+
return byPrefix;
|
|
194
|
+
};
|
|
195
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export * from "./evidence-stubs.js";
|
|
|
11
11
|
export * from "./evidence-transcripts.js";
|
|
12
12
|
export * from "./evidence-upload.js";
|
|
13
13
|
export * from "./image-descriptions.js";
|
|
14
|
+
export * from "./image-marker-resolver.js";
|
|
14
15
|
export * from "./transcript-images.js";
|
|
15
16
|
export * from "./ingest-dto.js";
|
|
16
17
|
export * from "./local-config.js";
|
package/dist/index.js
CHANGED
|
@@ -11,6 +11,7 @@ export * from "./evidence-stubs.js";
|
|
|
11
11
|
export * from "./evidence-transcripts.js";
|
|
12
12
|
export * from "./evidence-upload.js";
|
|
13
13
|
export * from "./image-descriptions.js";
|
|
14
|
+
export * from "./image-marker-resolver.js";
|
|
14
15
|
export * from "./transcript-images.js";
|
|
15
16
|
export * from "./ingest-dto.js";
|
|
16
17
|
export * from "./local-config.js";
|
|
@@ -17,3 +17,4 @@ export { BLIND_TEMPLATE_PREFIX, LOCAL_ACCESS_REFUSALS, STORED_MAX_TEMPLATES_PER_
|
|
|
17
17
|
export { PROJECT_TABLES, projectClassTable } from "./project-classes.js";
|
|
18
18
|
export { BRAIN_PROJECT_IDS, LOG_EXCLUDED, LOG_EXCLUDED_SUPABASE_REFS, LOG_PROJECTS, LOG_PROJECT_IDS, LOG_PROJECT_PATTERN, TOWER_RAILWAY, VERCEL_TEAM_ID, brainRouteFor, findLogProject, type PushSource, type BrainConfig, type BrainMergePolicy, type BrainRoute, type GcpSource, type LogProject, type LogSource, type NotWiredSource, type RailwaySource, type SupabaseSource, type SupabaseStream, type VercelSource, } from "./log-projects.js";
|
|
19
19
|
export { STORED_EXAMPLE_CHARS, STORED_TEMPLATE_CHARS, scrubForStorage, scrubLine, scrubUnit } from "./scrub.js";
|
|
20
|
+
export { isStaleTemplate, remaskedTemplate, sameFailureUnderCurrentMask } from "./remask.js";
|
package/dist/log-digest/index.js
CHANGED
|
@@ -17,3 +17,4 @@ export { BLIND_TEMPLATE_PREFIX, LOCAL_ACCESS_REFUSALS, STORED_MAX_TEMPLATES_PER_
|
|
|
17
17
|
export { PROJECT_TABLES, projectClassTable } from "./project-classes.js";
|
|
18
18
|
export { BRAIN_PROJECT_IDS, LOG_EXCLUDED, LOG_EXCLUDED_SUPABASE_REFS, LOG_PROJECTS, LOG_PROJECT_IDS, LOG_PROJECT_PATTERN, TOWER_RAILWAY, VERCEL_TEAM_ID, brainRouteFor, findLogProject, } from "./log-projects.js";
|
|
19
19
|
export { STORED_EXAMPLE_CHARS, STORED_TEMPLATE_CHARS, scrubForStorage, scrubLine, scrubUnit } from "./scrub.js";
|
|
20
|
+
export { isStaleTemplate, remaskedTemplate, sameFailureUnderCurrentMask } from "./remask.js";
|
package/dist/log-digest/mask.js
CHANGED
|
@@ -7,9 +7,15 @@
|
|
|
7
7
|
*
|
|
8
8
|
* Fixed masking order, so a memory address can never fake a status code
|
|
9
9
|
* (`0x7f...403...`): memory addresses, URLs, UUIDs, timestamps, IPs, long hex,
|
|
10
|
-
* long ids,
|
|
11
|
-
*
|
|
12
|
-
* `
|
|
10
|
+
* long ids, build-chunk names and minified frame names, absolute paths, long or
|
|
11
|
+
* numeric quoted strings, `#<digits>` ids, long digit runs glued to a word
|
|
12
|
+
* (`call_490779`), then every other number except a status code in a status
|
|
13
|
+
* position (`HTTP 403`, `status=500`, `[Errno 28]`).
|
|
14
|
+
*
|
|
15
|
+
* BLI-17921: `Call #490779` used to keep its id (the number rule skips a digit
|
|
16
|
+
* run right after `#`), so every call id was its own template, its own incident
|
|
17
|
+
* and its own ticket. The id shapes widened since the Python original are marked
|
|
18
|
+
* below; `python-contract.test.ts` still pins every other line.
|
|
13
19
|
*
|
|
14
20
|
* Known difference from the Python original: `\b`, `\w` and `\d` here are ASCII
|
|
15
21
|
* only, Python's are Unicode. A template next to a non-ASCII letter can differ.
|
|
@@ -38,12 +44,23 @@ const URL_PATTERN = /\b[a-zA-Z][a-zA-Z0-9+.-]*:\/\/([^/\s"'<>?#:]+)[^\s"'<>]*/g;
|
|
|
38
44
|
const UUID = /\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b/g;
|
|
39
45
|
const TIMESTAMP = /\b\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:[.,]\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?)?\b|\b\d{2}:\d{2}:\d{2}(?:[.,]\d+)?\b/g;
|
|
40
46
|
const IP_ADDRESS = /\b\d{1,3}(?:\.\d{1,3}){3}(?::\d+)?\b/g;
|
|
41
|
-
|
|
47
|
+
// 8+ hex characters that mix digits and letters, or any 32+ hex run (a md5, sha1, sha256 or a dashless uuid
|
|
48
|
+
// is never a word, even when luck left it without a digit).
|
|
49
|
+
const LONG_HEX = /\b(?=[0-9a-fA-F]*[0-9])(?=[0-9a-fA-F]*[a-fA-F])[0-9a-fA-F]{8,}\b|\b[0-9a-fA-F]{32,}\b/g;
|
|
42
50
|
// 4+ digits: an id, not a unit name.
|
|
43
51
|
const LONG_ID = /\b(?=(?:[A-Za-z_-]*\d){4})(?=[A-Za-z0-9_-]*[A-Za-z])[A-Za-z0-9_-]{20,}={0,2}/g;
|
|
52
|
+
// A Next/Turbopack chunk file name carries a per-build hash: `[root-of-the-server]__14_qotf._.js` (BLI-17921).
|
|
53
|
+
const BUILD_CHUNK = /(\[[\w.-]+\])__[A-Za-z0-9_]{4,}(?=\._\.js)/g;
|
|
54
|
+
// A minified stack frame names a function by one to three letters that change with every build (`at iz (.next/...)`).
|
|
55
|
+
const MINIFIED_FRAME = /\b(at (?:async )?)[A-Za-z_$][\w$]{0,2}( \((?=[^)\s]*\.next\/))/g;
|
|
44
56
|
// A ':' or ',' inside a segment, never a trailing one.
|
|
45
57
|
const PATH = /(?<![\w.<>/])(?:\/(?:[\w.@%+~-]|[:,](?=\w))+){2,}\/?/g;
|
|
46
58
|
const QUOTED = /'([^'\n]{0,300})'|"([^"\n]{0,300})"/g;
|
|
59
|
+
// `Call #490779`, `order #1042`: an id written with a hash sign (BLI-17921). Not `'`, not `#123abc`, not after a digit or another `#`.
|
|
60
|
+
const HASH_NUMBER = /(?<![\d#&])#\d+(?:[.,]\d+)*(?!\w)/g;
|
|
61
|
+
// Digits glued to a word are an id from five after an underscore or six after a letter (`call_49077`, `rec490779`),
|
|
62
|
+
// never `sha256`, `utf8`, `ipv6` or the `ED25519` key type that the Python golden pins.
|
|
63
|
+
const GLUED_DIGITS = /(?<=_)\d{5,}(?!\w)|(?<=[A-Za-z])\d{6,}(?!\w)/g;
|
|
47
64
|
const NUMBER = /(?<![A-Za-z_\d#])\d+(?:[.,]\d+)*/g;
|
|
48
65
|
// A number right after one of these is a status, not a quantity: kept so
|
|
49
66
|
// "HTTP 403" and "HTTP 404" stay different templates.
|
|
@@ -95,8 +112,12 @@ export function mask(input) {
|
|
|
95
112
|
text = text.replace(IP_ADDRESS, "<ip>");
|
|
96
113
|
text = text.replace(LONG_HEX, "<hex>");
|
|
97
114
|
text = text.replace(LONG_ID, "<id>");
|
|
115
|
+
text = text.replace(BUILD_CHUNK, "$1__<hash>");
|
|
116
|
+
text = text.replace(MINIFIED_FRAME, "$1<fn>$2");
|
|
98
117
|
text = text.replace(PATH, maskPath);
|
|
99
118
|
text = text.replace(QUOTED, maskQuoted);
|
|
119
|
+
text = text.replace(HASH_NUMBER, "#");
|
|
120
|
+
text = text.replace(GLUED_DIGITS, "#");
|
|
100
121
|
text = text.replace(NUMBER, keepStatusNumbers);
|
|
101
122
|
return cut(squash(text), TEMPLATE_CHARS);
|
|
102
123
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** The stored text a line with this old stored text would get today. */
|
|
2
|
+
export declare function remaskedTemplate(stored: string): string;
|
|
3
|
+
/** True when today's masker changes this stored text, so the id it was filed under is not the id a new sighting would get. */
|
|
4
|
+
export declare function isStaleTemplate(stored: string): boolean;
|
|
5
|
+
/** True when a sighting whose text today is `current` is the same failure as an incident stored as `stored`. */
|
|
6
|
+
export declare function sameFailureUnderCurrentMask(stored: string, current: string): boolean;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Re-masking a stored template under today's masker (BLI-17921).
|
|
3
|
+
*
|
|
4
|
+
* Plain words: when the masker learns a new id shape (`Call #490779` used to keep its number), the same log
|
|
5
|
+
* line gets a NEW template id, and an incident filed under the old id would look like it vanished while a
|
|
6
|
+
* twin opened under the new one. The old row still holds the old masked text, so running today's masker over
|
|
7
|
+
* that text yields the text the new id would have, and the two can be matched. The official term: a lazy
|
|
8
|
+
* schema migration, re-deriving a stored key from stored data at read time.
|
|
9
|
+
*
|
|
10
|
+
* Two rules keep the match honest:
|
|
11
|
+
* - A stored text that re-masks to itself was never touched by the new rules: it is not stale, and its
|
|
12
|
+
* template id is still the right one, so there is nothing to adopt.
|
|
13
|
+
* - The digest cuts a template at 160 characters. A text that was cut by the OLD masker holds fewer of the
|
|
14
|
+
* line's words than the NEW masker's text (the new placeholders are shorter), so equality is the wrong test
|
|
15
|
+
* for a cut text; its prefix, minus the last (possibly chopped) word, is.
|
|
16
|
+
*/
|
|
17
|
+
import { STORED_TEMPLATE_CHARS, scrubForStorage } from "./scrub.js";
|
|
18
|
+
/** A cut text shorter than this gives a prefix too short to tell two failures apart. */
|
|
19
|
+
const MIN_PREFIX_CHARS = 80;
|
|
20
|
+
/** The stored text a line with this old stored text would get today. */
|
|
21
|
+
export function remaskedTemplate(stored) {
|
|
22
|
+
return scrubForStorage(stored, STORED_TEMPLATE_CHARS);
|
|
23
|
+
}
|
|
24
|
+
/** True when today's masker changes this stored text, so the id it was filed under is not the id a new sighting would get. */
|
|
25
|
+
export function isStaleTemplate(stored) {
|
|
26
|
+
return remaskedTemplate(stored) !== stored;
|
|
27
|
+
}
|
|
28
|
+
/** True when a sighting whose text today is `current` is the same failure as an incident stored as `stored`. */
|
|
29
|
+
export function sameFailureUnderCurrentMask(stored, current) {
|
|
30
|
+
const again = remaskedTemplate(stored);
|
|
31
|
+
if (again === current)
|
|
32
|
+
return true;
|
|
33
|
+
if (Array.from(stored).length < STORED_TEMPLATE_CHARS)
|
|
34
|
+
return false;
|
|
35
|
+
const wholeWords = again.slice(0, Math.max(0, again.lastIndexOf(" ")));
|
|
36
|
+
return Array.from(wholeWords).length >= MIN_PREFIX_CHARS && current.startsWith(wholeWords);
|
|
37
|
+
}
|