Skip to content

fix(safety): count an identifier once when two rules match it - #267

Merged
M3gA-Mind merged 1 commit into
tinyhumansai:mainfrom
shaurya703:fix/dedupe-overlapping-identifier-counts
Nov 11, 2026
Merged

M3gA-Mind merged 1 commit into
tinyhumansai:mainfrom
shaurya703:fix/dedupe-overlapping-identifier-counts

Conversation

@shaurya703

Copy link
Copy Markdown
Contributor

Closes #261

Summary

scan incremented a category's count once per rule match. The US-format and international phone rules both match +1 415-555-0100, so one identifier came back as CategoryHit { PhoneNumber, count: 2 } at level Low.

Behaviour change

  • scan collects each validated match as a byte span per category and counts distinct spans: overlapping spans are one value seen by more than one rule; spans that touch or stand apart are separate values. +1 415-555-0100 is now count 1. Three different numbers are 3; the same number written twice is still 2.
  • The category set, weights, co-occurrence bonus and the strong-identifier rule are untouched, so the risk model is unchanged; only the per-category counts move.
  • The US rule's leading \b(?:\+?1[-.\s]?)? could never start on the + (a word boundary cannot sit between a space and a +), so its match began at the 1 and redact_identifiers("call +1 415-555-0100") left call +[redacted]. The rule now begins (?:\+|\b)(?:1[-.\s]?)?, which takes the + when it is there and otherwise behaves as before. With both rules covering the same span, redaction removes the whole value and the counts above fold cleanly.

Verification

cargo fmt --all -- --check
cargo clippy -p tinymemory-api --all-targets --all-features -- -D warnings
cargo test -p tinymemory-api --features local-safety -- safety     # 36 passed

New tests: distinct_spans unit cases (empty, single, nested, partial overlap, touching, unsorted separate); a_value_matched_by_two_rules_counts_once (count 1, level Low); distinct_values_in_one_category_each_count (3 distinct, and a repeated value as 2); a_doubly_matched_value_is_redacted_whole for both +1 415-555-0100 and +1 415.555.1234. On main the count test reports 2 and both redactions leave a +.

Independent of #265 (international digit bounds) at the file level: the two touch different rules, and both branches are cut from current main.

`scan` added one to a category's count for every rule match, so a value
two rules both recognise was reported twice: `+1 415-555-0100` is a
US-format phone number and an international one, and came back as
PhoneNumber count 2 for a single identifier.

Matches are now collected as byte spans per category and overlapping
spans are folded into one before counting. Distinct values, including
the same value written twice, still count separately.

The US rule also takes its own `+1`: `\b` cannot sit between a space and
a `+`, so the match began at the `1` and redaction left a stray `+`
behind (`call +[redacted]`). The leading alternation lets the match
start on the `+`, and the two rules now cover the same span.

Closes tinyhumansai#261
@coderabbitai

coderabbitai Bot commented Oct 13, 2026

Copy link
Copy Markdown

Warning

Review limit reached

  • Run on-demand review

This review includes 4 billable files and costs up to $1.00.

Or wait 37 minutes for your next included review.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 134af9b3-ea65-474c-85a3-a76e2a5b0cad
📥 Commits

Reviewing files that changed from the base of the PR and between 29c268c and 85e1c89.

📒 Files selected for processing (4)
  • crates/tinymemory-api/src/safety/detector.rs
  • crates/tinymemory-api/src/safety/detector_tests.rs
  • crates/tinymemory-api/src/safety/mod_tests.rs
  • crates/tinymemory-api/src/safety/rules.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@tinysweeper

tinysweeper Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Tiny Sweeper review

This pull request fixes double counting of a single identifier when two rules in the same category match it, and widens the US-style phone pattern so a leading `+1` is part of the match. `pub fn scan(content: &str) -> PiiScanResult` now accumulates `(start, end)` spans per category instead of incrementing a counter, then folds overlapping spans with the new `distinct_spans` helper before deriving counts, score and level. No findings were raised by any review lane: the critique, security, tests and commits lanes all reported empty finding lists. The description lane produced no usable reviewer response (`tinysweeper/description` left unanswered). All lanes flag that the code index is behind this pull request (indexed at `51eef64acaad`), so retrieved context may be out of date.

State: Incomplete
Priority: none
Reviewed head: 85e1c891912f
Updated: 2026-10-13T12:17:18Z

Review snapshot

Change surface Files Review signal Count
Production 2 Active findings 0
Tests 2 Noted findings 0
Documentation 0 Resolved findings 0
Configuration 0 Pending checks/questions 1

Completeness: Incomplete
Test assessment: Test coverage is assessed from changed tests and lane evidence; execution is not claimed without trusted check data.

