# pii


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

Personal information, called PII below, is text that identifies a person
or opens one of their accounts. Examples are an email address, a phone
number, a card or bank account number, a national identity number such
as a UK National Insurance number or an Indian Aadhaar number, a street
address, a date of birth, an API key and a password. The detector is a
separate package, [rahasya](https://vedicreader.github.io/rahasya/). It
uses patterns and checksums and no model. A card number must pass the
Luhn check, and an NHS number must pass its mod-11 check. The detector
gives the same answer on every run and downloads nothing. On a held-out
test set of 240 generated documents it missed nothing and raised no
false alarm (`evals/RESULTS.md`, section 3).

The detector does not find names on their own. No pattern describes a
name. Name detection is a second pass, and it finds only a name that
follows a title such as Mr, Dr or Prof. “Dr Charles Babbage” is found.
“Ada Lovelace” with no title is not. The pass runs when a document is
added, except on code, where identifiers are not names. It runs by
default in `ask` (`pii_ner=True`).
[`Vault.pii`](https://vedicreader.github.io/vishalakshi/pii.html#vault.pii)
and [`gated`](https://vedicreader.github.io/vishalakshi/pii.html#gated)
run it only with `ner=True`, and the retrieval methods run it only with
`pii_ner=True`. When a document holds personal information that the
detector cannot see, a person marks the document, as described below.

This page covers the parts that need a vault: the marks a person sets,
the privacy level of each document, the table of findings, and
[`gated`](https://vedicreader.github.io/vishalakshi/pii.html#gated),
which applies a policy to what search and reading return.
[ask](02_ask.ipynb) describes what `ask` and the other model calls send
to a model.

## Checking a document, and gating retrieval

`Vault.pii(ref)` reports on one whole document: whether it holds
personal information, which kinds, and whether a person’s mark overrides
the detector. `detected` is the detector’s verdict. `override` is the
mark. `mark_pii` marks a document as personal when the detector finds
nothing, for example a letter that names people without titles.
`mark_not_pii` clears a false alarm, such as your own invoice. Only a
person may clear a mark (see the next section).
[`pii_ctx`](https://vedicreader.github.io/vishalakshi/pii.html#pii_ctx)
runs the detector over every section of an assembled context, which is
the result of `context`. `ask` decides on that report.

Retrieval methods apply the same policy to their results. `search`,
`sections`, `context`, `read`, `document`, `toc`, `doc_context` and
`federate` all take `pii=`. It sets what happens to a row that holds
personal information:

<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr>
<th><code>pii=</code></th>
<th>the row’s text becomes</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>off</code>, the default in Python and the CLI</td>
<td>the stored text; nothing is checked</td>
</tr>
<tr>
<td><code>redact</code></td>
<td>the stored text with each recognised identifier replaced by its
kind, as in <code>[EMAIL]</code></td>
</tr>
<tr>
<td><code>refuse</code></td>
<td>a placeholder such as
<code>[withheld: personal information (card, email)]</code></td>
</tr>
</tbody>
</table>

Under `redact` and `refuse` the row stays in the results. Its title and
breadcrumb are masked, not removed. A `pii` key names the kinds found. A
caller can use it to say why the text was held back. Three cases depend
on the levels described in the next section.

- A Local only document is withheld under `redact` as well as `refuse`.
  Its title, breadcrumb and source are withheld too.
- A document that is private only because of a mark or the shelf’s
  floor, and that holds nothing the detector recognises, passes through
  `redact` unchanged. There is nothing to mask. Use `refuse` for such a
  document.
- A document that a person cleared with `mark_not_pii` comes back
  untouched, unless the shelf’s floor is above Open.

The [MCP server](05_mcp.ipynb) hands its results to a model that the
vault does not run. Every MCP tool defaults to `redact`, and the server
gates every result, including the result of `ask`. A client may ask for
`refuse`. A client may not ask for `off`.

## Learned detectors were measured and not shipped

Two learned PII models, an ONNX build and a TFLite build, were tried
against the patterns. Both were worse on precision and on recall, and
about 200 times slower, so the shipped detector is the patterns alone.
The model code stays in `evals/backends.py`, `evals/pii_model.py` reruns
the comparison, and `evals/RESULTS.md` has the numbers.

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L60"
target="_blank" style="float:right; font-size:smaller">source</a>

### pii_ctx

``` python
def pii_ctx(
    ctx, ner:bool=False
)->fastcore.basics.AttrDict:
```

*Run the detector over every section in an assembled context: its text,
heading, title and source.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L54"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.mark_pii

``` python
def mark_pii(
    ref, force:bool=True, reason:str='', by:str='person'
)->dict:
```

*Mark a document as personal when the detector finds nothing, or remove
that mark with `force=False`, which only a person may do.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L47"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.mark_not_pii

``` python
def mark_not_pii(
    ref, clear:bool=True, reason:str='', by:str='person'
)->dict:
```

*Mark a document as not personal, or with `clear=False` hand the
decision back to the detector; only a person may do either.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L29"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.pii

``` python
def pii(
    ref, # a doc_id, source, title or path: whatever `document` takes
    max_chars:int=200000, ner:bool=False, # also look for names that follow a title such as Dr; never on code
)->fastcore.basics.AttrDict:
```

*Report whether one document holds personal information, which kinds,
and any mark that overrides the detector.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L148"
target="_blank" style="float:right; font-size:smaller">source</a>

### gated

``` python
def gated(
    o, # whatever a retrieval primitive returned
    pii:str='off', # off | redact | refuse
    vault:NoneType=None, # the shelf whose marks apply; None -> no marks
    ner:bool=False, # also mask names that follow a title such as Dr
    store:str=None, # the shelf the rows came from; None -> the vault's own
):
```

*Apply a retrieval policy, `off`, `redact` or `refuse`, to search rows,
an assembled context, one section or a string.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L119"
target="_blank" style="float:right; font-size:smaller">source</a>

### held

``` python
def held(
    kinds
)->str:
```

*The placeholder that replaces withheld text: it names the kinds found,
or says the document is local only.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L176"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.toc

``` python
def toc(
    doc:NoneType=None, # narrow to one document, as `Index.toc` takes it
    *, pii:str='off', pii_ner:bool=False, stale:bool=False, **kw
)->list: # forwarded to `Index.toc`
```

*The heading tree of the shelf or of one document, with `pii=` applied
to the node titles as well as the text.*

## Privacy levels and findings

Every document has one of three privacy levels. The level sets what this
package’s own model calls may send to a model that runs on another
machine. This page calls such a model a hosted model.

<table>
<colgroup>
<col style="width: 33%" />
<col style="width: 33%" />
<col style="width: 33%" />
</colgroup>
<thead>
<tr>
<th>level</th>
<th>how a document gets it</th>
<th>what a hosted model may be sent</th>
</tr>
</thead>
<tbody>
<tr>
<td>Open (<code>open</code>)</td>
<td>nothing personal found, or a person cleared it</td>
<td>the text as it is</td>
</tr>
<tr>
<td>Personal (<code>personal</code>)</td>
<td>the detector found an identifier, or someone marked it</td>
<td>it depends on the policy: masked text, nothing, or with
<code>allow=True</code> the whole text (see <a
href="02_ask.ipynb">ask</a>)</td>
</tr>
<tr>
<td>Local only (<code>local</code>)</td>
<td>someone set it with <code>set_level</code>, or its type is
sensitive</td>
<td>nothing; only a model on this machine reads it</td>
</tr>
</tbody>
</table>

The sensitive types come from
[varga](https://vedicreader.github.io/varga/). They are passport, ID
card, bank statement, payslip, tax return and clinical record. These
rules cover what vishalakshi sends to models. They do not stop retrieval
in Python or the CLI. `read` or `search` with the default `pii='off'`
returns the text of a Local only document to the caller. The MCP server
defaults to `redact`, which withholds that text.

`level(ref)` works out a document’s level in two steps.

1.  A mark set with `mark_pii`, `mark_not_pii` or `set_level` decides
    the level. Without a mark, the level is the higher of the level of
    the document’s type and the level from its findings. Any identifier
    found in the text or the title makes the document Personal.
2.  The result is raised to the shelf’s floor.

The floor is the lowest level any document on the shelf can have.
`set_floor` sets it. A shelf for medical records can have a floor of
Local only. A new document on that shelf is protected before the
detector has found anything in it.

`level(ref, page=n)` gives the level of one page from the findings on
that page. A page with no text takes the level of the whole document.

Anyone may raise a level. Only a person may lower one. These actions
take `by=` and raise `PermissionError` unless `by='person'`: `set_level`
to a lower level, removing a mark, `mark_not_pii`, `reject_pii`,
`reset_pii` and lowering a floor. The default is `'person'`. Code that a
person runs passes nothing. An agent passes its own name. The [MCP
server](05_mcp.ipynb) does not offer `by`. A model client cannot claim
to be a person. It cannot turn the retrieval gate off with `pii='off'`
either.

When a document is added, `scan_pii` scans each page. It files every
identifier in the `pii_findings` table as a finding. A finding holds a
kind, a page, the start and end offsets in the page text, and a
fingerprint. The fingerprint is a 16-character hash of the kind and the
value, with spacing, dashes and case removed. `ADA@example.com` and
`ada@example.com` give the same fingerprint. `4111-1111-1111-1111` and
`4111 1111 1111 1111` give the same fingerprint.

The table does not hold the value. The value is still in the document
text, which the vault keeps as it was read. The hash has no secret key.
Someone who hashes every possible short value, such as a date of birth,
can recover it. Treat the findings table as private as the vault file.
`findings` lists the findings. Each has a masked preview such as
`a••@e••••••.com`, taken from the current page text.

A person can give a verdict on a finding. `accept_pii` confirms that a
finding is personal. An agent may also call it, because it can only
raise a level. `reject_pii` says that a finding is not personal. The
level falls if nothing else holds it up. `reset_pii` takes a verdict
back.

The vault stores a verdict against the fingerprint. The verdict covers
every occurrence of the same value in the document. It survives a new
read of the page. The new findings get new ids and keep the old verdict.
A finding id from before the new read comes back as `stale`.

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L361"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.reset_pii

``` python
def reset_pii(
    ref, ids, by:str='person'
)->dict:
```

*Remove the verdict on findings so that detection judges every
occurrence of the value again; only a person may.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L355"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.reject_pii

``` python
def reject_pii(
    ref, ids, by:str='person'
)->dict:
```

*Reject findings as not personal, for every occurrence of the same value
in the document; only a person may, and the verdict survives a re-read.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L350"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.accept_pii

``` python
def accept_pii(
    ref, ids, by:str='person'
)->dict:
```

*Confirm findings as personal; the verdict covers every occurrence of
the same value in the document.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L319"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.findings

``` python
def findings(
    ref:NoneType=None, state:str=None, page:int=None
)->fastcore.foundation.L:
```

*List filed findings, each with a masked `preview` such as
`a••@e••••••.com` built from the current page text.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L308"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.set_level

``` python
def set_level(
    ref, level:str=None, reason:str='', by:str='person'
)->dict:
```

*Set a document’s privacy level by hand, or pass `None` to remove the
mark and let detection decide; only a person may lower a level or remove
a mark.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L287"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.level

``` python
def level(
    ref, page:int=None
)->fastcore.basics.AttrDict:
```

*A document’s or one page’s privacy level: its mark if it has one, else
the higher of its type’s level and its findings’, never below the
shelf’s floor.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L257"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.scan_pii

``` python
def scan_pii(
    ref, pages:NoneType=None
)->dict:
```

*Scan a document page by page and file each identifier found as a
finding (kind, page, offsets and fingerprint) without its value.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L227"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.set_floor

``` python
def set_floor(
    level:str='open', by:str='person'
)->dict:
```

*Set this shelf’s floor; anyone may raise it, and only a person may
lower it.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L221"
target="_blank" style="float:right; font-size:smaller">source</a>

### Vault.floor

``` python
def floor()->str:
```

*This shelf’s floor, the lowest privacy level any of its documents can
have: `open`, `personal` or `local`.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L204"
target="_blank" style="float:right; font-size:smaller">source</a>

### type_policy

``` python
def type_policy(
    doctype:str=None, text:str=None, kind:str=None
)->fastcore.basics.AttrDict:
```

*For a document type, or for text whose type is guessed, give the
reading preset, the starting privacy level and which vision models may
read it.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/vishalakshi/blob/main/vishalakshi/pii.py#L193"
target="_blank" style="float:right; font-size:smaller">source</a>

### top

``` python
def top(
    *ls
)->str:
```

*The highest of the given privacy levels, or `open` when none is given.*
