openapi: 3.1.0
info:
  title: OHN-18 Clinical Audit and Data Provenance
  version: 1.1.0
  description: |
    Tamper-evident who/what/why/where/when evidence for access, changes and clinical assertions.
    All routes live under /api/ohn/v1/audit. Every POST requires an Idempotency-Key header.
    Default deny: only security_auditor and privacy_officer roles are permitted. Synthetic data only.
    1.1.0 (2026-11-23): read-only role inspector (purpose regulatory_audit). It may call every GET and POST /reports/{report_id}/export, nothing else
    (ingest, investigations, integrity/verify, disclosure-trace-requests and transitions answer 403 and are recorded as denied.<action>). Every GET it makes
    is recorded first in the audit records (action audit.inspector.read); when that record cannot be written the answer is 503 audit_unavailable. Its export
    download is an attachment (Content-Disposition) and carries the OHN-16 security watermark with "INSPECTION COPY" (header X-OHN-Document-Mark;
    X-OHN-Unmarked-SHA256 is the digest the export signature covers).
    Java: tokens are issued by the gateway (ES256, audience ohn-18); the TypeScript synthetic sign-in helpers are not part of this contract.
servers:
  - url: https://opeshealthnetwork.com
security:
  - bearerAuth: []
tags:
  - name: events
  - name: integrity
  - name: disclosures
  - name: workflows
  - name: reports
