@bli-cockpit/telemetry-core 0.1.51 → 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/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 +6 -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<{
|
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",
|
|
@@ -25,6 +25,11 @@
|
|
|
25
25
|
"import": "./dist/evidence-storage-encoding.js",
|
|
26
26
|
"default": "./dist/evidence-storage-encoding.js"
|
|
27
27
|
},
|
|
28
|
+
"./snapshot-supersede": {
|
|
29
|
+
"types": "./dist/snapshot-supersede.d.ts",
|
|
30
|
+
"import": "./dist/snapshot-supersede.js",
|
|
31
|
+
"default": "./dist/snapshot-supersede.js"
|
|
32
|
+
},
|
|
28
33
|
"./node-daemon-http-client": {
|
|
29
34
|
"types": "./dist/node-daemon-http-client.d.ts",
|
|
30
35
|
"import": "./dist/node-daemon-http-client.js",
|