phi-boundary-gate 0.5.0__tar.gz

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.
Files changed (33) hide show
  1. phi_boundary_gate-0.5.0/LICENSE +21 -0
  2. phi_boundary_gate-0.5.0/PKG-INFO +391 -0
  3. phi_boundary_gate-0.5.0/README.md +353 -0
  4. phi_boundary_gate-0.5.0/pyproject.toml +72 -0
  5. phi_boundary_gate-0.5.0/setup.cfg +4 -0
  6. phi_boundary_gate-0.5.0/src/phi_boundary_gate/__init__.py +49 -0
  7. phi_boundary_gate-0.5.0/src/phi_boundary_gate/actions.py +26 -0
  8. phi_boundary_gate-0.5.0/src/phi_boundary_gate/api.py +210 -0
  9. phi_boundary_gate-0.5.0/src/phi_boundary_gate/cli.py +127 -0
  10. phi_boundary_gate-0.5.0/src/phi_boundary_gate/compliance.py +462 -0
  11. phi_boundary_gate-0.5.0/src/phi_boundary_gate/detectors.py +306 -0
  12. phi_boundary_gate-0.5.0/src/phi_boundary_gate/policy.py +128 -0
  13. phi_boundary_gate-0.5.0/src/phi_boundary_gate/presidio_detector.py +92 -0
  14. phi_boundary_gate-0.5.0/src/phi_boundary_gate/project.py +148 -0
  15. phi_boundary_gate-0.5.0/src/phi_boundary_gate/py.typed +1 -0
  16. phi_boundary_gate-0.5.0/src/phi_boundary_gate/redacted_trace.py +70 -0
  17. phi_boundary_gate-0.5.0/src/phi_boundary_gate/report.py +247 -0
  18. phi_boundary_gate-0.5.0/src/phi_boundary_gate/sdk.py +85 -0
  19. phi_boundary_gate-0.5.0/src/phi_boundary_gate/templates/phi-compliance-policy.yml +69 -0
  20. phi_boundary_gate-0.5.0/src/phi_boundary_gate/templates/phi-policy.yml +122 -0
  21. phi_boundary_gate-0.5.0/src/phi_boundary_gate/trace.py +120 -0
  22. phi_boundary_gate-0.5.0/src/phi_boundary_gate.egg-info/PKG-INFO +391 -0
  23. phi_boundary_gate-0.5.0/src/phi_boundary_gate.egg-info/SOURCES.txt +31 -0
  24. phi_boundary_gate-0.5.0/src/phi_boundary_gate.egg-info/dependency_links.txt +1 -0
  25. phi_boundary_gate-0.5.0/src/phi_boundary_gate.egg-info/entry_points.txt +2 -0
  26. phi_boundary_gate-0.5.0/src/phi_boundary_gate.egg-info/requires.txt +11 -0
  27. phi_boundary_gate-0.5.0/src/phi_boundary_gate.egg-info/top_level.txt +1 -0
  28. phi_boundary_gate-0.5.0/tests/test_boundary_gate.py +317 -0
  29. phi_boundary_gate-0.5.0/tests/test_compliance_guard.py +245 -0
  30. phi_boundary_gate-0.5.0/tests/test_hybrid_detection.py +88 -0
  31. phi_boundary_gate-0.5.0/tests/test_repo_quality.py +85 -0
  32. phi_boundary_gate-0.5.0/tests/test_sdk_project.py +84 -0
  33. phi_boundary_gate-0.5.0/tests/test_trace_corpus_coverage.py +89 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tigerless Labs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,391 @@
