@bli-cockpit/telemetry-core 0.1.51 → 0.1.54

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.
@@ -0,0 +1,60 @@
1
+ /**
2
+ * "Which of my stored transcripts are only a redaction-crash stub?" — the wire
3
+ * contract for asking (BLI-11102).
4
+ *
5
+ * When the redactor crashed (BLI‑3238, fixed for the known cause in ticket
6
+ * 11094) the collector uploaded one marker line instead of the session, so the
7
+ * pointer exists and the content does not. A stub is a promise to come back,
8
+ * not a delivery. The collector's cursor cannot tell (it recorded an upload),
9
+ * so the server, which holds the label on the pointer, answers the question and
10
+ * `cockpit backfill` re-offers every session it names.
11
+ *
12
+ * The stub is always the same 42 bytes, so its receipt proves what it holds:
13
+ * content hash `REDACTION_STUB_SHA256`, byte size `REDACTION_STUB_BYTE_SIZE`.
14
+ * That is also why replacing it keeps every byte: it holds no session bytes.
15
+ */
16
+ import { z } from "zod";
17
+ export declare const EVIDENCE_STUBS_SCHEMA_VERSION = "evidence-stubs.v1";
18
+ /** The completeness label and rule id the sanitizer writes on a stub. */
19
+ export declare const REDACTION_STUB_LABEL = "redaction_crashed_stubbed";
20
+ /** The exact bytes of every stub the collector has ever uploaded. */
21
+ export declare const REDACTION_STUB_TEXT = "[REDACTED_LINE:redaction_crashed_stubbed]\n";
22
+ export declare const REDACTION_STUB_BYTE_SIZE = 42;
23
+ /** sha256 of `REDACTION_STUB_TEXT`; pinned by `evidence-stubs.test.ts`. */
24
+ export declare const REDACTION_STUB_SHA256 = "ddaf92c5ea8b178f735944dc753588ce788211340f5106d37b5f44568f6a0fe9";
25
+ /** One page holds at most this many stubs (under the 1,000-row REST cap). */
26
+ export declare const EVIDENCE_STUBS_MAX_PAGE = 500;
27
+ export declare const EvidenceStubsRequestSchema: z.ZodObject<{
28
+ schema_version: z.ZodLiteral<"evidence-stubs.v1">;
29
+ after: z.ZodOptional<z.ZodNullable<z.ZodString>>;
30
+ limit: z.ZodOptional<z.ZodNumber>;
31
+ }, z.core.$strict>;
32
+ export type EvidenceStubsRequest = z.infer<typeof EvidenceStubsRequestSchema>;
33
+ export declare const EvidenceStubSchema: z.ZodObject<{
34
+ ref_id: z.ZodString;
35
+ raw_evidence_pointer_id: z.ZodString;
36
+ object_key: z.ZodString;
37
+ received_at: z.ZodString;
38
+ }, z.core.$strict>;
39
+ export type EvidenceStub = z.infer<typeof EvidenceStubSchema>;
40
+ export declare const EvidenceStubsResponseSchema: z.ZodObject<{
41
+ schema_version: z.ZodLiteral<"evidence-stubs.v1">;
42
+ stubs: z.ZodArray<z.ZodObject<{
43
+ ref_id: z.ZodString;
44
+ raw_evidence_pointer_id: z.ZodString;
45
+ object_key: z.ZodString;
46
+ received_at: z.ZodString;
47
+ }, z.core.$strict>>;
48
+ next_after: z.ZodNullable<z.ZodString>;
49
+ }, z.core.$strict>;
50
+ export type EvidenceStubsResponse = z.infer<typeof EvidenceStubsResponseSchema>;
51
+ /**
52
+ * Which agent session a transcript object belongs to, read off its key
53
+ * (`.../codex/<session>/<hash>.jsonl`, `.../claude/<session>/<hash>.jsonl`).
54
+ * The collector names objects this way (`raw-evidence-codex-reader.ts`,
55
+ * `raw-evidence-claude-reader.ts`); anything else is null, never a guess.
56
+ */
57
+ export declare function transcriptSessionFromObjectKey(objectKey: string): {
58
+ source: "codex" | "claude_code";
59
+ session_id: string;
60
+ } | null;
@@ -0,0 +1,66 @@
1
+ /**
2
+ * "Which of my stored transcripts are only a redaction-crash stub?" — the wire
3
+ * contract for asking (BLI-11102).
4
+ *
5
+ * When the redactor crashed (BLI‑3238, fixed for the known cause in ticket
6
+ * 11094) the collector uploaded one marker line instead of the session, so the
7
+ * pointer exists and the content does not. A stub is a promise to come back,
8
+ * not a delivery. The collector's cursor cannot tell (it recorded an upload),
9
+ * so the server, which holds the label on the pointer, answers the question and
10
+ * `cockpit backfill` re-offers every session it names.
11
+ *
12
+ * The stub is always the same 42 bytes, so its receipt proves what it holds:
13
+ * content hash `REDACTION_STUB_SHA256`, byte size `REDACTION_STUB_BYTE_SIZE`.
14
+ * That is also why replacing it keeps every byte: it holds no session bytes.
15
+ */
16
+ import { z } from "zod";
17
+ import { IsoDateTimeSchema, NonEmptyStringSchema } from "./common.js";
18
+ export const EVIDENCE_STUBS_SCHEMA_VERSION = "evidence-stubs.v1";
19
+ /** The completeness label and rule id the sanitizer writes on a stub. */
20
+ export const REDACTION_STUB_LABEL = "redaction_crashed_stubbed";
21
+ /** The exact bytes of every stub the collector has ever uploaded. */
22
+ export const REDACTION_STUB_TEXT = `[REDACTED_LINE:${REDACTION_STUB_LABEL}]\n`;
23
+ export const REDACTION_STUB_BYTE_SIZE = 42;
24
+ /** sha256 of `REDACTION_STUB_TEXT`; pinned by `evidence-stubs.test.ts`. */
25
+ export const REDACTION_STUB_SHA256 = "ddaf92c5ea8b178f735944dc753588ce788211340f5106d37b5f44568f6a0fe9";
26
+ /** One page holds at most this many stubs (under the 1,000-row REST cap). */
27
+ export const EVIDENCE_STUBS_MAX_PAGE = 500;
28
+ export const EvidenceStubsRequestSchema = z
29
+ .object({
30
+ schema_version: z.literal(EVIDENCE_STUBS_SCHEMA_VERSION),
31
+ /** Keyset cursor: the last ref id of the previous page, or null for the first. */
32
+ after: z.string().uuid().nullable().optional(),
33
+ limit: z.number().int().min(1).max(EVIDENCE_STUBS_MAX_PAGE).optional(),
34
+ })
35
+ .strict();
36
+ export const EvidenceStubSchema = z
37
+ .object({
38
+ ref_id: z.string().uuid(),
39
+ raw_evidence_pointer_id: NonEmptyStringSchema,
40
+ object_key: NonEmptyStringSchema,
41
+ received_at: IsoDateTimeSchema,
42
+ })
43
+ .strict();
44
+ export const EvidenceStubsResponseSchema = z
45
+ .object({
46
+ schema_version: z.literal(EVIDENCE_STUBS_SCHEMA_VERSION),
47
+ stubs: z.array(EvidenceStubSchema),
48
+ /** Pass as `after` to read the next page; null once the list is exhausted. */
49
+ next_after: z.string().uuid().nullable(),
50
+ })
51
+ .strict();
52
+ /**
53
+ * Which agent session a transcript object belongs to, read off its key
54
+ * (`.../codex/<session>/<hash>.jsonl`, `.../claude/<session>/<hash>.jsonl`).
55
+ * The collector names objects this way (`raw-evidence-codex-reader.ts`,
56
+ * `raw-evidence-claude-reader.ts`); anything else is null, never a guess.
57
+ */
58
+ export function transcriptSessionFromObjectKey(objectKey) {
59
+ const match = /(?:^|\/)(codex|claude)\/([A-Za-z0-9._-]{1,80})\/[0-9a-f]{16}\.jsonl$/u.exec(objectKey);
60
+ if (!match)
61
+ return null;
62
+ return {
63
+ source: match[1] === "codex" ? "codex" : "claude_code",
64
+ session_id: match[2],
65
+ };
66
+ }
@@ -122,6 +122,11 @@ 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
+ stub_replaced: "stub_replaced";
129
+ }>>;
125
130
  }, z.core.$strict>;
