@bli-cockpit/telemetry-core 0.1.89 → 0.1.91
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/codex-hooks-trust-guide.d.ts +12 -0
- package/dist/codex-hooks-trust-guide.js +12 -0
- package/dist/image-byte-scan.d.ts +22 -0
- package/dist/image-byte-scan.js +187 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +6 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The click-by-click guide for the one manual step in the BLI Memory rollout:
|
|
3
|
+
* trusting the bli-memory hooks in Codex (CLI and ChatGPT desktop app, macOS
|
|
4
|
+
* and Windows). Every surface that tells a person "trust the hooks" links here
|
|
5
|
+
* instead of stopping at "run /hooks": the fleet board line, `cockpit doctor`,
|
|
6
|
+
* `cockpit memory install`, the machine health card.
|
|
7
|
+
*
|
|
8
|
+
* Its own file, and its own `./codex-hooks-trust-guide` export, so a browser
|
|
9
|
+
* component can import one string without pulling in the whole package.
|
|
10
|
+
* Repo copy of the guide: `docs/runbooks/codex-hooks-trust.md` (BLI-23315).
|
|
11
|
+
*/
|
|
12
|
+
export declare const CODEX_HOOKS_TRUST_GUIDE_URL = "https://tower.buildlaunchiterate.ca/docs/trust-the-bli-memory-hooks-in-codex";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The click-by-click guide for the one manual step in the BLI Memory rollout:
|
|
3
|
+
* trusting the bli-memory hooks in Codex (CLI and ChatGPT desktop app, macOS
|
|
4
|
+
* and Windows). Every surface that tells a person "trust the hooks" links here
|
|
5
|
+
* instead of stopping at "run /hooks": the fleet board line, `cockpit doctor`,
|
|
6
|
+
* `cockpit memory install`, the machine health card.
|
|
7
|
+
*
|
|
8
|
+
* Its own file, and its own `./codex-hooks-trust-guide` export, so a browser
|
|
9
|
+
* component can import one string without pulling in the whole package.
|
|
10
|
+
* Repo copy of the guide: `docs/runbooks/codex-hooks-trust.md` (BLI-23315).
|
|
11
|
+
*/
|
|
12
|
+
export const CODEX_HOOKS_TRUST_GUIDE_URL = "https://tower.buildlaunchiterate.ca/docs/trust-the-bli-memory-hooks-in-codex";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ImageMediaType } from "./image-descriptions.js";
|
|
2
|
+
import type { EmbeddedImage } from "./transcript-images.js";
|
|
3
|
+
/** Is there any chance this line holds an image? Same anchors as the string check, on bytes. */
|
|
4
|
+
export declare function bytesMayHoldImage(line: Buffer): boolean;
|
|
5
|
+
interface Digest {
|
|
6
|
+
sha256: string;
|
|
7
|
+
mediaType: ImageMediaType;
|
|
8
|
+
byteSize: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* sha256, size and type of the image `line[from, to)` encodes, or null when it is not a PNG, JPEG, GIF
|
|
12
|
+
* or WebP. Blocks of the run are decoded one at a time.
|
|
13
|
+
*/
|
|
14
|
+
export declare function digestBase64Run(line: Buffer, from: number, to: number): Digest | null;
|
|
15
|
+
/** The sha256 of an image's decoded bytes, or null when `line[from, to)` does not decode to an image (used to re-check a payload before it is sent). */
|
|
16
|
+
export declare function sha256OfBase64Run(line: Buffer, from: number, to: number): string | null;
|
|
17
|
+
/**
|
|
18
|
+
* Every embedded image in `line` (one physical line, no `\n` inside), in order, with byte offsets inside
|
|
19
|
+
* the line. Equal to `findEmbeddedImages` on the decoded text in everything but the unit of the offsets.
|
|
20
|
+
*/
|
|
21
|
+
export declare function scanImagesInLine(line: Buffer): EmbeddedImage[];
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lives in telemetry-core so the collector and the turn-end hook share one scanner (BLI-23349).
|
|
3
|
+
*
|
|
4
|
+
* Find the embedded images of one transcript line without ever building the base64 as a JS string
|
|
5
|
+
* (BLI-23317, stage D).
|
|
6
|
+
*
|
|
7
|
+
* `findEmbeddedImages` (telemetry-core/transcript-images.ts) is the RULE and stays the test oracle: it
|
|
8
|
+
* feeds `evidenceRulesFingerprint`, so its regex and its results do not change. This module is the same
|
|
9
|
+
* rule run on the line's BYTES, which is what the streamed prepare path calls:
|
|
10
|
+
*
|
|
11
|
+
* - a whole-string payload: a base64 run that starts right after a `"`, starts with one of the four
|
|
12
|
+
* image starts (`iVBOR`, `/9j/`, `R0lGO`, `UklGR`), is at least 495 characters past that start
|
|
13
|
+
* (so 499 for a JPEG, 500 for the others, exactly as the regex counts) and is closed by `"` or `\"`;
|
|
14
|
+
* - a data URL: `data:image/<[a-z0-9.+-]+>;base64,` and then a run of at least 500 characters, no closing
|
|
15
|
+
* quote needed. The whole URL is the replaced range.
|
|
16
|
+
*
|
|
17
|
+
* A match is accepted only when the decoded bytes sniff as PNG, JPEG, GIF or WebP; either way the scan
|
|
18
|
+
* resumes after the run, as `matchAll` does. All matched characters are ASCII, so a byte offset here is
|
|
19
|
+
* the character offset there on ASCII text, and the replaced ranges are the same everywhere else.
|
|
20
|
+
*
|
|
21
|
+
* Decoding is incremental: the run is decoded in blocks of 48 Ki characters (a multiple of 4, so each
|
|
22
|
+
* block decodes on its own) straight into a sha256, with the first block sniffed. At no point does a
|
|
23
|
+
* string of the run, or a Buffer of the whole decoded image, exist. The one exception is a run whose
|
|
24
|
+
* `=` characters are not plain trailing padding: Node's decoder rules for those are version specific,
|
|
25
|
+
* so such a run takes the whole-run decode the oracle takes (never seen in real transcripts).
|
|
26
|
+
*
|
|
27
|
+
* `byteSize` is the length Node's `Buffer.from(run, "base64")` gives, because every block, including a
|
|
28
|
+
* short last one, is decoded by that very function.
|
|
29
|
+
*/
|
|
30
|
+
import { createHash } from "node:crypto";
|
|
31
|
+
import { IMAGE_MIN_BASE64_CHARS, sniffImageMediaType } from "./image-descriptions.js";
|
|
32
|
+
const QUOTE = 0x22;
|
|
33
|
+
const BACKSLASH = 0x5c;
|
|
34
|
+
const EQUALS = 0x3d;
|
|
35
|
+
/** Characters per decoded block. A multiple of 4: each block is a whole number of base64 quanta. */
|
|
36
|
+
const BLOCK_CHARS = 48 * 1024;
|
|
37
|
+
const BASE64 = new Uint8Array(256);
|
|
38
|
+
for (const range of ["AZ", "az", "09"]) {
|
|
39
|
+
for (let code = range.charCodeAt(0); code <= range.charCodeAt(1); code += 1)
|
|
40
|
+
BASE64[code] = 1;
|
|
41
|
+
}
|
|
42
|
+
for (const char of "+/=")
|
|
43
|
+
BASE64[char.charCodeAt(0)] = 1;
|
|
44
|
+
const DATA_URL_LOWER = new Uint8Array(256);
|
|
45
|
+
for (const range of ["az", "09"]) {
|
|
46
|
+
for (let code = range.charCodeAt(0); code <= range.charCodeAt(1); code += 1)
|
|
47
|
+
DATA_URL_LOWER[code] = 1;
|
|
48
|
+
}
|
|
49
|
+
for (const char of ".+-")
|
|
50
|
+
DATA_URL_LOWER[char.charCodeAt(0)] = 1;
|
|
51
|
+
/** The four image starts of a whole-string payload, and the data URL anchor. */
|
|
52
|
+
const STARTS = ["iVBOR", "/9j/", "R0lGO", "UklGR"].map((text) => Buffer.from(text, "latin1"));
|
|
53
|
+
const DATA_URL = Buffer.from("data:image/", "latin1");
|
|
54
|
+
const BASE64_TAG = Buffer.from(";base64,", "latin1");
|
|
55
|
+
/** Is there any chance this line holds an image? Same anchors as the string check, on bytes. */
|
|
56
|
+
export function bytesMayHoldImage(line) {
|
|
57
|
+
return line.includes(DATA_URL) || STARTS.some((start) => line.includes(start));
|
|
58
|
+
}
|
|
59
|
+
/** The end (exclusive) of the base64-alphabet run that starts at `from`. */
|
|
60
|
+
function runEnd(line, from) {
|
|
61
|
+
let at = from;
|
|
62
|
+
while (at < line.length && BASE64[line[at]] === 1)
|
|
63
|
+
at += 1;
|
|
64
|
+
return at;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* sha256, size and type of the image `line[from, to)` encodes, or null when it is not a PNG, JPEG, GIF
|
|
68
|
+
* or WebP. Blocks of the run are decoded one at a time.
|
|
69
|
+
*/
|
|
70
|
+
export function digestBase64Run(line, from, to) {
|
|
71
|
+
const length = to - from;
|
|
72
|
+
const inRun = line.subarray(from, to).indexOf(EQUALS);
|
|
73
|
+
const firstPad = inRun === -1 ? -1 : from + inRun;
|
|
74
|
+
const padCount = firstPad === -1 ? 0 : to - firstPad;
|
|
75
|
+
const plainPadding = padCount === 0
|
|
76
|
+
|| (padCount <= 2 && length % 4 === 0 && onlyPaddingFrom(line, firstPad, to));
|
|
77
|
+
if (!plainPadding) {
|
|
78
|
+
const bytes = Buffer.from(line.toString("latin1", from, to), "base64");
|
|
79
|
+
const mediaType = sniffImageMediaType(bytes);
|
|
80
|
+
return mediaType ? { sha256: createHash("sha256").update(bytes).digest("hex"), mediaType, byteSize: bytes.byteLength } : null;
|
|
81
|
+
}
|
|
82
|
+
const hash = createHash("sha256");
|
|
83
|
+
let byteSize = 0;
|
|
84
|
+
let mediaType = null;
|
|
85
|
+
for (let at = from; at < to; at += BLOCK_CHARS) {
|
|
86
|
+
const block = Buffer.from(line.toString("latin1", at, Math.min(to, at + BLOCK_CHARS)), "base64");
|
|
87
|
+
if (at === from) {
|
|
88
|
+
mediaType = sniffImageMediaType(block);
|
|
89
|
+
if (!mediaType)
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
hash.update(block);
|
|
93
|
+
byteSize += block.byteLength;
|
|
94
|
+
}
|
|
95
|
+
return mediaType ? { sha256: hash.digest("hex"), mediaType, byteSize } : null;
|
|
96
|
+
}
|
|
97
|
+
/** True when everything from `firstPad` to `to` is `=` (padding at the very end of the run, nothing after it). */
|
|
98
|
+
function onlyPaddingFrom(line, firstPad, to) {
|
|
99
|
+
for (let at = firstPad; at < to; at += 1)
|
|
100
|
+
if (line[at] !== EQUALS)
|
|
101
|
+
return false;
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
/** The sha256 of an image's decoded bytes, or null when `line[from, to)` does not decode to an image (used to re-check a payload before it is sent). */
|
|
105
|
+
export function sha256OfBase64Run(line, from, to) {
|
|
106
|
+
return digestBase64Run(line, from, to)?.sha256 ?? null;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The next index at or after `from` where `anchor` sits right after a `"`, or -1. `current` is the answer
|
|
110
|
+
* of the last call: -2 not looked for yet, -1 none left, else still good while it is at or after `from`.
|
|
111
|
+
*/
|
|
112
|
+
function nextQuotedStart(line, anchor, from, current) {
|
|
113
|
+
if (current === -1 || current >= from)
|
|
114
|
+
return current;
|
|
115
|
+
let at = line.indexOf(anchor, from);
|
|
116
|
+
while (at !== -1 && !(at > 0 && line[at - 1] === QUOTE))
|
|
117
|
+
at = line.indexOf(anchor, at + 1);
|
|
118
|
+
return at;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Every embedded image in `line` (one physical line, no `\n` inside), in order, with byte offsets inside
|
|
122
|
+
* the line. Equal to `findEmbeddedImages` on the decoded text in everything but the unit of the offsets.
|
|
123
|
+
*/
|
|
124
|
+
export function scanImagesInLine(line) {
|
|
125
|
+
const found = [];
|
|
126
|
+
if (line.length < IMAGE_MIN_BASE64_CHARS - 1)
|
|
127
|
+
return found;
|
|
128
|
+
// For each whole-string start, the next quoted occurrence (-2: not looked for yet, -1: none left).
|
|
129
|
+
const nextStart = STARTS.map(() => -2);
|
|
130
|
+
let nextData = -2;
|
|
131
|
+
let position = 0;
|
|
132
|
+
while (position < line.length) {
|
|
133
|
+
let candidate = -1;
|
|
134
|
+
let kind = -1;
|
|
135
|
+
for (let index = 0; index < STARTS.length; index += 1) {
|
|
136
|
+
nextStart[index] = nextQuotedStart(line, STARTS[index], position, nextStart[index]);
|
|
137
|
+
const at = nextStart[index];
|
|
138
|
+
if (at !== -1 && (candidate === -1 || at < candidate)) {
|
|
139
|
+
candidate = at;
|
|
140
|
+
kind = index;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (nextData !== -1) {
|
|
144
|
+
nextData = nextData >= position ? nextData : line.indexOf(DATA_URL, position);
|
|
145
|
+
if (nextData !== -1 && (candidate === -1 || nextData < candidate)) {
|
|
146
|
+
candidate = nextData;
|
|
147
|
+
kind = STARTS.length;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (candidate === -1)
|
|
151
|
+
break;
|
|
152
|
+
if (kind < STARTS.length) {
|
|
153
|
+
const startLength = STARTS[kind].length;
|
|
154
|
+
const end = runEnd(line, candidate);
|
|
155
|
+
const closed = end < line.length && (line[end] === QUOTE || (line[end] === BACKSLASH && line[end + 1] === QUOTE));
|
|
156
|
+
if (!closed || end - candidate < startLength + (IMAGE_MIN_BASE64_CHARS - 5)) {
|
|
157
|
+
position = candidate + 1;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
position = end;
|
|
161
|
+
const digest = digestBase64Run(line, candidate, end);
|
|
162
|
+
if (digest)
|
|
163
|
+
found.push({ replaceStart: candidate, replaceEnd: end, base64Start: candidate, base64End: end, ...digest });
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
// data:image/<type>;base64,<run of at least 500>
|
|
167
|
+
let cursor = candidate + DATA_URL.length;
|
|
168
|
+
const typeStart = cursor;
|
|
169
|
+
while (cursor < line.length && DATA_URL_LOWER[line[cursor]] === 1)
|
|
170
|
+
cursor += 1;
|
|
171
|
+
if (cursor === typeStart || line.compare(BASE64_TAG, 0, BASE64_TAG.length, cursor, cursor + BASE64_TAG.length) !== 0) {
|
|
172
|
+
position = candidate + 1;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const base64Start = cursor + BASE64_TAG.length;
|
|
176
|
+
const end = runEnd(line, base64Start);
|
|
177
|
+
if (end - base64Start < IMAGE_MIN_BASE64_CHARS) {
|
|
178
|
+
position = candidate + 1;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
position = end;
|
|
182
|
+
const digest = digestBase64Run(line, base64Start, end);
|
|
183
|
+
if (digest)
|
|
184
|
+
found.push({ replaceStart: candidate, replaceEnd: end, base64Start, base64End: end, ...digest });
|
|
185
|
+
}
|
|
186
|
+
return found;
|
|
187
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export * from "./image-descriptions.js";
|
|
|
16
16
|
export * from "./image-marker-resolver.js";
|
|
17
17
|
export * from "./pdf-markers.js";
|
|
18
18
|
export * from "./transcript-images.js";
|
|
19
|
+
export * from "./image-byte-scan.js";
|
|
19
20
|
export * from "./ingest-dto.js";
|
|
20
21
|
export * from "./local-config.js";
|
|
21
22
|
export * from "./memory-daemon-endpoint.js";
|
|
@@ -23,6 +24,7 @@ export * from "./memory-hook-stats.js";
|
|
|
23
24
|
export * from "./sync-revive-contract.js";
|
|
24
25
|
export * from "./memory-extract-context.js";
|
|
25
26
|
export * from "./memory-hook-performance.js";
|
|
27
|
+
export * from "./codex-hooks-trust-guide.js";
|
|
26
28
|
export * from "./memory-install-receipt.js";
|
|
27
29
|
export * from "./outbound-guard.js";
|
|
28
30
|
export * from "./paths.js";
|
package/dist/index.js
CHANGED
|
@@ -16,6 +16,7 @@ export * from "./image-descriptions.js";
|
|
|
16
16
|
export * from "./image-marker-resolver.js";
|
|
17
17
|
export * from "./pdf-markers.js";
|
|
18
18
|
export * from "./transcript-images.js";
|
|
19
|
+
export * from "./image-byte-scan.js";
|
|
19
20
|
export * from "./ingest-dto.js";
|
|
20
21
|
export * from "./local-config.js";
|
|
21
22
|
export * from "./memory-daemon-endpoint.js";
|
|
@@ -23,6 +24,7 @@ export * from "./memory-hook-stats.js";
|
|
|
23
24
|
export * from "./sync-revive-contract.js";
|
|
24
25
|
export * from "./memory-extract-context.js";
|
|
25
26
|
export * from "./memory-hook-performance.js";
|
|
27
|
+
export * from "./codex-hooks-trust-guide.js";
|
|
26
28
|
export * from "./memory-install-receipt.js";
|
|
27
29
|
export * from "./outbound-guard.js";
|
|
28
30
|
export * from "./paths.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bli-cockpit/telemetry-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.91",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -15,6 +15,11 @@
|
|
|
15
15
|
"import": "./dist/index.js",
|
|
16
16
|
"default": "./dist/index.js"
|
|
17
17
|
},
|
|
18
|
+
"./codex-hooks-trust-guide": {
|
|
19
|
+
"types": "./dist/codex-hooks-trust-guide.d.ts",
|
|
20
|
+
"import": "./dist/codex-hooks-trust-guide.js",
|
|
21
|
+
"default": "./dist/codex-hooks-trust-guide.js"
|
|
22
|
+
},
|
|
18
23
|
"./memory-experience": {
|
|
19
24
|
"types": "./dist/memory-experience.d.ts",
|
|
20
25
|
"import": "./dist/memory-experience.js",
|