Features

  • Added — Overlapping-span folding helper: Overlapping byte ranges are collapsed to a single value, while ranges that merely touch or stand apart remain separate values, and the same value appearing twice counts as two. This is the mechanism that makes a hit count a count of identifiers rather than of rule matches. (crates/tinymemory-api/src/safety/detector.rs)
  • Modified — Span-based counting in the scan entry point: A per-category hit count now reflects distinct matched values instead of distinct rule matches, so a value matched by two rules in one category (for example a phone number matched by both the US-format and international patterns) is reported once; deterministic category ordering via the map is retained and the empty-result early return still holds. (crates/tinymemory-api/src/safety/detector.rs#pub fn scan(content: &str) -> PiiScanResult {, crates/tinymemory-api/src/safety/detector.rs)
  • Modified — US-style phone pattern widened to absorb a leading `+1`: A leading `+1` becomes part of the matched value rather than falling outside it, so redaction replaces the whole number and does not leave a stray `+` behind; the alternation starts the match on the `+` because a word boundary cannot sit between a space and a `+`. (crates/tinymemory-api/src/safety/rules.rs#fn build_rules() -> Vec<Rule> {, crates/tinymemory-api/src/safety/rules.rs)

Tests

  • integration — Scans `call +1 415-555-0100` and asserts the phone-category hit count is 1 with `RiskLevel::Low`, pinning that one value matched by two phone rules counts once.: Directly targets the double-count regression the pull request addresses and additionally pins the resulting risk level. (crates/tinymemory-api/src/safety/mod_tests.rs)
  • integration — Scans a string containing three distinct phone-shaped values and asserts a phone hit count of 3, then scans `415-555-0100 then again 415-555-0100` and asserts a phone hit count of 2.: Distinguishes the two halves of the new semantics: distinct values in one category each count, while a repeated identical value counts twice. (crates/tinymemory-api/src/safety/mod_tests.rs)
  • integration — Redacts `call +1 415-555-0100 now` and `call +1 415.555.1234 now`, asserting both yield `call [redacted] now`.: Pins the redaction consequence of the widened pattern, showing the leading `+1` is consumed rather than left behind. (crates/tinymemory-api/src/safety/mod_tests.rs)
  • unit — Asserts risk levels increase monotonically across score thresholds up to 1000.: Present in the touched test module as adjacent context for the scan path; the evidence does not state this test was added or modified by this pull request. (crates/tinymemory-api/src/safety/detector_tests.rs#fn level_thresholds_are_monotonic() {)
  • unit — Asserts a phone-shaped value is not double counted as a national identifier.: Sits in the modified test module and is adjacent to the new counting tests; the evidence does not state it was changed here. (crates/tinymemory-api/src/safety/mod_tests.rs#fn phone_shaped_ssn_not_double_counted_as_ssn() {)

Findings

No active actionable findings.

Could not review: tinysweeper/description

Before merge

  • Complete the description review for tinysweeper/description.
Agent review details

critique

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: The critique lane reported an empty finding list for the 4 reviewed files and notes the code index is behind this pull request (indexed at `51eef64acaad`), so retrieved context may be out of date. Its own summary text states 1 finding while supplying none, so no finding could be attributed from that lane.
  • Lane summary: Reviewed 4 files; 1 finding. _The code index is behind this pull request (indexed at `51eef64acaad`), so retrieved context may be out of date._

security

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: The security lane reviewed all 4 changed files and reported 0 findings.
  • Lane summary: Reviewed 4 files; 0 findings. _The code index is behind this pull request (indexed at `51eef64acaad`), so retrieved context may be out of date._

tests

  • Conclusion: Success
  • Scope reviewed: all assigned evidence
  • Positive: The test lane reports no regression and no uncovered new branch surfaced, and judges the diff safe to merge; both new behaviours it identifies are stated as pinned by new tests covering the fold, the separate-value case, the same-value-twice case and the redaction consequence of the widened regex.
  • Lane summary: The change makes the per-category hit count count distinct values rather than distinct rule matches, and widens the US phone pattern to take a leading `+1` into the match. Both behaviours are pinned by new tests: `distinct_spans` unit cases plus `a_value_matched_by_two_rules_counts_once`, `distinct_values_in_one_category_each_count` and `a_doubly_matched_value_is_redacted_whole`, which cover the fold, the separate-value case, the same-value-twice case, and the redaction consequence of the widened regex. No regression or uncovered new branch surfaced; the diff is safe to merge. _The code index is behind this pull request (indexed at `51eef64acaad`), so retrieved context may be out of date._

commits

  • Conclusion: Neutral
  • Scope reviewed: all assigned evidence
  • Positive: The commits lane found nothing sensitive in what this pull request commits.
  • Lane summary: Nothing sensitive found in what this pull request commits.

description

  • Conclusion: Neutral
  • Scope reviewed: incomplete; unanswered: tinysweeper/description
  • Lane summary: No reviewer produced a usable response.
Evidence and run details
  • Models: ladder/vectors-oai3, deepseek-v4.1-flash, glm-5.3-flash
  • Spend: $0.005067
  • Tokens: 411704 input · 54992 output · 41088 cached · 302 embedding
Head State Pass summary
85e1c891912f incomplete 0 active finding(s), 0 resolved finding(s) (at 2026-10-13T12:17:18Z)

tinysweeper 0.1.2

@tinysweeper tinysweeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tinysweeper found nothing blocking, but could not review everything, so this is not an approval: tinysweeper/description.

             $0.0051 · 411,704 in / 54,992 out · 41,088 cached (10%) · ladder/vectors-oai3, deepseek-v4.1-flash, glm-5.3-flash · 302 embedded
critique:    $0.0028 · 226,061 in / 29,455 out · 16,704 cached (7%)  · deepseek-v4.1-flash, glm-5.3-flash
security:    $0.0007 · 116,005 in / 8,489 out  · 22,464 cached (19%) · deepseek-v4.1-flash, glm-5.3-flash
tests:       $0.0010 · 59,340 in  / 10,096 out · 1,920 cached (3%)   · deepseek-v4.1-flash
description: $0.0003 · 6,856 in   / 3,916 out  · 0 cached (0%)       · deepseek-v4.1-flash

@tinysweeper tinysweeper Bot added the priority: p3 Whenever. Cosmetic, a nicety, or a cleanup with no user visible effect. label Oct 13, 2026
@M3gA-Mind
M3gA-Mind merged commit 9b9a36c into tinyhumansai:main Nov 11, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority: p3 Whenever. Cosmetic, a nicety, or a cleanup with no user visible effect.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deduplicate overlapping phone-rule identifier counts

2 participants