126
131
  chunk_size_bytes: z.ZodNumber;
127
132
  chunk_count: z.ZodNumber;
@@ -222,6 +227,11 @@ export declare const RawEvidenceUploadBeginRequestSchema: z.ZodObject<{
222
227
  sanitized_byte_size: z.ZodOptional<z.ZodNumber>;
223
228
  applied_at: z.ZodOptional<z.ZodString>;
224
229
  }, z.core.$strict>>;
230
+ supersedes_pointer_id: z.ZodOptional<z.ZodString>;
231
+ supersede_rule: z.ZodOptional<z.ZodEnum<{
232
+ prefix: "prefix";
233
+ stub_replaced: "stub_replaced";
234
+ }>>;
225
235
  }, z.core.$strict>;
226
236
  chunk_size_bytes: z.ZodNumber;
227
237
  chunk_count: z.ZodNumber;
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from "./decision-event.js";
6
6
  export * from "./decision-event-id.js";
7
7
  export * from "./evidence-completeness.js";
8
8
  export * from "./evidence-reconcile.js";
9
+ export * from "./evidence-stubs.js";
9
10
  export * from "./evidence-upload.js";
10
11
  export * from "./ingest-dto.js";
11
12
  export * from "./local-config.js";
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ export * from "./decision-event.js";
6
6
  export * from "./decision-event-id.js";
