@bli-cockpit/telemetry-core 0.1.108 → 0.1.110

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,35 @@
1
+ /**
2
+ * TEXT THE AGENT HOST INJECTED IS NOT A MEMORY (BLI-26056).
3
+ *
4
+ * Claude Code puts its own text into the conversation as if it were a user turn: `<system-reminder>` blocks (a
5
+ * file's CLAUDE.md, a skill list, the "[SYSTEM NOTIFICATION - NOT USER INPUT] This is an automated background-task
6
+ * event" notice) and bare `<task-notification>` blocks when a background command or agent reports back. Nobody wrote
7
+ * those words. The Stop hook read them as the person's turn and saved them; when extraction then failed, the door's
8
+ * verbatim fallback (BLI-3713) stored the whole block as a memory (two such rows were found on 2026-11-20).
9
+ *
10
+ * This is the one place that knows those wrappers, for every capture path: the Stop and PreCompact hooks strip them
11
+ * before they send anything, and `POST /api/memory/save` strips them again for clients that predate the hooks' fix.
12
+ *
13
+ * A closed textual test, like the prompt hook's `automated-event.ts`: only CLOSED blocks are taken out, so a sentence
14
+ * that merely names the tag ("Claude Code wraps notices in <system-reminder>") is left alone. An unclosed block stays
15
+ * as text: when in doubt, keep what the person may have written.
16
+ */
17
+ /** The first line of the host's automated-event notice, which can also arrive outside a reminder block. */
18
+ export declare const HARNESS_AUTOMATED_NOTICE_MARKER = "[SYSTEM NOTIFICATION - NOT USER INPUT]";
19
+ export interface HarnessTextReading {
20
+ /** The text with every closed harness block taken out (trimmed). Equal to the input, trimmed, when none was found. */
21
+ text: string;
22
+ /** How many blocks were taken out. Counts only, never content. */
23
+ removedBlocks: number;
24
+ /** True when harness text was found and nothing a person wrote is left. */
25
+ harnessOnly: boolean;
26
+ }
27
+ export declare function stripHarnessText(input: string): HarnessTextReading;
28
+ /**
29
+ * True when a stored memory body is harness text saved before this filter existed: the Stop hook's `User: ` label
30
+ * (or the bare text) opening with a reminder or notification, AND nothing a person wrote left in that user turn once
31
+ * the closed harness blocks are taken out. Claude Code also puts reminders in front of real prompts, so a body that
32
+ * merely STARTS with one ("User: <system-reminder>…</system-reminder> fix the login bug") is a real exchange and is
33
+ * never matched. Used by the cleanup script and its test; a memory that later quotes a tag is not touched either.
34
+ */
35
+ export declare function looksLikeSavedHarnessText(body: string): boolean;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * TEXT THE AGENT HOST INJECTED IS NOT A MEMORY (BLI-26056).
3
+ *
4
+ * Claude Code puts its own text into the conversation as if it were a user turn: `<system-reminder>` blocks (a
5
+ * file's CLAUDE.md, a skill list, the "[SYSTEM NOTIFICATION - NOT USER INPUT] This is an automated background-task
6
+ * event" notice) and bare `<task-notification>` blocks when a background command or agent reports back. Nobody wrote
7
+ * those words. The Stop hook read them as the person's turn and saved them; when extraction then failed, the door's
8
+ * verbatim fallback (BLI-3713) stored the whole block as a memory (two such rows were found on 2026-11-20).
9
+ *
10
+ * This is the one place that knows those wrappers, for every capture path: the Stop and PreCompact hooks strip them
11
+ * before they send anything, and `POST /api/memory/save` strips them again for clients that predate the hooks' fix.
12
+ *
13
+ * A closed textual test, like the prompt hook's `automated-event.ts`: only CLOSED blocks are taken out, so a sentence
14
+ * that merely names the tag ("Claude Code wraps notices in <system-reminder>") is left alone. An unclosed block stays
15
+ * as text: when in doubt, keep what the person may have written.
16
+ */
17
+ /** Every closed reminder block. The host writes them; a person never types one. */
18
+ const REMINDER_BLOCK = /<system-reminder>[\s\S]*?<\/system-reminder>/gu;
19
+ /** Every closed background-task notification. */
20
+ const NOTIFICATION_BLOCK = /<task-notification>[\s\S]*?<\/task-notification>/gu;
21
+ /** The first line of the host's automated-event notice, which can also arrive outside a reminder block. */
22
+ export const HARNESS_AUTOMATED_NOTICE_MARKER = "[SYSTEM NOTIFICATION - NOT USER INPUT]";
23
+ export function stripHarnessText(input) {
24
+ if (!input.includes("<system-reminder>") && !input.includes("<task-notification>") && !input.includes(HARNESS_AUTOMATED_NOTICE_MARKER)) {
25
+ return { text: input.trim(), removedBlocks: 0, harnessOnly: false };
26
+ }
27
+ let removedBlocks = 0;
28
+ const count = () => {
29
+ removedBlocks += 1;
30
+ return "";
31
+ };
32
+ // Reminders first: a notification wrapped inside one leaves with it, and is counted once.
33
+ let text = input.replace(REMINDER_BLOCK, count).replace(NOTIFICATION_BLOCK, count);
34
+ // The notice can arrive bare (its reminder unclosed or already stripped by the host): it and everything after it up to
35
+ // a blank line is the host's paragraph. Only taken out when it OPENS a line, so a quotation mid-sentence stays.
36
+ text = text.replace(/(^|\n)[ \t]*\[SYSTEM NOTIFICATION - NOT USER INPUT\][\s\S]*?(?=\n[ \t]*\n|$)/gu, (_match, lead) => {
37
+ removedBlocks += 1;
38
+ return lead;
39
+ });
40
+ const trimmed = text.replace(/\n{3,}/gu, "\n\n").trim();
41
+ return { text: trimmed, removedBlocks, harnessOnly: removedBlocks > 0 && trimmed.length === 0 };
42
+ }
43
+ /**
44
+ * True when a stored memory body is harness text saved before this filter existed: the Stop hook's `User: ` label
45
+ * (or the bare text) opening with a reminder or notification, AND nothing a person wrote left in that user turn once
46
+ * the closed harness blocks are taken out. Claude Code also puts reminders in front of real prompts, so a body that
47
+ * merely STARTS with one ("User: <system-reminder>…</system-reminder> fix the login bug") is a real exchange and is
48
+ * never matched. Used by the cleanup script and its test; a memory that later quotes a tag is not touched either.
49
+ */
50
+ export function looksLikeSavedHarnessText(body) {
51
+ const trimmed = body.trimStart();
52
+ const label = /^User(?: \(truncated\))?:\s*…?\s*/u.exec(trimmed);
53
+ const head = label ? trimmed.slice(label[0].length) : trimmed;
54
+ if (!head.startsWith("<system-reminder>") && !head.startsWith("<task-notification>") && !head.startsWith(HARNESS_AUTOMATED_NOTICE_MARKER))
55
+ return false;
56
+ // The Stop hook writes `User: <turn>\n\nAssistant: <reply>`; only the user turn decides.
57
+ const reply = head.indexOf("\n\nAssistant: ");
58
+ const userTurn = reply >= 0 ? head.slice(0, reply) : head;
59
+ return stripHarnessText(userTurn).harnessOnly;
60
+ }
package/dist/index.d.ts CHANGED
@@ -26,6 +26,7 @@ export * from "./ingest-dto.js";
26
26
  export * from "./local-config.js";
27
27
  export * from "./memory-daemon-endpoint.js";
28
28
  export * from "./memory-hook-stats.js";
29
+ export * from "./harness-text.js";
29
30
  export * from "./memory-hook-hours.js";
30
31
  export * from "./sync-revive-contract.js";
31
32
  export * from "./memory-capture-contract.js";
package/dist/index.js CHANGED
@@ -26,6 +26,7 @@ export * from "./ingest-dto.js";
26
26
  export * from "./local-config.js";
27
27
  export * from "./memory-daemon-endpoint.js";
28
28
  export * from "./memory-hook-stats.js";
29
+ export * from "./harness-text.js";
29
30
  export * from "./memory-hook-hours.js";
30
31
  export * from "./sync-revive-contract.js";
31
32
  export * from "./memory-capture-contract.js";
@@ -68,6 +68,8 @@ export declare const SECRET_ASSIGNMENT_SOURCE = "[\"']?\\s*[:=]\\s*[\"']?";
68
68
  */
69
69
  export declare const PASSWORD_NAME_MATCH_SOURCE = "(?<![A-Za-z0-9])(?:password|passwd|passcode|pwd(?![A-Za-z0-9_-]{0,40}\\s*[\"']?\\s*[:=]\\s*[\"']?\\s*[/~]))(?:[_-][A-Za-z0-9_-]{0,40})?";
70
70
  export declare const PASSWORD_VALUE_SOURCE = "[^\\s\"'`,;<>(){}\\[\\]]{6}[^\\s\"'`,;<>(){}\\[\\]]*";
71
+ export declare const KEY_BODY_RUN_SOURCE: string;
72
+ export declare const KEY_BODY_BEFORE_FOOTER_SOURCE: string;
71
73
  export declare const SECRET_LIKE_CONTENT_PATTERNS: RegExp[];
72
74
  /**
73
75
  * Does this text carry something the fleet must not upload as it stands? The
@@ -101,6 +101,44 @@ export const PASSWORD_VALUE_SOURCE = "[^\\s\"'`,;<>(){}\\[\\]]{6}[^\\s\"'`,;<>()
101
101
  */
102
102
  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");
103
103
  const PASSWORD_WITH_VALUE_PATTERN = new RegExp(`${PASSWORD_NAME_MATCH_SOURCE}${SECRET_ASSIGNMENT_SOURCE}${PASSWORD_VALUE_SOURCE}`, "i");
104
+ /**
105
+ * A private key body with its BEGIN line cut away (BLI-26798). On 2026-11-28 `railway variables --kv | cut -d= -f1`
106
+ * printed every body line of a multi-line key (no line has an `=`) and dropped the header, the only thing the rules
107
+ * below key on, so the collector masked 0 values and the body reached the raw evidence bucket.
108
+ *
109
+ * Two tells, both bounded so a megabyte base64 blob cannot overflow V8's backtrack stack, and both start only at the
110
+ * FIRST body line (a line after a full-width one is a continuation, which also keeps a long blob linear):
111
+ *
112
+ * - a RUN: 4+ lines of exactly 64 (PEM, PGP, PuTTY), 70 (OpenSSH) or 76 (MIME) base64 characters, each with both
113
+ * letter cases (a sha256 list is one-case hex and stays untouched), plus an optional short last line, at most 256
114
+ * lines per match (an 8192-bit RSA key is about 100);
115
+ * - a BODY BEFORE A FOOTER: one 40+ character base64 line and any lines after it, ending right before an END
116
+ * PRIVATE KEY footer, which catches a short key (EC, Ed25519) whose body is under four lines; at most 9 lines, so a
117
+ * long list of base64 hashes is not rescanned from every line.
118
+ *
119
+ * A line break is a real newline (`\n`, `\r\n`) or the escaped one a JSONL transcript carries (`\\n`, `\\r\\n`, up
120
+ * to eight backslashes deep), so macOS and Windows transcripts read the same. A line may carry indentation, never a diff
121
+ * `+`: `+` is base64 too, and reading it both ways backtracks exponentially (a diff of a key carries its header).
122
+ * Indentation and escape depth are bounded (16 blanks, 8 backslashes) for the same reason: an unbounded `[ \t]*`
123
+ * inside a lookbehind made a 200,000-space line take 100 seconds. A match starts AT the first body character, behind
124
+ * one-character checks and then `(?=[A-Za-z0-9+/]{40})` (both tells need a 40+ character first line), so a megabyte of blanks (a 277 MiB
125
+ * transcript of them is a collector test) or ordinary prose fails in a few steps, never reaching the lookbehinds. A run right under a certificate or public key
126
+ * armor line (`-----BEGIN CERTIFICATE-----`) is public and left alone; a PGP public key block, whose armor headers sit between, is masked (over-masking, never a leak).
127
+ */
128
+ const KEY_BODY_ALPHABET = "A-Za-z0-9+/";
129
+ const KEY_BODY_LINE_BREAK = "(?:\\r?\\n|(?:\\\\{1,8}r)?\\\\{1,8}n)[ \\t]{0,16}";
130
+ const KEY_BODY_LINE_START = [
131
+ `(?=[${KEY_BODY_ALPHABET}])`,
132
+ `(?:(?<![${KEY_BODY_ALPHABET}=\\\\])|(?<=\\\\n))`,
133
+ `(?=[${KEY_BODY_ALPHABET}]{40})`,
134
+ `(?<![${KEY_BODY_ALPHABET}]{64}${KEY_BODY_LINE_BREAK})`,
135
+ `(?<!-----BEGIN (?![A-Z ]*PRIVATE KEY)[A-Z0-9 ]+-----${KEY_BODY_LINE_BREAK})`,
136
+ ].join("");
137
+ const KEY_BODY_FULL_LINE = `(?=[${KEY_BODY_ALPHABET}]{0,75}[A-Z])(?=[${KEY_BODY_ALPHABET}]{0,75}[a-z])(?:[${KEY_BODY_ALPHABET}]{76}|[${KEY_BODY_ALPHABET}]{70}|[${KEY_BODY_ALPHABET}]{64})`;
138
+ const KEY_BODY_ANY_LINE = `[${KEY_BODY_ALPHABET}]{1,76}={0,2}`;
139
+ const KEY_FOOTER_SOURCE = "-----END [A-Z ]*PRIVATE KEY-----";
140
+ export const KEY_BODY_RUN_SOURCE = `${KEY_BODY_LINE_START}${KEY_BODY_FULL_LINE}(?:${KEY_BODY_LINE_BREAK}${KEY_BODY_FULL_LINE}){3}(?:${KEY_BODY_LINE_BREAK}${KEY_BODY_FULL_LINE}){0,252}(?:${KEY_BODY_LINE_BREAK}${KEY_BODY_ANY_LINE}(?![${KEY_BODY_ALPHABET}=]))?`;
141
+ export const KEY_BODY_BEFORE_FOOTER_SOURCE = `${KEY_BODY_LINE_START}[${KEY_BODY_ALPHABET}]{40,76}={0,2}(?:${KEY_BODY_LINE_BREAK}${KEY_BODY_ANY_LINE}){0,8}(?=${KEY_BODY_LINE_BREAK}${KEY_FOOTER_SOURCE})`;
104
142
  /**
105
143
  * Self-identifying secret values — blocked regardless of context because the
106
144
  * token shape itself is the credential, with no benign reading.
@@ -130,6 +168,8 @@ const SECRET_VALUE_PATTERNS = [
130
168
  /\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/,
131
169
  /\b(?:pk|sk)-lf-[A-Za-z0-9_-]{16}[A-Za-z0-9_-]*\b/i,
132
170
  /\bm0[-_][A-Za-z0-9_-]{16}[A-Za-z0-9_-]*\b/i,
171
+ new RegExp(KEY_BODY_RUN_SOURCE),
172
+ new RegExp(KEY_BODY_BEFORE_FOOTER_SOURCE),
133
173
  ];
134
174
  // BLI-22794: changing these rules changes `evidenceRulesFingerprint()` (evidence-rules-fingerprint.ts), which
135
175
  // rebuilds every collector's prepare memo once. That is intended; update the pin in its test.
@@ -1,4 +1,4 @@
1
- import { PASSWORD_NAME_MATCH_SOURCE, PASSWORD_VALUE_SOURCE, SECRET_ASSIGNMENT_SOURCE, SECRET_LIKE_CONTENT_PATTERNS, SECRET_NAME_MATCH_SOURCE, } from "./secret-guards-content-rules.js";
1
+ import { KEY_BODY_BEFORE_FOOTER_SOURCE, KEY_BODY_RUN_SOURCE, PASSWORD_NAME_MATCH_SOURCE, PASSWORD_VALUE_SOURCE, SECRET_ASSIGNMENT_SOURCE, SECRET_LIKE_CONTENT_PATTERNS, SECRET_NAME_MATCH_SOURCE, } from "./secret-guards-content-rules.js";
2
2
  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
@@ -110,6 +110,16 @@ export const SECRET_REDACTION_RULES = [
110
110
  pattern: PASSWORD_ASSIGNMENT_REDACTION_PATTERN,
111
111
  secretGroup: 2,
112
112
  },
113
+ // BLI-26798: a private key body whose BEGIN line was cut away. Appended so no
114
+ // existing rule's priority moves; both tells carry one label.
115
+ {
116
+ ruleId: "private_key_body",
117
+ pattern: new RegExp(KEY_BODY_RUN_SOURCE, "g"),
118
+ },
119
+ {
120
+ ruleId: "private_key_body",
121
+ pattern: new RegExp(KEY_BODY_BEFORE_FOOTER_SOURCE, "g"),
122
+ },
113
123
  ];
114
124
  /**
115
125
  * Mask every secret-shaped span and keep the rest of the text, because a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bli-cockpit/telemetry-core",
3
- "version": "0.1.108",
3
+ "version": "0.1.110",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",