policy

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 3 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 policy decides the tier for a step: escalation on a repeated tool failure, holds, votes with hysteresis, cost-gated upgrade and downgrade bars, and the context-fit tier search. Pure: Input in, Result (with the next State) out. Ported from claude-model-router lib/policy.mjs with taxes in uncached tokens.

Index

Constants

View Source
const VotesKept = 3

VotesKept is how many trailing votes a State remembers.

Variables

This section is empty.

Functions

func FitTier

func FitTier(tier config.Tier, tokens int, window func(config.Tier) (int, bool), fill float64) (config.Tier, bool)

FitTier returns the lowest tier at or above tier whose window holds tokens at fill. Routes need not grow in window with rank, so the search falls back to any tier that fits from the bottom. When none fits it returns the tier with the largest known window (tier itself on a tie or when no window is known) and false. An unknown window never fits.

func SameTask

func SameTask(p config.Policy, a *classifier.Advice, midTurn bool) bool

SameTask reports whether the answer is about the task already running: a review inside a turn, or a new prompt scored as a continuation.

func UpgradeBar

func UpgradeBar(p config.Policy, tax int) float64

UpgradeBar is the probability mass an upgrade needs when it costs tax tokens of cache: the base, plus the slope scaled by the tax share.

Types

type Estimate

type Estimate struct {
	TaxTokens int
	Threshold float64
	Mass      float64
	Streak    int
}

Estimate explains a switch bar: the tax it priced, the bar, the support and the vote streak.

type Input

type Input struct {
	Policy           config.Policy
	Baseline         config.Tier
	Incumbent        config.Tier
	Advice           *classifier.Advice
	FailureSignature string
	MidTurn          bool
	DownAllowed      bool
	Fresh            bool
	State            State
	TaxTokens        func(candidate config.Tier) int
	Escalate         func(incumbent config.Tier) (config.Tier, bool)
}

Input is one decision's facts. TaxTokens prices a candidate tier's next request in uncached tokens; Escalate names the tier a repeated failure moves to, or false when none helps.

type Reason

type Reason string

Reason names why a decision landed where it did.

const (
	ReasonEscalation       Reason = "escalation"
	ReasonHold             Reason = "hold"
	ReasonNoAdvice         Reason = "no-advice"
	ReasonUncertain        Reason = "uncertain"
	ReasonSameTier         Reason = "same-tier"
	ReasonUpgrade          Reason = "upgrade"
	ReasonJump             Reason = "jump"
	ReasonUpgradePending   Reason = "upgrade-pending"
	ReasonDowngrade        Reason = "downgrade"
	ReasonDowngradeHeld    Reason = "downgrade-held"
	ReasonDowngradePending Reason = "downgrade-pending"
	ReasonPinned           Reason = "pinned"
	ReasonPaused           Reason = "paused"
	ReasonManual           Reason = "manual"
	ReasonFit              Reason = "fit"
	ReasonOutOfGroup       Reason = "out-of-group"
	ReasonPassthrough      Reason = "passthrough"
	ReasonError            Reason = "error"
)

type Result

type Result struct {
	Tier     config.Tier
	Reason   Reason
	State    State
	Estimate *Estimate
}

Result is the decided tier with its reason, the next State and the estimate behind a bar, when one was priced.

func Decide

func Decide(in Input) Result

Decide returns the tier for one step. The incumbent is in.Incumbent, or in.Baseline when that is not a tier. A repeated failure signature escalates once per signature and holds; otherwise the advice votes, and a switch must clear a bar that rises with the candidate's tax. A cheaper tier on the same task also waits for in.DownAllowed.

Unlike the Claude router there is no cash gate: Codex bills the plan's limits, and a downgrade tax has no later-turn savings in tokens, so the downgrade bar prices only the next request.

type State

type State struct {
	Turn               int
	Votes              []Vote
	HoldUntilTurn      int
	EscalatedSignature string
}

State is carried from one decision to the next for one thread.

type Vote

type Vote struct {
	Tier config.Tier
}

Vote is one classifier answer counted toward a streak.

Jump to

Keyboard shortcuts

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