{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://iot.dev/contracts/device-wire/device-v1.schema.json",
  "title": "Clean-Slate Device Wire v1 Contract (Deterministic CBOR-Only, MQTT 3.1.1, Fixed up/down Topics)",
  "x-allowed-topics": [
    "up",
    "down"
  ],
  "x-uplink-types": [
    "hello",
    "telemetry",
    "vitals",
    "event",
    "command_result",
    "ota_status",
    "rekey_csr",
    "rekey_confirm",
    "debug_response",
    "policy_installed",
    "edge_wal"
  ],
  "x-downlink-types": [
    "policy_bundle",
    "command",
    "ota",
    "rekey_cert",
    "debug_request",
    "rekey_challenge",
    "edge_snapshot",
    "edge_ack",
    "snapshot_request",
    "rtc_peer"
  ],
  "x-connector-session": "A separately registered workspace Connector leaf is admitted through connector/v1 multiplexing. Ordinary device leaves use this document directly.",
  "x-ordered-recording": "Only debug_response with chunk_seq, final_chunk and current boot_nonce_hex is recordable. Sequences start at 1 per signed session across commands; settled_seq remains the unrelated actuator watermark. A legacy frame still decodes, but has no durable recording admission. Only previously buffered, redacted diagnostics are allowed; net_diag never accepts a destination or performs network probes.",
  "x-identity-source": "mtls_client_certificate_credential_id",
  "x-mqtt-version": "3.1.1",
  "x-limits": {
    "max_mqtt_packet_bytes": 65536,
    "max_payload_bytes": 61440,
    "qos": 1,
    "retain_allowed": false,
    "max_cmd_seq_forward_jump": 10000,
    "max_sequence_value": 9223372036854775806
  },
  "x-encoding-profile": {
    "encodings": [
      "application/cbor"
    ],
    "cbor_deterministic_rfc8949_4_2": true,
    "json_wire_rejected": true,
    "reject_duplicate_keys": true,
    "reject_nan_infinity": true,
    "reject_non_string_cbor_map_keys": true,
    "unknown_crit_must_reject": true
  },
  "x-debug-allowlist": [
    "log_tail",
    "sys_info",
    "net_diag",
    "heap_stats",
    "tsl_dump"
  ],
  "type": "object",
  "additionalProperties": false,
  "required": [
    "v",
    "type"
  ],
  "properties": {
    "v": {
      "const": 1
    },
    "type": {
      "enum": [
        "hello",
        "telemetry",
        "vitals",
        "event",
        "command_result",
        "ota_status",
        "rekey_csr",
        "rekey_confirm",
        "debug_response",
        "policy_bundle",
        "command",
        "ota",
        "rekey_cert",
        "debug_request",
        "policy_installed",
        "rekey_challenge",
        "edge_wal",
        "edge_snapshot",
        "edge_ack",
        "snapshot_request",
        "rtc_peer"
      ]
    },
    "stage4": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "enum": [
          "edge",
          "snapshot",
          "rtc"
        ]
      }
    },
    "crit": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 48
      }
    },
    "message_id": {
      "type": "string",
      "minLength": 8,
      "maxLength": 64
    },
    "sequence": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9223372036854775806
    },
    "settled_seq": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9223372036854775806
    },
    "indeterminate_seq": {
      "type": "integer",
      "minimum": 1,
      "maximum": 9223372036854775806
    },
    "boot_nonce_hex": {
      "type": "string",
      "pattern": "^[0-9a-f]{32}$"
    },
    "policy_rev": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9223372036854775806
    },
    "timestamp_ms": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9223372036854775806
    },
    "buffered_replay": {
      "type": "boolean"
    },
    "observed_age_ms": {
      "type": "integer",
      "minimum": 0
    },
    "command_id": {
      "type": "string",
      "format": "uuid"
    },
    "cmd_seq": {
      "type": "integer",
      "minimum": 1,
      "maximum": 9223372036854775806
    },
    "lease_ms": {
      "type": "integer",
      "minimum": 1000,
      "maximum": 3600000
    },
    "safe_stop": {
      "type": "boolean"
    },
    "download": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "token",
        "expires_at_ms"
      ],
      "properties": {
        "token": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "expires_at_ms": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        }
      }
    },
    "grant": {
      "$ref": "../crypto/envelopes-v1.schema.json#/$defs/SignedEnvelope",
      "properties": {
        "domain": {
          "const": "iot.command-grant.v1"
        }
      }
    },
    "chunk_seq": {
      "type": "integer",
      "minimum": 1,
      "maximum": 9007199254740991
    },
    "final_chunk": {
      "type": "boolean"
    },
    "debug_cmd": {
      "enum": [
        "log_tail",
        "sys_info",
        "net_diag",
        "heap_stats",
        "tsl_dump"
      ]
    },
    "payload": {
      "type": "object"
    },
    "service_id": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]{0,63}$"
    },
    "session_id": {
      "type": "string",
      "format": "uuid"
    },
    "authorization": {
      "oneOf": [
        {
          "$ref": "../crypto/envelopes-v1.schema.json#/$defs/SignedEnvelope",
          "properties": {
            "domain": {
              "const": "iot.ota-activation.v1"
            }
          }
        },
        {
          "$ref": "../crypto/envelopes-v1.schema.json#/$defs/SignedEnvelope",
          "properties": {
            "domain": {
              "const": "iot.debug-session.v1"
            }
          }
        }
      ]
    }
  },
  "x-stage-four": "Only send optional new frame types to a peer advertising the matching stage4 capabilities in hello. Never send RTC signaling or image bytes over MQTT.",
  "allOf": [
    {
      "if": {
        "required": [
          "stage4"
        ]
      },
      "then": {
        "properties": {
          "type": {
            "const": "hello"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "edge_wal"
          }
        }
      },
      "then": {
        "required": [
          "payload"
        ],
        "propertyNames": {
          "enum": [
            "v",
            "type",
            "payload",
            "crit"
          ]
        },
        "properties": {
          "payload": {
            "oneOf": [
              {
                "$ref": "../edge-sync/v1.schema.json#/$defs/WalBatch"
              },
              {
                "$ref": "../edge-sync/site-v1.schema.json#/$defs/Fragment",
                "properties": {
                  "content": {
                    "const": "edge_wal"
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "edge_snapshot"
          }
        }
      },
      "then": {
        "required": [
          "payload"
        ],
        "propertyNames": {
          "enum": [
            "v",
            "type",
            "payload",
            "crit"
          ]
        },
        "properties": {
          "payload": {
            "oneOf": [
              {
                "$ref": "../edge-sync/site-v1.schema.json#/$defs/FullSnapshot"
              },
              {
                "$ref": "../edge-sync/site-v1.schema.json#/$defs/Fragment",
                "properties": {
                  "content": {
                    "const": "edge_snapshot"
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "edge_ack"
          }
        }
      },
      "then": {
        "required": [
          "payload"
        ],
        "propertyNames": {
          "enum": [
            "v",
            "type",
            "payload",
            "crit"
          ]
        },
        "properties": {
          "payload": {
            "$ref": "../edge-sync/v1.schema.json#/$defs/WalAck"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "snapshot_request"
          }
        }
      },
      "then": {
        "required": [
          "payload"
        ],
        "propertyNames": {
          "enum": [
            "v",
            "type",
            "payload",
            "crit"
          ]
        },
        "properties": {
          "payload": {
            "$ref": "../openapi/media-v1.schema.json#/$defs/UploadGrant"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "rtc_peer"
          }
        }
      },
      "then": {
        "required": [
          "payload"
        ],
        "propertyNames": {
          "enum": [
            "v",
            "type",
            "payload",
            "crit"
          ]
        },
        "properties": {
          "payload": {
            "$ref": "../openapi/media-v1.schema.json#/$defs/PeerGrant"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "hello"
          }
        }
      },
      "then": {
        "required": [
          "boot_nonce_hex",
          "settled_seq",
          "policy_rev"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "enum": [
              "telemetry",
              "vitals",
              "event",
              "command_result",
              "ota_status"
            ]
          }
        }
      },
      "then": {
        "required": [
          "message_id",
          "sequence",
          "settled_seq",
          "payload"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "ota_status"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "campaign_id",
              "approval_id",
              "firmware_sha256_hex",
              "status"
            ],
            "properties": {
              "campaign_id": {
                "type": "string",
                "format": "uuid"
              },
              "approval_id": {
                "type": "string",
                "format": "uuid"
              },
              "firmware_sha256_hex": {
                "type": "string",
                "pattern": "^[0-9a-f]{64}$"
              },
              "status": {
                "enum": [
                  "downloaded",
                  "self_test_passed",
                  "self_test_failed",
                  "rolled_back"
                ]
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "vitals"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "type": "object",
            "properties": {
              "uptime_ms": {
                "type": "integer",
                "minimum": 0
              },
              "firmware_version": {
                "type": "string",
                "minLength": 1,
                "maxLength": 128
              },
              "firmware_sha256_hex": {
                "type": "string",
                "pattern": "^[0-9a-f]{64}$"
              },
              "min_free_heap_bytes": {
                "type": "integer",
                "minimum": 0
              },
              "reset_reason": {
                "type": "string",
                "maxLength": 128
              },
              "rssi_dbm": {
                "type": "integer"
              },
              "battery_mv": {
                "type": "integer",
                "minimum": 0
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "enum": [
              "rekey_csr",
              "rekey_confirm"
            ]
          }
        }
      },
      "then": {
        "required": [
          "settled_seq",
          "payload"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "buffered_replay": {
            "const": true
          }
        },
        "required": [
          "buffered_replay"
        ]
      },
      "then": {
        "required": [
          "observed_age_ms"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "command"
          }
        }
      },
      "then": {
        "required": [
          "command_id",
          "lease_ms",
          "payload"
        ],
        "properties": {
          "payload": {
            "$ref": "../commands/tx-binding-v1.schema.json#/properties/writes"
          }
        },
        "if": {
          "required": [
            "service_id"
          ]
        },
        "then": {
          "properties": {
            "safe_stop": {
              "const": false
            }
          }
        },
        "else": {
          "properties": {
            "payload": {
              "minProperties": 1
            }
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "safe_stop": {
                  "const": true
                }
              },
              "required": [
                "safe_stop"
              ]
            },
            "then": {
              "not": {
                "required": [
                  "service_id"
                ]
              }
            },
            "else": {
              "required": [
                "cmd_seq"
              ]
            }
          }
        ]
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "policy_bundle"
          }
        }
      },
      "then": {
        "required": [
          "policy_rev",
          "payload"
        ],
        "properties": {
          "payload": {
            "$ref": "../crypto/envelopes-v1.schema.json#/$defs/SignedEnvelope",
            "properties": {
              "domain": {
                "const": "iot.policy-bundle.v1"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "ota"
          }
        }
      },
      "then": {
        "required": [
          "payload",
          "authorization"
        ],
        "properties": {
          "payload": {
            "$ref": "../crypto/envelopes-v1.schema.json#/$defs/SignedEnvelope",
            "properties": {
              "domain": {
                "const": "iot.ota-manifest.v1"
              }
            }
          },
          "authorization": {
            "$ref": "../crypto/envelopes-v1.schema.json#/$defs/SignedEnvelope",
            "properties": {
              "domain": {
                "const": "iot.ota-activation.v1"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "debug_request"
          }
        }
      },
      "then": {
        "required": [
          "debug_cmd",
          "session_id",
          "authorization"
        ],
        "propertyNames": {
          "enum": [
            "v",
            "type",
            "message_id",
            "crit",
            "session_id",
            "debug_cmd",
            "authorization"
          ]
        },
        "properties": {
          "authorization": {
            "$ref": "../crypto/envelopes-v1.schema.json#/$defs/SignedEnvelope",
            "properties": {
              "domain": {
                "const": "iot.debug-session.v1"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "debug_response"
          }
        }
      },
      "then": {
        "required": [
          "settled_seq",
          "debug_cmd",
          "payload",
          "session_id"
        ]
      }
    },
    {
      "if": {
        "anyOf": [
          {
            "required": [
              "chunk_seq"
            ]
          },
          {
            "required": [
              "final_chunk"
            ]
          }
        ]
      },
      "then": {
        "required": [
          "chunk_seq",
          "final_chunk",
          "boot_nonce_hex",
          "debug_cmd",
          "session_id",
          "payload"
        ],
        "propertyNames": {
          "enum": [
            "v",
            "type",
            "message_id",
            "crit",
            "settled_seq",
            "chunk_seq",
            "final_chunk",
            "boot_nonce_hex",
            "debug_cmd",
            "session_id",
            "payload"
          ]
        },
        "properties": {
          "type": {
            "const": "debug_response"
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "debug_cmd": {
                  "const": "log_tail"
                }
              }
            },
            "then": {
              "properties": {
                "payload": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "lines"
                  ],
                  "properties": {
                    "lines": {
                      "type": "array",
                      "maxItems": 64,
                      "items": {
                        "type": "string",
                        "maxLength": 512
                      }
                    }
                  }
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "debug_cmd": {
                  "const": "sys_info"
                }
              }
            },
            "then": {
              "properties": {
                "payload": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "firmware_version",
                    "uptime_ms"
                  ],
                  "properties": {
                    "firmware_version": {
                      "type": "string",
                      "maxLength": 128
                    },
                    "uptime_ms": {
                      "type": "integer",
                      "minimum": 0,
                      "maximum": 9007199254740991
                    }
                  }
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "debug_cmd": {
                  "const": "net_diag"
                }
              }
            },
            "then": {
              "properties": {
                "payload": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "online"
                  ],
                  "properties": {
                    "online": {
                      "type": "boolean"
                    },
                    "reconnects": {
                      "type": "integer",
                      "minimum": 0
                    },
                    "rssi_dbm": {
                      "type": "integer",
                      "minimum": -140,
                      "maximum": 0
                    }
                  }
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "debug_cmd": {
                  "const": "heap_stats"
                }
              }
            },
            "then": {
              "properties": {
                "payload": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "free_bytes"
                  ],
                  "properties": {
                    "free_bytes": {
                      "type": "integer",
                      "minimum": 0
                    },
                    "min_free_bytes": {
                      "type": "integer",
                      "minimum": 0
                    }
                  }
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "debug_cmd": {
                  "const": "tsl_dump"
                }
              }
            },
            "then": {
              "properties": {
                "payload": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "product_id",
                    "version",
                    "property_ids"
                  ],
                  "properties": {
                    "product_id": {
                      "type": "string",
                      "maxLength": 64
                    },
                    "version": {
                      "type": "string",
                      "maxLength": 64
                    },
                    "property_ids": {
                      "type": "array",
                      "maxItems": 32,
                      "uniqueItems": true,
                      "items": {
                        "type": "string",
                        "pattern": "^[a-z][a-z0-9_]{0,63}$"
                      }
                    }
                  }
                }
              }
            }
          }
        ]
      }
    },
    {
      "if": {
        "properties": {
          "type": {
            "const": "rekey_cert"
          }
        }
      },
      "then": {
        "required": [
          "payload"
        ],
        "properties": {
          "payload": {
            "$ref": "rekey-v1.schema.json#/$defs/IssueCertResponse"
          }
        }
      }
    },
    {
      "if": {
        "required": [
          "type"
        ],
        "properties": {
          "type": {
            "const": "rekey_challenge"
          }
        }
      },
      "then": {
        "required": [
          "payload"
        ],
        "properties": {
          "payload": {
            "$ref": "rekey-v1.schema.json#/$defs/RekeyChallenge"
          }
        }
      }
    },
    {
      "if": {
        "required": [
          "type"
        ],
        "properties": {
          "type": {
            "const": "rekey_csr"
          }
        }
      },
      "then": {
        "properties": {
          "payload": {
            "$ref": "rekey-v1.schema.json#/$defs/RekeyExchangeRequest"
          }
        }
      }
    },
    {
      "if": {
        "required": [
          "type"
        ],
        "properties": {
          "type": {
            "const": "policy_installed"
          }
        }
      },
      "then": {
        "required": [
          "message_id",
          "sequence",
          "settled_seq",
          "boot_nonce_hex",
          "policy_rev"
        ]
      }
    },
    {
      "if": {
        "required": [
          "type"
        ],
        "properties": {
          "type": {
            "const": "command_result"
          }
        }
      },
      "then": {
        "required": [
          "command_id"
        ],
        "properties": {
          "payload": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "status",
              "error_code"
            ],
            "properties": {
              "status": {
                "enum": [
                  "executing",
                  "succeeded",
                  "failed",
                  "unknown"
                ]
              },
              "error_code": {
                "oneOf": [
                  {
                    "type": "string",
                    "pattern": "^[a-z][a-z0-9_]{0,63}$"
                  },
                  {
                    "type": "null"
                  }
                ]
              }
            },
            "allOf": [
              {
                "if": {
                  "properties": {
                    "status": {
                      "enum": [
                        "failed",
                        "unknown"
                      ]
                    }
                  }
                },
                "then": {
                  "properties": {
                    "error_code": {
                      "type": "string",
                      "pattern": "^[a-z][a-z0-9_]{0,63}$"
                    }
                  }
                },
                "else": {
                  "properties": {
                    "error_code": {
                      "type": "null"
                    }
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "if": {
        "required": [
          "authorization"
        ]
      },
      "then": {
        "properties": {
          "type": {
            "enum": [
              "ota",
              "debug_request"
            ]
          }
        }
      }
    },
    {
      "if": {
        "required": [
          "download"
        ]
      },
      "then": {
        "properties": {
          "type": {
            "const": "ota"
          }
        }
      }
    },
    {
      "if": {
        "anyOf": [
          {
            "required": [
              "grant"
            ]
          },
          {
            "required": [
              "safe_stop"
            ]
          },
          {
            "required": [
              "service_id"
            ]
          }
        ]
      },
      "then": {
        "properties": {
          "type": {
            "const": "command"
          }
        }
      }
    }
  ],
  "x-command-action": "payload is the writes map (nonempty for property writes) or exact params map for the top-level service_id. safe_stop is accepted only for the locally certified safe-value action and never as a service. A certified safe_stop may omit cmd_seq and does not advance or depend on the ordinary watermark. Its durable command_result carries boot_nonce_hex from the executing boot and the original device sequence, including on replay. A receipt without that evidence cannot confirm a migration stop.",
  "x-command-durability": "For ordinary commands, all firmware must atomically persist the replay watermark and the pending command identity before actuation. If the write fails, do not actuate. Persist the outcome after actuation. On recovery report unknown for an unfinished command using its original identity and never repeat the action. The reference implementation stores seq_high and pending_seq in a 16-byte watermark alongside the command identity.",
  "x-authorization-verification": {
    "ota": "Verify publisher manifest and Authority activation separately using their pinned public keys. Match grant device/workspace/project and SHA-256 of exact publisher payload_det_cbor before staging or activating.",
    "debug_request": "Verify Authority debug-session domain and body, local device/boot nonce, session_id, allowlisted command, issued_uptime_ms <= now < uptime_deadline_ms and lifetime <= 1800000 ms. Unknown time fails closed.",
    "policy_bundle": "Verify Authority signature, schema, device identity, controller transition authorization, outer policy_rev == body.safety_policy_revision, anti-rollback and local hard envelope; acknowledge only after durable install.",
    "rekey_csr": "The current mTLS session supplies authoritative identity. Match duplicated settled_seq to the outer waterline. Never accept request-supplied current tier or owner verification flags."
  }
}
