Add countSensitiveMessages for pre-scan sensitive content detection

Export a new countSensitiveMessages() function that returns how many
messages in an array contain at least one sensitive pattern match.
Checks both content and toolInput fields, counting each message at
most once regardless of how many matches it contains.

Tests verify zero counts for clean messages, correct counting with
mixed sensitive/clean messages, and the single-count-per-message
invariant when multiple secrets appear in one message.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-30 13:35:15 -05:00
parent 4027dd65be
commit 6681f07fc0
2 changed files with 97 additions and 0 deletions

View File

@@ -449,3 +449,25 @@ export function redactMessage(msg: ParsedMessage): ParsedMessage {
// toolName is typically safe (e.g. "Bash", "Read") — pass through unchanged
};
}
/**
* Counts how many messages contain at least one sensitive match.
* Checks both content and toolInput fields.
*/
export function countSensitiveMessages(messages: ParsedMessage[]): number {
let count = 0;
for (const msg of messages) {
const contentResult = redactSensitiveContent(msg.content);
if (contentResult.redactionCount > 0) {
count++;
continue;
}
if (msg.toolInput) {
const inputResult = redactSensitiveContent(msg.toolInput);
if (inputResult.redactionCount > 0) {
count++;
}
}
}
return count;
}