|
| 1 | +#!/usr/bin/env bash |
| 2 | +# shellcheck shell=bash |
| 3 | +# fm-provider-lib.sh - single owner of the model -> provider identity mapping and |
| 4 | +# the per-provider live-lane concurrency cap. |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# . bin/fm-provider-lib.sh |
| 8 | +# |
| 9 | +# A provider is the billing pool a dispatch draws on, NOT the harness that |
| 10 | +# launches it. The identity is taken from the resolved model string first - a |
| 11 | +# provider-qualified `<provider>/<id>` prefix, else a model-id pattern - and the |
| 12 | +# harness table is consulted only when the model carries no signal. That is what |
| 13 | +# makes `opencode-go/deepseek-v4.1-flash` and `opencode-go/deepseek-v4-pro` ONE |
| 14 | +# pool while `pi/deepseek-v4p1-flash`, billed through Fireworks, is a different |
| 15 | +# one. The harness fallback reuses the existing quota tables |
| 16 | +# (fm_quota_provider_for_harness / fm_quota_single_provider_for_harness in |
| 17 | +# bin/fm-quota-axi-lib.sh) rather than restating them; those tables are frozen |
| 18 | +# for no-key quota routing and are never edited here. |
| 19 | +# |
| 20 | +# Functions: |
| 21 | +# fm_provider_for_model <harness> <model> |
| 22 | +# Print the provider id the tuple bills against, or return 1 when the |
| 23 | +# model and harness together name no known pool. |
| 24 | +# fm_lane_provider <harness> <model> |
| 25 | +# Print fm_provider_for_model's answer, or the harness name as a |
| 26 | +# last-resort bucket, or return 1 when even that is empty. |
| 27 | +# fm_provider_cap_for <provider> [<config-dir>] |
| 28 | +# Print the operator-configured cap: `providerCaps.<provider>` from |
| 29 | +# config/crew-dispatch.json, else `providerCaps.default`, else |
| 30 | +# FM_PROVIDER_LANE_CAP_DEFAULT. |
| 31 | +# fm_provider_lane_counts <state-dir> [<config-dir>] [<exclude-id>] |
| 32 | +# Print `<provider> <used> <cap>` for every provider carrying a live lane, |
| 33 | +# sorted by provider. One line per provider, no header. |
| 34 | +# fm_provider_cap_refuse <state-dir> <config-dir> <harness> <model> [<exclude-id>] |
| 35 | +# Print an operator-facing refusal to stderr and return 1 when dispatching |
| 36 | +# this tuple would push its provider past the cap; return 0 otherwise. |
| 37 | +# |
| 38 | +# Seat accounting: a lane occupies a seat unless its recorded endpoint is |
| 39 | +# PROVABLY dead or missing (fm_backend_agent_state). A lane whose endpoint cannot |
| 40 | +# be proven gone - alive, ambiguous, unreadable, or unverified (every backend |
| 41 | +# except tmux and herdr) - keeps its seat, and a record with no endpoint target |
| 42 | +# keeps it too. The count can therefore under-admit, never over-admit; a |
| 43 | +# stranded record on an unverifiable backend is released by the stranded-record |
| 44 | +# detector that retires it, not by this counter. |
| 45 | +# |
| 46 | +# Scope: the cap is per home. A remote secondmate's own lanes live on another |
| 47 | +# host and are invisible here, so this counter governs only the lanes this |
| 48 | +# home's state directory records. |
| 49 | +set -u |
| 50 | + |
| 51 | +if [ -n "${FM_PROVIDER_LIB_SOURCED:-}" ]; then |
| 52 | + return 0 |
| 53 | +fi |
| 54 | +FM_PROVIDER_LIB_SOURCED=1 |
| 55 | + |
| 56 | +FM_PROVIDER_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 57 | +# shellcheck source=bin/fm-backend.sh |
| 58 | +. "$FM_PROVIDER_LIB_DIR/fm-backend.sh" |
| 59 | +# shellcheck source=bin/fm-quota-axi-lib.sh |
| 60 | +. "$FM_PROVIDER_LIB_DIR/fm-quota-axi-lib.sh" |
| 61 | + |
| 62 | +# Fallback cap when config/crew-dispatch.json is absent or declares no |
| 63 | +# providerCaps entry. The operator overrides it per provider (or for every |
| 64 | +# provider through providerCaps.default) in config/crew-dispatch.json. |
| 65 | +FM_PROVIDER_LANE_CAP_DEFAULT=4 |
| 66 | + |
| 67 | +# fm_provider_for_model <harness> <model> |
| 68 | +fm_provider_for_model() { # <harness> <model> |
| 69 | + local harness=${1:-} model=${2:-} segment |
| 70 | + case "$model" in |
| 71 | + '' | default | -) ;; |
| 72 | + */*) |
| 73 | + segment=${model%%/*} |
| 74 | + case "$segment" in |
| 75 | + fireworks_ai | fireworks) printf 'fireworks\n'; return 0 ;; |
| 76 | + openai-codex | openai) printf 'codex\n'; return 0 ;; |
| 77 | + claude-bridge | anthropic) printf 'claude\n'; return 0 ;; |
| 78 | + esac |
| 79 | + # An unrecognized provider-qualified prefix is its own billing pool when |
| 80 | + # it is a well-formed provider id (the same shape docs/configuration.md |
| 81 | + # pins for the resolver's provider fields). |
| 82 | + case "$segment" in |
| 83 | + '' | -* | *- | *--* | *[!a-z0-9-]*) ;; |
| 84 | + *) printf '%s\n' "$segment"; return 0 ;; |
| 85 | + esac |
| 86 | + ;; |
| 87 | + esac |
| 88 | + case "$model" in |
| 89 | + '' | default | -) ;; |
| 90 | + deepseek-v4p1*) printf 'fireworks\n'; return 0 ;; |
| 91 | + deepseek*) printf 'deepseek\n'; return 0 ;; |
| 92 | + composer* | cursor-*) printf 'cursor\n'; return 0 ;; |
| 93 | + grok-*) printf 'grok\n'; return 0 ;; |
| 94 | + claude-* | sonnet | haiku | opus | fable) printf 'claude\n'; return 0 ;; |
| 95 | + kimi-*) printf 'kimi\n'; return 0 ;; |
| 96 | + gemini-*) printf 'gemini\n'; return 0 ;; |
| 97 | + muse*) printf 'meta\n'; return 0 ;; |
| 98 | + codex* | gpt-* | o[0-9]*) printf 'codex\n'; return 0 ;; |
| 99 | + esac |
| 100 | + if fm_quota_provider_for_harness "$harness" 2>/dev/null; then |
| 101 | + return 0 |
| 102 | + fi |
| 103 | + if fm_quota_single_provider_for_harness "$harness" 2>/dev/null; then |
| 104 | + return 0 |
| 105 | + fi |
| 106 | + return 1 |
| 107 | +} |
| 108 | + |
| 109 | +# fm_lane_provider <harness> <model> |
| 110 | +# The bucket a lane is counted against: the model-derived provider when one is |
| 111 | +# known, else the harness name so unmapped lanes still count somewhere. |
| 112 | +fm_lane_provider() { # <harness> <model> |
| 113 | + local harness=${1:-} |
| 114 | + if fm_provider_for_model "$1" "${2:-}" 2>/dev/null; then |
| 115 | + return 0 |
| 116 | + fi |
| 117 | + [ -n "$harness" ] || return 1 |
| 118 | + printf '%s\n' "$harness" |
| 119 | +} |
| 120 | + |
| 121 | +# fm_provider_cap_for <provider> [<config-dir>] |
| 122 | +fm_provider_cap_for() { # <provider> [<config-dir>] |
| 123 | + local provider=${1:-} config=${2:-} cap='' |
| 124 | + if [ -n "$config" ] && [ -r "$config/crew-dispatch.json" ] && command -v jq >/dev/null 2>&1; then |
| 125 | + cap=$(jq -r --arg p "$provider" ' |
| 126 | + (.providerCaps // {}) as $c |
| 127 | + | ($c[$p] // $c.default // empty) |
| 128 | + | select(type == "number" and . >= 1 and . == floor) |
| 129 | + ' "$config/crew-dispatch.json" 2>/dev/null || true) |
| 130 | + fi |
| 131 | + case "$cap" in |
| 132 | + '' | *[!0-9]*) cap=$FM_PROVIDER_LANE_CAP_DEFAULT ;; |
| 133 | + esac |
| 134 | + printf '%s\n' "$cap" |
| 135 | +} |
| 136 | + |
| 137 | +# fm_provider_lane_counts <state-dir> [<config-dir>] [<exclude-id>] |
| 138 | +fm_provider_lane_counts() { # <state-dir> [<config-dir>] [<exclude-id>] |
| 139 | + local state=${1:-} config=${2:-} exclude=${3:-} |
| 140 | + local meta id harness model provider backend target endpoint_state |
| 141 | + [ -n "$state" ] || return 0 |
| 142 | + for meta in "$state"/*.meta; do |
| 143 | + [ -e "$meta" ] || [ -L "$meta" ] || continue |
| 144 | + id=${meta##*/} |
| 145 | + id=${id%.meta} |
| 146 | + [ -n "$exclude" ] && [ "$id" = "$exclude" ] && continue |
| 147 | + harness=$(fm_meta_get "$meta" harness) |
| 148 | + model=$(fm_meta_get "$meta" model) |
| 149 | + provider=$(fm_lane_provider "$harness" "$model" 2>/dev/null) || continue |
| 150 | + [ -n "$provider" ] || continue |
| 151 | + backend=$(fm_backend_of_meta "$meta") |
| 152 | + target=$(fm_backend_target_of_meta "$meta") |
| 153 | + if [ -n "$target" ]; then |
| 154 | + endpoint_state=$(fm_backend_agent_state "$backend" "$target" 2>/dev/null || printf 'unreadable') |
| 155 | + case "$endpoint_state" in |
| 156 | + dead | missing) continue ;; |
| 157 | + esac |
| 158 | + fi |
| 159 | + printf '%s\n' "$provider" |
| 160 | + done | LC_ALL=C sort | uniq -c | while read -r used provider; do |
| 161 | + printf '%s %s %s\n' "$provider" "$used" "$(fm_provider_cap_for "$provider" "$config")" |
| 162 | + done |
| 163 | +} |
| 164 | + |
| 165 | +# fm_provider_cap_refuse <state-dir> <config-dir> <harness> <model> [<exclude-id>] |
| 166 | +fm_provider_cap_refuse() { # <state-dir> <config-dir> <harness> <model> [<exclude-id>] |
| 167 | + local state=${1:-} config=${2:-} harness=${3:-} model=${4:-} exclude=${5:-} |
| 168 | + local provider used cap |
| 169 | + provider=$(fm_lane_provider "$harness" "$model" 2>/dev/null) || return 0 |
| 170 | + [ -n "$provider" ] || return 0 |
| 171 | + cap=$(fm_provider_cap_for "$provider" "$config") |
| 172 | + used=$(fm_provider_lane_counts "$state" "$config" "$exclude" | awk -v p="$provider" '$1 == p { print $2; exit }') |
| 173 | + case "$used" in |
| 174 | + '' | *[!0-9]*) used=0 ;; |
| 175 | + esac |
| 176 | + if [ "$used" -ge "$cap" ]; then |
| 177 | + printf 'error: provider lane cap: %s already carries %s live lanes (cap %s), so dispatching harness=%s model=%s would exceed it; reassign to a provider with headroom, or raise providerCaps.%s in %s/crew-dispatch.json (fallback cap %s).\n' \ |
| 178 | + "$provider" "$used" "$cap" "${harness:-unknown}" "${model:-default}" \ |
| 179 | + "$provider" "${config:-<config>}" "$FM_PROVIDER_LANE_CAP_DEFAULT" >&2 |
| 180 | + return 1 |
| 181 | + fi |
| 182 | + return 0 |
| 183 | +} |
0 commit comments