route

package
v0.3.29 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 15 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 decides which model a piece of work runs on, from rules the user can read and edit in agents.json.

Deciding is pure: it reads no file and makes no request. It is given the policy, the task, and the agent and model the work would run on without it, and answers with a model and a sentence saying why. The caller shows that before the work starts -- a fan-out row's select is pre-set to it, and the user confirms -- so a choice shown is the choice run. The one thing here that touches the disk is the routing log (log.go), which holds rule names and model ids and never the task.

Index

Constants

View Source
const (
	OutcomeKept       = "kept"
	OutcomeOverridden = "overridden:"
)

Outcomes a decision can have.

View Source
const (
	KindFanout = "fanout"
	KindSpawn  = "spawn"
	KindTurn   = "turn"
)

The kinds of work a rule's "kind" can name.

View Source
const LogName = "routing.jsonl"

LogName is the routing log, in Flockdeck's state directory.

It is how the user, and later the spend view, can tell whether routing saves anything and how often its choices are overridden. It stays on this machine: nothing reads it but Flockdeck, on the user's behalf, and Settings clears it. It holds no task text and no prompt -- only the name of the rule that chose -- because the task is already in the user's own transcripts, and a log of prompts would be one more copy of them to leak.

View Source
const SourceFallback = "fallback"

SourceFallback is the Source of a decision made not by a rule but by Strategy == agent.StrategyCost, when no rule matched: see Router.fallback.

View Source
const SourceRule = "rule"

SourceRule is the Source of a decision a rule made.

Variables

This section is empty.

Functions

func AppendLog

func AppendLog(dir string, entries ...LogEntry) error

AppendLog adds decisions to the log in dir.

func BuiltinRules

func BuiltinRules() []agent.RoutingRule

BuiltinRules are the rules a policy that names none of its own applies.

They are deliberately timid. They move clearly mechanical work down and clearly hard work up, and leave everything else exactly where it would have been: a rule set that routes a third of a typical fan-out is doing well, and one that routes everything is a bug. The rules for hard work come first, because the first rule to match wins, and a task that reads as both -- "move the session store behind a lock to fix the race" -- is the one a cheap attempt would cost most to get wrong.

func ClearLog

func ClearLog(dir string) error

ClearLog deletes the log in dir.

func MatchGlob

func MatchGlob(glob, name string) bool

MatchGlob matches a path against a glob: path.Match for each part, with "**" standing for any number of directories. A glob with no slash in it is matched against the path's last part, so "*.sql" finds db/0003.sql as well as 0003.sql. Case is not compared, since the same path is written both ways on Windows and macOS.

A glob is read with forward slashes, and from the project root without a leading "./", as the paths it is matched against already are: one written the way Windows writes a path, "db\migrations\**", or "./migrations/**", matched nothing at all and said nothing about it.

func RulesOf

func RulesOf(p agent.RoutingPolicy) []agent.RoutingRule

RulesOf is the rules a policy applies: its own, or the built-in ones when it names none. An empty list is a policy that deliberately has no rules.

func TierOf

func TierOf(spec agent.Spec, model string) int

TierOf is the rank of a model among the agent's own, and 0 where the agent does not list it or gives it no tier.

Types

type Decision

type Decision struct {
	// Model is the model to run, and Tier its tier; both are empty, and
	// Routed false, when the work is left alone.
	Model  string
	Tier   string
	Routed bool
	// Up says the work was moved to a stronger model than it would have had.
	Up bool
	// Agent is the agent the work moves to, and empty when it stays on the
	// one it was going to run on. Set only by a rule naming a model of
	// another agent, and only when Input.OtherAgent allowed it.
	Agent string
	// Source is what chose (SourceRule), and Rule the rule's name.
	Source string
	Rule   string
	// Reason is one line a person reads: why the model was chosen, or why a
	// rule that matched changed nothing.
	Reason string
}

Decision is what routing made of one piece of work.

func Decide

func Decide(p agent.RoutingPolicy, in Input) Decision

Decide is New(p).Decide(in), for one piece of work.

type Input

