{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://limecloud.github.io/agentapp/schemas/app-runtime.schema.json",
  "title": "Agent App Runtime Contract",
  "description": "v0.6 Agent task runtime control plane for lime.agent tasks; v0.9 App Server bridge profile; v0.10 shared host runtime profile for mini-program style apps and host-supervised app backend services.",
  "type": "object",
  "anyOf": [
    { "required": ["agentRuntime"] },
    { "required": ["agentTask"] }
  ],
  "properties": {
    "agentRuntime": {
      "type": "object",
      "properties": {
        "bridge": { "$ref": "#/$defs/bridge" },
        "agentTask": { "$ref": "#/$defs/agentTask" }
      },
      "additionalProperties": true
    },
    "agentTask": { "$ref": "#/$defs/agentTask" }
  },
  "$defs": {
    "bridge": {
      "type": "object",
      "description": "v0.9 host-mediated bridge from Capability SDK / Host Bridge to App Server JSON-RPC. v0.10 keeps app UI, workflow, storage, and app backend services behind shared host runtime capabilities instead of exposing transport handles.",
      "properties": {
        "kind": {
          "type": "string",
          "enum": ["app-server-json-rpc", "host-native", "reference-mock"]
        },
        "transport": {
          "type": "string",
          "enum": ["host-mediated", "stdio-jsonl", "local-socket", "web-host"]
        },
        "protocolVersion": { "type": "string" },
        "clientSurface": {
          "type": "string",
          "enum": ["capability-sdk", "host-bridge", "server-client"]
        },
        "hostBoundary": {
          "type": "string",
          "enum": ["desktop-host-ipc", "web-host-postmessage", "host-native"]
        },
        "runtimeOwner": {
          "type": "string",
          "enum": ["runtime-core", "host-native", "reference-mock"]
        },
        "methods": { "$ref": "#/$defs/bridgeMethods" },
        "events": {
          "type": "object",
          "properties": {
            "notification": { "type": "string", "const": "agentSession/event" },
            "deriveFromRuntimeFacts": { "type": "boolean" },
            "allowUiSynthesis": { "type": "boolean", "const": false }
          },
          "additionalProperties": true
        }
      },
      "additionalProperties": true
    },
    "bridgeMethods": {
      "type": "object",
      "properties": {
        "initialize": { "type": "string", "const": "initialize" },
        "initialized": { "type": "string", "const": "initialized" },
        "startSession": { "type": "string", "const": "agentSession/start" },
        "readSession": { "type": "string", "const": "agentSession/read" },
        "startTurn": { "type": "string", "const": "agentSession/turn/start" },
        "cancelTurn": { "type": "string", "const": "agentSession/turn/cancel" },
        "respondAction": { "type": "string", "const": "agentSession/action/respond" },
        "events": { "type": "string", "const": "agentSession/event" },
        "listCapabilities": { "type": "string", "const": "capability/list" },
        "readArtifact": { "type": "string", "const": "artifact/read" },
        "exportEvidence": { "type": "string", "const": "evidence/export" }
      },
      "additionalProperties": true
    },
    "agentTask": {
      "type": "object",
      "properties": {
        "eventSchema": {
          "type": "string",
          "const": "lime.agent-task-event.v1"
        },
        "resultSchema": {
          "type": "string",
          "const": "lime.agent-task-result.v1"
        },
        "structuredOutput": { "$ref": "#/$defs/structuredOutput" },
        "approval": { "$ref": "#/$defs/approval" },
        "sessionPolicy": { "$ref": "#/$defs/sessionPolicy" },
        "toolDiscovery": { "$ref": "#/$defs/toolDiscovery" },
        "checkpointScope": { "$ref": "#/$defs/checkpointScope" },
        "observability": { "$ref": "#/$defs/observability" }
      },
      "additionalProperties": true
    },
    "structuredOutput": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "enum": ["json_schema", "none"]
        },
        "schemaRef": { "type": "string" },
        "schema": { "type": "object" },
        "maxValidationRetries": { "type": "integer", "minimum": 0, "maximum": 10 },
        "failureSubtype": {
          "type": "string",
          "enum": ["error_max_structured_output_retries", "error_during_execution"]
        }
      },
      "additionalProperties": true
    },
    "approval": {
      "type": "object",
      "properties": {
        "behavior": {
          "type": "string",
          "enum": ["host-mediated", "auto-deny", "manifest-only"]
        },
        "supportsUpdatedInput": { "type": "boolean" },
        "supportsDefer": { "type": "boolean" },
        "rememberScopes": {
          "type": "array",
          "items": { "type": "string", "enum": ["task", "session", "workspace"] },
          "uniqueItems": true
        }
      },
      "additionalProperties": true
    },
    "sessionPolicy": {
      "type": "object",
      "properties": {
        "modes": {
          "type": "array",
          "items": { "type": "string", "enum": ["new", "resume", "continue", "fork"] },
          "uniqueItems": true
        },
        "compactionEvents": { "type": "boolean" }
      },
      "additionalProperties": true
    },
    "toolDiscovery": {
      "type": "object",
      "properties": {
        "mode": { "type": "string", "enum": ["eager", "on_demand", "none"] },
        "topK": { "type": "integer", "minimum": 1, "maximum": 20 },
        "includeSchemas": { "type": "string", "enum": ["all", "selected_only", "none"] }
      },
      "additionalProperties": true
    },
    "checkpointScope": {
      "type": "object",
      "properties": {
        "workflowState": { "type": "boolean" },
        "appStorage": { "type": "boolean" },
        "artifacts": { "type": "boolean" },
        "files": { "type": "string", "enum": ["tracked_only", "none"] },
        "conversation": { "type": "string", "enum": ["resume_only", "rewind", "none"] },
        "externalSideEffects": { "type": "string", "enum": ["record_only", "manual_compensation", "none"] }
      },
      "additionalProperties": true
    },
    "observability": {
      "type": "object",
      "properties": {
        "profileEvents": { "type": "boolean" },
        "openTelemetryMapping": { "type": "boolean" },
        "exportContentByDefault": { "type": "boolean" }
      },
      "additionalProperties": true
    }
  },
  "additionalProperties": true
}
