{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aisa.ablera.com/contracts/write-shape.schema.json",
  "title": "Write shape and write packet (platform seam 5 of 5)",
  "description": "Gating § 4 (the shape a person approves at HW-approve — an operation, a template with its parameters declared, expected counts as a function of the plan, assertions, a derived teardown, permitted case types) and Gating § 9.1 (the closed-form packet a stage agent hands up for one instance — shape id, every parameter bound, expected counts as numbers, effect class per step, the derived teardown, assertions and their replay, the grant, the artefact hash). The write auditor's four checks (Gating § 6) are stated against these two objects.",
  "oneOf": [ { "$ref": "#/$defs/WriteShape" }, { "$ref": "#/$defs/WritePacket" } ],
  "$defs": {
    "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
    "param_name": { "type": "string", "pattern": "^[a-z][a-z0-9_]*$" },
    "WriteShape": {
      "type": "object", "additionalProperties": false,
      "required": ["object", "shape_key", "version", "module", "customer_code", "operation_id", "connector", "template", "parameters", "target_class", "effect_class", "write_class", "expected_counts", "assertions", "teardown_template", "case_types", "state"],
      "properties": {
        "object": { "const": "write_shape" },
        "shape_key": { "type": "string", "pattern": "^[a-z][a-z0-9_.\\-]+$" },
        "version": { "type": "integer", "minimum": 1 },
        "module": { "type": "string", "enum": ["configuration", "support", "source"] },
        "customer_code": { "type": "string" },
        "operation_id": { "type": "string", "description": "the CONNECTOR_SCOPES operation it runs on" },
        "connector": { "type": "string" },
        "template": { "type": "string", "minLength": 1, "description": "the statement, endpoint call or script with {{param}} placeholders; nothing interpolated free-hand at run time" },
        "parameters": {
          "type": "array", "minItems": 0,
          "items": { "type": "object", "additionalProperties": false, "required": ["name", "type", "bound_from"],
            "properties": {
              "name": { "$ref": "#/$defs/param_name" },
              "type": { "type": "string", "enum": ["string", "integer", "number", "date", "identifier", "handle", "list"] },
              "bound_from": { "type": "string", "enum": ["plan_step", "specification", "returned_id", "case", "constant"], "description": "where the value is bound at run time — never free text" },
              "pattern": { "type": "string" }
            } }
        },
        "target_class": { "type": "string", "enum": ["working", "shared", "customer_facing", "external", "customer_visible"] },
        "effect_class": { "type": "string", "enum": ["transactional", "compensable", "irreversible", "ddl"] },
        "write_class": { "type": "string", "enum": ["W1", "W2", "W3", "W4", "W5", "W6", "W7"] },
        "expected_counts": { "type": "string", "minLength": 1, "description": "an expression over the plan's own numbers, e.g. 'covers.length' or '1' — never a range, never an adjective (auditor check 1)" },
        "assertions": {
          "type": "array", "minItems": 1,
          "items": { "type": "object", "additionalProperties": false, "required": ["name", "query", "expect"],
            "properties": { "name": { "type": "string" }, "query": { "type": "string" }, "expect": { "type": "string", "description": "an expression evaluated by a tool without judgement" } } }
        },
        "teardown_template": { "type": "string", "minLength": 1, "description": "the compensating operation, derived from the template — written with the shape, never later (auditor check 3)" },
        "case_types": { "type": "array", "minItems": 1, "items": { "type": "string" } },
        "state": { "type": "string", "enum": ["draft", "approved", "suspended", "retired"] },
        "expires_on": { "type": "string", "format": "date-time", "description": "set at approval from writes.shape_expiry_default; an approver may extend it once by at most one day (D133)" },
        "target_traits": {"type": "array", "minItems": 1, "uniqueItems": true, "items": {"enum": ["working", "shared", "customer_facing", "external", "customer_visible"]}}
      },
      "allOf": [
        {"if": {"properties": {"effect_class": {"const": "ddl"}}}, "then": {"properties": {"write_class": {"const": "W5"}}}},
        {"if": {"properties": {"effect_class": {"const": "irreversible"}}}, "then": {"properties": {"write_class": {"const": "W4"}}}}
      ]
    },
    "WritePacket": {
      "type": "object", "additionalProperties": false,
      "required": ["object", "case_id", "plan_revision_id", "shape_key", "shape_version", "artefact_hash", "steps", "teardown", "replay", "auditor_verdict_required", "target", "requested_mode", "target_traits"],
      "properties": {
        "object": { "const": "write_packet" },
        "case_id": { "type": "string" },
        "plan_revision_id": { "type": "string" },
        "shape_key": { "type": "string" },
        "shape_version": { "type": "integer", "minimum": 1 },
        "artefact_hash": {"$ref": "#/$defs/sha256", "description": "Hash of immutable semantic proposal content, excluding artefact_hash, grant_id, auditor_verdict_required and preflight. Grant issuance is later."},
        "grant_id": {"type": "string", "description": "Optional execution-envelope reference; excluded from the immutable proposal digest."},
        "target": { "type": "object", "required": ["environment", "system", "object"], "properties": { "environment": { "type": "string" }, "system": { "type": "string" }, "object": { "type": "string" } } },
        "steps": {
          "type": "array", "minItems": 1,
          "items": { "type": "object", "additionalProperties": false, "required": ["step_id", "bound_parameters", "expected_count", "effect_class", "statement_hash"],
            "properties": {
              "step_id": { "type": "string" },
              "bound_parameters": {"type": "object", "additionalProperties": {"type": ["string", "number", "integer", "boolean", "array", "null"]}, "description": "every template parameter bound to a concrete value (auditor check 1: exactness)"},
              "expected_count": { "type": "integer", "minimum": 0, "description": "a number, not a range (auditor check 2: boundedness re-derives it from the target)" },
              "effect_class": { "type": "string", "enum": ["transactional", "compensable", "irreversible", "ddl"] },
              "statement_hash": { "$ref": "#/$defs/sha256" },
              "returned_bindings": {"type": "object", "additionalProperties": {"type": "object", "additionalProperties": false, "required": ["source_step", "returned_field"], "properties": {"source_step": {"type": "string"}, "returned_field": {"type": "string"}}}, "description": "Only reviewed intra-operation bindings; otherwise bind a dependent packet after the predecessor result."}
            } }
        },
        "teardown": { "type": "object", "required": ["derived_from_shape", "statement_hashes"], "properties": { "derived_from_shape": { "type": "boolean", "const": true }, "statement_hashes": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/sha256" } } } },
        "replay": { "type": "object", "required": ["assertions"], "properties": { "assertions": { "type": "array", "minItems": 1, "items": { "type": "string" } } } },
        "preflight": { "type": "object", "properties": { "run_at": { "type": "string", "format": "date-time" }, "target_as_assumed": { "type": "boolean" } } },
        "auditor_verdict_required": { "type": "boolean", "const": true },
        "uncertainties": { "type": "array", "items": { "type": "string" } },
        "requested_mode": {"enum": ["write", "send", "ddl"]},
        "target_traits": {"type": "array", "minItems": 1, "uniqueItems": true, "items": {"enum": ["working", "shared", "customer_facing", "external", "customer_visible"]}}
      }
    }
  }
}
