config

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 16 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 config holds the router configuration: tiers, activities, routes, policy, classifiers, and the JSON file under the XDG config dir they load from. It is a leaf package: nothing in internal/ is imported here.

Index

Constants

View Source
const (
	DirName  = "codex-router"
	FileName = "config.json"
)

File locations under the XDG config dir.

View Source
const (
	APISystemOne       = "system-one"
	APIOpenAIDecisions = "openai-decisions"
	APIOllama          = "ollama"
)

Classifier wire adapters.

View Source
const ClassifierOff = "off"

ClassifierOff as Config.Classifier turns the classifier off: no prompt leaves the machine, and only pins and the context guard change the model.

View Source
const ProjectFile = ".codex/router.json"

ProjectFile is a project's routing mode file, relative to a directory between a session's cwd and its repository root.

Variables

View Source
var (
	ErrEnvFileMode = errors.New("open to group or others; chmod 600 it")
	ErrKeyNotInEnv = errors.New("key not in file")
)

Env file errors. Their text never holds a value from the file.

Activities lists the seven routable activities.

Efforts lists efforts weakest first.

RoutingModes lists the modes, cheapest first.

Tiers lists every tier by rank.

Functions

func EndpointPlaceholders added in v0.5.0

func EndpointPlaceholders(endpoint string) ([]string, error)

EndpointPlaceholders returns the NAMEs of the {NAME} placeholders in endpoint, in order of first use. A brace that is not part of a well-formed placeholder is an error.

func ExpandHome added in v0.3.0

func ExpandHome(path, home string) string

ExpandHome turns a leading "~/" into home. Other paths are returned as is.

func Path

func Path() (string, error)

Path returns the config file location: $XDG_CONFIG_HOME/codex-router/config.json, else ~/.config/codex-router/config.json. A relative XDG_CONFIG_HOME is ignored per the XDG spec.

func ProjectModes added in v0.9.0

func ProjectModes(cwd func(threadID string) string) func(threadID string) (RoutingMode, error)

ProjectModes is ProjectMode per thread, from each thread's cwd; nil when cwd is nil.

func ReadEnvKey added in v0.3.0

func ReadEnvKey(path, name string) (string, error)

ReadEnvKey returns the value of name in the dotenv file at path: lines KEY=value or export KEY=value, values optionally in single or double quotes (no escapes), # comment lines, and a " #" comment after an unquoted value. The last assignment wins; an empty value counts as missing. Nothing is exported to the process env. A file that group or others can read is refused, as a shared key file is a leaked key.

Types

type Activity

type Activity string

Activity names what a turn produces. ActivityUncertain is a classifier answer, never a config key.

const (
	ActivityCode      Activity = "code"
	ActivityDebug     Activity = "debug"
	ActivityExplore   Activity = "explore"
	ActivityPlan      Activity = "plan"
	ActivityReview    Activity = "review"
	ActivityOps       Activity = "ops"
	ActivityDocs      Activity = "docs"
	ActivityUncertain Activity = "uncertain"
)

type Cache

type Cache struct {
	TTLMs        int `json:"ttlMs"`
	WarmMarginMs int `json:"warmMarginMs"`
}

Cache models prompt-cache warmth: per model, effort-neutral (spikes 3, 5).

type Classifier

type Classifier struct {
	Label     string `json:"label"`
	API       string `json:"api"`
	Endpoint  string `json:"endpoint"`
	Model     string `json:"model"`
	KeyEnv    string `json:"keyEnv,omitempty"`
	EnvFile   string `json:"envFile,omitempty"`
	TimeoutMs int    `json:"timeoutMs"`
}

Classifier describes one classifier endpoint. API names the wire adapter ("system-one", "openai-decisions" or "ollama"). KeyEnv names the environment variable holding the bearer key; empty means no key. Endpoint may hold {NAME} placeholders, filled from the variable NAME. EnvFile is a dotenv file (absolute or ~/ path) read for KeyEnv and each placeholder when the process env does not set it: service units do not see the shell's env.

type Classifiers added in v0.3.2