7
7
  export * from "./evidence-completeness.js";
8
8
  export * from "./evidence-reconcile.js";
9
+ export * from "./evidence-stubs.js";
9
10
  export * from "./evidence-upload.js";
10
11
  export * from "./ingest-dto.js";
11
12
  export * from "./local-config.js";
@@ -318,6 +318,11 @@ 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
+ stub_replaced: "stub_replaced";
325
+ }>>;
321
326
  }, z.core.$strict>>>;
322
327
  otel: z.ZodOptional<z.ZodObject<{
323
328
  trace_id: z.ZodOptional<z.ZodString>;
@@ -797,6 +802,11 @@ export declare const TelemetryIngestEnvelopeSchema: z.ZodObject<{
797
802
  sanitized_byte_size: z.ZodOptional<z.ZodNumber>;
798
803
  applied_at: z.ZodOptional<z.ZodString>;
799
804
  }, z.core.$strict>>;
805
+ supersedes_pointer_id: z.ZodOptional<z.ZodString>;
806
+ supersede_rule: z.ZodOptional<z.ZodEnum<{
807
+ prefix: "prefix";
808
+ stub_replaced: "stub_replaced";
809
+ }>>;
800
810
  }, z.core.$strict>>>;
801
811
  otel: z.ZodOptional<z.ZodObject<{
802
812
  trace_id: z.ZodOptional<z.ZodString>;
@@ -951,6 +961,11 @@ export declare const TelemetryIngestEnvelopeSchema: z.ZodObject<{
951
961
  sanitized_byte_size: z.ZodOptional<z.ZodNumber>;
952
962
  applied_at: z.ZodOptional<z.ZodString>;
953
963
  }, z.core.$strict>>;
964
+ supersedes_pointer_id: z.ZodOptional<z.ZodString>;
965
+ supersede_rule: z.ZodOptional<z.ZodEnum<{
966
+ prefix: "prefix";
967
+ stub_replaced: "stub_replaced";
968
+ }>>;
954
969
  }, z.core.$strict>>>;
955
970
  }, z.core.$strict>>>;
956
971
  ticket_binding_candidates: z.ZodDefault<z.ZodArray<z.ZodObject<{
package/dist/privacy.d.ts CHANGED
@@ -96,6 +96,11 @@ 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
+ stub_replaced: "stub_replaced";
103
+ }>>;
99
104
  }, z.core.$strict>;
100
105
  export type RawEvidencePointer = z.infer<typeof RawEvidencePointerSchema>;
