# P0 ENFORCEMENT EXTENSION
# Authority: S. Jason Prohaska (AC-1)
# Version: 3.1
# Purpose: Eliminate skip-drift by making coverage mathematically provable
# Status: BINDING

schema: arcanum.p0.enforcement.v3.1
extends: arcanum.exec.contract.v3

meta:
  created: "2026-01-18T18:45:00+01:00"
  author: "CT-MIRROR"
  authority: "AC-1"
  purpose: "Close the gap between intent and agent execution"

# ============================================================================
# SECTION 1: EXPLICIT COVERAGE KEYS
# ============================================================================
# Every registry MUST declare its coverage keys.
# These are the atomic units that MUST appear in the ledger.

coverage_keys:
  systems:
    source: "SYSTEM_MAP.json"
    key_field: "system_id"
    required_count: null  # To be filled from source
    verification: "count(ledger.systems) == count(source.systems)"

  surfaces:
    source: "SURFACE_REGISTRY.md"
    key_field: "surface_id"
    required_count: null
    verification: "count(ledger.surfaces) == count(source.surfaces)"

  domains:
    source: "DOMAIN_TO_SYSTEM_BINDINGS.json"
    key_field: "domain"
    required_count: null
    verification: "count(ledger.domains) == count(source.domains)"
    includes:
      - ethraeon_ai_subdomains
      - legacy_1984_is
      - ethraeon_systems

  breadcrumbs:
    source: "page_scan"
    key_field: "breadcrumb_id"
    required_count: null
    verification: "count(ledger.breadcrumbs) == count(scan.breadcrumbs)"

  people:
    source: "PEOPLE_REGISTRY.json"
    key_field: "person_id"
    required_count: null
    verification: "count(ledger.people) == count(source.people)"

# ============================================================================
# SECTION 2: ENUMERATED OBJECT COUNTS
# ============================================================================
# Before P0 completes, these counts MUST be locked.
# Any discrepancy = STOP.

enumeration_requirements:
  pre_execution:
    - action: "Count all systems in SYSTEM_MAP.json"
      output: "counts.systems_declared"

    - action: "Count all surfaces in SURFACE_REGISTRY.md"
      output: "counts.surfaces_declared"

    - action: "Count all domains in bindings + Cloudflare + legacy"
      output: "counts.domains_declared"

    - action: "Count all breadcrumb blocks in all pages"
      output: "counts.breadcrumbs_declared"

    - action: "Count all people in scope"
      output: "counts.people_declared"

  post_ledger:
    - action: "Count LIVE items in ledger"
      output: "counts.live"

    - action: "Count STAGED items in ledger"
      output: "counts.staged"

    - action: "Count DORMANT items in ledger"
      output: "counts.dormant"

    - action: "Count BLOCKED items in ledger"
      output: "counts.blocked"

  proof_equation: |
    FOR EACH category IN [systems, surfaces, domains, breadcrumbs, people]:
      counts.{category}_declared ==
        counts.{category}.live +
        counts.{category}.staged +
        counts.{category}.dormant +
        counts.{category}.blocked

    IF ANY mismatch:
      EMIT DISCREPANCY_REPORT.md
      SET status = BLOCKED
      STOP

# ============================================================================
# SECTION 3: FAIL-CLOSED RULES
# ============================================================================
# These rules cannot be reasoned around.

fail_closed_rules:
  - id: "FC-01"
    name: "No Proceed Without Proof"
    rule: "P1 CANNOT start until P0_PROOF_PASS == true"
    enforcement: "Gate check before any P1 operation"
    bypass: "NONE"

  - id: "FC-02"
    name: "No Partial Ledger"
    rule: "Ledger must contain ALL items from ALL sources"
    enforcement: "Count verification equation"
    bypass: "NONE"

  - id: "FC-03"
    name: "No Silent Skip"
    rule: "Every item not LIVE must have explicit status + reason"
    enforcement: "Status field required for all ledger entries"
    bypass: "NONE"

  - id: "FC-04"
    name: "No Reasoning Around Blocks"
    rule: "BLOCKED status requires BLOCKER.md entry"
    enforcement: "Ledger validation rejects BLOCKED without blocker file"
    bypass: "NONE"

  - id: "FC-05"
    name: "No Legacy Dropout"
    rule: "1984.IS domains must appear in ledger with explicit status"
    enforcement: "Domain enumeration includes legacy namespace"
    bypass: "NONE"

  - id: "FC-06"
    name: "No Breadcrumb Ignorance"
    rule: "Every breadcrumb block must be in ledger"
    enforcement: "Breadcrumb scan count == ledger breadcrumb count"
    bypass: "NONE"

  - id: "FC-07"
    name: "No GUI Deferral"
    rule: "P3 GUI must exist and reflect ledger truth"
    enforcement: "S6_HANDOFF_CERTIFICATE.md required for P4"
    bypass: "NONE"