type Classifiers map[string]Classifier

Classifiers maps a name to a classifier. A config file's entry is decoded over the entry of the same name, so it names only the fields it changes.

func (*Classifiers) UnmarshalJSON added in v0.3.2

func (cs *Classifiers) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes each entry over the one already in cs. Unknown fields are errors, as in the rest of the file.

type Config

type Config struct {
	Baseline   Tier                        `json:"baseline"`
	Routes     map[Tier]Route              `json:"routes"`
	Activities map[Activity]map[Tier]Route `json:"activities,omitempty"`
	Prices     map[string]Price            `json:"prices,omitempty"`
	Policy     Policy                      `json:"policy"`
	// Mode is the profile's routing mode; empty means not set (balanced).
	Mode        RoutingMode `json:"mode,omitempty"`
	Modes       Modes       `json:"modes"`
	Classifier  string      `json:"classifier"`
	Classifiers Classifiers `json:"classifiers"`
	Context     Context     `json:"context"`
	Cache       Cache       `json:"cache"`
	Proxy       Proxy       `json:"proxy"`
	CodexBin    string      `json:"codexBin,omitempty"`
}

Config is the whole router configuration.

func Default

func Default() Config

Default returns the built-in configuration, classifier Jev with Clef, OpenAI and a local Ollama option. Routes use two models of one compatibility group (plan D2): gpt-6-luna for micro and low, gpt-6.1-sol for medium and high. Codex caches per model, not per effort, so only a move between the two models costs cache. Tuned on live routing tests and benchmarks on 2026-10-15/16: Luna at high effort matched Sol on bounded review, debug, ops, code, explore and plan tasks at 5-10% of the list cost; xhigh on either model cost more and planned no better than high.

func Load

func Load(path string) (Config, error)

Load reads the config file over Default(): keys the file omits keep their defaults, a map entry the file names replaces the default entry whole, and a classifiers entry changes only the fields it names. A missing file yields the defaults. Unknown keys and invalid values are errors.

func (Config) DecideTimeout

func (c Config) DecideTimeout() time.Duration

DecideTimeout bounds one routing decision: the selected classifier's timeout plus a margin, so the proxy outlasts the classifier call.

func (Config) Effective

func (c Config) Effective(mode RoutingMode) Policy

Effective returns Policy with mode's patch applied: a field the patch sets replaces the configured value. An empty mode is balanced.

func (Config) RouteFor

func (c Config) RouteFor(t Tier, a Activity) Route

RouteFor returns the route for a tier and activity. An activity override replaces the tier's model and effort field by field; an empty override field inherits the tier's route.

func (Config) Validate

func (c Config) Validate() error

Validate checks the config and reports the first problem with its field path.

type Context

type Context struct {
	RecentTurns  int `json:"recentTurns"`
	MaxTextChars int `json:"maxTextChars"`
}

Context bounds what the classifier sees.

type Effort

type Effort string

Effort is a Codex reasoning effort as sent in reasoning.effort.

const (
	EffortLow    Effort = "low"
	EffortMedium Effort = "medium"
	EffortHigh   Effort = "high"
	EffortXHigh  Effort = "xhigh"
)

type ModeConfig added in v0.9.0

type ModeConfig struct {
	Policy PolicyPatch `json:"policy"`
}

ModeConfig is one mode's settings.

type Modes added in v0.9.0

type Modes map[RoutingMode]ModeConfig

Modes maps a mode to its settings. A config file's entry is decoded over the built-in entry of the same mode, so it names only the fields it changes.

func (*Modes) UnmarshalJSON added in v0.9.0

func (ms *Modes) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes each entry over the one already in ms, so a file changes only the patch fields it names; null clears a built-in field. Unknown fields are errors.

type Policy

