@bli-cockpit/telemetry-core 0.1.50 → 0.1.53
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/evidence-upload.d.ts +8 -0
- package/dist/ingest-dto.d.ts +12 -0
- package/dist/memory-daemon-endpoint.d.ts +12 -0
- package/dist/memory-daemon-endpoint.js +12 -0
- package/dist/node-daemon-http-client.d.ts +91 -0
- package/dist/node-daemon-http-client.js +170 -0
- package/dist/node-read-cache.d.ts +81 -0
- package/dist/node-read-cache.js +137 -0
- package/dist/privacy.d.ts +4 -0
- package/dist/privacy.js +14 -0
- package/dist/secret-guards-content-rules.js +15 -12
- package/dist/secret-guards-masking.d.ts +15 -0
- package/dist/secret-guards-masking.js +16 -13
- package/dist/snapshot-supersede.d.ts +54 -0
- package/dist/snapshot-supersede.js +111 -0
- package/dist/source-adapter.d.ts +8 -0
- package/package.json +16 -1
|
@@ -122,6 +122,10 @@ export declare const RawEvidenceUploadBeginObjectSchema: z.ZodObject<{
|
|
|
122
122
|
sanitized_byte_size: z.ZodOptional<z.ZodNumber>;
|
|
123
123
|
applied_at: z.ZodOptional<z.ZodString>;
|
|
124
124
|
}, z.core.$strict>>;
|
|
125
|
+
supersedes_pointer_id: z.ZodOptional<z.ZodString>;
|
|
126
|
+
supersede_rule: z.ZodOptional<z.ZodEnum<{
|
|
127
|
+
prefix: "prefix";
|
|
128
|
+
}>>;
|
|
125
129
|
}, z.core.$strict>;
|
|
126
130
|
chunk_size_bytes: z.ZodNumber;
|
|
127
131
|
chunk_count: z.ZodNumber;
|
|
@@ -222,6 +226,10 @@ export declare const RawEvidenceUploadBeginRequestSchema: z.ZodObject<{
|
|
|
222
226
|
sanitized_byte_size: z.ZodOptional<z.ZodNumber>;
|
|
223
227
|
applied_at: z.ZodOptional<z.ZodString>;
|
|
224
228
|
}, z.core.$strict>>;
|
|
229
|
+
supersedes_pointer_id: z.ZodOptional<z.ZodString>;
|
|
230
|
+
supersede_rule: z.ZodOptional<z.ZodEnum<{
|
|
231
|
+
prefix: "prefix";
|
|
232
|
+
}>>;
|
|
225
233
|
}, z.core.$strict>;
|
|
226
234
|
chunk_size_bytes: z.ZodNumber;
|
|
227
235
|
chunk_count: z.ZodNumber;
|
package/dist/ingest-dto.d.ts
CHANGED
|
@@ -318,6 +318,10 @@ export declare const TelemetryIngestEventDtoSchema: z.ZodObject<{
|
|
|
318
318
|
sanitized_byte_size: z.ZodOptional<z.ZodNumber>;
|
|
319
319
|
applied_at: z.ZodOptional<z.ZodString>;
|
|
320
320
|
}, z.core.$strict>>;
|
|
321
|
+
supersedes_pointer_id: z.ZodOptional<z.ZodString>;
|
|
322
|
+
supersede_rule: z.ZodOptional<z.ZodEnum<{
|
|
323
|
+
prefix: "prefix";
|
|
324
|
+
}>>;
|
|
321
325
|
}, z.core.$strict>>>;
|
|
322
326
|
otel: z.ZodOptional<z.ZodObject<{
|
|
323
327
|
trace_id: z.ZodOptional<z.ZodString>;
|
|
@@ -797,6 +801,10 @@ export declare const TelemetryIngestEnvelopeSchema: z.ZodObject<{
|
|
|
797
801
|
sanitized_byte_size: z.ZodOptional<z.ZodNumber>;
|
|
798
802
|
applied_at: z.ZodOptional<z.ZodString>;
|
|
799
803
|
}, z.core.$strict>>;
|
|
804
|
+
supersedes_pointer_id: z.ZodOptional<z.ZodString>;
|
|
805
|
+
supersede_rule: z.ZodOptional<z.ZodEnum<{
|
|
806
|
+
prefix: "prefix";
|
|
807
|
+
}>>;
|
|
800
808
|
}, z.core.$strict>>>;
|
|
801
809
|
otel: z.ZodOptional<z.ZodObject<{
|
|
802
810
|
trace_id: z.ZodOptional<z.ZodString>;
|
|
@@ -951,6 +959,10 @@ export declare const TelemetryIngestEnvelopeSchema: z.ZodObject<{
|
|
|
951
959
|
sanitized_byte_size: z.ZodOptional<z.ZodNumber>;
|
|
952
960
|
applied_at: z.ZodOptional<z.ZodString>;
|
|
953
961
|
}, z.core.$strict>>;
|
|
962
|
+
supersedes_pointer_id: z.ZodOptional<z.ZodString>;
|
|
963
|
+
supersede_rule: z.ZodOptional<z.ZodEnum<{
|
|
964
|
+
prefix: "prefix";
|
|
965
|
+
}>>;
|
|
954
966
|
}, z.core.$strict>>>;
|
|
955
967
|
}, z.core.$strict>>>;
|
|
956
968
|
ticket_binding_candidates: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
@@ -121,6 +121,18 @@ export declare const MEMORY_DAEMON_UNIX_PATH_MAX = 100;
|
|
|
121
121
|
* 1 MB is far above both and bounds a peer that never sends a newline.
|
|
122
122
|
*/
|
|
123
123
|
export declare const MEMORY_DAEMON_MAX_LINE_BYTES = 1048576;
|
|
124
|
+
/**
|
|
125
|
+
* How long a proxied `http` request may run before the daemon gives up on
|
|
126
|
+
* Tower and answers `daemon_http_timeout` (BLI-10746).
|
|
127
|
+
*
|
|
128
|
+
* This is the DAEMON's own ceiling on the request it makes with its warm
|
|
129
|
+
* dispatcher, separate from `deadlineMs` a caller passes to
|
|
130
|
+
* `askMemoryDaemonHttp` (the whole round trip including the local socket).
|
|
131
|
+
* CLI reads today (`work list`, `docs list`, …) already carry a
|
|
132
|
+
* `READ_DEADLINE_MS` of their own well under this; the daemon's ceiling only
|
|
133
|
+
* has to sit under that, never define it.
|
|
134
|
+
*/
|
|
135
|
+
export declare const MEMORY_DAEMON_HTTP_PROXY_TIMEOUT_MS = 8000;
|
|
124
136
|
export interface MemoryDaemonEndpoint {
|
|
125
137
|
/** Which kind of endpoint this platform gets. */
|
|
126
138
|
kind: "unix" | "pipe";
|
|
@@ -122,6 +122,18 @@ export const MEMORY_DAEMON_UNIX_PATH_MAX = 100;
|
|
|
122
122
|
* 1 MB is far above both and bounds a peer that never sends a newline.
|
|
123
123
|
*/
|
|
124
124
|
export const MEMORY_DAEMON_MAX_LINE_BYTES = 1_048_576;
|
|
125
|
+
/**
|
|
126
|
+
* How long a proxied `http` request may run before the daemon gives up on
|
|
127
|
+
* Tower and answers `daemon_http_timeout` (BLI-10746).
|
|
128
|
+
*
|
|
129
|
+
* This is the DAEMON's own ceiling on the request it makes with its warm
|
|
130
|
+
* dispatcher, separate from `deadlineMs` a caller passes to
|
|
131
|
+
* `askMemoryDaemonHttp` (the whole round trip including the local socket).
|
|
132
|
+
* CLI reads today (`work list`, `docs list`, …) already carry a
|
|
133
|
+
* `READ_DEADLINE_MS` of their own well under this; the daemon's ceiling only
|
|
134
|
+
* has to sit under that, never define it.
|
|
135
|
+
*/
|
|
136
|
+
export const MEMORY_DAEMON_HTTP_PROXY_TIMEOUT_MS = 8_000;
|
|
125
137
|
/**
|
|
126
138
|
* The one place the daemon's address is computed. Both packages call this;
|
|
127
139
|
* nobody joins a socket path by hand.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE CLIENT HALF OF THE DAEMON'S `http` EVENT, NODE ONLY (BLI-10746).
|
|
3
|
+
*
|
|
4
|
+
* `memory-daemon-endpoint.ts` next door is imported by the dashboard as well
|
|
5
|
+
* as by two CLIs, so it stays pure path/string arithmetic — nothing in it
|
|
6
|
+
* ever opens a socket. This module is the part that does, and it lives
|
|
7
|
+
* behind this package's own `./node-daemon-http-client` export rather than
|
|
8
|
+
* the root one, so a browser/edge bundle that imports `@bli-cockpit/
|
|
9
|
+
* telemetry-core` never pulls in `node:net`.
|
|
10
|
+
*
|
|
11
|
+
* `@bli-cockpit/local-collector` (the CLI) and `@bli-cockpit/mcp` (the MCP
|
|
12
|
+
* server) both need to ask the SAME resident daemon
|
|
13
|
+
* (`@bli-cockpit/memory-mcp`'s `bli-memory-mcp daemon`) to proxy one HTTP
|
|
14
|
+
* request over its already-warm connection to Tower (BLI-10741 built that
|
|
15
|
+
* connection for recall; this reuses it for every other read). Neither
|
|
16
|
+
* package may casually depend on `@bli-cockpit/memory-mcp` for this — the MCP
|
|
17
|
+
* server does not depend on it at all today, and the packed CLI's dependency
|
|
18
|
+
* list is its own contract — so this module speaks the daemon's
|
|
19
|
+
* `memory-daemon.v1` wire format directly, using only the address and
|
|
20
|
+
* timing constants both sides already share from `memory-daemon-endpoint.ts`.
|
|
21
|
+
* The daemon's own request/answer parsing (`@bli-cockpit/memory-mcp`'s
|
|
22
|
+
* `daemon/protocol.ts`) reads and writes the exact same shape; the two are
|
|
23
|
+
* bound together by `daemon/http-proxy.test.ts` on the daemon's side and by
|
|
24
|
+
* this module's own tests here, the same "two independent envelopes, proven
|
|
25
|
+
* by tests on each side" shape `memory-daemon-probe.ts` already uses for
|
|
26
|
+
* `ping`.
|
|
27
|
+
*
|
|
28
|
+
* **The device token is NOT part of this contract.** The caller's own
|
|
29
|
+
* `authorization` header travels inside `request.headers` exactly as it
|
|
30
|
+
* would have gone out directly — the daemon only forwards bytes over a
|
|
31
|
+
* socket it already trusts (owner-only, same OS user), never inspects or
|
|
32
|
+
* caches the credential. A daemon that is absent, refuses, times out or
|
|
33
|
+
* answers something this client cannot parse is a NAMED reason, never a
|
|
34
|
+
* thrown exception: every caller's contract is "fall back to a direct
|
|
35
|
+
* request", and that only works if this never hangs or throws.
|
|
36
|
+
*/
|
|
37
|
+
import net from "node:net";
|
|
38
|
+
/** What travels out: enough to replay one `fetch(url, init)` on the daemon's own dispatcher. */
|
|
39
|
+
export interface DaemonHttpProxyRequest {
|
|
40
|
+
method: string;
|
|
41
|
+
url: string;
|
|
42
|
+
headers: Record<string, string>;
|
|
43
|
+
/** Already-serialised, exactly what `fetch`'s `body` would have been. */
|
|
44
|
+
body?: string;
|
|
45
|
+
}
|
|
46
|
+
/** What comes back: enough to reconstruct the `Response` the caller expected. */
|
|
47
|
+
export interface DaemonHttpProxyAnswer {
|
|
48
|
+
status: number;
|
|
49
|
+
headers: Record<string, string>;
|
|
50
|
+
body: string;
|
|
51
|
+
}
|
|
52
|
+
export type AskMemoryDaemonHttpSettled = {
|
|
53
|
+
ok: true;
|
|
54
|
+
answer: DaemonHttpProxyAnswer;
|
|
55
|
+
} | {
|
|
56
|
+
ok: false;
|
|
57
|
+
reason: string;
|
|
58
|
+
};
|
|
59
|
+
export type AskMemoryDaemonHttpResult = AskMemoryDaemonHttpSettled & {
|
|
60
|
+
elapsedMs: number;
|
|
61
|
+
};
|
|
62
|
+
export interface AskMemoryDaemonHttpOptions {
|
|
63
|
+
/** The unix socket path or the Windows pipe name (`memoryDaemonEndpoint(...).address`). */
|
|
64
|
+
address: string;
|
|
65
|
+
request: DaemonHttpProxyRequest;
|
|
66
|
+
/** Bounds the connect alone. Defaults to the shared 50 ms. */
|
|
67
|
+
connectTimeoutMs?: number;
|
|
68
|
+
/** Bounds the whole ask, local socket plus the daemon's own outbound call. */
|
|
69
|
+
deadlineMs: number;
|
|
70
|
+
/** Test seam. Real callers use `net.createConnection`. */
|
|
71
|
+
connectImpl?: (address: string) => net.Socket;
|
|
72
|
+
now?: () => number;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Ask the daemon to proxy one HTTP request over its warm connection.
|
|
76
|
+
* Resolves once, always — never throws, never hangs, never holds the event
|
|
77
|
+
* loop open past its own timers (all `unref`'d, same discipline as
|
|
78
|
+
* `memory-daemon-probe.ts` and `@bli-cockpit/memory-mcp`'s `askDaemon`).
|
|
79
|
+
*/
|
|
80
|
+
export declare function askMemoryDaemonHttp(options: AskMemoryDaemonHttpOptions): Promise<AskMemoryDaemonHttpResult>;
|
|
81
|
+
/** One JSON line, the same envelope every daemon event uses. */
|
|
82
|
+
export declare function encodeHttpProxyLine(request: DaemonHttpProxyRequest): string;
|
|
83
|
+
export type ParsedHttpProxyAnswer = {
|
|
84
|
+
ok: true;
|
|
85
|
+
answer: DaemonHttpProxyAnswer;
|
|
86
|
+
} | {
|
|
87
|
+
ok: false;
|
|
88
|
+
reason: string;
|
|
89
|
+
};
|
|
90
|
+
/** Reads one answer line. Any shape this client cannot trust is a named reason, never a guess. */
|
|
91
|
+
export declare function parseHttpProxyAnswerLine(line: string): ParsedHttpProxyAnswer;
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE CLIENT HALF OF THE DAEMON'S `http` EVENT, NODE ONLY (BLI-10746).
|
|
3
|
+
*
|
|
4
|
+
* `memory-daemon-endpoint.ts` next door is imported by the dashboard as well
|
|
5
|
+
* as by two CLIs, so it stays pure path/string arithmetic — nothing in it
|
|
6
|
+
* ever opens a socket. This module is the part that does, and it lives
|
|
7
|
+
* behind this package's own `./node-daemon-http-client` export rather than
|
|
8
|
+
* the root one, so a browser/edge bundle that imports `@bli-cockpit/
|
|
9
|
+
* telemetry-core` never pulls in `node:net`.
|
|
10
|
+
*
|
|
11
|
+
* `@bli-cockpit/local-collector` (the CLI) and `@bli-cockpit/mcp` (the MCP
|
|
12
|
+
* server) both need to ask the SAME resident daemon
|
|
13
|
+
* (`@bli-cockpit/memory-mcp`'s `bli-memory-mcp daemon`) to proxy one HTTP
|
|
14
|
+
* request over its already-warm connection to Tower (BLI-10741 built that
|
|
15
|
+
* connection for recall; this reuses it for every other read). Neither
|
|
16
|
+
* package may casually depend on `@bli-cockpit/memory-mcp` for this — the MCP
|
|
17
|
+
* server does not depend on it at all today, and the packed CLI's dependency
|
|
18
|
+
* list is its own contract — so this module speaks the daemon's
|
|
19
|
+
* `memory-daemon.v1` wire format directly, using only the address and
|
|
20
|
+
* timing constants both sides already share from `memory-daemon-endpoint.ts`.
|
|
21
|
+
* The daemon's own request/answer parsing (`@bli-cockpit/memory-mcp`'s
|
|
22
|
+
* `daemon/protocol.ts`) reads and writes the exact same shape; the two are
|
|
23
|
+
* bound together by `daemon/http-proxy.test.ts` on the daemon's side and by
|
|
24
|
+
* this module's own tests here, the same "two independent envelopes, proven
|
|
25
|
+
* by tests on each side" shape `memory-daemon-probe.ts` already uses for
|
|
26
|
+
* `ping`.
|
|
27
|
+
*
|
|
28
|
+
* **The device token is NOT part of this contract.** The caller's own
|
|
29
|
+
* `authorization` header travels inside `request.headers` exactly as it
|
|
30
|
+
* would have gone out directly — the daemon only forwards bytes over a
|
|
31
|
+
* socket it already trusts (owner-only, same OS user), never inspects or
|
|
32
|
+
* caches the credential. A daemon that is absent, refuses, times out or
|
|
33
|
+
* answers something this client cannot parse is a NAMED reason, never a
|
|
34
|
+
* thrown exception: every caller's contract is "fall back to a direct
|
|
35
|
+
* request", and that only works if this never hangs or throws.
|
|
36
|
+
*/
|
|
37
|
+
import net from "node:net";
|
|
38
|
+
import { MEMORY_DAEMON_CONNECT_TIMEOUT_MS, MEMORY_DAEMON_MAX_LINE_BYTES, MEMORY_DAEMON_PROTOCOL_VERSION, } from "./memory-daemon-endpoint.js";
|
|
39
|
+
/**
|
|
40
|
+
* Ask the daemon to proxy one HTTP request over its warm connection.
|
|
41
|
+
* Resolves once, always — never throws, never hangs, never holds the event
|
|
42
|
+
* loop open past its own timers (all `unref`'d, same discipline as
|
|
43
|
+
* `memory-daemon-probe.ts` and `@bli-cockpit/memory-mcp`'s `askDaemon`).
|
|
44
|
+
*/
|
|
45
|
+
export function askMemoryDaemonHttp(options) {
|
|
46
|
+
const now = options.now ?? Date.now;
|
|
47
|
+
const started = now();
|
|
48
|
+
const connectTimeoutMs = options.connectTimeoutMs ?? MEMORY_DAEMON_CONNECT_TIMEOUT_MS;
|
|
49
|
+
const connect = options.connectImpl ?? ((address) => net.createConnection({ path: address }));
|
|
50
|
+
return new Promise((resolve) => {
|
|
51
|
+
let settled = false;
|
|
52
|
+
let buffered = "";
|
|
53
|
+
let socket;
|
|
54
|
+
const timers = [];
|
|
55
|
+
const finish = (result) => {
|
|
56
|
+
if (settled)
|
|
57
|
+
return;
|
|
58
|
+
settled = true;
|
|
59
|
+
for (const timer of timers)
|
|
60
|
+
clearTimeout(timer);
|
|
61
|
+
try {
|
|
62
|
+
socket?.destroy();
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
// Already gone.
|
|
66
|
+
}
|
|
67
|
+
resolve({ ...result, elapsedMs: now() - started });
|
|
68
|
+
};
|
|
69
|
+
const arm = (ms, reason) => {
|
|
70
|
+
const timer = setTimeout(() => finish({ ok: false, reason }), Math.max(1, ms));
|
|
71
|
+
timer.unref?.();
|
|
72
|
+
timers.push(timer);
|
|
73
|
+
};
|
|
74
|
+
let connectTimer;
|
|
75
|
+
try {
|
|
76
|
+
socket = connect(options.address);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
finish({ ok: false, reason: connectReason(error) });
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
socket.setNoDelay?.(true);
|
|
83
|
+
arm(options.deadlineMs, "daemon_http_deadline");
|
|
84
|
+
connectTimer = setTimeout(() => finish({ ok: false, reason: "daemon_connect_timeout" }), Math.max(1, connectTimeoutMs));
|
|
85
|
+
connectTimer.unref?.();
|
|
86
|
+
timers.push(connectTimer);
|
|
87
|
+
socket.on("error", (error) => {
|
|
88
|
+
finish({ ok: false, reason: connectReason(error) });
|
|
89
|
+
});
|
|
90
|
+
socket.on("connect", () => {
|
|
91
|
+
clearTimeout(connectTimer);
|
|
92
|
+
socket.write(encodeHttpProxyLine(options.request));
|
|
93
|
+
});
|
|
94
|
+
socket.on("data", (chunk) => {
|
|
95
|
+
buffered += chunk.toString("utf8");
|
|
96
|
+
if (buffered.length > MEMORY_DAEMON_MAX_LINE_BYTES) {
|
|
97
|
+
finish({ ok: false, reason: "daemon_answer_too_large" });
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const newline = buffered.indexOf("\n");
|
|
101
|
+
if (newline === -1)
|
|
102
|
+
return;
|
|
103
|
+
const parsed = parseHttpProxyAnswerLine(buffered.slice(0, newline));
|
|
104
|
+
if (!parsed.ok) {
|
|
105
|
+
finish({ ok: false, reason: parsed.reason });
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
finish({ ok: true, answer: parsed.answer });
|
|
109
|
+
});
|
|
110
|
+
socket.on("close", () => {
|
|
111
|
+
finish({ ok: false, reason: "daemon_closed_without_answer" });
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/** One JSON line, the same envelope every daemon event uses. */
|
|
116
|
+
export function encodeHttpProxyLine(request) {
|
|
117
|
+
return `${JSON.stringify({
|
|
118
|
+
v: MEMORY_DAEMON_PROTOCOL_VERSION,
|
|
119
|
+
event: "http",
|
|
120
|
+
http: request,
|
|
121
|
+
})}\n`;
|
|
122
|
+
}
|
|
123
|
+
/** Reads one answer line. Any shape this client cannot trust is a named reason, never a guess. */
|
|
124
|
+
export function parseHttpProxyAnswerLine(line) {
|
|
125
|
+
let parsed;
|
|
126
|
+
try {
|
|
127
|
+
parsed = JSON.parse(line);
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return { ok: false, reason: "daemon_answer_unparseable" };
|
|
131
|
+
}
|
|
132
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
133
|
+
return { ok: false, reason: "daemon_answer_not_an_object" };
|
|
134
|
+
}
|
|
135
|
+
const record = parsed;
|
|
136
|
+
if (record["ok"] !== true) {
|
|
137
|
+
const reason = record["reason"];
|
|
138
|
+
return { ok: false, reason: typeof reason === "string" ? reason : "daemon_refused" };
|
|
139
|
+
}
|
|
140
|
+
const http = record["http"];
|
|
141
|
+
if (!http || typeof http !== "object" || Array.isArray(http)) {
|
|
142
|
+
return { ok: false, reason: "daemon_answer_without_http" };
|
|
143
|
+
}
|
|
144
|
+
const httpRecord = http;
|
|
145
|
+
const status = httpRecord["status"];
|
|
146
|
+
const body = httpRecord["body"];
|
|
147
|
+
if (typeof status !== "number" || typeof body !== "string") {
|
|
148
|
+
return { ok: false, reason: "daemon_answer_http_malformed" };
|
|
149
|
+
}
|
|
150
|
+
const headers = {};
|
|
151
|
+
const rawHeaders = httpRecord["headers"];
|
|
152
|
+
if (rawHeaders && typeof rawHeaders === "object" && !Array.isArray(rawHeaders)) {
|
|
153
|
+
for (const [key, value] of Object.entries(rawHeaders)) {
|
|
154
|
+
if (typeof value === "string")
|
|
155
|
+
headers[key] = value;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return { ok: true, answer: { status, headers, body } };
|
|
159
|
+
}
|
|
160
|
+
/** The closed reason vocabulary for a failed connect. `errno` codes only, never an exception message. */
|
|
161
|
+
function connectReason(error) {
|
|
162
|
+
const code = error?.code;
|
|
163
|
+
if (code === "ENOENT" || code === "ECONNREFUSED")
|
|
164
|
+
return "daemon_absent";
|
|
165
|
+
if (code === "EACCES" || code === "EPERM")
|
|
166
|
+
return "daemon_forbidden";
|
|
167
|
+
if (code === "ETIMEDOUT")
|
|
168
|
+
return "daemon_connect_timeout";
|
|
169
|
+
return typeof code === "string" ? `daemon_socket_${code.toLowerCase()}` : "daemon_socket_error";
|
|
170
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE ON-DISK, PER-OPERATOR, STALE-WHILE-REVALIDATE READ CACHE (BLI-10746).
|
|
3
|
+
*
|
|
4
|
+
* Node only — file permissions and `node:fs`/`node:crypto` are not something
|
|
5
|
+
* a dashboard bundle needs — so this lives behind this package's own
|
|
6
|
+
* `./node-read-cache` export, the same isolation `./node-daemon-http-client`
|
|
7
|
+
* next door uses.
|
|
8
|
+
*
|
|
9
|
+
* `@bli-cockpit/local-collector` (the CLI) owns the actual stale-while-
|
|
10
|
+
* revalidate DECISION — when to serve cache, when a fresh call is required,
|
|
11
|
+
* when to kick off a background refresh — because that decision needs
|
|
12
|
+
* `--fresh`/`--allow-stale`/`--json`, which are CLI concepts this module
|
|
13
|
+
* knows nothing about. What lives HERE is the shared, dumb, provably-correct
|
|
14
|
+
* bottom half both the CLI (reading and writing) and `@bli-cockpit/mcp` (only
|
|
15
|
+
* ever invalidating, on its own writes) need to agree on byte-for-byte:
|
|
16
|
+
* where an entry lives on disk, how it is written so a reader never sees a
|
|
17
|
+
* half-written file, and how a whole surface is thrown away after a write.
|
|
18
|
+
* Two independent computations of the same cache path is exactly the
|
|
19
|
+
* BLI-2541 shape (a reader that "worked" against a path nothing ever wrote)
|
|
20
|
+
* this module exists to rule out.
|
|
21
|
+
*
|
|
22
|
+
* **Per-operator, never shared.** The device token is never stored — only
|
|
23
|
+
* its SHA-256, truncated, as a directory segment — but two different people
|
|
24
|
+
* pairing the same machine still get two different cache trees, so a shared
|
|
25
|
+
* intern laptop can never show one operator's Tower data to another.
|
|
26
|
+
*
|
|
27
|
+
* **Owner-only on disk.** Directories `0700`, files `0600`, same as every
|
|
28
|
+
* other collector state directory (`local-state-paths.ts`). No secret is
|
|
29
|
+
* ever written here — this stores API RESPONSE bodies, the same bodies the
|
|
30
|
+
* CLI already prints to a terminal — but the discipline is kept anyway.
|
|
31
|
+
*/
|
|
32
|
+
/** The seven surfaces this ticket's cache covers. A write to any other path invalidates nothing (and is never asked to). */
|
|
33
|
+
export declare const READ_CACHE_SURFACES: readonly ["work", "docs", "cal", "mail", "msg", "settings", "memory"];
|
|
34
|
+
export type ReadCacheSurface = (typeof READ_CACHE_SURFACES)[number];
|
|
35
|
+
/** `/api/work/...` → `"work"`, and so on. `null` for anything this cache does not cover. */
|
|
36
|
+
export declare function readCacheSurfaceForPath(apiPath: string): ReadCacheSurface | null;
|
|
37
|
+
/** One entry, as it sits on disk. */
|
|
38
|
+
export interface ReadCacheEntry {
|
|
39
|
+
storedAt: number;
|
|
40
|
+
status: number;
|
|
41
|
+
body: unknown;
|
|
42
|
+
}
|
|
43
|
+
/** Never the raw token — a machine-scoping label only, never reversible in practice and never logged with its input. */
|
|
44
|
+
export declare function operatorScope(deviceToken: string): string;
|
|
45
|
+
/** One request, folded to a filename-safe key. Includes the body so `memory search`'s different queries never collide. */
|
|
46
|
+
export declare function readCacheKey(label: string, apiPath: string, body: unknown): string;
|
|
47
|
+
export declare function readCacheSurfaceDir(stateDir: string, deviceToken: string, surface: ReadCacheSurface): string;
|
|
48
|
+
export declare function readCacheEntryPath(stateDir: string, deviceToken: string, surface: ReadCacheSurface, key: string): string;
|
|
49
|
+
/**
|
|
50
|
+
* Reads one entry. Missing, corrupt, or an unreadable shape are all the same
|
|
51
|
+
* answer — `null`, a cache miss — never a thrown error: a cache is a
|
|
52
|
+
* performance optimisation, and a command that crashed because its cache
|
|
53
|
+
* file was half-written would be worse than no cache at all.
|
|
54
|
+
*/
|
|
55
|
+
export declare function readReadCacheEntry(entryPath: string): ReadCacheEntry | null;
|
|
56
|
+
/**
|
|
57
|
+
* Writes one entry atomically: a temp file in the SAME directory (so the
|
|
58
|
+
* rename is on one filesystem, never a cross-device copy), then `rename`,
|
|
59
|
+
* which POSIX and NTFS both make an atomic replace. A reader mid-refresh
|
|
60
|
+
* therefore always sees either the old whole entry or the new whole entry,
|
|
61
|
+
* never a partial write.
|
|
62
|
+
*/
|
|
63
|
+
export declare function writeReadCacheEntryAtomic(entryPath: string, entry: Omit<ReadCacheEntry, "storedAt"> & {
|
|
64
|
+
storedAt?: number;
|
|
65
|
+
}): void;
|
|
66
|
+
/**
|
|
67
|
+
* Throws away every cached read for one surface — the whole answer to "any
|
|
68
|
+
* write through the CLI or MCP invalidates the affected cache" (BLI-10746).
|
|
69
|
+
* Coarse on purpose: a write to `/api/work/issues/BLI-1` could in principle
|
|
70
|
+
* only stale one `work list` filter combination, but naming exactly which
|
|
71
|
+
* cached queries a given write affects is its own project, and "the next
|
|
72
|
+
* read of this surface is live" costs one extra request, never a wrong
|
|
73
|
+
* answer.
|
|
74
|
+
*/
|
|
75
|
+
export declare function invalidateReadCacheSurface(stateDir: string, deviceToken: string, surface: ReadCacheSurface): {
|
|
76
|
+
ok: true;
|
|
77
|
+
removed: boolean;
|
|
78
|
+
} | {
|
|
79
|
+
ok: false;
|
|
80
|
+
reason: string;
|
|
81
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE ON-DISK, PER-OPERATOR, STALE-WHILE-REVALIDATE READ CACHE (BLI-10746).
|
|
3
|
+
*
|
|
4
|
+
* Node only — file permissions and `node:fs`/`node:crypto` are not something
|
|
5
|
+
* a dashboard bundle needs — so this lives behind this package's own
|
|
6
|
+
* `./node-read-cache` export, the same isolation `./node-daemon-http-client`
|
|
7
|
+
* next door uses.
|
|
8
|
+
*
|
|
9
|
+
* `@bli-cockpit/local-collector` (the CLI) owns the actual stale-while-
|
|
10
|
+
* revalidate DECISION — when to serve cache, when a fresh call is required,
|
|
11
|
+
* when to kick off a background refresh — because that decision needs
|
|
12
|
+
* `--fresh`/`--allow-stale`/`--json`, which are CLI concepts this module
|
|
13
|
+
* knows nothing about. What lives HERE is the shared, dumb, provably-correct
|
|
14
|
+
* bottom half both the CLI (reading and writing) and `@bli-cockpit/mcp` (only
|
|
15
|
+
* ever invalidating, on its own writes) need to agree on byte-for-byte:
|
|
16
|
+
* where an entry lives on disk, how it is written so a reader never sees a
|
|
17
|
+
* half-written file, and how a whole surface is thrown away after a write.
|
|
18
|
+
* Two independent computations of the same cache path is exactly the
|
|
19
|
+
* BLI-2541 shape (a reader that "worked" against a path nothing ever wrote)
|
|
20
|
+
* this module exists to rule out.
|
|
21
|
+
*
|
|
22
|
+
* **Per-operator, never shared.** The device token is never stored — only
|
|
23
|
+
* its SHA-256, truncated, as a directory segment — but two different people
|
|
24
|
+
* pairing the same machine still get two different cache trees, so a shared
|
|
25
|
+
* intern laptop can never show one operator's Tower data to another.
|
|
26
|
+
*
|
|
27
|
+
* **Owner-only on disk.** Directories `0700`, files `0600`, same as every
|
|
28
|
+
* other collector state directory (`local-state-paths.ts`). No secret is
|
|
29
|
+
* ever written here — this stores API RESPONSE bodies, the same bodies the
|
|
30
|
+
* CLI already prints to a terminal — but the discipline is kept anyway.
|
|
31
|
+
*/
|
|
32
|
+
import crypto from "node:crypto";
|
|
33
|
+
import fs from "node:fs";
|
|
34
|
+
import path from "node:path";
|
|
35
|
+
/** The seven surfaces this ticket's cache covers. A write to any other path invalidates nothing (and is never asked to). */
|
|
36
|
+
export const READ_CACHE_SURFACES = [
|
|
37
|
+
"work",
|
|
38
|
+
"docs",
|
|
39
|
+
"cal",
|
|
40
|
+
"mail",
|
|
41
|
+
"msg",
|
|
42
|
+
"settings",
|
|
43
|
+
"memory",
|
|
44
|
+
];
|
|
45
|
+
/** `/api/work/...` → `"work"`, and so on. `null` for anything this cache does not cover. */
|
|
46
|
+
export function readCacheSurfaceForPath(apiPath) {
|
|
47
|
+
const segment = apiPath.replace(/^\/+/, "").split("/")[1];
|
|
48
|
+
return READ_CACHE_SURFACES.includes(segment ?? "")
|
|
49
|
+
? segment
|
|
50
|
+
: null;
|
|
51
|
+
}
|
|
52
|
+
/** Never the raw token — a machine-scoping label only, never reversible in practice and never logged with its input. */
|
|
53
|
+
export function operatorScope(deviceToken) {
|
|
54
|
+
return crypto.createHash("sha256").update(deviceToken).digest("hex").slice(0, 16);
|
|
55
|
+
}
|
|
56
|
+
/** One request, folded to a filename-safe key. Includes the body so `memory search`'s different queries never collide. */
|
|
57
|
+
export function readCacheKey(label, apiPath, body) {
|
|
58
|
+
const material = `${label}\n${apiPath}\n${body === undefined ? "" : JSON.stringify(body)}`;
|
|
59
|
+
return crypto.createHash("sha256").update(material).digest("hex").slice(0, 40);
|
|
60
|
+
}
|
|
61
|
+
export function readCacheSurfaceDir(stateDir, deviceToken, surface) {
|
|
62
|
+
return path.join(stateDir, "read-cache", operatorScope(deviceToken), surface);
|
|
63
|
+
}
|
|
64
|
+
export function readCacheEntryPath(stateDir, deviceToken, surface, key) {
|
|
65
|
+
return path.join(readCacheSurfaceDir(stateDir, deviceToken, surface), `${key}.json`);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Reads one entry. Missing, corrupt, or an unreadable shape are all the same
|
|
69
|
+
* answer — `null`, a cache miss — never a thrown error: a cache is a
|
|
70
|
+
* performance optimisation, and a command that crashed because its cache
|
|
71
|
+
* file was half-written would be worse than no cache at all.
|
|
72
|
+
*/
|
|
73
|
+
export function readReadCacheEntry(entryPath) {
|
|
74
|
+
let raw;
|
|
75
|
+
try {
|
|
76
|
+
raw = fs.readFileSync(entryPath, "utf8");
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const parsed = JSON.parse(raw);
|
|
83
|
+
const storedAt = parsed["storedAt"];
|
|
84
|
+
const status = parsed["status"];
|
|
85
|
+
if (typeof storedAt !== "number" || typeof status !== "number" || !("body" in parsed)) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
return { storedAt, status, body: parsed["body"] };
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Writes one entry atomically: a temp file in the SAME directory (so the
|
|
96
|
+
* rename is on one filesystem, never a cross-device copy), then `rename`,
|
|
97
|
+
* which POSIX and NTFS both make an atomic replace. A reader mid-refresh
|
|
98
|
+
* therefore always sees either the old whole entry or the new whole entry,
|
|
99
|
+
* never a partial write.
|
|
100
|
+
*/
|
|
101
|
+
export function writeReadCacheEntryAtomic(entryPath, entry) {
|
|
102
|
+
const dir = path.dirname(entryPath);
|
|
103
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
104
|
+
const payload = { storedAt: entry.storedAt ?? Date.now(), status: entry.status, body: entry.body };
|
|
105
|
+
const tmpPath = path.join(dir, `.${path.basename(entryPath)}.${process.pid}.tmp`);
|
|
106
|
+
fs.writeFileSync(tmpPath, JSON.stringify(payload), { mode: 0o600 });
|
|
107
|
+
fs.renameSync(tmpPath, entryPath);
|
|
108
|
+
if (process.platform !== "win32") {
|
|
109
|
+
try {
|
|
110
|
+
fs.chmodSync(dir, 0o700);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// Best-effort tightening only; the mkdir mode above already asked for it.
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Throws away every cached read for one surface — the whole answer to "any
|
|
119
|
+
* write through the CLI or MCP invalidates the affected cache" (BLI-10746).
|
|
120
|
+
* Coarse on purpose: a write to `/api/work/issues/BLI-1` could in principle
|
|
121
|
+
* only stale one `work list` filter combination, but naming exactly which
|
|
122
|
+
* cached queries a given write affects is its own project, and "the next
|
|
123
|
+
* read of this surface is live" costs one extra request, never a wrong
|
|
124
|
+
* answer.
|
|
125
|
+
*/
|
|
126
|
+
export function invalidateReadCacheSurface(stateDir, deviceToken, surface) {
|
|
127
|
+
const dir = readCacheSurfaceDir(stateDir, deviceToken, surface);
|
|
128
|
+
try {
|
|
129
|
+
const existed = fs.existsSync(dir);
|
|
130
|
+
if (existed)
|
|
131
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
132
|
+
return { ok: true, removed: existed };
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
return { ok: false, reason: error instanceof Error ? error.name : "unknown" };
|
|
136
|
+
}
|
|
137
|
+
}
|
package/dist/privacy.d.ts
CHANGED
|
@@ -96,6 +96,10 @@ export declare const RawEvidencePointerSchema: z.ZodObject<{
|
|
|
96
96
|
sanitized_byte_size: z.ZodOptional<z.ZodNumber>;
|
|
97
97
|
applied_at: z.ZodOptional<z.ZodString>;
|
|
98
98
|
}, z.core.$strict>>;
|
|
99
|
+
supersedes_pointer_id: z.ZodOptional<z.ZodString>;
|
|
100
|
+
supersede_rule: z.ZodOptional<z.ZodEnum<{
|
|
101
|
+
prefix: "prefix";
|
|
102
|
+
}>>;
|
|
99
103
|
}, z.core.$strict>;
|
|
100
104
|
export type RawEvidencePointer = z.infer<typeof RawEvidencePointerSchema>;
|
|
101
105
|
export declare const RedactionMetadataSchema: z.ZodObject<{
|
package/dist/privacy.js
CHANGED
|
@@ -95,9 +95,23 @@ export const RawEvidencePointerSchema = z
|
|
|
95
95
|
media_type: NonEmptyStringSchema.optional(),
|
|
96
96
|
redacted_summary: z.string().trim().min(1).max(4000).optional(),
|
|
97
97
|
redaction: RawEvidenceRedactionMetadataSchema.optional(),
|
|
98
|
+
/**
|
|
99
|
+
* BLI-11088: this snapshot claims to hold every byte of an earlier
|
|
100
|
+
* snapshot of the same transcript, under `supersede_rule`. A claim, never
|
|
101
|
+
* a verdict: the ingest door re-derives it from Storage before deleting.
|
|
102
|
+
*/
|
|
103
|
+
supersedes_pointer_id: NonEmptyStringSchema.optional(),
|
|
104
|
+
supersede_rule: z.enum(["prefix"]).optional(),
|
|
98
105
|
})
|
|
99
106
|
.strict()
|
|
100
107
|
.superRefine((pointer, context) => {
|
|
108
|
+
if ((pointer.supersedes_pointer_id === undefined) !== (pointer.supersede_rule === undefined)) {
|
|
109
|
+
context.addIssue({
|
|
110
|
+
code: "custom",
|
|
111
|
+
message: "a supersede claim names both the pointer and the rule",
|
|
112
|
+
path: ["supersede_rule"],
|
|
113
|
+
});
|
|
114
|
+
}
|
|
101
115
|
if (pointer.privacy_classification !==
|
|
102
116
|
pointer.retention_policy.privacy_classification) {
|
|
103
117
|
context.addIssue({
|
|
@@ -80,7 +80,7 @@ export const SECRET_ASSIGNMENT_SOURCE = `["']?\\s*[:=]\\s*["']?`;
|
|
|
80
80
|
* `GRANT ... TO service_role` (no assignment) do not match; the same name with
|
|
81
81
|
* an assigned value does.
|
|
82
82
|
*/
|
|
83
|
-
const SECRET_NAME_WITH_VALUE_PATTERN = new RegExp(`${SECRET_NAME_MATCH_SOURCE}${SECRET_ASSIGNMENT_SOURCE}[A-Za-z0-9_./+=-]{12
|
|
83
|
+
const SECRET_NAME_WITH_VALUE_PATTERN = new RegExp(`${SECRET_NAME_MATCH_SOURCE}${SECRET_ASSIGNMENT_SOURCE}[A-Za-z0-9_./+=-]{12}[A-Za-z0-9_./+=-]*`, "i");
|
|
84
84
|
/**
|
|
85
85
|
* Self-identifying secret values — blocked regardless of context because the
|
|
86
86
|
* token shape itself is the credential, with no benign reading.
|
|
@@ -92,21 +92,24 @@ const SECRET_NAME_WITH_VALUE_PATTERN = new RegExp(`${SECRET_NAME_MATCH_SOURCE}${
|
|
|
92
92
|
* (`chart_m0_2024_revenue…`) for no safety gain. A real leak of any of these
|
|
93
93
|
* shapes is preceded by a quote, whitespace, `=`, `:` or `/` — all of which
|
|
94
94
|
* `\b` already admits.
|
|
95
|
+
*
|
|
96
|
+
* `C{n}C*` rather than `C{n,}`: same matches, no per-character backtrack stack
|
|
97
|
+
* on a multi-megabyte run (BLI-11094; see `secret-guards-masking.ts`).
|
|
95
98
|
*/
|
|
96
99
|
const SECRET_VALUE_PATTERNS = [
|
|
97
100
|
/-----BEGIN [A-Z ]*PRIVATE KEY-----/,
|
|
98
|
-
/\bBearer\s+[A-Za-z0-9._~+/=-]{16
|
|
99
|
-
/\beyJ[A-Za-z0-9_-]{10
|
|
100
|
-
/\b(?:sk|pk)_(?:live|test)_[A-Za-z0-9]{16
|
|
101
|
-
/\bsk-[A-Za-z0-9_-]{16
|
|
102
|
-
/\bgh[pousr]_[A-Za-z0-9_]{16
|
|
103
|
-
/\bgithub_pat_[A-Za-z0-9_]{20
|
|
104
|
-
/\blin_api_[A-Za-z0-9]{16
|
|
105
|
-
/\bsb_secret_[A-Za-z0-9_]{16
|
|
106
|
-
/\bxox[baprs]-[A-Za-z0-9-]{16
|
|
101
|
+
/\bBearer\s+[A-Za-z0-9._~+/=-]{16}[A-Za-z0-9._~+/=-]*/,
|
|
102
|
+
/\beyJ[A-Za-z0-9_-]{10}[A-Za-z0-9_-]*\.[A-Za-z0-9_-]{10}[A-Za-z0-9_-]*\.[A-Za-z0-9_-]{10}[A-Za-z0-9_-]*\b/,
|
|
103
|
+
/\b(?:sk|pk)_(?:live|test)_[A-Za-z0-9]{16}[A-Za-z0-9]*/,
|
|
104
|
+
/\bsk-[A-Za-z0-9_-]{16}[A-Za-z0-9_-]*/,
|
|
105
|
+
/\bgh[pousr]_[A-Za-z0-9_]{16}[A-Za-z0-9_]*/,
|
|
106
|
+
/\bgithub_pat_[A-Za-z0-9_]{20}[A-Za-z0-9_]*/,
|
|
107
|
+
/\blin_api_[A-Za-z0-9]{16}[A-Za-z0-9]*/,
|
|
108
|
+
/\bsb_secret_[A-Za-z0-9_]{16}[A-Za-z0-9_]*/,
|
|
109
|
+
/\bxox[baprs]-[A-Za-z0-9-]{16}[A-Za-z0-9-]*/,
|
|
107
110
|
/\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/,
|
|
108
|
-
/\b(?:pk|sk)-lf-[A-Za-z0-9_-]{16
|
|
109
|
-
/\bm0[-_][A-Za-z0-9_-]{16
|
|
111
|
+
/\b(?:pk|sk)-lf-[A-Za-z0-9_-]{16}[A-Za-z0-9_-]*\b/i,
|
|
112
|
+
/\bm0[-_][A-Za-z0-9_-]{16}[A-Za-z0-9_-]*\b/i,
|
|
110
113
|
];
|
|
111
114
|
export const SECRET_LIKE_CONTENT_PATTERNS = [
|
|
112
115
|
SECRET_NAME_WITH_VALUE_PATTERN,
|
|
@@ -14,12 +14,27 @@ import type { RawEvidenceRedactionMetadata, RawEvidenceRedactionSource } from ".
|
|
|
14
14
|
* after every real rule: masking is best-effort and NEVER drops a file, so a
|
|
15
15
|
* detector hit no rule can pinpoint is masked coarsely rather than costing the
|
|
16
16
|
* session (the BLI-3116 note on the first rule explains what that cost was).
|
|
17
|
+
*
|
|
18
|
+
* Every open-ended run is written `C{n}C*`, never `C{n,}` (BLI-11094). The two
|
|
19
|
+
* match exactly the same spans, but V8 runs `C{n,}` by pushing one backtrack
|
|
20
|
+
* entry per character, so a token run of about 10 million characters (a
|
|
21
|
+
* base64 blob after `api_key=` or `sk-` in a tool output) threw `RangeError:
|
|
22
|
+
* Maximum call stack size exceeded` and cost the whole file. `C*` over a
|
|
23
|
+
* single class runs as a flat loop with no stack, at any length.
|
|
24
|
+
* `secret-guards-long-runs.test.ts` pins both halves: the same answers as the
|
|
25
|
+
* old spelling, and no throw on a 20 million character run.
|
|
17
26
|
*/
|
|
18
27
|
export interface SecretRedactionResult {
|
|
19
28
|
redacted: boolean;
|
|
20
29
|
text: string;
|
|
21
30
|
metadata: RawEvidenceRedactionMetadata | null;
|
|
22
31
|
}
|
|
32
|
+
export interface RedactionRule {
|
|
33
|
+
ruleId: string;
|
|
34
|
+
pattern: RegExp;
|
|
35
|
+
secretGroup?: number;
|
|
36
|
+
}
|
|
37
|
+
export declare const SECRET_REDACTION_RULES: RedactionRule[];
|
|
23
38
|
/**
|
|
24
39
|
* Mask every secret-shaped span and keep the rest of the text, because a
|
|
25
40
|
* session is never dropped for carrying one: this returns the redacted bytes
|
|
@@ -3,8 +3,11 @@ import { RawEvidenceRedactionMetadataSchema } from "./secret-guards-metadata.js"
|
|
|
3
3
|
// Same fence as the detector (BLI-3116) — the two must agree or a prefixed
|
|
4
4
|
// name would be detected and then left unredacted, which costs whole sessions
|
|
5
5
|
// (see findSecretRedactionMatches).
|
|
6
|
-
const SECRET_ASSIGNMENT_REDACTION_PATTERN = new RegExp(`(${SECRET_NAME_MATCH_SOURCE}${SECRET_ASSIGNMENT_SOURCE})([A-Za-z0-9_./+=-]{12
|
|
7
|
-
|
|
6
|
+
const SECRET_ASSIGNMENT_REDACTION_PATTERN = new RegExp(`(${SECRET_NAME_MATCH_SOURCE}${SECRET_ASSIGNMENT_SOURCE})([A-Za-z0-9_./+=-]{12}[A-Za-z0-9_./+=-]*)`, "gi");
|
|
7
|
+
// Exported for `secret-guards-long-runs.test.ts` alone and deliberately NOT
|
|
8
|
+
// re-exported by `secret-guards.ts`: the test proves each rule answers exactly
|
|
9
|
+
// what its pre-BLI-11094 spelling answered.
|
|
10
|
+
export const SECRET_REDACTION_RULES = [
|
|
8
11
|
{
|
|
9
12
|
ruleId: "credential_assignment",
|
|
10
13
|
pattern: SECRET_ASSIGNMENT_REDACTION_PATTERN,
|
|
@@ -54,36 +57,36 @@ const SECRET_REDACTION_RULES = [
|
|
|
54
57
|
},
|
|
55
58
|
{
|
|
56
59
|
ruleId: "bearer_token",
|
|
57
|
-
pattern: /\b(Bearer\s+)([A-Za-z0-9._~+/=-]{16
|
|
60
|
+
pattern: /\b(Bearer\s+)([A-Za-z0-9._~+/=-]{16}[A-Za-z0-9._~+/=-]*)/gi,
|
|
58
61
|
secretGroup: 2,
|
|
59
62
|
},
|
|
60
63
|
{
|
|
61
64
|
ruleId: "jwt",
|
|
62
|
-
pattern: /\beyJ[A-Za-z0-9_-]{10
|
|
65
|
+
pattern: /\beyJ[A-Za-z0-9_-]{10}[A-Za-z0-9_-]*\.[A-Za-z0-9_-]{10}[A-Za-z0-9_-]*\.[A-Za-z0-9_-]{10}[A-Za-z0-9_-]*\b/g,
|
|
63
66
|
},
|
|
64
67
|
{
|
|
65
68
|
ruleId: "stripe_token",
|
|
66
|
-
pattern: /\b(?:sk|pk)_(?:live|test)_[A-Za-z0-9]{16
|
|
69
|
+
pattern: /\b(?:sk|pk)_(?:live|test)_[A-Za-z0-9]{16}[A-Za-z0-9]*/g,
|
|
67
70
|
},
|
|
68
71
|
{
|
|
69
72
|
ruleId: "openai_token",
|
|
70
|
-
pattern: /\bsk-[A-Za-z0-9_-]{16
|
|
73
|
+
pattern: /\bsk-[A-Za-z0-9_-]{16}[A-Za-z0-9_-]*/g,
|
|
71
74
|
},
|
|
72
75
|
{
|
|
73
76
|
ruleId: "github_token",
|
|
74
|
-
pattern: /\b(?:gh[pousr]_[A-Za-z0-9_]{16
|
|
77
|
+
pattern: /\b(?:gh[pousr]_[A-Za-z0-9_]{16}[A-Za-z0-9_]*|github_pat_[A-Za-z0-9_]{20}[A-Za-z0-9_]*)/g,
|
|
75
78
|
},
|
|
76
79
|
{
|
|
77
80
|
ruleId: "linear_token",
|
|
78
|
-
pattern: /\blin_api_[A-Za-z0-9]{16
|
|
81
|
+
pattern: /\blin_api_[A-Za-z0-9]{16}[A-Za-z0-9]*/g,
|
|
79
82
|
},
|
|
80
83
|
{
|
|
81
84
|
ruleId: "supabase_secret_token",
|
|
82
|
-
pattern: /\bsb_secret_[A-Za-z0-9_]{16
|
|
85
|
+
pattern: /\bsb_secret_[A-Za-z0-9_]{16}[A-Za-z0-9_]*/g,
|
|
83
86
|
},
|
|
84
87
|
{
|
|
85
88
|
ruleId: "slack_token",
|
|
86
|
-
pattern: /\bxox[baprs]-[A-Za-z0-9-]{16
|
|
89
|
+
pattern: /\bxox[baprs]-[A-Za-z0-9-]{16}[A-Za-z0-9-]*/g,
|
|
87
90
|
},
|
|
88
91
|
{
|
|
89
92
|
ruleId: "aws_access_key_id",
|
|
@@ -91,11 +94,11 @@ const SECRET_REDACTION_RULES = [
|
|
|
91
94
|
},
|
|
92
95
|
{
|
|
93
96
|
ruleId: "langfuse_token",
|
|
94
|
-
pattern: /\b(?:pk|sk)-lf-[A-Za-z0-9_-]{16
|
|
97
|
+
pattern: /\b(?:pk|sk)-lf-[A-Za-z0-9_-]{16}[A-Za-z0-9_-]*\b/gi,
|
|
95
98
|
},
|
|
96
99
|
{
|
|
97
100
|
ruleId: "mem0_token",
|
|
98
|
-
pattern: /\bm0[-_][A-Za-z0-9_-]{16
|
|
101
|
+
pattern: /\bm0[-_][A-Za-z0-9_-]{16}[A-Za-z0-9_-]*\b/gi,
|
|
99
102
|
},
|
|
100
103
|
];
|
|
101
104
|
/**
|
|
@@ -163,7 +166,7 @@ function findSecretRedactionMatches(value) {
|
|
|
163
166
|
const uncovered = detectorFallbackCandidates(value).filter((candidate) => !overlapsAny(candidate, accepted));
|
|
164
167
|
if (uncovered.length === 0)
|
|
165
168
|
return accepted;
|
|
166
|
-
return acceptNonOverlapping(
|
|
169
|
+
return acceptNonOverlapping(accepted.concat(uncovered));
|
|
167
170
|
}
|
|
168
171
|
function secretRuleCandidates(value) {
|
|
169
172
|
const candidates = [];
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export type SupersedeRule = "prefix";
|
|
2
|
+
/**
|
|
3
|
+
* Truncated prefix hashes at every power of two from 64 bytes, so a rewrite
|
|
4
|
+
* can be located to a byte range without keeping the old bytes: about 20
|
|
5
|
+
* entries of 16 hex characters for a 100 MB file.
|
|
6
|
+
*/
|
|
7
|
+
export declare function prefixCheckpoints(bytes: Uint8Array): Record<string, string>;
|
|
8
|
+
/**
|
|
9
|
+
* Where two snapshots first differ, as a half-open byte range: the largest
|
|
10
|
+
* checkpoint that still matches and the smallest one that does not (or the
|
|
11
|
+
* shorter file's end). Offsets only; never content.
|
|
12
|
+
*/
|
|
13
|
+
export declare function firstDifferenceRange(newBytes: Uint8Array, prior: {
|
|
14
|
+
byte_size: number;
|
|
15
|
+
checkpoints?: Record<string, string> | null;
|
|
16
|
+
}): {
|
|
17
|
+
at_least: number;
|
|
18
|
+
below: number;
|
|
19
|
+
};
|
|
20
|
+
export interface PriorSnapshot {
|
|
21
|
+
content_hash_sha256: string;
|
|
22
|
+
byte_size: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The collector's side: do these new bytes rebuild the prior committed
|
|
26
|
+
* snapshot of the same file? Only a hash of the new bytes is compared with
|
|
27
|
+
* what the cursor remembered; the server re-derives the answer from Storage
|
|
28
|
+
* before anything is deleted.
|
|
29
|
+
*/
|
|
30
|
+
export declare function isPrefixSuccessor(newBytes: Uint8Array, prior: PriorSnapshot): boolean;
|
|
31
|
+
export interface PrefixDigests {
|
|
32
|
+
/** offset -> sha256 of the stream's first `offset` bytes, for every offset reached. */
|
|
33
|
+
digests: Map<number, string>;
|
|
34
|
+
total_bytes: number;
|
|
35
|
+
full_digest: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Streams bytes through one sha256 and snapshots the digest at each requested
|
|
39
|
+
* offset. An offset past the end is absent, which a caller reads as "not a
|
|
40
|
+
* prefix". Peak memory is one chunk plus the hash state.
|
|
41
|
+
*/
|
|
42
|
+
export declare function prefixDigestsFromStream(chunks: AsyncIterable<Uint8Array>, offsets: readonly number[]): Promise<PrefixDigests>;
|
|
43
|
+
export type SupersedeVerification = {
|
|
44
|
+
ok: true;
|
|
45
|
+
rule: SupersedeRule;
|
|
46
|
+
} | {
|
|
47
|
+
ok: false;
|
|
48
|
+
reason: "newer_not_larger" | "prefix_mismatch";
|
|
49
|
+
};
|
|
50
|
+
/** The ingest door's side: re-derive one claim from the newer object's bytes. */
|
|
51
|
+
export declare function verifySupersedeFromStream(newer: AsyncIterable<Uint8Array>, claim: {
|
|
52
|
+
older_byte_size: number;
|
|
53
|
+
older_sha256: string;
|
|
54
|
+
}): Promise<SupersedeVerification>;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When may one stored transcript snapshot replace another (BLI-11088)?
|
|
3
|
+
*
|
|
4
|
+
* The collector uploads the current bytes of a growing transcript every sync,
|
|
5
|
+
* so the same file lands as many objects. An older snapshot is redundant, and
|
|
6
|
+
* may be deleted, when it is a byte PREFIX of a newer snapshot of the same
|
|
7
|
+
* file: every one of its bytes is still there. That is the only rule.
|
|
8
|
+
* Anything else keeps both objects and is logged `snapshot_rewritten`.
|
|
9
|
+
*
|
|
10
|
+
* Measured 2026-09-30: Claude transcripts are append-only; Codex rewrote
|
|
11
|
+
* every line of older rollouts once (an `ordinal` on each record), so a
|
|
12
|
+
* pre-rewrite snapshot is a prefix of the last pre-rewrite snapshot, never of
|
|
13
|
+
* the post-rewrite one. Comparing with the nearest KEPT successor, not only
|
|
14
|
+
* the newest, is what recovers those.
|
|
15
|
+
*
|
|
16
|
+
* One module, three callers, so the rule cannot drift: the collector decides
|
|
17
|
+
* what to claim, the ingest door re-derives the claim from what Storage holds
|
|
18
|
+
* before it deletes anything, and the backfill applies it to what is already
|
|
19
|
+
* stored. Pure except for `node:crypto`; never logs; holds one chunk at a time.
|
|
20
|
+
*/
|
|
21
|
+
import crypto from "node:crypto";
|
|
22
|
+
function sha256(bytes) {
|
|
23
|
+
return crypto.createHash("sha256").update(bytes).digest("hex");
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Truncated prefix hashes at every power of two from 64 bytes, so a rewrite
|
|
27
|
+
* can be located to a byte range without keeping the old bytes: about 20
|
|
28
|
+
* entries of 16 hex characters for a 100 MB file.
|
|
29
|
+
*/
|
|
30
|
+
export function prefixCheckpoints(bytes) {
|
|
31
|
+
const out = {};
|
|
32
|
+
const hash = crypto.createHash("sha256");
|
|
33
|
+
let fed = 0;
|
|
34
|
+
for (let at = 64; at < bytes.byteLength; at *= 2) {
|
|
35
|
+
hash.update(bytes.subarray(fed, at));
|
|
36
|
+
fed = at;
|
|
37
|
+
out[String(at)] = hash.copy().digest("hex").slice(0, 16);
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Where two snapshots first differ, as a half-open byte range: the largest
|
|
43
|
+
* checkpoint that still matches and the smallest one that does not (or the
|
|
44
|
+
* shorter file's end). Offsets only; never content.
|
|
45
|
+
*/
|
|
46
|
+
export function firstDifferenceRange(newBytes, prior) {
|
|
47
|
+
const limit = Math.min(prior.byte_size, newBytes.byteLength);
|
|
48
|
+
let atLeast = 0;
|
|
49
|
+
let below = limit;
|
|
50
|
+
const hash = crypto.createHash("sha256");
|
|
51
|
+
let fed = 0;
|
|
52
|
+
const offsets = Object.keys(prior.checkpoints ?? {}).map(Number).filter((at) => at <= limit).sort((a, b) => a - b);
|
|
53
|
+
for (const at of offsets) {
|
|
54
|
+
hash.update(newBytes.subarray(fed, at));
|
|
55
|
+
fed = at;
|
|
56
|
+
if (hash.copy().digest("hex").slice(0, 16) === prior.checkpoints[String(at)])
|
|
57
|
+
atLeast = at;
|
|
58
|
+
else {
|
|
59
|
+
below = at;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return { at_least: atLeast, below };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The collector's side: do these new bytes rebuild the prior committed
|
|
67
|
+
* snapshot of the same file? Only a hash of the new bytes is compared with
|
|
68
|
+
* what the cursor remembered; the server re-derives the answer from Storage
|
|
69
|
+
* before anything is deleted.
|
|
70
|
+
*/
|
|
71
|
+
export function isPrefixSuccessor(newBytes, prior) {
|
|
72
|
+
const n = prior.byte_size;
|
|
73
|
+
return n > 0 && n < newBytes.byteLength && sha256(newBytes.subarray(0, n)) === prior.content_hash_sha256;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Streams bytes through one sha256 and snapshots the digest at each requested
|
|
77
|
+
* offset. An offset past the end is absent, which a caller reads as "not a
|
|
78
|
+
* prefix". Peak memory is one chunk plus the hash state.
|
|
79
|
+
*/
|
|
80
|
+
export async function prefixDigestsFromStream(chunks, offsets) {
|
|
81
|
+
const wanted = [...new Set(offsets.filter((offset) => offset > 0))].sort((a, b) => a - b);
|
|
82
|
+
const digests = new Map();
|
|
83
|
+
const hash = crypto.createHash("sha256");
|
|
84
|
+
let seen = 0;
|
|
85
|
+
let next = 0;
|
|
86
|
+
for await (const chunk of chunks) {
|
|
87
|
+
let cursor = 0;
|
|
88
|
+
while (next < wanted.length && seen + (chunk.byteLength - cursor) >= wanted[next]) {
|
|
89
|
+
const take = wanted[next] - seen;
|
|
90
|
+
hash.update(chunk.subarray(cursor, cursor + take));
|
|
91
|
+
cursor += take;
|
|
92
|
+
seen += take;
|
|
93
|
+
digests.set(wanted[next], hash.copy().digest("hex"));
|
|
94
|
+
next += 1;
|
|
95
|
+
}
|
|
96
|
+
if (cursor < chunk.byteLength) {
|
|
97
|
+
hash.update(chunk.subarray(cursor));
|
|
98
|
+
seen += chunk.byteLength - cursor;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return { digests, total_bytes: seen, full_digest: hash.digest("hex") };
|
|
102
|
+
}
|
|
103
|
+
/** The ingest door's side: re-derive one claim from the newer object's bytes. */
|
|
104
|
+
export async function verifySupersedeFromStream(newer, claim) {
|
|
105
|
+
const result = await prefixDigestsFromStream(newer, [claim.older_byte_size]);
|
|
106
|
+
if (result.total_bytes <= claim.older_byte_size)
|
|
107
|
+
return { ok: false, reason: "newer_not_larger" };
|
|
108
|
+
return result.digests.get(claim.older_byte_size) === claim.older_sha256
|
|
109
|
+
? { ok: true, rule: "prefix" }
|
|
110
|
+
: { ok: false, reason: "prefix_mismatch" };
|
|
111
|
+
}
|
package/dist/source-adapter.d.ts
CHANGED
|
@@ -104,6 +104,10 @@ export declare const SourceScanEventSummarySchema: z.ZodObject<{
|
|
|
104
104
|
sanitized_byte_size: z.ZodOptional<z.ZodNumber>;
|
|
105
105
|
applied_at: z.ZodOptional<z.ZodString>;
|
|
106
106
|
}, z.core.$strict>>;
|
|
107
|
+
supersedes_pointer_id: z.ZodOptional<z.ZodString>;
|
|
108
|
+
supersede_rule: z.ZodOptional<z.ZodEnum<{
|
|
109
|
+
prefix: "prefix";
|
|
110
|
+
}>>;
|
|
107
111
|
}, z.core.$strict>>>;
|
|
108
112
|
}, z.core.$strict>;
|
|
109
113
|
export type SourceScanEventSummary = z.infer<typeof SourceScanEventSummarySchema>;
|
|
@@ -222,6 +226,10 @@ export declare const SourceScanResultSchema: z.ZodObject<{
|
|
|
222
226
|
sanitized_byte_size: z.ZodOptional<z.ZodNumber>;
|
|
223
227
|
applied_at: z.ZodOptional<z.ZodString>;
|
|
224
228
|
}, z.core.$strict>>;
|
|
229
|
+
supersedes_pointer_id: z.ZodOptional<z.ZodString>;
|
|
230
|
+
supersede_rule: z.ZodOptional<z.ZodEnum<{
|
|
231
|
+
prefix: "prefix";
|
|
232
|
+
}>>;
|
|
225
233
|
}, z.core.$strict>>>;
|
|
226
234
|
}, z.core.$strict>>>;
|
|
227
235
|
ticket_binding_candidates: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
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.53",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -24,6 +24,21 @@
|
|
|
24
24
|
"types": "./dist/evidence-storage-encoding.d.ts",
|
|
25
25
|
"import": "./dist/evidence-storage-encoding.js",
|
|
26
26
|
"default": "./dist/evidence-storage-encoding.js"
|
|
27
|
+
},
|
|
28
|
+
"./snapshot-supersede": {
|
|
29
|
+
"types": "./dist/snapshot-supersede.d.ts",
|
|
30
|
+
"import": "./dist/snapshot-supersede.js",
|
|
31
|
+
"default": "./dist/snapshot-supersede.js"
|
|
32
|
+
},
|
|
33
|
+
"./node-daemon-http-client": {
|
|
34
|
+
"types": "./dist/node-daemon-http-client.d.ts",
|
|
35
|
+
"import": "./dist/node-daemon-http-client.js",
|
|
36
|
+
"default": "./dist/node-daemon-http-client.js"
|
|
37
|
+
},
|
|
38
|
+
"./node-read-cache": {
|
|
39
|
+
"types": "./dist/node-read-cache.d.ts",
|
|
40
|
+
"import": "./dist/node-read-cache.js",
|
|
41
|
+
"default": "./dist/node-read-cache.js"
|
|
27
42
|
}
|
|
28
43
|
},
|
|
29
44
|
"publishConfig": {
|