{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/multi-agent-ecosystem/multi-agent-pipeline/pipeline/schemas/route-config.schema.json",
  "title": "Model routing configuration",
  "description": "Policy-driven model selection, OFF by default. Lives under prefs.global.modelRouting. Two layers, and the difference is the whole safety argument. Layer 1 chooses a rung INSIDE the Anthropic ladder and writes to the existing PHASE_MODEL_OVERRIDE seam - no new network path, no new account. Layer 2 is the `free` rung: a simple text task (a bulk read, a summary, an extraction, a label, a translation draft) goes to the multi-agent-mcp's verified free-model chain (llm_delegate / multi-agent-mcp-llm), whose every answer is checked against its source and discarded when it does not hold, with the Anthropic default taking over. It is reachable only where the pipeline makes the call itself (`bulk-read`, `delegate`); a subagent is dispatched by the host, so model-dispatch.sh refuses `free` and every other non-Anthropic rung at the subagent site and falls to the rule's next rung. `scope` has no `host-session` member and that absence is enforced here rather than written in prose: rewriting the host's base URL would route the user's ENTIRE session, including work that has nothing to do with this pipeline, through a third layer - breaking the subscription's auth model and silently changing which model answers. Shipping this disabled is the same reasoning as modelFallback.fableEnabled: a cost control that is on by default is not a control.",
  "type": "object",
  "additionalProperties": false,
  "required": ["enabled"],
  "properties": {
    "enabled": {
      "type": "boolean",
      "default": false,
      "description": "Master switch, ships false. While false not one line of dispatch behaviour changes: rules are read by route-status and by nothing else. route-off sets this to false and KEEPS the rules, so turning it back on does not re-ask for a configuration the user already gave."
    },
    "strategy": {
      "type": "string",
      "enum": ["manual", "task-fit", "cost-ceiling"],
      "default": "manual",
      "description": "manual: only the explicit rules[] apply, nothing is inferred. task-fit: the matched rule's rungs are tried cheapest first (cost-table.json prices, free first). cost-ceiling: rules apply in order until the run reaches the warn threshold of budgetCeilingUsd, then the cheapest rung is tried first."
    },
    "scope": {
      "type": "array",
      "default": ["subagent"],
      "uniqueItems": true,
      "description": "Where routing is allowed to act. Every member names a call site the pipeline itself owns: subagent (persona dispatch, Anthropic rungs only), bulk-read (bulk-read.sh), delegate (simple text tasks the skills hand to llm_delegate), research. There is deliberately no `host-session` member - see the title description.",
      "items": {
        "type": "string",
        "enum": ["subagent", "bulk-read", "delegate", "research"]
      }
    },
    "rules": {
      "type": "array",
      "default": [],
      "description": "Ordered; first match wins. An empty list with enabled:true is valid and means 'routing is armed but nothing matches yet' - which route-status reports as such rather than as an error.",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["when", "prefer"],
        "properties": {
          "when": {
            "type": "object",
            "additionalProperties": false,
            "minProperties": 1,
            "properties": {
              "persona": {
                "type": "string"
              },
              "phase": {
                "type": "integer",
                "minimum": 0,
                "maximum": 5
              },
              "taskKind": {
                "type": "string",
                "enum": ["bugfix", "feature", "refactor", "chore", "component"]
              }
            }
          },
          "prefer": {
            "type": "array",
            "minItems": 1,
            "description": "Rungs in descending preference. Rung NAMES are the contract; model ids are not - they live in cost-table.json and change without a config edit.",
            "items": {
              "type": "string"
            }
          }
        }
      }
    },
    "free": {
      "type": "object",
      "additionalProperties": false,
      "description": "Layer 2. With enabled:true, the bulk-read and delegate call sites try the free rung before the rule's Anthropic rungs. Written by route-on --free.",
      "properties": {
        "enabled": {
          "type": "boolean",
          "default": false,
          "description": "Try the verified free-model chain first at bulk-read and delegate. Ships false."
        },
        "allowTraining": {
          "type": "boolean",
          "default": false,
          "description": "Also use free tiers that may train on prompts (Mistral, Gemini) or whose data use is unknown. false keeps text to providers that do not (Groq) and a local Ollama."
        }
      }
    },
    "budgetCeilingUsd": {
      "type": ["number", "null"],
      "default": null,
      "minimum": 0,
      "description": "Only consulted by the cost-ceiling strategy, through cost-budget-check.mjs against the run's tracker. null means no ceiling, which is not the same as a ceiling of 0."
    },
    "recordDecisions": {
      "type": "boolean",
      "default": true,
      "description": "Write every routing decision to the cost ledger: which rule matched, which rung it chose, and why. On by default because a router whose choices are not recorded cannot be audited after the fact - and the question that always arrives later is 'why did this run cost that'."
    },
    "learned": {
      "type": "object",
      "additionalProperties": false,
      "description": "Learned rung order (route-learn.mjs). Ships disabled. While enabled, model-dispatch.sh hands the strategy's order to route-learn.mjs choose, which reorders the rule's own candidates from the PROMOTED policy: per call site, task kind and complexity band, a Beta posterior over a reward taken from gate verdicts and review findings, never from a model's report on itself, sampled by seeded Thompson sampling. A policy changes only through route-learn.mjs promote, after it beats the current one on a time-ordered holdout. Too few outcomes, no promoted policy or any failure leaves the order unchanged.",
      "properties": {
        "enabled": { "type": "boolean", "default": false, "description": "Ships false." },
        "minSamples": {
          "type": "integer",
          "minimum": 1,
          "default": 20,
          "description": "Outcomes a rung needs in a bucket before its posterior is used; with fewer than two such rungs the rule's order stands."
        },
        "target": {
          "type": "number",
          "minimum": 0,
          "maximum": 1,
          "default": 0.8,
          "description": "A rung whose sampled reward clears this is good enough; the cheapest such rung is tried first."
        },
        "halfLifeDays": {
          "type": "integer",
          "minimum": 1,
          "default": 60,
          "description": "An outcome's weight halves every this many days."
        },
        "explore": {
          "type": "boolean",
          "default": false,
          "description": "Draw a rung with too few outcomes from its prior instead of leaving it after the known ones. Off by default: exploring means sometimes trying a rung with no record."
        },
        "holdout": {
          "type": "number",
          "exclusiveMinimum": 0,
          "exclusiveMaximum": 1,
          "default": 0.25,
          "description": "Newest fraction of the outcomes promote scores a candidate on."
        },
        "minRows": {
          "type": "integer",
          "minimum": 1,
          "default": 10,
          "description": "Holdout rows promote requires."
        },
        "margin": {
          "type": "number",
          "minimum": 0,
          "default": 0.005,
          "description": "Brier-score improvement over the current policy promote requires."
        }
      }
    }
  }
}