type Policy struct {
	UpgradeVotes         int     `json:"upgradeVotes"`
	UpgradeBase          float64 `json:"upgradeBase"`
	UpgradeSlope         float64 `json:"upgradeSlope"`
	UpgradePivotTokens   int     `json:"upgradePivotTokens"`
	JumpConfidence       float64 `json:"jumpConfidence"`
	DowngradeVotes       int     `json:"downgradeVotes"`
	DowngradeMass        float64 `json:"downgradeMass"`
	DowngradeSlope       float64 `json:"downgradeSlope"`
	DowngradePivotTokens int     `json:"downgradePivotTokens"`
	ContinuationMass     float64 `json:"continuationMass"`
	EscalationHoldTurns  int     `json:"escalationHoldTurns"`
	ReviewEveryRequests  int     `json:"reviewEveryRequests"`
	MinDwellRequests     int     `json:"minDwellRequests"`
	ActivityMass         float64 `json:"activityMass"`
	ContextFill          float64 `json:"contextFill"`
	// Deprecated: accepted for existing configs; has no effect.
	DowngradeHorizonTurns int `json:"downgradeHorizonTurns,omitempty"`
}

Policy holds the switching thresholds. Taxes are in uncached tokens.

type PolicyPatch added in v0.9.0

type PolicyPatch struct {
	UpgradeVotes         *int     `json:"upgradeVotes,omitempty"`
	UpgradeBase          *float64 `json:"upgradeBase,omitempty"`
	UpgradeSlope         *float64 `json:"upgradeSlope,omitempty"`
	UpgradePivotTokens   *int     `json:"upgradePivotTokens,omitempty"`
	JumpConfidence       *float64 `json:"jumpConfidence,omitempty"`
	DowngradeVotes       *int     `json:"downgradeVotes,omitempty"`
	DowngradeMass        *float64 `json:"downgradeMass,omitempty"`
	DowngradeSlope       *float64 `json:"downgradeSlope,omitempty"`
	DowngradePivotTokens *int     `json:"downgradePivotTokens,omitempty"`
	ContinuationMass     *float64 `json:"continuationMass,omitempty"`
	EscalationHoldTurns  *int     `json:"escalationHoldTurns,omitempty"`
	ReviewEveryRequests  *int     `json:"reviewEveryRequests,omitempty"`
	MinDwellRequests     *int     `json:"minDwellRequests,omitempty"`
	ActivityMass         *float64 `json:"activityMass,omitempty"`
	ContextFill          *float64 `json:"contextFill,omitempty"`
}

PolicyPatch holds the Policy fields a mode replaces; a nil field keeps the configured value.

type Price

type Price struct {
	Input       float64 `json:"input"`
	CachedInput float64 `json:"cachedInput"`
	Output      float64 `json:"output"`
}

Price is a model's list price in USD per million tokens. Zero values mean unpriced: estimates then stay in tokens.

type Proxy

type Proxy struct {
	Port int `json:"port"`
}

Proxy holds listener settings. Port 0 binds an ephemeral port per session.

type Route

type Route struct {
	Model  string `json:"model"`
	Effort Effort `json:"effort,omitempty"`
}

Route is a model slug plus the effort the router sends for it. An empty Effort keeps the effort Codex sent.

type RoutingMode added in v0.9.0

type RoutingMode string

RoutingMode trades cost against answer quality: it moves the route a tier aims at and patches the policy. It is not route.Mode (on, paused, manual).

const (
	ModeCost     RoutingMode = "cost"
	ModeBalanced RoutingMode = "balanced"
	ModeQuality  RoutingMode = "quality"
)

func ProjectMode added in v0.9.0

func ProjectMode(cwd string) (RoutingMode, string, error)

ProjectMode returns the mode set by the project file nearest to cwd, and that file's path. The search runs from cwd up to the nearest directory holding .git, inclusive; outside a repository only cwd is searched. No file gives no mode, no path and no error. A file that does not parse, holds a key other than mode, or names an unknown mode is an error, with its path.

type Tier

type Tier string

Tier is a task-complexity level. Tiers is ordered weakest first; the index in Tiers is the rank.

const (
	TierMicro  Tier = "micro"
	TierLow    Tier = "low"
	TierMedium Tier = "medium"
	TierHigh   Tier = "high"
)

Jump to

Keyboard shortcuts

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