# Kuan Yin — Privacy Implementation

> Technical doc 32.16.1.13. Engine in
> `@kuanyin/dharma-analytics/privacy-protection.ts`; user-facing controls in
> `user-autonomy.ts`.

Privacy is engineered in, not bolted on. The core principle: **analyse the
minimum, retain less, and keep the user in control.**

## Data minimisation at the source

- **Typing dynamics** — `analyzeTypingOnDevice` reduces raw keystrokes to
  aggregate features on-device; raw timings are discarded and never leave the
  client.
- **Content analysis** — `analyzeEphemeralContent` works over a SHA-256
  fingerprint with no retention of the original text.
- **Aggregates** — `aggregatePatternsWithKAnonymity` enforces a k-anonymity
  floor; `laplaceNoisyCount` adds calibrated differential-privacy noise before
  any pattern count is exposed.
- `enforceDataMinimization` strips fields not justified by the stated purpose.

## Consent

`manageConsent({ subjectId, purpose, action, now, ttlMs? })` is a state machine
with expiry; `getConsentStatus` resolves the effective state
(`granted | withdrawn | expired | never_set`). Predictive features and learning
are opt-in and honour withdrawal immediately (`user-autonomy.ts`).

## Data subject rights (GDPR)

- **Art. 20 export** — `exportUserData(subjectId, categories, now)` returns a
  bundle with a record count and integrity checksum.
- **Art. 17 erasure** — `processRightToDeletion(records)` deletes everything
  except legal-hold records, reporting what was retained and why
  (`fullyErased: false` when a hold applies).
- **Retention** — `applyRetentionPolicy` flags records past their window for
  deletion.

## Pseudonymisation & audit

- `pseudonymizeIdentifiers` / `hmacPseudonym` replace identifiers with
  HMAC-SHA-256 pseudonyms; `anonymizeDataset` generalises quasi-identifiers.
- Every data access is appended to a **tamper-evident hash chain**
  (`auditDataAccess`); `verifyAuditChain()` detects any break.

## Encryption & breach response

`assessEncryptionAtRest` / `assessEncryptionInTransit` validate NIST-aligned
policy; the module includes a GDPR 72-hour breach-response planner.

## User controls

`user-autonomy.ts` exposes opt-out for predictive features, friction-skip
preservation (with a no-shadow-penalty guarantee), appeal rights, human-review
requests, notification/data-sharing/learning/recommendation/visibility controls,
account portability, and complete account deletion. These map to the user-facing
"Privacy controls guide" (`../user/07-privacy-controls.md`).