type Input struct {
	Kind string // KindFanout, KindSpawn or KindTurn
	Task string // the row, the spawn task, or the prompt
	// Agent is the agent the work would run on without routing. A rule may
	// still move it to another agent -- see OtherAgent -- but routing never
	// does that on its own: another agent is another login, other tools and
	// another transcript, which is far more than a model.
	Agent agent.Spec
	// Current is the model the work would get without routing.
	Current string
	// NoDowngrade says the work may only be moved to a stronger model.
	NoDowngrade bool
	// OtherAgent resolves an agent id to its Spec, but only when work could
	// be handed to it here and now: installed or keyed, trusted for the
	// project, and, for an endpoint of its own, answering. It is the
	// caller's to supply, because deciding stays pure and does no I/O of its
	// own. Nil means work is never moved to another agent, whatever a rule
	// asks for -- which is also true for any Kind other than KindFanout and
	// KindSpawn, and for a policy whose CrossAgent is off: a running
	// conversation cannot change agent under it, and a switch nobody turned
	// on for this project is not one routing takes on its own.
	OtherAgent func(id string) (agent.Spec, bool)
}

Input is one piece of work to decide a model for.

type LogEntry

type LogEntry struct {
	At      time.Time `json:"at"`
	Kind    string    `json:"kind"`
	Project string    `json:"project,omitempty"`
	Pane    string    `json:"pane,omitempty"`
	// Agent is the agent the work ran on, as it always has been. BaselineAgent
	// is the agent it would have run on without routing, and is left empty
	// unless a rule moved the work to another agent -- so an old log line,
	// and every same-agent decision since, still reads as it always has.
	Agent         string `json:"agent,omitempty"`
	BaselineAgent string `json:"baselineAgent,omitempty"`
	Source        string `json:"source"`
	Rule          string `json:"rule,omitempty"`
	// Baseline is the model the work would have run without routing, and
	// Routed the model routing chose for it.
	Baseline string `json:"baseline"`
	Routed   string `json:"routed"`
	// Outcome is what became of the choice: "kept", or "overridden:<model>"
	// when the user chose another before it started.
	Outcome string `json:"outcome"`
}

LogEntry is one decision, as the log records it.

func ReadLog

func ReadLog(dir string) ([]LogEntry, error)

ReadLog returns every decision in the log in dir, oldest first. A line that does not parse is passed over: the log is the user's to edit or truncate.

type Router

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

Router is a policy made ready to decide with: its rules compiled once, for a fan-out that asks about a dozen rows at a time.

func New

func New(p agent.RoutingPolicy) *Router

New makes a Router of a policy. A rule that cannot be used -- a pattern that does not compile, a tier that is not one -- is left out; the catalog has already named it in its notice when agents.json was read.

func (*Router) Decide

func (r *Router) Decide(in Input) Decision

Decide applies the rules to one piece of work, in order: the first that matches decides, and no match leaves the work exactly as it was -- unless the policy's Strategy is agent.StrategyCost, which routes unmatched work too (see fallback). Then the floor, "never downgrade" and what the agent offers bound what that rule, or the fallback, may choose.

func (*Router) On

func (r *Router) On() bool

On reports whether the policy routes anything at all.

type RuleStat added in v0.3.26

type RuleStat struct {
	Rule       string
	Kept       int
	Overridden int
}

RuleStat is one rule's record in the routing log: how often the log has it deciding, and how often what it chose was changed before the work started.

func Stats added in v0.3.26

func Stats(entries []LogEntry) []RuleStat

Stats summarises a routing log by rule: this is the evidence routing.jsonl is kept for -- the override rate that says which rules are worth hand-editing -- read back rather than left for a person to add up line by line. It only reports; nothing here changes a rule or a policy. A decision StrategyCost's fallback made (Source SourceFallback, Rule "") is not a named rule, so it is left out: there is nothing in agents.json to edit for it. Rules are returned in the order they are first seen, oldest first.

func StatsByOverrideRate added in v0.3.26

func StatsByOverrideRate(entries []LogEntry) []RuleStat

StatsByOverrideRate is Stats, ordered worst first -- the rule most often overridden is the one most worth a person's attention -- and stable for rules tied on rate, so the same log always reads the same way.

func (RuleStat) OverrideRate added in v0.3.26

func (s RuleStat) OverrideRate() float64

OverrideRate is the share of the rule's decisions that were changed before the work started, or 0 for a rule the log has never seen decide.

func (RuleStat) Total added in v0.3.26

func (s RuleStat) Total() int

Total is how many logged decisions the rule made, kept or overridden.

Jump to

Keyboard shortcuts

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