route

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 21 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 route is the router engine: it keys state by thread, turn and request fingerprint, asks the classifier on a turn's first step and in background reviews, runs the policy, checks the compatibility group and context fit, pins a decision for retries, and publishes decisions and snapshots. It implements proxy.Decider, proxy.Observer and control.Handler without importing those packages. Every error fails open: no rewrite.

Index

Constants

View Source
const (
	ReasonActivityUp      policy.Reason = "activity-up"
	ReasonActivityDown    policy.Reason = "activity-down"
	ReasonActivityPending policy.Reason = "activity-pending"
	ReasonObserved        policy.Reason = "observed"
	ReasonContinue        policy.Reason = "continue"
)

Reasons the engine adds to policy's: activity moves inside a tier, a move up on the turn's own tool calls, and a later step keeping the turn's route.

View Source
const (
	MaxThreads = 256
	RecentKept = 64
	StripCells = 48
)

Bounds on remembered state.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	Op     Op            `json:"op"`
	Tier   config.Tier   `json:"tier,omitempty"`
	Preset config.Preset `json:"preset,omitempty"`
}

Command is what the TUI or hook asks the engine to do.

type Decision

type Decision struct {
	ThreadID  string
	TurnID    string
	Step      int
	Tier      config.Tier
	Activity  config.Activity
	Route     config.Route
	Original  config.Route
	Rewritten bool
	Reason    policy.Reason
	Why       string
	Support   string
	Estimate  *policy.Estimate
	Reading   *Reading
	MidTurn   bool
	At        time.Time
}

Decision is one per-request outcome.

type Deps

type Deps struct {
	Config     config.Config
	Catalog    catalog.Source
	Classifier classifier.Asker
	Now        func() time.Time
	Sink       Sink
	Logger     *slog.Logger
}

Deps are the engine's boundaries.

type Engine

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

Engine routes user steps. All state lives under mu; step serializes the requests of one thread so a classifier call never runs under mu.

func New

func New(d Deps) *Engine

New returns an engine. A nil Now uses time.Now, a nil Logger discards.

func (*Engine) Apply

func (e *Engine) Apply(c Command) (err error)

Apply runs a control command: pin or unpin the next turn's tier, pause or resume routing, or pick a policy preset. Pause writes each routed thread's own route back to its settings; resume also ends manual mode. A panic is logged and returned as an error.

func (*Engine) Decide

func (e *Engine) Decide(ctx context.Context, req *responses.Request, m responses.Meta) (rw responses.Rewrite, ok bool)

Decide returns the rewrite for one request, or false to forward it unchanged. Any panic or error forwards unchanged.

func (*Engine) Observe

func (e *Engine) Observe(m responses.Meta, served string, u responses.Usage, status int)

Observe records a reply: the cache ledger, the thread's size, and the session stats when the reply answers a routed step.

func (*Engine) SettingsUpdated

func (e *Engine) SettingsUpdated(threadID string, r config.Route)

SettingsUpdated takes a thread/settings/updated notification. A value the router wrote is its echo; any other value is the user's own pick (plan D12): the thread goes manual until resume. A panic is logged and dropped.

func (*Engine) Snapshot

func (e *Engine) Snapshot() Snapshot

Snapshot returns the full state for the control socket and the TUI.

type Finding

type Finding struct {
	Kind FindingKind
	Text string
}

Finding is one WORTH A LOOK line derived from the session's facts.

type FindingKind

type FindingKind string

FindingKind names what a WORTH A LOOK finding is about, and so where the user changes it.

const (
	FindingClassifier FindingKind = "classifier" // paused or failing
	FindingManual     FindingKind = "manual"     // the footer model was picked with /model
	FindingContext    FindingKind = "context"    // the context guard kept larger windows
	FindingBlocked    FindingKind = "blocked"    // configured routes outside the group
	FindingCache      FindingKind = "cache"      // low cache reuse after switches
)

type Mode

type Mode string

Mode is the routing mode of a thread.

const (
	ModeOn     Mode = "on"
	ModePaused Mode = "paused"
	ModeManual Mode = "manual"
)

type Op

type Op string

Op is a control command.

const (
	OpPin      Op = "pin"
	OpUnpin    Op = "unpin"
	OpPause    Op = "pause"
	OpResume   Op = "resume"
	OpPreset   Op = "preset"
	OpSnapshot Op = "snapshot"
)

type Reading

type Reading struct {
	Tiers      map[config.Tier]float64
	Activity   config.Activity
	ActivityP  float64
	Alternates []Share
}

Reading is the classifier answer the decision's turn runs on, for display: the probability per tier, and the activity answer with its probability and the two likeliest alternates. Tiers holds only the tiers the answer scored.

type Share

type Share struct {
	Activity config.Activity
	P        float64
}

Share is one activity's probability in an answer.

type Sink

type Sink interface {
	OnDecision(d Decision)
	OnSnapshot(s Snapshot)
}

Sink receives decisions and snapshots; the launcher fans them out to the app-server client and the control socket. Called outside engine locks. OnDecision runs inside Decide, before the request goes upstream, and holds that thread's next step so its settings writes keep their order; an implementation must not block on I/O (queue it instead).

type Snapshot

type Snapshot struct {
	// Seq orders snapshots: they are taken under the engine lock but
	// published after it, so a consumer drops one older than it holds.
	Seq              uint64
	At               time.Time
	Mode             Mode
	Preset           config.Preset
	Pin              *config.Tier
	Baseline         config.Route
	Footer           config.Route
	FooterSynced     bool
	Last             *Decision
	Recent           []Decision
	Classifier       classifier.Health
	ClassifierOff    bool // no classifier configured (e.g. its key is unset)
	ClassifierLabel  string
	ClassifierMedian time.Duration
	Routes           map[config.Tier]config.Route
	Group            catalog.Group
	Blocked          []string
	Policy           config.Policy
	Stats            Stats
	ApproxTokens     int
	Window           int
	Worth            []Finding
}

Snapshot is the full state the control socket publishes and the TUI renders.

type Stats

type Stats struct {
	Since                time.Time
	Replies              int
	Switches             int
	ByTier               map[config.Tier]int
	ByActivity           map[config.Activity]int
	Strip                []config.Tier
	RepliesPerCell       int
	LastSwitch           *Switch
	CacheReusePct        int
	LastInputTokens      int
	LastCachedTokens     int
	UncachedSwitchTokens int
	FitHeld              int
	RoutedUSD            float64
	BaselineUSD          float64
}

Stats sums the session since its first routed reply. LastInputTokens and LastCachedTokens are the last routed reply's; FitHeld counts the steps the context guard moved off the policy's tier.

type Switch

type Switch struct {
	At       time.Time
	From     config.Tier
	To       config.Tier
	Activity config.Activity
	MidTurn  bool
}

Switch is the last route change.

Jump to

Keyboard shortcuts

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