openapi: 3.1.0
info:
  title: OHN-16 Authorization and Policy Decision Engine
  version: 1.3.0
  description: >
    Deny-by-default policy decision point and policy release workflow. Built and tested with synthetic data.
    Real-patient use requires accepted OHN-01/05/06/15/38 integrations and the legal, privacy and safety
    authorizations named in governance/IMPLEMENTATION_AUTHORIZATION.md.
jsonSchemaDialect: https://json-schema.org/draft/2020-12/schema
servers:
  - url: /
security:
  - bearerAuth: []
tags:
  - { name: Decisions, description: OHN-16-API-001 and the decision explorer }
  - { name: Policies, description: 'OHN-16-API-002, OHN-16-API-003 and the release workflow OHN-16-W001' }
  - { name: Fixtures }
  - { name: Relationships }
  - { name: Emergency, description: OHN-16-W003 }
  - { name: Reports }
  - { name: Support }
  - { name: Marks, description: 'Enforcement of the watermark_export obligation: document marks (security watermark) for printouts and exports, and their public verification' }
paths:
  /api/ohn/v1/access/marks:
    post:
      operationId: ohn16_issueMark
      x-inventory-id: OHN-16-API-010
      tags: [Marks]
      description: >
        Asked before every printout or export. Evaluates an "export" of the named resource against the published policy and answers whether the security
        watermark is required: when the permit carries the obligation watermark_export, and also when the decision is deny or indeterminate or cannot be
        evaluated at all (fail closed). Only a permit without the obligation answers required=false. A required mark carries a verification code that
        /access/marks/verify resolves, an Ed25519 signature over the canonical payload, and the OHN-45 registration when OHN-45 answers. Facilities cannot
        switch the mark off. Roles: document_printer (purpose document_marking; actor and facility are the token's own) and policy_enforcement_point
        (purpose access_enforcement; marks a download on behalf of actor_ref at facility_ref, used by the platform export filter).
        1.2.0 (2026-11-23): role inspection_printer (purpose inspection_marking), the read-only inspector's print-only grant, marks like a
        document_printer; its mark is always issued (basis inspection_copy when the policy would not require it) and the text ends with
        " · INSPECTION COPY". The enforcement point asks for the same with inspection_copy=true (the platform export filter does so for an
        inspector's download). The public verification answers inspection_copy.
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/MarkCommand' } } } }
      responses:
        '200': { description: No mark required (permit without watermark_export), content: { application/json: { schema: { $ref: '#/components/schemas/MarkResult' } } } }
        '201': { description: Mark required and issued, content: { application/json: { schema: { $ref: '#/components/schemas/MarkResult' } } } }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Forbidden' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
        '429': { $ref: '#/components/responses/RateLimited' }
  /api/ohn/v1/access/marks/verify:
    post:
      operationId: ohn16_verifyMark
      x-inventory-id: OHN-16-API-011
      tags: [Marks]
      security: []
      description: >
        Public check of the verification code printed in a security watermark (no sign-in, rate limited per address). Answers only what the paper shows:
        who printed it, the facility code, when, the channel and the title. result is valid (local signature and OHN-45 agree), unverified (recorded but
        unsigned, unregistered or OHN-45 unavailable) or invalid. Every check is recorded.
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/MarkVerifyCommand' } } } }
      responses:
        '200': { description: The recorded mark, content: { application/json: { schema: { $ref: '#/components/schemas/MarkVerification' } } } }
        '404': { $ref: '#/components/responses/NotFound' }
        '422': { $ref: '#/components/responses/Validation' }
        '429': { $ref: '#/components/responses/RateLimited' }
  /api/ohn/v1/access/decisions:
    post:
      operationId: ohn16_decideAccess
      x-inventory-id: OHN-16-API-001
      tags: [Decisions]
      description: >
        Evaluates a request against the published policy and records the decision, obligations, workflow trail and
        audit receipt. A deny or indeterminate result is a successful evaluation (HTTP 200); a permit is HTTP 201.
        Callers must treat indeterminate as deny and must not cache decisions (cache.ttl_seconds is 0).
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/DecisionRequest' } } } }
      responses:
        '200': { description: Denied or indeterminate, content: { application/json: { schema: { $ref: '#/components/schemas/DecisionResult' } } } }
        '201': { description: Permitted, content: { application/json: { schema: { $ref: '#/components/schemas/DecisionResult' } } } }
        '400': { $ref: '#/components/responses/BadRequest' }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Forbidden' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
        '429': { $ref: '#/components/responses/RateLimited' }
        '503': { $ref: '#/components/responses/Unavailable' }
    get:
      operationId: ohn16_listDecisions
      tags: [Decisions]
      parameters:
        - { name: decision, in: query, schema: { enum: [permit, deny, indeterminate] } }
        - { name: resource_type, in: query, schema: { type: string, maxLength: 64 } }
        - { name: simulated, in: query, schema: { enum: ['true', 'false'] } }
        - { $ref: '#/components/parameters/Cursor' }
      responses:
        '200': { description: 'Page of decisions, newest first; patient references are masked', content: { application/json: { schema: { $ref: '#/components/schemas/Page' } } } }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Forbidden' }
        '422': { $ref: '#/components/responses/Validation' }
        '429': { $ref: '#/components/responses/RateLimited' }
  /api/ohn/v1/access/decisions/{id}:
    get:
      operationId: ohn16_getDecision
      tags: [Decisions]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses:
        '200': { description: 'Decision with trace, obligations and workflow history', content: { application/json: { schema: { $ref: '#/components/schemas/DecisionResult' } } } }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
  /api/ohn/v1/policies:
    get:
      operationId: ohn16_listPolicies
      x-inventory-id: OHN-16-API-002
      tags: [Policies]
      parameters:
        - { name: state, in: query, schema: { enum: [draft, peer_review, simulation_passed, dual_approved, published, deprecated, revoked] } }
        - { $ref: '#/components/parameters/Cursor' }
      responses:
        '200': { description: Page of policy bundles in the caller's jurisdiction and organization, content: { application/json: { schema: { $ref: '#/components/schemas/Page' } } } }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Forbidden' }
        '422': { $ref: '#/components/responses/Validation' }
        '429': { $ref: '#/components/responses/RateLimited' }
    post:
      operationId: ohn16_createPolicyDraft
      tags: [Policies]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/BundleCreateCommand' } } } }
      responses:
        '201': { description: Draft created, content: { application/json: { schema: { $ref: '#/components/schemas/BundleWithReceipt' } } } }
        '400': { $ref: '#/components/responses/BadRequest' }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/policies/{id}:
    get:
      operationId: ohn16_getPolicy
      tags: [Policies]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses:
        '200': { description: Bundle with rules and release history, content: { application/json: { schema: { $ref: '#/components/schemas/Bundle' } } } }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
  /api/ohn/v1/policies/{id}/diff:
    get:
      operationId: ohn16_diffPolicy
      tags: [Policies]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses:
        '200': { description: Rule and setting differences against the superseded or currently published bundle, content: { application/json: { schema: { type: object, required: [bundle, base, added, removed, unchanged, settings_changed, widening] } } } }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
  /api/ohn/v1/policies/{id}/rules:
    post:
      operationId: ohn16_addPolicyRule
      x-inventory-id: OHN-16-F001-SUBMIT
      tags: [Policies]
      description: Adds a rule to a draft. Only the author may edit; rules are immutable once the bundle leaves draft (also enforced by a database trigger).
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/RuleCommand' } } } }
      responses:
        '201': { description: Rule added, content: { application/json: { schema: { type: object, required: [rule, bundle, receipt] } } } }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/policies/{id}/rules/{ruleId}/remove:
    post:
      operationId: ohn16_removePolicyRule
      tags: [Policies]
      parameters: [{ $ref: '#/components/parameters/Id' }, { name: ruleId, in: path, required: true, schema: { type: string, format: uuid } }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/VersionedCommand' } } } }
      responses:
        '200': { description: Rule removed, content: { application/json: { schema: { type: object, required: [bundle, receipt] } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/policies/{id}/submit:
    post:
      operationId: ohn16_submitPolicy
      tags: [Policies]
      description: draft -> peer_review. Refused (422 domain_refusal) for a bundle without rules.
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/VersionedCommand' } } } }
      responses:
        '200': { description: Submitted, content: { application/json: { schema: { $ref: '#/components/schemas/BundleWithReceipt' } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/policies/{id}/run-fixtures:
    post:
      operationId: ohn16_runPolicyFixtures
      tags: [Policies, Fixtures]
      description: >
        Runs every active fixture. In peer_review a clean run advances the bundle to simulation_passed and records the run
        as test evidence. Refused (422 negative_tests_missing) unless at least one fixture expects deny or indeterminate.
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/VersionedCommand' } } } }
      responses:
        '200': { description: Run recorded, content: { application/json: { schema: { type: object, required: [run_id, bundle, total, passed, failed, negative_fixtures, advanced, results, receipt] } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/policies/{id}/simulate:
    post:
      operationId: ohn16_simulatePolicy
      x-inventory-id: OHN-16-F002-SUBMIT
      tags: [Policies]
      description: Evaluates a request against the named bundle in any state. Recorded with simulated=true; never creates a grant, exception or access.denied event.
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/DecisionRequest' } } } }
      responses:
        '200': { description: Simulated decision, content: { application/json: { schema: { $ref: '#/components/schemas/DecisionResult' } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '422': { $ref: '#/components/responses/Validation' }
        '429': { $ref: '#/components/responses/RateLimited' }
  /api/ohn/v1/policies/{id}/approve:
    post:
      operationId: ohn16_approvePolicy
      tags: [Policies]
      description: Records one of two approvals on a simulation_passed bundle. The author can never approve and one reviewer cannot give both (403 dual_control_violation).
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ApprovalCommand' } } } }
      responses:
        '200': { description: Approval recorded, content: { application/json: { schema: { $ref: '#/components/schemas/BundleWithReceipt' } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/policies/{id}/reject:
    post:
      operationId: ohn16_rejectPolicy
      tags: [Policies]
      description: Returns a bundle in review to draft and clears its evidence and approvals.
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ReasonedCommand' } } } }
      responses:
        '200': { description: Returned to draft, content: { application/json: { schema: { $ref: '#/components/schemas/BundleWithReceipt' } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/policies/{id}/publish:
    post:
      operationId: ohn16_publishPolicy
      x-inventory-id: OHN-16-API-003
      x-form-id: OHN-16-F003-SUBMIT
      tags: [Policies]
      description: >
        dual_approved -> published. The publisher restates the evidence; anything that differs from the recorded regression
        run and approvals is refused. The previous published bundle becomes deprecated and is recorded as rollback_ref.
        Emits policy.published and access.policy-version.changed through the transactional outbox.
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/PublishCommand' } } } }
      responses:
        '200': { description: Published; downstream delivery is pending confirmation, content: { application/json: { schema: { $ref: '#/components/schemas/BundleWithReceipt' } } } }
        '400': { $ref: '#/components/responses/BadRequest' }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
        '429': { $ref: '#/components/responses/RateLimited' }
        '503': { $ref: '#/components/responses/Unavailable' }
  /api/ohn/v1/policies/{id}/revoke:
    post:
      operationId: ohn16_revokePolicy
      tags: [Policies]
      description: published -> revoked. Nothing replaces the bundle automatically, so every decision denies (no_published_policy) until another bundle is published.
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ReasonedCommand' } } } }
      responses:
        '200': { description: Revoked, content: { application/json: { schema: { $ref: '#/components/schemas/BundleWithReceipt' } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/access/fixtures:
    get:
      operationId: ohn16_listFixtures
      tags: [Fixtures]
      parameters: [{ $ref: '#/components/parameters/Cursor' }]
      responses:
        '200': { description: Page of fixtures, content: { application/json: { schema: { $ref: '#/components/schemas/Page' } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
    post:
      operationId: ohn16_createFixture
      tags: [Fixtures]
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/FixtureCommand' } } } }
      responses:
        '201': { description: Fixture created, content: { application/json: { schema: { type: object, required: [id, name, expected_decision, status, version, receipt] } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/access/fixtures/{id}/retire:
    post:
      operationId: ohn16_retireFixture
      tags: [Fixtures]
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/VersionedCommand' } } } }
      responses:
        '200': { description: 'Fixture retired', content: { application/json: { schema: { type: object } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
  /api/ohn/v1/access/fixture-runs:
    get:
      operationId: ohn16_listFixtureRuns
      tags: [Fixtures]
      parameters: [{ $ref: '#/components/parameters/Release' }, { $ref: '#/components/parameters/ResourceTypeFilter' }, { $ref: '#/components/parameters/Cursor' }]
      responses:
        '200': { description: Same content as report OHN-16-R002, content: { application/json: { schema: { $ref: '#/components/schemas/Report' } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
  /api/ohn/v1/access/relationships:
    get:
      operationId: ohn16_listRelationships
      tags: [Relationships]
      parameters: [{ $ref: '#/components/parameters/Cursor' }]
      responses:
        '200': { description: Page of relationship assertions; patient references are masked, content: { application/json: { schema: { $ref: '#/components/schemas/Page' } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
    post:
      operationId: ohn16_registerRelationship
      tags: [Relationships]
      description: >
        Registers a time-bounded clinical relationship (role relationship_registrar). Until OHN-06 integration this is the only source of relationship facts.
        The 201 body is the assertion (id, version, status, valid_from, expires_at, evidence_ref, asserted_by, receipt); a caller keeps id and version to revoke it.
        OHN-32 registers with its service identity as relationship_registrar (see its ADRs).
        1.3.0 (owner decision 2026-11-29): role care_team_registrar, purpose relationship_registration, the OHN-61 triage service identity, in the organisation scope of the
        ticket's facility. It may register only relationship_type care_team (another type answers 403 forbidden, details.reason relationship_type_not_permitted) and only
        with expires_at at most 72 hours after valid_from (more answers 422 domain_validation_failed, details.fieldErrors.expires_at). It implies no other action (no decisions, no reads).
        OHN-61 registers one care_team assertion per receiving clinician, when the clinician takes up the ticket (call, start of consultation, handover acknowledgement, summary) or when the
        handover names them: actor_ref = the clinician's OHN-06 practitioner id (from their own token),
        patient_ref = the triage patient, facility_ref = the ticket's facility, evidence_ref = the OHN-61 ticket id, valid_from = now - 1 minute,
        expires_at = valid_from + the facility's OHN-61 care_team_relationship_hours (default 24). Audited here as relationship.register.
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/RelationshipCommand' } } } }
      responses:
        '201': { description: 'Assertion registered', content: { application/json: { schema: { type: object } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/access/relationships/{id}/revoke:
    post:
      operationId: ohn16_revokeRelationship
      tags: [Relationships]
      description: >
        Revokes an assertion (role relationship_registrar; 1.3.0: also care_team_registrar, for assertions it registered itself only: asserted_by must be the
        caller, otherwise 404 not_found, never disclosing the assertion). expected_version is the assertion's current version; a second revoke answers
        409 already_revoked, which a service caller such as OHN-61 treats as done. Audited as relationship.revoke.
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ReasonedCommand' } } } }
      responses:
        '200': { description: 'Assertion revoked; later decisions deny with relationship_revoked', content: { application/json: { schema: { type: object } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
  /api/ohn/v1/access/emergency-exceptions:
    get:
      operationId: ohn16_listEmergencyExceptions
      tags: [Emergency]
      parameters: [{ $ref: '#/components/parameters/Cursor' }]
      responses:
        '200': { description: Page of emergency exceptions, content: { application/json: { schema: { $ref: '#/components/schemas/Page' } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
  /api/ohn/v1/access/emergency-exceptions/{id}/review:
    post:
      operationId: ohn16_reviewEmergencyException
      tags: [Emergency]
      description: bounded_grant -> post_review. The reviewer must not be the person the grant was for.
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ExceptionReviewCommand' } } } }
      responses:
        '200': { description: 'Review recorded', content: { application/json: { schema: { type: object } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/access/vocabulary:
    get:
      operationId: ohn16_getVocabulary
      tags: [Support]
      responses:
        '200': { description: Controlled vocabularies used by the forms (ADR-0001), content: { application/json: { schema: { type: object, required: [resource_types, actions, purposes, policy_effects, obligation_types, bundle_states, decisions, subject_attributes, resource_attributes, emergency_grant_max_minutes] } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
  /api/ohn/v1/access/context:
    get:
      operationId: ohn16_getCallerContext
      tags: [Support]
      responses:
        '200': { description: "The caller's tenant, role and permitted actions", content: { application/json: { schema: { type: object, required: [principal_id, role, purpose, jurisdiction_id, organization_id, can, dependency_mode] } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
  /api/ohn/v1/access/reports/{reportId}:
    get:
      operationId: ohn16_getReport
      tags: [Reports]
      description: >
        OHN-16-R001 decision distribution (filters country, facility, from, to, action), OHN-16-R002 authorization regression
        (release, resource_type), OHN-16-R003 privileged policy changes (from, to, author). A country other than the caller's
        returns no rows. Metric formulas are in modules/OHN-16/adr/0005-report-formulas.md.
      parameters:
        - { $ref: '#/components/parameters/ReportId' }
        - { name: country, in: query, schema: { type: string, format: uuid } }
        - { name: facility, in: query, schema: { type: string, format: uuid } }
        - { name: from, in: query, schema: { type: string, format: date-time } }
        - { name: to, in: query, schema: { type: string, format: date-time } }
        - { name: action, in: query, schema: { type: string, maxLength: 64 } }
        - { name: author, in: query, schema: { type: string, format: uuid } }
        - { $ref: '#/components/parameters/Release' }
        - { $ref: '#/components/parameters/ResourceTypeFilter' }
        - { $ref: '#/components/parameters/Cursor' }
      responses:
        '200': { description: Report, content: { application/json: { schema: { $ref: '#/components/schemas/Report' } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/access/reports/{reportId}/export:
    post:
      operationId: ohn16_createExport
      tags: [Reports]
      description: Creates a signed CSV or PDF export, audited, retrievable only by the same principal for five minutes.
      parameters: [{ $ref: '#/components/parameters/ReportId' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ExportCommand' } } } }
      responses:
        '201': { description: Export ready, content: { application/json: { schema: { type: object, required: [export_id, report_id, format, rows, expires_at, download_path, receipt] } } } }
        '403': { $ref: '#/components/responses/Forbidden' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/access/exports/{token}:
    get:
      operationId: ohn16_downloadExport
      tags: [Reports]
      parameters: [{ name: token, in: path, required: true, schema: { type: string, pattern: '^[A-Za-z0-9_-]{20,64}$' } }]
      responses:
        '200': { description: The export file; every download is audited, content: { application/json: { schema: { type: object } } }, x-media-types: [text/csv, application/pdf] }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Forbidden' }
        '404': { $ref: '#/components/responses/NotFound' }
        '410': { description: Export link expired, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
        '500': { description: 'Export failed its integrity check (export_integrity_failed)', content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
  /api/ohn/v1/access/synthetic/actors:
    post:
      operationId: ohn16_seedSyntheticActor
      tags: [Support]
      description: Seeds the OHN-05/06 test fake. Returns 404 unless the service runs with synthetic dependencies.
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/SyntheticActorCommand' } } } }
      responses:
        '201': { description: 'Fake actor stored in memory', content: { application/json: { schema: { type: object } } } }
        '404': { $ref: '#/components/responses/NotFound' }
  /api/ohn/v1/access/synthetic/consents:
    post:
      operationId: ohn16_seedSyntheticConsent
      tags: [Support]
      description: Seeds the OHN-15 test fake. Returns 404 unless the service runs with synthetic dependencies.
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/SyntheticConsentCommand' } } } }
      responses:
        '201': { description: 'Fake consent stored in memory', content: { application/json: { schema: { type: object } } } }
        '404': { $ref: '#/components/responses/NotFound' }
components:
  securitySchemes:
    bearerAuth: { type: http, scheme: bearer, bearerFormat: JWT, description: 'Gateway token (ES256 in the Java build, HS256 in the TypeScript reference) carrying sub, role, jurisdiction, organization_id and purpose (TokenVerifier 1.0.0, ADR-0003).' }
  parameters:
    Id: { name: id, in: path, required: true, schema: { type: string, format: uuid } }
    ReportId: { name: reportId, in: path, required: true, schema: { enum: [OHN-16-R001, OHN-16-R002, OHN-16-R003] } }
    Cursor: { name: cursor, in: query, schema: { type: string, maxLength: 64 }, description: Opaque cursor from next_cursor. }
    Release: { name: release, in: query, schema: { type: string, maxLength: 512 } }
    ResourceTypeFilter: { name: resource_type, in: query, schema: { type: string, maxLength: 64 } }
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: true
      schema: { type: string, minLength: 8, maxLength: 128, pattern: '^[A-Za-z0-9_.:-]+$' }
      description: Same key and payload replays the stored response (Idempotent-Replay true); same key with a different payload is 409.
  responses:
    BadRequest: { description: Malformed request or missing Idempotency-Key, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Unauthenticated: { description: 'Missing, invalid or expired bearer token', content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Forbidden: { description: 'Denied by role, purpose of use, tenant scope or dual control. The denial is audited.', content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    NotFound: { description: Not found in the caller's scope (also returned for unknown routes), content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Conflict: { description: 'Stale expected_version, invalid state transition, or idempotency key reuse. No partial change is made.', content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Validation: { description: Field validation failure or domain refusal; details names the fields, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    RateLimited: { description: Per-principal rate limit exceeded, headers: { Retry-After: { schema: { type: integer } } }, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Unavailable: { description: Database or dependency unavailable; nothing was changed, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
  schemas:
    Error:
      type: object
      required: [error]
      description: >
        Machine code in `error` (unauthenticated, forbidden, not_found, domain_validation_failed, invalid_filter, version_conflict, invalid_transition, dual_control_violation, conflict, idempotency_key_required,
        idempotency_key_reused, request_in_progress, rate_limited, dependency_unavailable, ...). The Java service answers application/problem+json and adds type, title, status, detail and trace_id.
      properties:
        error: { type: string }
        message: { type: string }
        details:
          description: >
            422 domain_validation_failed: {formErrors: string[], fieldErrors: {field: string[]}} (zod flatten, unknown keys are a form error); 422 invalid_filter: the same shape;
            422 for a rule condition: {conditions: string[]}; 409 version_conflict: {current_version}; 409 invalid_transition: {state, allowed_from}; 503: none.
    Decision: { enum: [permit, deny, indeterminate] }
    BundleState: { enum: [draft, peer_review, simulation_passed, dual_approved, published, deprecated, revoked] }
    ResourceType: { enum: [patient_record, clinical_document, observation, medication, encounter, consent_record, patient_export, search_index, policy_bundle, jurisdiction_config, audit_log, report_export] }
    Action: { enum: [read, create, update, delete, export, search, share] }
    Purpose: { enum: [treatment, payment, operations, public_health, research, patient_access, emergency_treatment, administration] }
    PolicyEffect: { enum: [permit, deny, emergency_permit] }
    ObligationType: { enum: [audit_access, mask_fields, notify_patient, require_post_review, limit_scope, watermark_export] }
    Receipt:
      type: object
      required: [receipt_id, committed_at, aggregate_id, aggregate_type, version, status]
      properties: { receipt_id: { type: string, format: uuid }, committed_at: { type: string, format: date-time }, aggregate_id: { type: string, format: uuid }, aggregate_type: { type: string }, version: { type: integer, minimum: 1 }, status: { enum: [committed, denied, pending] } }
    Page:
      type: object
      required: [items, next_cursor]
      properties: { items: { type: array, items: { type: object } }, next_cursor: { type: [string, 'null'] } }
    ApprovalCommand: { type: object, additionalProperties: false, required: [expected_version], properties: { expected_version: { type: integer, minimum: 1 }, comment: { type: string, maxLength: 2048 } } }
    ExceptionReviewCommand: { type: object, additionalProperties: false, required: [expected_version, outcome, reason], properties: { expected_version: { type: integer, minimum: 1 }, outcome: { enum: [justified, not_justified] }, reason: { type: string, minLength: 10, maxLength: 4096 } } }
    ExportCommand: { type: object, additionalProperties: false, required: [format], properties: { format: { enum: [csv, pdf] }, filters: { type: object, additionalProperties: { type: string, maxLength: 128 } } } }
    SyntheticActorCommand: { type: object, additionalProperties: false, required: [actor_ref, roles, facility_refs], properties: { actor_ref: { type: string, format: uuid }, roles: { type: array, maxItems: 20, items: { type: string, maxLength: 64 } }, facility_refs: { type: array, maxItems: 50, items: { type: string, format: uuid } }, status: { enum: [active, suspended] } } }
    SyntheticConsentCommand: { type: object, additionalProperties: false, required: [patient_ref, purpose, status], properties: { patient_ref: { type: string, format: uuid }, purpose: { $ref: '#/components/schemas/Purpose' }, status: { enum: [granted, withdrawn, unknown] } } }
    MarkCommand:
      type: object
      additionalProperties: false
      required: [channel, resource_type, resource_ref, purpose]
      properties:
        channel: { enum: [print, csv, pdf, json] }
        resource_type: { $ref: '#/components/schemas/ResourceType' }
        resource_ref: { type: string, format: uuid }
        patient_ref: { type: string, format: uuid, x-classification: restricted }
        purpose: { $ref: '#/components/schemas/Purpose' }
        facility_code: { type: string, pattern: '^[A-Z]{3}-[A-Z]{2}-[0-9A-Z]{5}$', description: The OHN-05 facility code printed in the mark, for example PHA-CM-7K4QX. }
        document_title: { type: string, minLength: 1, maxLength: 160 }
        document_sha256: { type: string, pattern: '^[0-9a-f]{64}$' }
        actor_ref: { type: string, format: uuid, description: policy_enforcement_point only (required there); a document_printer is always the token subject. }
        actor_name: { type: string, minLength: 1, maxLength: 120, description: policy_enforcement_point only; a document_printer's name is the attested display_name of the token. }
        facility_ref: { type: string, format: uuid, description: policy_enforcement_point only (required there); a document_printer is always marked at the token's organization. }
        inspection_copy: { type: boolean, description: "policy_enforcement_point only (1.2.0): the download is a read-only inspector's copy; the mark is always issued and says INSPECTION COPY" }
    MarkResult:
      type: object
      required: [required, basis]
      properties:
        required: { type: boolean }
        basis: { enum: [policy_obligation, fail_closed, no_obligation, inspection_copy] }
        basis_reason: { type: string }
        decision: { type: [string, 'null'] }
        decision_id: { type: [string, 'null'], format: uuid }
        receipt: { $ref: '#/components/schemas/Receipt' }
        event_id: { type: string, format: uuid }
        mark:
          type: object
          required: [id, verify_code, verify_path, channel, printed_by, printed_at, text, signature]
          properties:
            id: { type: string, format: uuid }
            verify_code: { type: string, pattern: '^[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}$' }
            verify_path: { type: string }
            channel: { enum: [print, csv, pdf, json] }
            printed_by: { type: string }
            facility_code: { type: [string, 'null'] }
            printed_at: { type: string, format: date-time }
            text: { type: string, description: '"Printed by <name>, <facility code>, <date-time> · COPY · Verify <code>"' }
            signature: { enum: [unsigned, signed_not_registered, signed_and_registered] }
    MarkVerifyCommand: { type: object, additionalProperties: false, required: [code], properties: { code: { type: string, minLength: 4, maxLength: 40 } } }
    MarkVerification:
      type: object
      required: [code, result, signature]
      properties:
        code: { type: string }
        result: { enum: [valid, unverified, invalid] }
        signature: { enum: [verified_by_ohn45, signed_not_registered, registry_unavailable, unsigned, invalid] }
        printed_by: { type: string }
        facility_code: { type: [string, 'null'] }
        printed_at: { type: string, format: date-time }
        channel: { enum: [print, csv, pdf, json] }
        document_title: { type: [string, 'null'] }
        copy: { type: boolean }
        message: { type: string }
    DecisionRequest:
      description: OHN-16-F002. patient_ref is required for patient-data resource types; unknown resource types are treated as patient data.
      type: object
      additionalProperties: false
      required: [actor_ref, resource_ref, resource_type, action, facility_ref, purpose]
      properties:
        actor_ref: { type: string, format: uuid }
        patient_ref: { type: string, format: uuid, x-classification: restricted }
        resource_ref: { type: string, format: uuid }
        resource_type: { $ref: '#/components/schemas/ResourceType' }
        action: { $ref: '#/components/schemas/Action' }
        facility_ref: { type: string, format: uuid, description: A claim until the directory confirms the affiliation. }
        purpose: { $ref: '#/components/schemas/Purpose' }
        relationship_evidence: { type: string, format: uuid, x-classification: confidential }
        resource_facility_ref: { type: string, format: uuid }
        emergency:
          type: object
          additionalProperties: false
          required: [reason]
          properties: { reason: { type: string, minLength: 10, maxLength: 2048 } }
    DecisionResult:
      type: object
      required: [id, decision, reason_code, simulated, actor_ref, patient_ref_masked, resource_ref, resource_type, action, facility_ref, purpose, policy_bundle_id, policy_version, matched_rule_id, failed_clause, obligations, workflow_state, grant_expires_at, version, created_at]
      properties:
        id: { type: string, format: uuid }
        decision: { $ref: '#/components/schemas/Decision' }
        reason_code: { type: string }
        simulated: { type: boolean }
        patient_ref_masked: { type: [string, 'null'], description: Last four characters only. }
        policy_bundle_id: { type: [string, 'null'], format: uuid }
        policy_version: { type: [string, 'null'] }
        matched_rule_id: { type: [string, 'null'], format: uuid }
        failed_clause: { type: [string, 'null'] }
        obligations: { type: array, items: { type: object, required: [type, parameters], properties: { type: { $ref: '#/components/schemas/ObligationType' }, parameters: { type: object } } } }
        trace: { type: array, items: { type: object, required: [step, outcome, detail], properties: { step: { type: string }, outcome: { enum: [ok, deny, indeterminate, info] }, detail: { type: string } } } }
        workflow_state: { enum: [authenticated, context_resolved, country_policy_applied, permit_or_deny, obligations_recorded] }
        emergency_exception_id: { type: [string, 'null'], format: uuid }
        grant_expires_at: { type: [string, 'null'], format: date-time }
        receipt: { $ref: '#/components/schemas/Receipt' }
        event_id: { type: [string, 'null'], format: uuid }
        cache: { type: object, required: [ttl_seconds], properties: { ttl_seconds: { const: 0 } } }
    BundleCreateCommand:
      type: object
      additionalProperties: false
      required: [name, revision, change_summary]
      properties:
        name: { type: string, minLength: 3, maxLength: 120 }
        revision: { type: string, minLength: 1, maxLength: 512 }
        change_summary: { type: string, minLength: 10, maxLength: 4096 }
        emergency_grant_minutes: { type: integer, minimum: 1, maximum: 1440, description: No default. When absent, emergency_permit rules can never grant (ADR-0004). }
        consent_required_for_phi: { type: boolean, default: true }
        clone_from: { type: string, format: uuid }
    RuleCommand:
      description: OHN-16-F001
      type: object
      additionalProperties: false
      required: [jurisdiction_id, resource_type, action, purpose, subject_condition, resource_condition, policy_effect, expected_version]
      properties:
        jurisdiction_id: { type: string, format: uuid, description: Must equal the authenticated jurisdiction. }
        resource_type: { $ref: '#/components/schemas/ResourceType' }
        action: { $ref: '#/components/schemas/Action' }
        purpose: { $ref: '#/components/schemas/Purpose' }
        subject_condition: { type: string, minLength: 1, maxLength: 8192, description: Condition language of ADR-0002. }
        resource_condition: { type: string, minLength: 1, maxLength: 8192 }
        policy_effect: { $ref: '#/components/schemas/PolicyEffect' }
        obligations: { type: array, maxItems: 10, items: { type: object, additionalProperties: false, required: [type], properties: { type: { $ref: '#/components/schemas/ObligationType' }, parameters: { type: object, additionalProperties: { type: string, maxLength: 256 } } } } }
        expected_version: { type: integer, minimum: 1 }
    PublishCommand:
      description: OHN-16-F003
      type: object
      additionalProperties: false
      required: [policy_bundle_id, revision, test_evidence_ref, reviewer_a, reviewer_b, effective_at, expected_version]
      properties:
        policy_bundle_id: { type: string, format: uuid }
        revision: { type: string, minLength: 1, maxLength: 512 }
        test_evidence_ref: { type: string, format: uuid, x-classification: confidential }
        reviewer_a: { type: string, format: uuid }
        reviewer_b: { type: string, format: uuid }
        effective_at: { type: string, format: date-time, description: Between the second approval and now; scheduled activation is not supported. }
        expected_version: { type: integer, minimum: 1 }
    VersionedCommand:
      type: object
      additionalProperties: false
      required: [expected_version]
      properties: { expected_version: { type: integer, minimum: 1 } }
    ReasonedCommand:
      type: object
      additionalProperties: false
      required: [expected_version, reason]
      properties: { expected_version: { type: integer, minimum: 1 }, reason: { type: string, minLength: 10, maxLength: 4096 } }
    RelationshipCommand:
      type: object
      additionalProperties: false
      required: [actor_ref, patient_ref, facility_ref, relationship_type, valid_from, expires_at, evidence_ref]
      properties:
        actor_ref: { type: string, format: uuid }
        patient_ref: { type: string, format: uuid, x-classification: restricted }
        facility_ref: { type: string, format: uuid }
        relationship_type: { enum: [treating, consulting, care_team, administrative] }
        valid_from: { type: string, format: date-time }
        expires_at: { type: string, format: date-time, description: Must be after valid_from. Relationships are always time-bounded. }
        evidence_ref: { type: string, format: uuid }
    FixtureCommand:
      type: object
      additionalProperties: false
      required: [name, request, context, expected_decision]
      properties:
        name: { type: string, minLength: 3, maxLength: 200 }
        request: { $ref: '#/components/schemas/DecisionRequest' }
        context:
          type: object
          additionalProperties: false
          required: [actor, relationship, consent]
          properties:
            jurisdiction: { enum: [active, unavailable], default: active }
            actor: { type: [object, 'null'] }
            relationship: { type: [object, 'null'] }
            consent: { enum: [granted, withdrawn, unknown, unavailable] }
        expected_decision: { $ref: '#/components/schemas/Decision' }
    Bundle:
      type: object
      required: [id, name, revision, state, version, author_id, change_summary, emergency_grant_minutes, consent_required_for_phi, test_evidence_ref, reviewer_a, reviewer_b, effective_at, rollback_ref, jurisdiction_id]
      properties:
        id: { type: string, format: uuid }
        state: { $ref: '#/components/schemas/BundleState' }
        version: { type: integer, minimum: 1 }
        rules: { type: array, items: { type: object } }
        history: { type: array, items: { type: object } }
    BundleWithReceipt:
      allOf:
        - { $ref: '#/components/schemas/Bundle' }
        - { type: object, required: [receipt], properties: { receipt: { $ref: '#/components/schemas/Receipt' }, event_ids: { type: array, items: { type: string, format: uuid } }, delivery: { const: pending_downstream_confirmation } } }
    Report:
      type: object
      required: [report_id, title, columns, rows, next_cursor, summary, state, generated_at]
      properties:
        report_id: { enum: [OHN-16-R001, OHN-16-R002, OHN-16-R003] }
        columns: { type: array, items: { type: string } }
        rows: { type: array, maxItems: 100, items: { type: object } }
        next_cursor: { type: [string, 'null'] }
        summary: { type: object }
        state: { enum: [rendered, no_authorized_data] }
        generated_at: { type: string, format: date-time }
