{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://weverlabs.com/schemas/agent-result-contract-v2.schema.json",
  "title": "Agent Result Contract v2",
  "description": "Canonical result envelope for agent-paid Wever Labs OS work. The contract gathers machine result, human summary, proof objects, settlement receipt, usage receipt, credit usage, callback state, exception state, and replay state into one downstream-readable object.",
  "type": "object",
  "required": [
    "object_type",
    "contract_version",
    "result_contract_id",
    "task_token",
    "requested_tool",
    "lifecycle_state",
    "result_status",
    "machine_result",
    "human_summary",
    "proof_objects",
    "receipts",
    "callback",
    "issued_at"
  ],
  "additionalProperties": true,
  "properties": {
    "object_type": {
      "type": "string",
      "const": "agent_result_contract"
    },
    "contract_version": {
      "type": "string",
      "const": "2.0"
    },
    "result_contract_id": {
      "type": "string"
    },
    "task_token": {
      "type": "string"
    },
    "requested_tool": {
      "type": "string",
      "enum": [
        "tokenops",
        "packetops",
        "distributionops",
        "wever_labs_os",
        "custom_workflow"
      ]
    },
    "requested_capability": {
      "type": "string"
    },
    "lifecycle_state": {
      "type": "string",
      "enum": [
        "created",
        "preflight_checked",
        "contract_ready",
        "contract_accepted",
        "trusted",
        "quoted",
        "credit_pending",
        "credit_funded",
        "queued",
        "operating",
        "result_ready",
        "attested",
        "settlement_receipt_issued",
        "usage_receipt_issued",
        "callback_pending",
        "callback_delivered",
        "exception_open",
        "replay_ready",
        "failed",
        "closed"
      ]
    },
    "result_status": {
      "type": "string",
      "enum": [
        "ready",
        "partial",
        "delivered",
        "failed",
        "replay_ready",
        "exception_open"
      ]
    },
    "work_order_id": {
      "type": "string"
    },
    "agent_task_run_id": {
      "type": "string"
    },
    "handshake_id": {
      "type": "string"
    },
    "preflight_id": {
      "type": "string"
    },
    "run_contract_id": {
      "type": "string"
    },
    "trust_profile_id": {
      "type": "string"
    },
    "quote_id": {
      "type": "string"
    },
    "credit_entitlement_id": {
      "type": "string"
    },
    "machine_result": {
      "type": "object",
      "additionalProperties": true,
      "description": "Machine-readable payload returned to the requesting agent."
    },
    "human_summary": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "title": {
          "type": "string"
        },
        "summary": {
          "type": "string"
        },
        "work_performed": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "findings": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "open_items": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "next_actions": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "proof_objects": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "task_ledger_url": {
          "type": "string",
          "format": "uri"
        },
        "attestation_url": {
          "type": "string",
          "format": "uri"
        },
        "run_contract_url": {
          "type": "string",
          "format": "uri"
        },
        "settlement_receipt_url": {
          "type": "string",
          "format": "uri"
        },
        "usage_receipt_url": {
          "type": "string",
          "format": "uri"
        },
        "result_package_url": {
          "type": "string",
          "format": "uri"
        },
        "callback_payload_url": {
          "type": "string",
          "format": "uri"
        },
        "exception_url": {
          "type": "string",
          "format": "uri"
        },
        "replay_package_url": {
          "type": "string",
          "format": "uri"
        }
      }
    },
    "receipts": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "settlement_receipt_id": {
          "type": "string"
        },
        "settlement_receipt": {
          "$ref": "https://weverlabs.com/schemas/agent-settlement-receipt.schema.json"
        },
        "usage_receipt_id": {
          "type": "string"
        },
        "usage_receipt": {
          "$ref": "https://weverlabs.com/schemas/tokenops-usage-receipt.schema.json"
        },
        "receipt_split": {
          "type": "string",
          "const": "settlement_funds_credits_usage_consumes_credits"
        }
      }
    },
    "credit_usage": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "credits_funded": {
          "type": "number",
          "minimum": 0
        },
        "credits_debited": {
          "type": "number",
          "minimum": 0
        },
        "remaining_credits": {
          "type": "number",
          "minimum": 0
        },
        "credit_unit": {
          "type": "string"
        }
      }
    },
    "callback": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "callback_status": {
          "type": "string",
          "enum": [
            "not_requested",
            "pending",
            "ready",
            "delivered",
            "failed",
            "retry_required"
          ]
        },
        "callback_payload_url": {
          "type": "string",
          "format": "uri"
        },
        "delivered_at": {
          "type": "string",
          "format": "date-time"
        },
        "next_action": {
          "type": "string"
        }
      }
    },
    "exception_or_replay": {
      "type": "object",
      "additionalProperties": true
    },
    "public_safe_note": {
      "type": "string"
    },
    "issued_at": {
      "type": "string",
      "format": "date-time"
    },
    "os_support_matrix_url": {
      "type": "string",
      "format": "uri"
    },
    "rail_binding_url": {
      "type": "string",
      "format": "uri"
    },
    "runtime_execution_plan_url": {
      "type": "string",
      "format": "uri"
    }
  }
}
