routing

package
v0.10.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package routing resolves an inbound inference request to a fully-typed RequestPlan that the pipeline can consume. All catalog lookups happen here, against the in-memory snapshot. The pipeline itself is ignorant of the snapshot.

Resolution flow:

  1. Model: caller supplies a snapshot name (from the request body's `model` field); look it up via snapshot.SnapshotByName. The owning Model + the picked Snapshot are carried into the Plan.
  2. Policy: supplied by the caller, already resolved from the credential's principal at the edge. (No "default route" indirection; anonymous traffic is served by a separate package.)
  3. Authorization: model must be allowed by the Policy. Allowed if its id is in Spec.ModelIDs, OR Spec.Models (modelref DSL) matches, OR — when both grant fields are empty — the policy is an implicit wildcard: any model reachable via its hostkeys is allowed. The hostkey-coverage check below is the real gate in that case.
  4. HostBinding: pick one of the model's HostBindings (snapshot. BindingsForModel) the operator has configured. v1 picks the first enabled binding; multi-host failover is a future feature.
  5. Host: lookup by binding.Spec.HostID for BaseURL.
  6. Keys: Policy.Spec.HostKeyIDs filtered to those whose Owner.ID is the chosen Host (a key authenticates against one host).
  7. RateLimit: Policy.Spec.RateLimitID, resolved to []pkgratelimit.Rule.

Each lookup is a snapshot.Get — no PG, no I/O. Resolve() is allocation- conscious where it matters but not micro-optimised; the hot-path budget dominates this.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrModelNotFound    = errors.New("routing: model not found")
	ErrModelDisabled    = errors.New("routing: model disabled")
	ErrPolicyNotFound   = errors.New("routing: policy not found")
	ErrPolicyDisabled   = errors.New("routing: policy disabled")
	ErrModelNotInPolicy = errors.New("routing: model not allowed by policy")
	ErrNoHostBinding    = errors.New("routing: no enabled host binding for model")
	ErrHostNotFound     = errors.New("routing: host not found")
	ErrNoKeys           = errors.New("routing: no host keys available for this host")

	// ErrPolicyless is returned when a Key has no PolicyID and the
	// inference settings forbid policy-less traffic.
	ErrPolicyless = errors.New("routing: relay key has no policy and policy-less traffic is disabled")
)

Errors returned by Resolve. Each maps to a distinct HTTP status in the handler; routing keeps them as typed sentinels so handlers can errors.Is() rather than parse strings.

Functions

This section is empty.

Types

type Option added in v0.10.0

type Option func(*Resolver)

Option configures a Resolver at composition time.

func PersonalRowsOwnerOnly added in v0.10.0

func PersonalRowsOwnerOnly() Option

PersonalRowsOwnerOnly makes a personal (user-owned) host or binding exist only for its owner's requests. RBAC authorization wires it: there users are distinct tenants. Under single-user authorization every caller is the operator, whose personal rows stay shared with every credential.

func RequirePolicy added in v0.10.0

func RequirePolicy() Option

RequirePolicy refuses policy-less traffic whatever settings.Inference.AllowMissingPolicy says. RBAC authorization wires it: there a credential's grants are the whole access model, so a key whose policy does not resolve has no access rather than the shared pool's.

type Plan

type Plan struct {
	Model       *model.Model
	Snapshot    *model.Snapshot
	Policy      *policy.Policy
	HostBinding *binding.Binding
	Host        *host.Host
	Provider    string
	Keys        []*hostkey.HostKey

	// Pricing is the rate sheet billing against the chosen binding (explicit
	// binding ref first, else the host-owned (model, host) cover). Resolved
	// here so emit-time cost stamping costs zero extra lookups. Nil = unpriced.
	Pricing *pricing.Pricing

	// PayloadLoggingEnabled is the resolved opt-in for full request/response
	// body capture: true when the matched Policy or the inbound Key
	// sets PayloadLoggingEnabled. Read by the inference entry to flag the
	// lifecycle Context so the payloadlog observer captures bodies.
	PayloadLoggingEnabled bool

	// UpstreamOverride, when non-empty, replaces Snapshot.Upstream() as
	// the wire model name. Set only by declared-alias resolution: the
	// alias string itself (exact match) or the caller's raw request
	// string (wildcard match). Consumers read the wire name via
	// UpstreamModel(), never Snapshot.Upstream() directly.
	UpstreamOverride string

	// ResolvedVia tags non-canonical resolution for usage events, e.g.
	// "alias:claude-fable-5[1m]" (the declared alias or pattern that
	// matched). Empty for normal snapshot-name resolution.
	ResolvedVia string
}

Plan is the fully-resolved input the pipeline consumes. The handler converts this to pipeline.Request, dropping fields the pipeline doesn't need.

