pii_ctx
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.
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. 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 and 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, which applies a policy to what search and reading return. ask describes what ask and the other model calls send to a model.
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 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:
pii= |
the row’s text becomes |
|---|---|
off, the default in Python and the CLI |
the stored text; nothing is checked |
redact |
the stored text with each recognised identifier replaced by its kind, as in [EMAIL] |
refuse |
a placeholder such as [withheld: personal information (card, email)] |
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.
redact as well as refuse. Its title, breadcrumb and source are withheld too.redact unchanged. There is nothing to mask. Use refuse for such a document.mark_not_pii comes back untouched, unless the shelf’s floor is above Open.The MCP server 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.
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.
Run the detector over every section in an assembled context: its text, heading, title and source.
Mark a document as personal when the detector finds nothing, or remove that mark with force=False, which only a person may do.
Mark a document as not personal, or with clear=False hand the decision back to the detector; only a person may do either.
Report whether one document holds personal information, which kinds, and any mark that overrides the detector.
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.
The placeholder that replaces withheld text: it names the kinds found, or says the document is local only.
The heading tree of the shelf or of one document, with pii= applied to the node titles as well as the text.
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.
| level | how a document gets it | what a hosted model may be sent |
|---|---|---|
Open (open) |
nothing personal found, or a person cleared it | the text as it is |
Personal (personal) |
the detector found an identifier, or someone marked it | it depends on the policy: masked text, nothing, or with allow=True the whole text (see ask) |
Local only (local) |
someone set it with set_level, or its type is sensitive |
nothing; only a model on this machine reads it |
The sensitive types come from 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.
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.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 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.
Remove the verdict on findings so that detection judges every occurrence of the value again; only a person may.
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.
Confirm findings as personal; the verdict covers every occurrence of the same value in the document.
List filed findings, each with a masked preview such as a••@e••••••.com built from the current page text.
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 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.
Scan a document page by page and file each identifier found as a finding (kind, page, offsets and fingerprint) without its value.
Set this shelf’s floor; anyone may raise it, and only a person may lower it.
This shelf’s floor, the lowest privacy level any of its documents can have: open, personal or local.
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.
The highest of the given privacy levels, or open when none is given.