# ============================================================================
# SECTION 4: P0 COMPLETION GATE
# ============================================================================

p0_completion_gate:
  required_artifacts:
    - path: "RUN65/LEDGER/RUN65_COMPLETENESS_LEDGER.json"
      validation: "JSON schema valid + counts match"

    - path: "RUN65/LEDGER/RUN65_SOURCE_INDEX.json"
      validation: "All source files enumerated"

    - path: "RUN65/LEDGER/RUN65_BREADCRUMB_INDEX.json"
      validation: "All breadcrumb blocks indexed"

    - path: "RUN65/LEDGER/RUN65_COUNT_PROOF.json"
      validation: "Proof equation passes"

    - path: "RUN65/CERTS/P0_COMPLETION_CERTIFICATE.md"
      validation: "All gates passed, signed"

  gate_check_command: |
    # Verify P0 completion
    jq '.proof_passed' RUN65/LEDGER/RUN65_COUNT_PROOF.json | grep -q 'true'
    if [ $? -ne 0 ]; then
      echo "P0 GATE FAILED: Proof equation did not pass"
      exit 1
    fi
    echo "P0 GATE PASSED: Proceeding to P1"

  on_failure:
    action: "STOP"
    output: "P0_GATE_FAILURE.md"
    message: "Cannot proceed to P1. Ledger incomplete or proof failed."

# ============================================================================
# SECTION 5: ANTI-LOOP ENFORCEMENT
# ============================================================================

anti_loop:
  definition: "A loop is any cycle that replans without producing new artifacts"

  detection:
    - "Same files edited twice without new ledger entry"
    - "Same command run twice without artifact change"
    - "Plan regenerated without completing prior plan"

  prevention:
    - name: "Artifact Delta Required"
      rule: "Every response must change repo state OR create BLOCKER.md"

    - name: "No Replan Without Abort"
      rule: "New plan requires ABORT_CERTIFICATE for prior plan"

    - name: "Progress Monotonic"
      rule: "Ledger completion count must increase or explain why not"

  on_loop_detected:
    action: "STOP"
    output: "LOOP_DETECTED.md"
    contents:
      - "Last artifact produced"
      - "Last command run"
      - "Reason loop was detected"
      - "Required action to break loop"

# ============================================================================
# SECTION 6: LEGACY DOMAIN EXPLICIT HANDLING
# ============================================================================

legacy_domains:
  namespace: "1984.is"
  requirement: "All legacy domains must be explicitly handled"

  handling_options:
    LIVE:
      definition: "Domain resolves to functional content"
      requirements:
        - "index.html exists"
        - "Content is not placeholder"
        - "Navigation works"

    DORMANT:
      definition: "Domain resolves to placeholder"
      requirements:
        - "index.html exists"
        - "Placeholder indicates dormant status"
        - "No broken links from placeholder"

    REDIRECT:
      definition: "Domain redirects to canonical"
      requirements:
        - "301 redirect configured"
        - "Target is LIVE"

    BLOCKED:
      definition: "Cannot be resolved"
      requirements:
        - "BLOCKER.md entry"
        - "Reason documented"
        - "Next action specified"

  enumeration_source: "Cloudflare DNS + DOMAIN_TO_SYSTEM_BINDINGS.json"

# ============================================================================
# SECTION 7: BREADCRUMB EXECUTION CONTRACT
# ============================================================================

breadcrumb_contract:
  definition: "Breadcrumbs are machine-readable build orders, not documentation"

  format:
    identifier: "<!-- BREADCRUMB: ... -->"
    required_fields:
      - "action"
      - "target"
      - "spec"

  execution_rules:
    - "Every breadcrumb MUST be in ledger"
    - "Every breadcrumb MUST be executed or BLOCKED"
    - "Execution follows spec exactly (no invention)"
    - "Missing dependencies = BLOCKED (not skipped)"

  verification:
    - "Breadcrumb count in pages == Breadcrumb count in ledger"
    - "Executed count + Blocked count == Total count"

# ============================================================================
# END OF P0 ENFORCEMENT EXTENSION
# ============================================================================