1
+ Metadata-Version: 2.4
2
+ Name: phi-boundary-gate
3
+ Version: 0.5.0
4
+ Summary: Synthetic PHI boundary gating, redaction, and audit reporting toolkit.
5
+ Author: tigerless-labs
6
+ Maintainer: tigerless-labs
7
+ License-Expression: MIT
8
+ Project-URL: Homepage, https://github.com/tigerless-labs/phi-boundary-gate
9
+ Project-URL: Repository, https://github.com/tigerless-labs/phi-boundary-gate
10
+ Project-URL: Issues, https://github.com/tigerless-labs/phi-boundary-gate/issues
11
+ Project-URL: Changelog, https://github.com/tigerless-labs/phi-boundary-gate/blob/main/CHANGELOG.md
12
+ Project-URL: Documentation, https://github.com/tigerless-labs/phi-boundary-gate/tree/main/docs
13
+ Keywords: phi,hipaa,redaction,privacy,ai-safety,audit,healthcare
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Environment :: Console
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Topic :: Security
23
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: >=3.11
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Requires-Dist: PyYAML>=6.0
29
+ Provides-Extra: dev
30
+ Requires-Dist: build>=1.2.0; extra == "dev"
31
+ Requires-Dist: ruff>=0.6.0; extra == "dev"
32
+ Requires-Dist: twine>=5.0.0; extra == "dev"
33
+ Provides-Extra: ner
34
+ Requires-Dist: presidio-analyzer>=2.2.0; extra == "ner"
35
+ Requires-Dist: presidio-anonymizer>=2.2.0; extra == "ner"
36
+ Requires-Dist: spacy>=3.7.0; extra == "ner"
37
+ Dynamic: license-file
38
+
39
+ <h1 align="center">PHI Boundary Gate</h1>
40
+
41
+ <p align="center">
42
+ <img src="https://img.shields.io/badge/release-v0.5.0-brightgreen.svg" alt="release v0.5.0" /> <img src="https://img.shields.io/badge/python-3.11%2B-blue.svg" alt="Python 3.11+" /> <img src="https://img.shields.io/badge/output-Markdown%20%7C%20JSON%20%7C%20JSONL-lightgrey.svg" alt="Markdown, JSON, and JSONL output" /> <img src="https://img.shields.io/badge/data-synthetic%20PHI%20only-yellow.svg" alt="synthetic PHI only" /> <img src="https://img.shields.io/badge/license-MIT-yellow.svg" alt="license MIT" />
43
+ </p>
44
+
45
+ **PHI Boundary Gate** detects, gates, redacts, and reports PHI candidate
46
+ movement across AI context boundaries: user messages, RAG context, tool output,
47
+ model input, memory, debug logs, and provider requests.
48
+
49
+ It is built for healthcare and insurance AI workflows where an identifier match
50
+ is only the start. The report answers which layer the value entered, where it
51
+ came from, where it is going, and what policy says should happen before it moves
52
+ again.
53
+
54
+ The package ships as both a CLI and a Python library. Use the CLI for offline
55
+ audits that produce Markdown, JSON, and redacted JSONL traces. Use the library
56
+ inside another service to scan text, redact policy-matched spans, or block model
57
+ calls when the configured PHI and compliance policy says the route is not allowed.
58
+
59
+ | | |
60
+ |---|---|
61
+ | **Boundary-first reports** | Groups repeated PHI candidates across trace events so you can see the path, not only the match. |
62
+ | **Hybrid candidate detection** | Built-in regex rules cover common synthetic PHI variants; optional local Presidio detection can add NER-backed spans. |
63
+ | **Trace corpus baseline** | Synthetic trace expectations cover boundary flow, near misses, free text, structured payloads, and provider-boundary paths. |
64
+ | **Policy-driven redaction** | YAML policy decides whether each category is allowed, should be redacted, or is a violation in each layer. |
65
+ | **Provider-call guard** | Checks organization-supplied BAA, covered service, model, feature, logging, and storage facts before PHI is sent. |
66
+ | **Audit-safe by default** | Compliance decisions can be serialized without detected raw PHI values unless controlled debugging explicitly asks for them. |
67
+ | **Synthetic samples only** | The repository contains no real PHI and does not claim HIPAA compliance. |
68
+
69
+ ## Quick Start
70
+
71
+ ### Run the bundled sample
72
+
73
+ Use this path when you have cloned this repository and are running commands from
74
+ the repo root. The sample trace, policy, and report paths below are repository
75
+ files, not package data installed into another project.
76
+
77
+ ```bash
78
+ python3 -m pip install -e .
79
+ phi-boundary-gate \
80
+ --trace samples/traces/claim_agent_minimal.jsonl \
81
+ --policy samples/policies/default.yml \
82
+ --out reports/sample-report.md \
83
+ --json reports/sample-report.json
84
+ ```
85
+
86
+ Run the same command without installing the package:
87
+
88
+ ```bash
89
+ PYTHONPATH=src python3 -m phi_boundary_gate.cli \
90
+ --trace samples/traces/claim_agent_minimal.jsonl \
91
+ --policy samples/policies/default.yml \
92
+ --out reports/sample-report.md \
93
+ --json reports/sample-report.json \
94
+ --redacted-trace reports/sample-redacted-trace.jsonl
95
+ ```
96
+
97
+ Invalid trace or policy input returns exit code `2` and writes the validation
98
+ error to stderr.
99
+
100
+ ### Install from another project
101
+
102
+ Use the PyPI package for normal consumption:
103
+
104
+ ```bash
105
+ python3 -m pip install "phi-boundary-gate>=0.5,<0.6"
106
+ ```
107
+
108
+ The consuming environment needs Python 3.11 or newer and `pip`. `pip` installs
109
+ the runtime dependency `PyYAML>=6.0`.
110
+
111
+ Optional local NER support is available for projects that want Presidio-assisted
112
+ span detection in addition to the built-in regex rules:
113
+
114
+ ```bash
115
+ python3 -m pip install "phi-boundary-gate[ner]>=0.5,<0.6"
116
+ python3 -m spacy download en_core_web_lg
117
+ ```
118
+
119
+ Then enable it explicitly:
120
+
121
+ ```bash
122
+ phi-boundary-gate \
123
+ --trace samples/traces/expanded_phi_variants.jsonl \
124
+ --policy samples/policies/default.yml \
125
+ --out reports/expanded-report.md \
126
+ --json reports/expanded-report.json \
127
+ --enable-presidio
128
+ ```
129
+
130
+ Without `--enable-presidio`, scans stay dependency-light and use only the bundled
131
+ deterministic rules.
132
+
133
+ Consuming projects must provide their own PHI policy YAML. If they use the
134
+ compliance guard, they must also provide their own compliance policy YAML with
135
+ organization-approved BAA, covered service, model, feature, logging, and storage
136
+ facts. The sample files under `samples/` are examples to copy and adapt; they are
137
+ not installed as importable package resources.
138
+
139
+ To bootstrap a consuming project with starter policy files:
140
+
141
+ ```bash
142
+ phi-boundary-gate init
143
+ phi-boundary-gate check-config
144
+ ```
145
+
146
+ This creates `.phi-boundary-gate/config.json`, `config/phi-policy.yml`, and
147
+ `config/phi-compliance-policy.yml`. Review those files with the owners of your
148
+ logging, prompting, memory, provider, and compliance controls before using them
149
+ with real PHI.
150
+
151
+ ### Update from another project
152
+
153
+ Update consuming projects through the package index:
154
+
155
+ ```bash
156
+ python3 -m pip install --upgrade "phi-boundary-gate>=0.5,<0.6"
157
+ ```
158
+
159
+ Production projects should use a compatible version range such as
160
+ `phi-boundary-gate>=0.5,<0.6` and let Dependabot, Renovate, or a lockfile update
161
+ workflow propose patch/minor updates through CI. Git tag installs remain a
162
+ fallback for environments that cannot access PyPI, but they are no longer the
163
+ primary consumption path.
164
+
165
+ For `requirements.txt` and `pyproject.toml` examples, see
166
+ [Install and Consume as a Package](docs/install.md). For release notes, see
167
+ [CHANGELOG](CHANGELOG.md).
168
+
169
+ ## What It Reads
170
+
171
+ The trace is JSONL. Each event records the layer, content, source path, and
172
+ destination path for one piece of context:
173
+
174
+ ```json
175
+ {"event_id":"evt_003","timestamp":"2026-01-15T09:00:03Z","layer":"tool_output","source":{"type":"synthetic_claim_lookup","path":"tools.claim_lookup.response"},"destinations":[{"layer":"model_input","path":"prompt.context[1]"},{"layer":"debug_log","path":"logs.debug.claim_lookup"}],"content":"Lookup result: claim_id=CLM-SYN-44501 member_id=MBR-SYN-8842 mrn=MRN-SYN-22091 address=101 Example Harbor Rd."}
176
+ ```
177
+
178
+ Supported source layers:
179
+
180
+ - `user_message`
181
+ - `rag_context`
182
+ - `tool_output`
183
+ - `model_input`
184
+ - `memory`
185
+ - `debug_log`
186
+
187
+ Destination paths may also point at `model_provider`.
188
+
189
+ The PHI policy is YAML. It maps detector categories to layer decisions:
190
+
191
+ ```yaml
192
+ version: 1
193
+ categories:
194
+ member_id:
195
+ description: Synthetic insurance member identifier.
196
+ high_risk: true
197
+ deny_layers:
198
+ - debug_log
199
+ redact_layers:
200
+ - model_input
201
+ - rag_context
202
+ - tool_output
203
+ - memory
204
+ redaction: "[REDACTED_MEMBER_ID]"
205
+ ```
206
+
207
+ See [Trace Schema](docs/trace-schema.md) and [Policy Schema](docs/policy-schema.md)
208
+ for the full contract.
209
+
210
+ ## Configuration You Provide
211
+
212
+ At minimum, callers provide a PHI policy YAML file. Keep it in the consuming
213
+ project's config path, for example `config/phi-policy.yml`, and review it with
214
+ the team that owns logging, prompting, memory, and trace retention.
215
+
216
+ If a project sends PHI to model providers or other covered services, also provide
217
+ a compliance policy YAML file, for example `config/phi-compliance-policy.yml`.
218
+ That file should be owned by the organization, not inferred by this package. The
219
+ guard only enforces the facts in the file; it does not verify contracts or vendor
220
+ terms.
221
+
222
+ Do not commit real PHI, real traces, raw provider payloads, raw logs, or generated
223
+ reports that contain real PHI. The bundled samples are synthetic fixtures for
224
+ development and documentation.
225
+
226
+ ## Operational Safety
227
+
228
+ - Treat every detector result as a PHI candidate, not confirmed PHI.
229
+ - Keep real PHI, raw provider payloads, raw logs, and reports containing real PHI
230
+ out of source control.
231
+ - Remember that Markdown and JSON reports include matched values unless the caller
232
+ keeps reports synthetic or adds its own report-value redaction workflow.
233
+ - Enable Presidio only in environments approved to process the text locally; it
234
+ adds local candidate spans but does not replace policy review.
235
+ - Use `guard_compliance` before routing PHI-bearing text to a covered service; the
236
+ guard enforces only the BAA/service/model facts supplied by your organization.
237
+
238
+ ## What It Reports
239
+
240
+ The CLI writes two report formats from the same scan:
241
+
242
+ - Markdown for human review, with summary counts, boundary exposures, findings,
243
+ sources, destinations, and recommended actions.
244
+ - JSON for CI, dashboards, or downstream audit storage.
245
+
246
+ Each finding includes the matched value, category, span, detector confidence,
247
+ trace source, trace destinations, policy disposition, risk level, and suggested
248
+ redaction value. Boundary exposures group the same PHI candidate across events,
249
+ then sort by the worst policy disposition so violations rise to the top.
250
+
251
+ When `--redacted-trace` is provided, the CLI also writes a JSONL trace whose
252
+ `content` fields use policy redaction placeholders. Exact repeats of a detected
253
+ value are replaced across the trace.
254
+
255
+ Redaction is detector-driven. If the detector misses a value, the package cannot
256
+ redact it, so production use still needs caller-side controls and human review.
257
+
258
+ The bundled regex detector now covers broader synthetic variants for phone
259
+ numbers, fax numbers, email addresses, SSNs, street addresses, PO boxes, ZIP
260
+ codes, healthcare dates, member/subscriber IDs, claims and authorization IDs,
261
+ MRNs, policy/group/account/license/device/vehicle identifiers, URLs, and IP
262
+ addresses. Optional Presidio support can add local NER spans for names,
263
+ locations, dates, and other PII-like entities; policy decisions and redaction are
264
+ still made by this package.
265
+
266
+ The synthetic trace corpus is documented in [Trace Corpus](docs/trace-corpus.md).
267
+ Its committed coverage baseline is regenerated by `tools/trace_corpus_report.py`
268
+ and checked in CI.
269
+
270
+ ## Library API
271
+
272
+ After installing the package, other Python projects can import the scanner and
273
+ redactor directly:
274
+
275
+ ```python
276
+ from pathlib import Path
277
+
278
+ from phi_boundary_gate import guard_text, load_policy
279
+
280
+ policy = load_policy(Path("config/phi-policy.yml"))
281
+ decision = guard_text(
282
+ "member_id=MBR-SYN-8842",
283
+ layer="debug_log",
284
+ policy=policy,
285
+ mode="block_on_violation",
286
+ )
287
+
288
+ if decision.should_block:
289
+ raise RuntimeError(decision.recommended_action)
290
+
291
+ safe_text = decision.redacted_text
292
+ ```
293
+
294
+ `guard_text` handles PHI detection and layer policy only. It supports
295
+ `report_only`, `redact`, and `block_on_violation` modes. See
296
+ [Library API](docs/library-api.md) for the typed `ScanFinding` and
297
+ `GuardDecision` shapes.
298
+
299
+ Projects that initialize `.phi-boundary-gate/config.json` can use the SDK facade:
300
+
301
+ ```python
302
+ from phi_boundary_gate import PhiBoundaryGate
303
+
304
+ gate = PhiBoundaryGate.from_project()
305
+ decision = gate.guard_model_input("member_id=MBR-SYN-8842")
306
+
307
+ if decision.should_block:
308
+ raise RuntimeError(decision.recommended_action)
309
+
310
+ safe_log_text = gate.redact_for_log("debug member_id=MBR-SYN-8842")
311
+ audit_payload = decision.to_safe_dict()
312
+ ```
313
+
314
+ ## Compliance Guard
315
+
316
+ Projects that route PHI to covered services can run the compliance guard before
317
+ provider calls:
318
+
319
+ ```python
320
+ from pathlib import Path
321
+
322
+ from phi_boundary_gate import (
323
+ ComplianceContext,
324
+ guard_compliance,
325
+ load_compliance_policy,
326
+ load_policy,
327
+ )
328
+
329
+ phi_policy = load_policy(Path("config/phi-policy.yml"))
330
+ compliance_policy = load_compliance_policy(Path("config/phi-compliance-policy.yml"))
331
+
332
+ decision = guard_compliance(
333
+ "member_id=MBR-SYN-8842",
334
+ layer="model_input",
335
+ phi_policy=phi_policy,
336
+ compliance_policy=compliance_policy,
337
+ context=ComplianceContext(
338
+ phi_status="real_phi",
339
+ vendor="google",
340
+ service="vertex_ai",
341
+ endpoint="generate_content",
342
+ model="gemini-2.5-pro",
343
+ feature="online_prediction",
344
+ environment="production",
345
+ logging="redacted_only",
346
+ storage="none",
347
+ ),
348
+ )
349
+
350
+ if decision.should_block:
351
+ raise RuntimeError(decision.block_reasons)
352
+
353
+ text_for_model = decision.redacted_text
354
+ audit_payload = decision.to_dict()
355
+ ```
356
+
357
+ The guard enforces facts supplied by your organization. It cannot discover
358
+ whether a BAA is signed, whether a service is covered, or whether a vendor changed
359
+ its terms. Keep the bundled compliance sample as a schema example, not a contract
360
+ source of truth.
361
+
362
+ See [Compliance Guard](docs/compliance-guard.md) and
363
+ [Compliance Policy Schema](docs/compliance-policy-schema.md).
364
+
365
+ ## Development
366
+
367
+ Set up a local development environment:
368
+
369
+ ```bash
370
+ python3 -m pip install -e ".[dev]"
371
+ ```
372
+
373
+ Run the tests:
374
+
375
+ ```bash
376
+ PYTHONPATH=src python3 -m unittest discover -s tests
377
+ ```
378
+
379
+ Current release: `v0.5.0`.
380
+
381
+ ## Limits
382
+
383
+ - No real PHI is stored in this repository.
384
+ - No HIPAA compliance guarantee is provided.
385
+ - No medical decision-making is performed.
386
+ - No automatic vendor contract discovery is attempted.
387
+ - Detector results are PHI candidates and need human review.
388
+
389
+ ## License
390
+
391
+ MIT - see [LICENSE](LICENSE).