openapi: 3.1.0
info:
  title: OHN-03 Master Patient Index (MPI)
  version: 1.0.0
  description: >
    Written from the observed behaviour of the TypeScript reference (backend/modules/ohn-03, 2026-11-17).
    Every route requires a token scoped to ONE jurisdiction (UUID claim); the wildcard is refused (403 forbidden).
    A different x-ohn-purpose header than the token purpose is refused (403 purpose_mismatch).
    A record of another jurisdiction is reported as not found.
servers: [{ url: /api/ohn/v1 }]
security: [{ gatewayToken: [] }]
tags: [{ name: mpi }, { name: governance }]
paths:
  /mpi/match:
    post:
      operationId: matchPatient
      tags: [mpi]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/PatientIdentityCommand' } } } }
      responses:
        '201': { description: Identity stored and candidates evaluated, content: { application/json: { schema: { $ref: '#/components/schemas/MatchResult' } } } }
        '403': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /mpi/candidates:
    get:
      operationId: listCandidates
      tags: [mpi]
      responses:
        '200': { description: Latest 50 pairs of the jurisdiction, content: { application/json: { schema: { $ref: '#/components/schemas/CandidateList' } } } }
  /mpi/candidates/{id}:
    get:
      operationId: getCandidate
      tags: [governance]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses:
        '200': { description: Pair with decision history, content: { application/json: { schema: { $ref: '#/components/schemas/CandidateDetail' } } } }
        '404': { $ref: '#/components/responses/Problem' }
  /mpi/cases/{id}/decisions:
    post:
      operationId: decideCase
      tags: [mpi]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/MatchDecisionCommand' } } } }
      responses:
        '201': { description: Decision recorded, content: { application/json: { schema: { $ref: '#/components/schemas/MatchDecision' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /mpi/patients:
    get:
      operationId: searchPatients
      tags: [mpi]
      parameters:
        - { name: family_name, in: query, schema: { type: string } }
        - { name: given_name, in: query, schema: { type: string } }
        - { name: birth_date, in: query, schema: { type: string } }
      responses:
        '200': { description: At most 25 identities, content: { application/json: { schema: { $ref: '#/components/schemas/PatientList' } } } }
        '422': { $ref: '#/components/responses/Problem' }
  /mpi/patients/{id}:
    get:
      operationId: getPatient
      tags: [mpi]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses:
        '200': { description: Identity, content: { application/json: { schema: { $ref: '#/components/schemas/PatientIdentity' } } } }
        '404': { $ref: '#/components/responses/Problem' }
  /mpi/rule-sets:
    get:
      operationId: listRuleSets
      tags: [governance]
      responses:
        '200': { description: Rule sets and the active rule, content: { application/json: { schema: { $ref: '#/components/schemas/RuleSetList' } } } }
    post:
      operationId: submitRuleSet
      tags: [governance]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/RuleSetDraft' } } } }
      responses:
        '201': { description: Submitted, content: { application/json: { schema: { $ref: '#/components/schemas/RuleSet' } } } }
        '422': { $ref: '#/components/responses/Problem' }
  /mpi/rule-sets/{id}:
    get:
      operationId: getRuleSet
      tags: [governance]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses:
        '200': { description: Rule set, content: { application/json: { schema: { $ref: '#/components/schemas/RuleSet' } } } }
        '404': { $ref: '#/components/responses/Problem' }
  /mpi/rule-sets/{id}/approve:
    post:
      operationId: approveRuleSet
      tags: [governance]
      description: A different person than the submitter must approve (403 second_approver_required). The previous active rule becomes superseded.
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ReasonCommand' } } } }
      responses:
        '200': { description: Active, content: { application/json: { schema: { $ref: '#/components/schemas/RuleSet' } } } }
        '403': { $ref: '#/components/responses/Problem' }
        '409': { $ref: '#/components/responses/Problem' }
  /mpi/rule-sets/{id}/reject:
    post:
      operationId: rejectRuleSet
      tags: [governance]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ReasonCommand' } } } }
      responses:
        '200': { description: Rejected, content: { application/json: { schema: { $ref: '#/components/schemas/RuleSet' } } } }
        '409': { $ref: '#/components/responses/Problem' }
  /mpi/rule-sets/{id}/retire:
    post:
      operationId: retireRuleSet
      tags: [governance]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ReasonCommand' } } } }
      responses:
        '200': { description: Retired, content: { application/json: { schema: { $ref: '#/components/schemas/RuleSet' } } } }
        '409': { $ref: '#/components/responses/Problem' }
  /mpi/investigations:
    get:
      operationId: listInvestigations
      tags: [governance]
      parameters: [{ name: status, in: query, schema: { type: string, enum: [open, closed] } }]
      responses:
        '200': { description: Up to 200 newest first, content: { application/json: { schema: { $ref: '#/components/schemas/InvestigationList' } } } }
    post:
      operationId: openInvestigation
      tags: [governance]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/InvestigationCommand' } } } }
      responses:
        '201': { description: Opened, content: { application/json: { schema: { $ref: '#/components/schemas/Investigation' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
  /mpi/investigations/{id}:
    get:
      operationId: getInvestigation
      tags: [governance]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses:
        '200': { description: Investigation, content: { application/json: { schema: { $ref: '#/components/schemas/Investigation' } } } }
        '404': { $ref: '#/components/responses/Problem' }
  /mpi/investigations/{id}/close:
    post:
      operationId: closeInvestigation
      tags: [governance]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/CloseCommand' } } } }
      responses:
        '200': { description: Closed, content: { application/json: { schema: { $ref: '#/components/schemas/Investigation' } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '409': { $ref: '#/components/responses/Problem' }
  /mpi/reports/{reportId}:
    get:
      operationId: runMpiReport
      tags: [governance]
      description: OHN-03-R001..R003. format=csv downloads and records an export event.
      parameters:
        - { name: reportId, in: path, required: true, schema: { type: string } }
        - { name: from, in: query, schema: { type: string } }
        - { name: to, in: query, schema: { type: string } }
        - { name: format, in: query, schema: { type: string } }
      responses:
        '200': { description: Report json or csv, content: { application/json: { schema: { type: object, additionalProperties: true } }, text/csv: { schema: { type: string } } } }
        '404': { $ref: '#/components/responses/Problem' }
        '422': { $ref: '#/components/responses/Problem' }
components:
  securitySchemes:
    gatewayToken: { type: http, scheme: bearer, bearerFormat: JWT, description: ES256 gateway token with audience ohn-03 }
  parameters:
    Id: { name: id, in: path, required: true, schema: { type: string } }
    IdempotencyKey: { name: Idempotency-Key, in: header, required: true, schema: { type: string, maxLength: 512 } }
  responses:
    Problem:
      description: application/problem+json with the legacy error code
      content: { application/problem+json: { schema: { $ref: '#/components/schemas/Problem' } } }
  schemas:
    Problem:
      type: object
      required: [error]
      properties:
        error: { type: string }
        message: { type: string }
        id: { type: string }
        details: { type: object, additionalProperties: true }
        trace_id: { type: string }
    ReasonCommand:
      type: object
      required: [reason]
      properties: { reason: { type: string, minLength: 10, maxLength: 4096 } }
    PatientIdentityCommand:
      type: object
      required: [jurisdiction_id, source_facility, purpose]
      properties:
        jurisdiction_id: { type: string, format: uuid }
        given_name: { type: string, maxLength: 512 }
        family_name: { type: string, maxLength: 512 }
        birth_date: { type: string, format: date }
        sex_at_birth: { type: string, enum: [male, female, unknown, other] }
        local_identifier: { type: string, maxLength: 512 }
        source_facility: { type: string, format: uuid }
        purpose: { type: string, minLength: 1, maxLength: 128 }
    PatientIdentity:
      type: object
      properties:
        id: { type: string, format: uuid }
        jurisdiction_id: { type: string, format: uuid }
        given_name: { type: [string, 'null'] }
        family_name: { type: [string, 'null'] }
        birth_date: { type: [string, 'null'] }
        sex_at_birth: { type: [string, 'null'] }
        local_identifier: { type: [string, 'null'] }
        source_facility: { type: string, format: uuid }
        purpose: { type: string }
        match_status: { type: string, enum: [unresolved, review_required, resolved] }
        version: { type: integer }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    PatientList:
      type: object
      properties: { items: { type: array, items: { $ref: '#/components/schemas/PatientIdentity' } } }
    MatchCandidate:
      type: object
      properties:
        id: { type: string, format: uuid }
        left_patient_id: { type: string, format: uuid }
        right_patient_id: { type: string, format: uuid }
        score: { type: number, format: double }
        rule_set_id: { type: string, format: uuid }
        status: { type: string }
        reviewer_id: { type: [string, 'null'] }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    CandidateList:
      type: object
      properties:
        items: { type: array, items: { $ref: '#/components/schemas/MatchCandidate' } }
        total: { type: integer }
    MatchResult:
      type: object
      properties:
        patient: { $ref: '#/components/schemas/PatientIdentity' }
        candidates: { type: array, items: { $ref: '#/components/schemas/MatchCandidate' } }
    DecisionEntry:
      type: object
      properties:
        id: { type: string, format: uuid }
        decision: { type: string }
        match_explanation: { type: string }
        reason: { type: string }
        reviewer_id: { type: string }
        created_at: { type: string, format: date-time }
    CandidateDetail:
      allOf:
        - { $ref: '#/components/schemas/MatchCandidate' }
        - type: object
          properties:
            rule_version: { type: string }
            decisions: { type: array, items: { $ref: '#/components/schemas/DecisionEntry' } }
    MatchDecisionCommand:
      type: object
      required: [decision, match_explanation, reason, reviewer_id]
      properties:
        candidate_pair_id: { type: string, format: uuid }
        decision: { type: string, enum: [link, non_link, needs_review] }
        evidence_refs: { type: array, items: { type: string, format: uuid } }
        match_explanation: { type: string, maxLength: 8192 }
        reason: { type: string, minLength: 10, maxLength: 4096 }
        reviewer_id: { type: string, format: uuid }
    MatchDecision:
      type: object
      properties:
        id: { type: string, format: uuid }
        candidate_pair_id: { type: string, format: uuid }
        decision: { type: string }
        match_explanation: { type: string }
        reason: { type: string }
        reviewer_id: { type: string }
        created_at: { type: string, format: date-time }
    RuleSetDraft:
      type: object
      required: [rule_version, auto_link_threshold, manual_review_threshold, effective_from, reason]
      properties:
        rule_version: { type: string, minLength: 1, maxLength: 512 }
        feature_weights: { type: string, maxLength: 8192 }
        auto_link_threshold: { type: number, format: double, minimum: 0, maximum: 1 }
        manual_review_threshold: { type: number, format: double, minimum: 0, maximum: 1 }
        effective_from: { type: string, format: date }
        reason: { type: string, minLength: 10, maxLength: 4096 }
    RuleSet:
      type: object
      properties:
        id: { type: string, format: uuid }
        rule_version: { type: string }
        feature_weights: { type: string }
        auto_link_threshold: { type: number, format: double }
        manual_review_threshold: { type: number, format: double }
        effective_from: { type: string }
        status: { type: string, enum: [submitted, active, rejected, retired, superseded] }
        submitted_by: { type: [string, 'null'] }
        approved_by: { type: [string, 'null'] }
        reason: { type: [string, 'null'] }
        decided_at: { type: [string, 'null'] }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    ActiveRule:
      type: object
      properties:
        id: { type: [string, 'null'] }
        rule_version: { type: string }
        auto_link_threshold: { type: number, format: double }
        manual_review_threshold: { type: number, format: double }
    RuleSetList:
      type: object
      properties:
        items: { type: array, items: { $ref: '#/components/schemas/RuleSet' } }
        active: { $ref: '#/components/schemas/ActiveRule' }
    InvestigationCommand:
      type: object
      required: [patient_ref, suspected_duplicate_ref, investigation_reason]
      properties:
        patient_ref: { type: string, format: uuid }
        suspected_duplicate_ref: { type: string, format: uuid }
        investigation_reason: { type: string, minLength: 10, maxLength: 4096 }
        source_evidence: { type: string, format: uuid }
    CloseCommand:
      type: object
      required: [outcome, reason]
      properties:
        outcome: { type: string, enum: [duplicate_confirmed, not_duplicate, inconclusive] }
        reason: { type: string, minLength: 10, maxLength: 4096 }
    Investigation:
      type: object
      properties:
        id: { type: string, format: uuid }
        patient_ref: { type: string, format: uuid }
        suspected_duplicate_ref: { type: string, format: uuid }
        investigation_reason: { type: string }
        source_evidence: { type: [string, 'null'] }
        status: { type: string, enum: [open, closed] }
        outcome: { type: [string, 'null'] }
        opened_by: { type: [string, 'null'] }
        closed_by: { type: [string, 'null'] }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    InvestigationList:
      type: object
      properties: { items: { type: array, items: { $ref: '#/components/schemas/Investigation' } } }