paths:
  /health/ohn-18:
    get:
      operationId: ohn18_health
      security: []
      responses:
        '200': { description: 'Database reachable', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
        '503': { description: Database unavailable }
  /api/ohn/v1/audit/health:
    get:
      operationId: ohn18_externalHealth
      security: []
      description: Database and dependency modes (live / unavailable-fail-closed). Reachable through the platform's /api/ohn proxy.
      responses:
        '200': { description: 'Healthy', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
        '503': { description: Database unavailable }
  /api/ohn/v1/audit/jurisdictions:
    get:
      operationId: ohn18_listJurisdictions
      description: OHN-01 jurisdictions in force (status active or reinstated).
      responses:
        '200': { description: 'Jurisdictions from OHN-01', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
        '503': { $ref: '#/components/responses/Unavailable' }
  /api/ohn/v1/audit/events:
    get:
      operationId: ohn18_getAuditEvents
      tags: [events]
      description: OHN-18-API-001. Cursor-paginated audit envelopes in the caller's jurisdiction. Patient references are masked.
      parameters:
        - { name: cursor, in: query, schema: { type: string } }
        - { name: page_size, in: query, schema: { type: string }, description: 'Clamped to 1..100, default 25' }
        - { name: decision, in: query, schema: { type: string } }
        - { name: kind, in: query, schema: { type: string } }
        - { name: domain, in: query, description: Resource type, schema: { type: string } }
        - { name: patient_id, in: query, schema: { type: string } }
        - { name: period_from, in: query, schema: { type: string } }
        - { name: period_to, in: query, schema: { type: string } }
      responses:
        '200': { description: 'Page of envelopes with provenance and next_cursor', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Denied' }
        '429': { $ref: '#/components/responses/RateLimited' }
    post:
      operationId: ohn18_ingestAuditEvent
      tags: [events]
      description: Append one access decision to the jurisdiction's hash chain (W001 access_decided).
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/AuditEventCommand' }
      responses:
        '201': { description: 'Envelope appended; receipt with sequence_number and entry_hash', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
        '400': { $ref: '#/components/responses/IdempotencyRequired' }
        '403': { $ref: '#/components/responses/Denied' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
        '503': { $ref: '#/components/responses/Unavailable' }
  /api/ohn/v1/audit/events/{id}:
    get:
      operationId: ohn18_getAuditEvent
      tags: [events]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses:
        '200': { description: 'Envelope detail with chain verification, provenance links and history timeline', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
        '404': { $ref: '#/components/responses/NotFound' }
  /api/ohn/v1/audit/investigations:
    post:
      operationId: ohn18_openInvestigation
      tags: [events]
      description: Form OHN-18-F001 (Audit investigation).
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/AuditInvestigationCommand' }
      responses:
        '201': { description: 'Investigation envelope appended with receipt', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
        '404': { $ref: '#/components/responses/NotFound' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/audit/integrity/verify:
    post:
      operationId: ohn18_verifyIntegrity
      tags: [integrity]
      description: OHN-18-API-003, form OHN-18-F002. Recomputes the hash chain over a sequence range and compares with expected_root_hash. Never repairs. A mismatch emits audit.integrity.failure.
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/IntegrityVerificationCommand' }
      responses:
        '201': { description: 'Checkpoint recorded (result passed or failed)', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
        '403': { $ref: '#/components/responses/Denied' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
        '503': { $ref: '#/components/responses/Unavailable' }
  /api/ohn/v1/audit/integrity/state:
    get:
      operationId: ohn18_getIntegrityState
      tags: [integrity]
      responses:
        '200': { description: 'Current chain head, length and computed root hash for the jurisdiction', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
  /api/ohn/v1/audit/integrity/verifications:
    get:
      operationId: ohn18_listVerifications
      tags: [integrity]
      responses:
        '200': { description: 'Provenance links (verification runs)', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
  /api/ohn/v1/audit/checkpoints:
    get:
      operationId: ohn18_listCheckpoints
      tags: [disclosures]
      responses:
        '200': { description: 'Disclosure trace requests (AuditIntegrityCheckpoint aggregate)', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
  /api/ohn/v1/audit/disclosure-trace-requests:
    post:
      operationId: ohn18_requestDisclosureTrace
      tags: [disclosures]
      description: Form OHN-18-F003. Opens W003 at rights_request_authorized.
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/DisclosureTraceCommand' }
      responses:
        '201': { description: 'Trace request recorded', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/audit/patients/{id}/disclosures:
    get:
      operationId: ohn18_getPatientDisclosures
      tags: [disclosures]
      description: OHN-18-API-002 (relocated under /audit because /api/ohn/v1/patients is owned by OHN-01/02/03). Never returns other patients or staff security secrets.
      parameters:
        - { $ref: '#/components/parameters/Id' }
        - { name: purpose, in: query, schema: { type: string } }
        - { name: date_from, in: query, schema: { type: string } }
        - { name: date_to, in: query, schema: { type: string } }
      responses:
        '200': { description: 'Disclosure entries with authorized and denied counts', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
        '400': { description: Malformed patient id }
  /api/ohn/v1/audit/workflows/{kind}/{id}/transitions:
    post:
      operationId: ohn18_transitionWorkflow
      tags: [workflows]
      description: Advance W001 clinical_access_audit, W002 integrity_review or W003 patient_disclosure_accounting by exactly one step with optimistic concurrency.
      parameters:
        - { name: kind, in: path, required: true, schema: { type: string }, description: 'clinical_access_audit, integrity_review or patient_disclosure_accounting; anything else is 404' }
        - { $ref: '#/components/parameters/Id' }
        - { $ref: '#/components/parameters/IdempotencyKey' }
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/WorkflowTransitionCommand' }
      responses:
        '200': { description: 'Transition committed with receipt', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/audit/reports/{reportId}:
    get:
      operationId: ohn18_getReport
      tags: [reports]
      parameters: [{ $ref: '#/components/parameters/ReportId' }]
      responses:
        '200': { description: 'Report rows, metric definition, period and source freshness', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
  /api/ohn/v1/audit/reports/{reportId}/export:
    post:
      operationId: ohn18_createExport
      tags: [reports]
      parameters: [{ $ref: '#/components/parameters/ReportId' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/ExportCommand' }
      responses:
        '201': { description: 'Ed25519-signed export, registered in OHN-45 (signature.ohn45_artifact_ref / ohn45_signature_ref); one-time retrieval path; emits audit.export.generated', content: { application/json: { schema: { $ref: '#/components/schemas/JsonObject' } } } }
        '503': { description: 'signing_unavailable (no key or OHN-45 unreachable) or policy_not_configured (R003); nothing generated' }
  /api/ohn/v1/audit/exports/{token}:
    get:
      operationId: ohn18_downloadExport
      tags: [reports]
      parameters: [{ name: token, in: path, required: true, schema: { type: string } }]
      responses:
        '200':
          description: CSV or PDF body with X-OHN-Signature header
          content:
            text/csv: { schema: { type: string, format: binary } }
            application/pdf: { schema: { type: string, format: binary } }
        '404': { $ref: '#/components/responses/NotFound' }
components:
  securitySchemes:
    bearerAuth: { type: http, scheme: bearer, bearerFormat: JWT, description: 'Gateway-issued ES256 token (audience ohn-18) carrying role, jurisdiction, organization_id and purpose' }
  parameters:
    IdempotencyKey: { name: Idempotency-Key, in: header, required: true, schema: { type: string, minLength: 1, maxLength: 255 } }
    Id: { name: id, in: path, required: true, schema: { type: string } }
    ReportId: { name: reportId, in: path, required: true, schema: { type: string }, description: 'OHN-18-R001, OHN-18-R002 or OHN-18-R003; anything else is 404' }
  responses:
    Unauthenticated: { description: 'Missing or invalid bearer token' }
    Denied: { description: 'denied_by_policy (role, purpose or jurisdiction)' }
    NotFound: { description: 'not_found' }
    Validation: { description: 'domain_validation_failed with field details' }
    Conflict: { description: 'stale_version, idempotency_conflict or request_in_progress' }
    RateLimited: { description: 'rate_limited, with Retry-After' }
    Unavailable: { description: 'jurisdiction_unavailable, identity_provider_unavailable or dependency_unavailable; nothing written' }
    JurisdictionNotActive: { description: '''jurisdiction_not_active (422): not registered, or not active/reinstated in OHN-01''' }
    IdempotencyRequired: { description: 'missing_idempotency_key' }
  schemas:
    JsonObject: { type: object, additionalProperties: true }
    AuditDecision: { enum: [permitted, denied] }
    RequestPurpose: { enum: [patient_rights_request, regulatory_audit, legal_disclosure, continuity_of_care] }
    AuditEventCommand:
      type: object
      additionalProperties: false
      required: [jurisdiction_id, actor_id, actor_role, action, resource_type, resource_id, purpose, decision]
      properties:
        jurisdiction_id: { type: string, format: uuid }
        actor_id: { type: string, format: uuid }
        actor_role: { type: string, minLength: 1, maxLength: 64 }
        action: { type: string, minLength: 1, maxLength: 128 }
        resource_type: { type: string, minLength: 1, maxLength: 64 }
        resource_id: { type: string, format: uuid }
        patient_id: { type: string, format: uuid }
        facility_id: { type: string, format: uuid }
        purpose: { $ref: '#/components/schemas/RequestPurpose' }
        decision: { $ref: '#/components/schemas/AuditDecision' }
        reason: { type: string, maxLength: 4096 }
        event_time: { type: string, format: date-time }
    AuditInvestigationCommand:
      type: object
      additionalProperties: false
      required: [event_id, investigation_reason, investigator_id, scope]
      properties:
        event_id: { type: string, format: uuid }
        investigation_reason: { type: string, minLength: 10, maxLength: 4096 }
        investigator_id: { type: string, format: uuid }
        scope: { type: string, minLength: 1, maxLength: 8192 }
        case_ref: { type: string, format: uuid }
    IntegrityVerificationCommand:
      type: object
      additionalProperties: false
      required: [jurisdiction_id, sequence_from, sequence_to, expected_root_hash, verification_environment]
      properties:
        jurisdiction_id: { type: string, format: uuid }
        sequence_from: { type: integer, minimum: 0 }
        sequence_to: { type: integer, minimum: 0, description: Must be >= sequence_from }
        expected_root_hash: { type: string, minLength: 1, maxLength: 512 }
        verification_environment: { enum: [production, staging, disaster_recovery] }
    DisclosureTraceCommand:
      type: object
      additionalProperties: false
      required: [patient_id, date_from, date_to, request_purpose]
      properties:
        patient_id: { type: string, format: uuid }
        date_from: { type: string, format: date }
        date_to: { type: string, format: date }
        request_purpose: { $ref: '#/components/schemas/RequestPurpose' }
        recipient_scope: { type: string, maxLength: 8192 }
    WorkflowTransitionCommand:
      type: object
      additionalProperties: false
      required: [expected_version, to_state, evidence_ref, reason]
      properties:
        expected_version: { type: integer, minimum: 1 }
        to_state: { type: string }
        evidence_ref: { type: string, format: uuid }
        reason: { type: string, minLength: 10, maxLength: 4096 }
    ExportCommand:
      type: object
      additionalProperties: false
      required: [format]
      properties:
        format: { enum: [csv, pdf] }
        period_from: { type: string, format: date-time }
        period_to: { type: string, format: date-time }
        facility_id: { type: string, format: uuid }
        severity: { enum: [low, medium, high, critical] }
        patient_id: { type: string, format: uuid }
        request_purpose: { $ref: '#/components/schemas/RequestPurpose' }