101
106
  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", "stub_replaced"]).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,}`, "i");
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,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/,
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,}\b/i,
109
- /\bm0[-_][A-Za-z0-9_-]{16,}\b/i,
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,})`, "gi");
7
- const SECRET_REDACTION_RULES = [
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,})/gi,
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,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/g,
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,}/g,
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,}/g,
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,}|github_pat_[A-Za-z0-9_]{20,})/g,
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,}/g,
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,}/g,
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,}/g,
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,}\b/gi,
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,}\b/gi,
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([...accepted, ...uncovered]);
169
+ return acceptNonOverlapping(accepted.concat(uncovered));
167
170
  }
168
171
  function secretRuleCandidates(value) {
169
172
  const candidates = [];
@@ -0,0 +1,61 @@
1
+ /**
2
+ * `prefix`: the older snapshot is a byte prefix of the newer one (BLI-11088).
3
+ * `stub_replaced`: the older object is the 42-byte redaction-crash stub, which
4
+ * holds no session bytes, and the newer one is the same agent session
5
+ * redacted whole (BLI-11102). Verified by the ingest door from the stub's
6
+ * receipt and Storage, never taken on the collector's word.
7
+ */
8
+ export type SupersedeRule = "prefix" | "stub_replaced";
9
+ /**
10
+ * Truncated prefix hashes at every power of two from 64 bytes, so a rewrite
11
+ * can be located to a byte range without keeping the old bytes: about 20
12
+ * entries of 16 hex characters for a 100 MB file.
13
+ */
14
+ export declare function prefixCheckpoints(bytes: Uint8Array): Record<string, string>;
15
+ /**
16
+ * Where two snapshots first differ, as a half-open byte range: the largest
17
+ * checkpoint that still matches and the smallest one that does not (or the
18
+ * shorter file's end). Offsets only; never content.
19
+ */
20
+ export declare function firstDifferenceRange(newBytes: Uint8Array, prior: {
21
+ byte_size: number;
22
+ checkpoints?: Record<string, string> | null;
23
+ }): {
24
+ at_least: number;
25
+ below: number;
26
+ };
27
+ export interface PriorSnapshot {
28
+ content_hash_sha256: string;
29
+ byte_size: number;
30
+ }
31
+ /**
32
+ * The collector's side: do these new bytes rebuild the prior committed
33
+ * snapshot of the same file? Only a hash of the new bytes is compared with
34
+ * what the cursor remembered; the server re-derives the answer from Storage
35
+ * before anything is deleted.
36
+ */
37
+ export declare function isPrefixSuccessor(newBytes: Uint8Array, prior: PriorSnapshot): boolean;
38
+ export interface PrefixDigests {
39
+ /** offset -> sha256 of the stream's first `offset` bytes, for every offset reached. */
40
+ digests: Map<number, string>;
41
+ total_bytes: number;
42
+ full_digest: string;
43
+ }
44
+ /**
45
+ * Streams bytes through one sha256 and snapshots the digest at each requested
46
+ * offset. An offset past the end is absent, which a caller reads as "not a
47
+ * prefix". Peak memory is one chunk plus the hash state.
48
+ */
49
+ export declare function prefixDigestsFromStream(chunks: AsyncIterable<Uint8Array>, offsets: readonly number[]): Promise<PrefixDigests>;
50
+ export type SupersedeVerification = {
51
+ ok: true;
52
+ rule: SupersedeRule;
53
+ } | {
54
+ ok: false;
55
+ reason: "newer_not_larger" | "prefix_mismatch";
56
+ };
57
+ /** The ingest door's side: re-derive one claim from the newer object's bytes. */
58
+ export declare function verifySupersedeFromStream(newer: AsyncIterable<Uint8Array>, claim: {
59
+ older_byte_size: number;
60
+ older_sha256: string;
61
+ }): 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
+ }
@@ -104,6 +104,11 @@ 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
+ stub_replaced: "stub_replaced";
111
+ }>>;
107
112
  }, z.core.$strict>>>;
108
113
  }, z.core.$strict>;
109
114
  export type SourceScanEventSummary = z.infer<typeof SourceScanEventSummarySchema>;
@@ -222,6 +227,11 @@ export declare const SourceScanResultSchema: z.ZodObject<{
222
227
  sanitized_byte_size: z.ZodOptional<z.ZodNumber>;
223
228
  applied_at: z.ZodOptional<z.ZodString>;
224
229
  }, z.core.$strict>>;
230
+ supersedes_pointer_id: z.ZodOptional<z.ZodString>;
231
+ supersede_rule: z.ZodOptional<z.ZodEnum<{
232
+ prefix: "prefix";
233
+ stub_replaced: "stub_replaced";
234
+ }>>;
225
235
  }, z.core.$strict>>>;
226
236
  }, z.core.$strict>>>;
227
237
  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.51",
3
+ "version": "0.1.54",
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",