← Back to Pull requests

fix(kernel): legal entity names and codes are personal data, kept by law unless the entity is the org (#1312) #1326

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 24, 2026 12:20 UTC
Author

Closes #1312

What


  • crove_kernel.legal_entity.name is registered in the personal-data registry (kind name).

  • legal_entity.name and legal_entity.tax_code are keep_legal, with a per-row override: the org's default entity (is_default) stays keep_org. A registry entry may now carry override: { when, handling, reason }, applied by rowHandling() to each matched row.

  • The finder (kernel.personal_data.find) reports each row's own handling and reason, so a household found by its 12-digit code (its owner's national ID number) shows as kept by law, with its name in the exported row, and the default entity as the org's own.

  • The erasure (kernel.personal_data.erase) does not change entities (both handlings keep). Its kept list now groups by column and reason, so rows of one column kept for different reasons are reported separately; the audit row still holds counts per column only.

  • Runbook docs/runbooks/personal-data-requests.md lists legal entities under "Kept by law".
  • Not changed


  • The org purge keeps every entity as #1292 does; deleting or anonymizing non-default entities waits on the lawyer (#1315).

  • No migration, no protected path (tools/ci/protected-paths.mjs: none), no invariant file. The name pattern of the invariant registry test does not match a bare name, so it needs no change.
  • Decisions


  • Searching by a person's name is still not supported (the finder takes an email, a phone or a code); the name is registered so it is classified and appears in the exported row of a match by code. Name search stays out of scope, as for parties.
  • Tests (local, Windows, own PostgreSQL 17)


  • apps/kernel/test/personal-data.test.ts (new, unit): registry entries, rowHandling for default and non-default rows, the 12-digit probe.

  • apps/kernel/test/personal-data.db.test.ts: a household entity (12-digit code) and a non-default company found as keep_legal with their names, not seen from another org; the default entity's code found as keep_org next to the tenant's; erasing by the ID number keeps the entity and reports it; every override names a boolean column. 7/7 passed; legal-entities.db.test.ts passed.

  • pnpm lint, pnpm typecheck, pnpm docs:check passed.

  • pnpm vitest run --project unit: only the known Windows-only failures (STATUS_FILE, C:\ paths, backup lib shell, i18n runtime, migrate copies); mst-fixture-guard passes with a checksum-invalid synthetic code.

  • Invariant suite with --no-file-parallelism: 1036 passed, 2 failed in route-coverage.test.ts (STATUS_FILE, Windows only).
  • 🤖 Generated with Claude Code

    ---

    <!-- dos-pr-summary -->

    📌 TL;DR


    This PR refines the personal data erasure and search logic to correctly handle legal entities, distinguishing between the organization's default entity (kept as org data) and external entities like households or clients (kept by law). It introduces a row-level handling override mechanism to ensure that 12-digit national ID numbers linked to household businesses are preserved with appropriate legal reasoning during data subject requests.

    🎯 Type of Change


  • 🚀 New feature

  • 🐛 Bugfix

  • 🧹 Refactor

  • ⚡ Performance

  • 📚 Documentation

  • ⚙️ CI / Configuration
  • 🔍 Changes Walkthrough


    | File | Summary of Changes |
    | :--- | :--- |
    | packages/kernel/src/personal-data.ts | Core logic update. Added override support to PersonalDataColumn to allow row-specific handling (e.g., is_default flag). Updated legal_entity columns to use keep_legal by default with an override to keep_org for the default entity. Refactored findPersonalData and personalDataErase to apply rowHandling per row and group "kept" results by both column and reason. |
    | apps/kernel/test/personal-data.test.ts | New unit tests verifying the registry configuration for legal entities, the rowHandling logic for default vs. non-default entities, and the probe normalization for 12-digit ID numbers. |
    | apps/kernel/test/personal-data.db.test.ts | Integration tests verifying end-to-end behavior: finding legal entities by ID number, ensuring cross-org isolation, verifying that erasure keeps legal entities with the correct "kept by law" reason, and validating that override columns exist in the database schema. |
    | docs/runbooks/personal-data-requests.md | Updated documentation to clarify that legal entities (clients, households) are kept by law, while the default entity is treated as the org's own data. |

    📊 Architectural Flow


    flowchart TD
        A[Personal Data Request] --> B{Probe Type}
        B -->|Email/Phone/ID| C[findPersonalData]
        C --> D[Iterate PERSONAL_DATA_COLUMNS]
        D --> E{Is Handling Searchable?}
        E -->|No| F[Skip Column]
        E -->|Yes| G[Query DB for Matches]
        G --> H[For Each Row]
        H --> I[rowHandling]
        I --> J{Override Condition Met?}
        J -->|Yes| K[Use Override Handling/Reason]
        J -->|No| L[Use Default Handling/Reason]
        K --> M{Is Result Searchable?}
        L --> M
        M -->|No| N[Exclude from Matches]
        M -->|Yes| O[Add to Matches]
        
        O --> P[Return Matches]
        P --> Q{User Action}
        Q -->|View| R[Display Matches]
        Q -->|Erase| S[personalDataErase]
        S --> T[Execute Erasure on 'erase' rows]
        T --> U[Re-run findPersonalData]
        U --> V[Group Kept Rows by Column + Reason]
        V --> W[Return Erasure Result]
    

    <!-- /dos-pr-summary -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: claude[bot] Review duration: 27m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #1326"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%
    Repository