{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://agla.local/schemas/agla-compilation-unit.schema.json",
  "title": "AGLA source-authoritative natural-language compilation unit",
  "type": "object",
  "additionalProperties": false,
  "required": ["contract_id","contract_version","unit_id","status","source_authority","controlled_specification","ir","capability_request","transformation_ledger","target_profile","result_class","upward_revisit_packet"],
  "properties": {
    "contract_id": {"const":"AGLA_NL_COMPILATION_UNIT"},
    "contract_version": {"type":"string","pattern":"^[0-9]+\\.[0-9]+\\.[0-9]+$"},
    "unit_id": {"type":"string","minLength":1},
    "status": {"const":"CANDIDATE_NON_RUNTIME_NO_AUTHORITY_EFFECT"},
    "source_authority": {
      "type":"object","additionalProperties":false,
      "required":["precedence","embedded_snapshot_policy","stages","source_refs"],
      "properties": {
        "precedence":{"const":["P0","P1","P2","MEDIATED_SPECIFICATION","IR","TARGET","EVIDENCE"]},
        "embedded_snapshot_policy":{"const":"QUALIFIED_NONAUTHORITATIVE_NEVER_OVERRIDES_SOURCE"},
        "stages":{"type":"array","minItems":3,"maxItems":3,"items":{"$ref":"#/$defs/sourceStage"}},
        "source_refs":{"type":"array","minItems":1,"items":{"$ref":"#/$defs/sourceRef"}}
      }
    },
    "controlled_specification": {
      "type":"object","additionalProperties":false,
      "required":["spec_id","statements","unresolved_questions","conflicts"],
      "properties": {
        "spec_id":{"type":"string","minLength":1},
        "statements":{"type":"array","minItems":1,"items":{"$ref":"#/$defs/statement"}},
        "unresolved_questions":{"type":"array","items":{"type":"string"},"uniqueItems":true},
        "conflicts":{"type":"array","items":{"type":"string"},"uniqueItems":true}
      }
    },
    "ir": {
      "type":"object","additionalProperties":false,
      "required":["ir_id","authority_effect","semantic_ids","typed_relations","provider_refs","graph_binding","handoff_refs","arithmetic_constraints","agent_package","inputs","expected_outputs","finite_loop"],
      "properties": {
        "ir_id":{"type":"string","minLength":1},
        "authority_effect":{"const":"NONE"},
        "semantic_ids":{"type":"array","minItems":1,"uniqueItems":true,"items":{"type":"string"}},
        "typed_relations":{"type":"array","items":{"$ref":"#/$defs/relation"}},
        "provider_refs":{"type":"array","items":{"type":"object"}},
        "graph_binding":{"type":["object","null"]},
        "handoff_refs":{"type":"array","items":{"type":"object"}},
        "arithmetic_constraints":{"type":"array","items":{"type":"object"}},
        "agent_package":{"$ref":"#/$defs/agentPackage"},
        "inputs":{"type":"array","minItems":1,"items":{"type":"object"}},
        "expected_outputs":{"type":"array","minItems":1,"items":{"type":"object"}},
        "finite_loop":{"$ref":"#/$defs/finiteLoop"}
      }
    },
    "capability_request":{"type":"array","minItems":1,"uniqueItems":true,"items":{"type":"string"}},
    "transformation_ledger":{"type":"array","minItems":1,"items":{"$ref":"#/$defs/transformation"}},
    "target_profile":{"type":"string","minLength":1},
    "result_class":{"const":"CANDIDATE_NON_EXECUTED"},
    "upward_revisit_packet":{"$ref":"#/$defs/upwardPacket"}
  },
  "$defs": {
    "sourceStage": {
      "type":"object","additionalProperties":false,
      "required":["stage_id","author_role","authority","content","sha256","recoverable","may_override_prior"],
      "properties": {
        "stage_id":{"enum":["P0","P1","P2"]},
        "author_role":{"enum":["HUMAN_MAINTAINER","EXPLICIT_INTERPRETER","OPERATIONAL_REWRITER"]},
        "authority":{"enum":["HUMAN_SOURCE","INTERPRETATION_RECORD","OPERATIONAL_REWRITE"]},
        "content":{"type":"string","minLength":1},
        "sha256":{"type":"string","pattern":"^[0-9a-f]{64}$"},
        "recoverable":{"const":true},
        "may_override_prior":{"const":false}
      }
    },
    "sourceRef": {
      "type":"object","additionalProperties":false,
      "required":["artifact_id","version","class","species","authority","use_mode","source_scope","freshness","ref","hash","prohibited_use"],
      "properties": {
        "artifact_id":{"type":"string"},"version":{"type":"string"},"class":{"type":"string"},"species":{"type":"string"},
        "authority":{"type":"string"},"use_mode":{"type":"string"},"source_scope":{"type":"string"},"freshness":{"type":"string"},
        "ref":{"type":"string"},"hash":{"type":"string"},"prohibited_use":{"type":"string"}
      }
    },
    "statement": {
      "type":"object","additionalProperties":false,
      "required":["statement_id","modality","subject","predicate","object","qualifiers","provenance_refs","resolution_state"],
      "properties": {
        "statement_id":{"type":"string"},"modality":{"enum":["MUST","MUST_NOT","MAY","QUESTION"]},
        "subject":{"type":"string"},"predicate":{"type":"string"},"object":{"type":"string"},
        "qualifiers":{"type":"array","items":{"type":"string"}},"provenance_refs":{"type":"array","minItems":1,"items":{"type":"string"}},
        "resolution_state":{"enum":["RESOLVED","UNRESOLVED","CONFLICT"]}
      }
    },
    "relation": {
      "type":"object","additionalProperties":false,"required":["relation_id","type","source","target"],
      "properties":{"relation_id":{"type":"string"},"type":{"type":"string"},"source":{"type":"string"},"target":{"type":"string"}}
    },
    "agentPackage": {
      "type":"object","additionalProperties":false,
      "required":["package_id","invocation_epoch","local_goal","read_scope","write_scope","graph_visibility","context_depth","delegation_limit","mutation_authority","may_execute_opera","may_self_admit","return_schema","escalation_rule","expiry"],
      "properties": {
        "package_id":{"type":"string"},"invocation_epoch":{"type":"string"},"local_goal":{"type":"string"},
        "read_scope":{"type":"array","minItems":1,"items":{"type":"string"}},"write_scope":{"type":"array","items":{"type":"string"}},
        "graph_visibility":{"type":"string"},"context_depth":{"type":"integer","minimum":0},"delegation_limit":{"type":"integer","minimum":0},
        "mutation_authority":{"type":"string"},"may_execute_opera":{"type":"boolean"},"may_self_admit":{"type":"boolean"},
        "return_schema":{"type":"string"},"escalation_rule":{"type":"string"},"expiry":{"type":"string"}
      }
    },
    "finiteLoop": {
      "type":"object","additionalProperties":false,
      "required":["loop_id","objective","max_iterations","max_unchanged_repetitions","max_escalations","progress_predicate","success_state","failure_state","exhaustion_state","retry_until_success","recoverable_checkpoint"],
      "properties": {
        "loop_id":{"type":"string"},"objective":{"type":"string"},"max_iterations":{"type":"integer","minimum":1},
        "max_unchanged_repetitions":{"type":"integer","minimum":0},"max_escalations":{"type":"integer","minimum":0},
        "progress_predicate":{"type":"string"},"success_state":{"type":"string"},"failure_state":{"type":"string"},
        "exhaustion_state":{"const":"SUSPENDED_FOR_REVIEW"},"retry_until_success":{"const":false},"recoverable_checkpoint":{"type":"string"}
      }
    },
    "transformation": {
      "type":"object","additionalProperties":false,
      "required":["record_id","source_stage","target_stage","rule_id","input_hashes","output_hash","assumptions","declared_loss","conflict_refs","actor","reversible"],
      "properties": {
        "record_id":{"type":"string"},"source_stage":{"type":"string"},"target_stage":{"type":"string"},"rule_id":{"type":"string"},
        "input_hashes":{"type":"array","minItems":1,"items":{"type":"string","pattern":"^[0-9a-f]{64}$"}},
        "output_hash":{"type":"string","pattern":"^[0-9a-f]{64}$"},"assumptions":{"type":"array","items":{"type":"string"}},
        "declared_loss":{"type":"array","items":{"type":"string"}},"conflict_refs":{"type":"array","items":{"type":"string"}},
        "actor":{"type":"string"},"reversible":{"const":true}
      }
    },
    "upwardPacket": {
      "type":"object","additionalProperties":false,
      "required":["packet_id","origin_stage","target_stage","triggering_evidence_refs","original_prompt_ref","affected_interpretation_refs","affected_source_refs","transformation_chain_refs","conflict_refs","test_or_playtest_refs","current_candidate_ref","proposed_revision","declared_loss","preserved_state_ref","authority_change_requested","human_decision_required"],
      "properties": {
        "packet_id":{"type":"string"},"origin_stage":{"type":"string"},"target_stage":{"type":"string"},
        "triggering_evidence_refs":{"type":"array","items":{"type":"string"}},"original_prompt_ref":{"type":"string"},
        "affected_interpretation_refs":{"type":"array","items":{"type":"string"}},"affected_source_refs":{"type":"array","items":{"type":"string"}},
        "transformation_chain_refs":{"type":"array","items":{"type":"string"}},"conflict_refs":{"type":"array","items":{"type":"string"}},
        "test_or_playtest_refs":{"type":"array","items":{"type":"string"}},"current_candidate_ref":{"type":"string"},
        "proposed_revision":{"type":"string"},"declared_loss":{"type":"array","items":{"type":"string"}},
        "preserved_state_ref":{"type":"string"},"authority_change_requested":{"type":"boolean"},"human_decision_required":{"type":"boolean"}
      }
    }
  }
}