Snapshot is the resolved checkpoint. The handler rewrites the request body's `model` field to Plan.UpstreamModel() before invoking the adapter.

func (*Plan) UpstreamModel added in v0.3.0

func (p *Plan) UpstreamModel() string

UpstreamModel returns the wire model name for this plan — the alias verbatim override when resolution went through a declared alias, otherwise the snapshot's upstream name.

type Request

type Request struct {
	// ModelName is the slug or upstream-name reference the caller asked
	// for (typically from the body's "model" field), possibly with a
	// header-derived "@host" pin folded in by the handler.
	ModelName string

	// RawModelName is the verbatim caller string before any header pin
	// folding. Carried upstream as the wire name when the ref matches a
	// wildcard alias. Falls back to ModelName when empty.
	RawModelName string

	// Policy is the caller's resolved inbound policy (the middleware walks
	// key → service account → policy binding). Nil selects the policy-less
	// flow, which only a project-less personal key, or a key in the legacy
	// project with no policy bound, can reach.
	Policy *policy.Policy

	// UserID is the calling user, from the credential's principal. It scopes
	// the policy-less pool to that user's own host keys; empty (no user
	// resolved) narrows the pool to system-owned keys.
	UserID string

	// PayloadLoggingEnabled is the caller's own opt-in for body capture;
	// the matched Policy's flag is OR'd onto it.
	PayloadLoggingEnabled bool

	// SkipKeyCheck, when true, suppresses the Policy.HostKeyIDs → host
	// coverage gate. Used by proxy mode: the caller brings their own
	// upstream credentials, so the relay's keypool is irrelevant — only
	// the (model, binding, host) tuple matters. Plan.Keys is nil in
	// this mode.
	SkipKeyCheck bool

	// Snapshot pins the catalog view this resolution reads. Nil takes the
	// live one; handlers pass the snapshot the auth middleware already
	// resolved against, so one request never straddles two of them.
	Snapshot *appcatalog.Snapshot
}

Request carries the inbound resolution inputs.

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver wraps a Catalog snapshot accessor and answers Resolve calls.

func New

func New(cat *appcatalog.Catalog, opts ...Option) *Resolver

New constructs a Resolver against the live catalog. The Resolver reads cat.Current() on every Resolve — picking up the latest snapshot after any NOTIFY-driven reload.

func (*Resolver) PolicyAllows added in v0.10.0

func (r *Resolver) PolicyAllows(snap *appcatalog.Snapshot, pol *policy.Policy, m *model.Model, userID string) bool

PolicyAllows reports whether m is reachable through pol over any of its enabled bindings. Used to enumerate accessible models for inventory endpoints. Single-shot; not optimised for tight loops. userID is the calling user, which decides whether personal rows exist for the caller.

func (*Resolver) PolicyAllowsBinding added in v0.10.0

func (r *Resolver) PolicyAllowsBinding(snap *appcatalog.Snapshot, pol *policy.Policy, m *model.Model, hb *binding.Binding, userID string) bool

PolicyAllowsBinding reports whether pol would let Resolve route m over hb: first the grant check, then the same key + tier gate Resolve applies (a NoAuth host yields the anonymous key), so a listed binding is one the caller can actually reach. Callers filter enabled bindings themselves.

func (*Resolver) PolicylessAllows added in v0.10.0

func (r *Resolver) PolicylessAllows(snap *appcatalog.Snapshot, m *model.Model, adapter adapters.Name, userID string) bool

PolicylessAllows reports whether m is reachable by a request that resolved no policy — the inventory question matching resolvePolicyless, which is the only thing that serves such a request. adapter narrows the answer to bindings declaring that wire shape; empty accepts any. userID is the calling user, scoping the pool exactly as resolution does.

Mirrors resolvePolicyless step for step: enabled model, not deprecated, enabled binding, resolvable host, and a key the policy-less pool yields.

func (*Resolver) PolicylessAllowsBinding added in v0.10.0

func (r *Resolver) PolicylessAllowsBinding(snap *appcatalog.Snapshot, m *model.Model, hb *binding.Binding, userID string) bool

PolicylessAllowsBinding is the per-binding form of PolicylessAllows: would resolvePolicyless route m over hb for userID. Callers filter enabled bindings themselves.

func (*Resolver) PolicylessTrafficAllowed added in v0.10.0

func (r *Resolver) PolicylessTrafficAllowed() bool

PolicylessTrafficAllowed reports whether a caller whose credential resolved no policy is served at all. Exported so the /v1/models listing asks the same gate resolution applies and cannot advertise what a request would be refused. Nil-safe.

func (*Resolver) Resolve

func (r *Resolver) Resolve(req Request) (*Plan, error)

Resolve maps the inbound request to a Plan. Errors are typed; handlers pick the appropriate HTTP status.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL