runtimes

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package runtimes models where an autonomous task's inference actually runs.

Five things are kept deliberately separate, because collapsing them is how a credential someone happens to have becomes authority a daemon quietly spends:

AVAILABILITY   a runtime exists on this machine
AUTHORIZATION  the user has explicitly allowed tasks to use it
CAPABILITY     it can actually serve what this task needs
SELECTION      which authorized, capable runtime to use
FALLBACK       what may be tried when the RUNTIME itself fails

Availability never implies authorization. A Claude Code or Codex login lives in another tool's files; the user signed into THAT tool, not into a scheduler that will spend their quota at 3am unattended. Discovering one may produce an offer, never an entitlement — the same line internal/provider/credsource.go draws for interactive sessions, held here for unattended ones.

Index

Constants

View Source
const Hosted = "memcode-hosted"

Hosted is the memcode gateway: the one runtime that is always available and never needs authorizing, because it is memcode's own metered service rather than someone else's credential.

View Source
const ModelAuto = "auto"

ModelAuto means the task expressed no preference.

Variables

This section is empty.

Functions

func Chain

func Chain(p Policy, env Env, chosen string) []string

Chain returns the alternates that may be attempted when the RESOLVED runtime fails as a runtime.

The distinction this enforces is the important one. A runtime that is unreachable, unauthenticated or broken is a reason to try another; a runtime that worked perfectly and produced a change whose tests failed is NOT. Handing the same codebase to a different model in the hope of a different answer is not fallback, it is rerolling — and it turns one honest failure into a silent search for a model that happens to agree.

func Detect

func Detect() []string

Detect reports which runtimes are PRESENT on this machine.

Presence only. Nothing here reads a token, opens a session, or spends anything, and a runtime appearing in this list has no authority whatsoever — it is a candidate to OFFER the user, and that is all.

func IDs

func IDs() []string

IDs lists every known runtime id, sorted, for validation messages.

func ValidScope

func ValidScope(s Scope) bool

ValidScope reports whether s names a real scope.

func ValidStrategy

func ValidStrategy(s Strategy) bool

ValidStrategy reports whether s names a real strategy.

Types

type Authorizations

type Authorizations []Grant

Authorizations is the set of grants in force.

func (Authorizations) AnyGrant

func (a Authorizations) AnyGrant(runtime string) (Grant, bool)

AnyGrant reports whether a runtime has ANY active authorization, of any scope. Distinct from Authorized, which answers a specific task: a task-scoped grant is real permission even when the caller has no task in hand, and treating it as none would tell a user to authorize something they already did.

func (Authorizations) Authorized

func (a Authorizations) Authorized(runtime, taskName, runID string) bool

Authorized reports whether a runtime may be used for this task/run.

The hosted gateway is always authorized: it is memcode's own metered service, not a credential borrowed from another tool, and the user's memcode account IS the permission.

func (Authorizations) Find

func (a Authorizations) Find(runtime, taskName, runID string) (Grant, bool)

Find returns the grant that authorizes a runtime, preferring the NARROWEST one that applies. A run-scoped grant is reported over a blanket one so the record says which permission was actually relied on, which is the question someone asks when they want to withdraw it.

func (Authorizations) Offerable

func (a Authorizations) Offerable(taskName string) []string

Offerable lists runtimes present on this machine with NO authorization at all — the set actually worth offering. A task name narrows it to those that cannot serve THAT task; empty means "anything entirely unauthorized".

func (Authorizations) Revoke

func (a Authorizations) Revoke(id string) (Authorizations, int)

Revoke marks matching grants withdrawn and reports how many changed.

type Env

type Env struct {
	Available      []string
	Authorizations Authorizations
	Task           string
	Run            string
}

Env is the machine's answer to "what is available and what is allowed".

type ErrNoRuntime

type ErrNoRuntime struct {
	Reason string
	// Offer lists runtimes that are present but unauthorized — the actionable
	// part, because the fix is usually one authorization away.
	Offer []string
}

ErrNoRuntime means nothing authorized and capable remains.

func (ErrNoRuntime) Error

func (e ErrNoRuntime) Error() string

type Grant

type Grant struct {
	ID      string `yaml:"id" json:"id"`
	Runtime string `yaml:"runtime" json:"runtime"`
	Scope   Scope  `yaml:"scope" json:"scope"`
	// Task is required for ScopeTask and meaningless otherwise.
	Task string `yaml:"task,omitempty" json:"task,omitempty"`
	// Run is required for ScopeRun.
	Run       string `yaml:"run,omitempty" json:"run,omitempty"`
	GrantedAt string `yaml:"granted_at" json:"granted_at"`
	// Revoked keeps a withdrawn grant on the record rather than deleting it, so
	// "when did this stop being allowed" stays answerable.
	Revoked bool `yaml:"revoked,omitempty" json:"revoked,omitempty"`
}

Grant is one recorded authorization.

func NewGrant

func NewGrant(runtime string, scope Scope, taskName, runID string, now time.Time) (Grant, error)

NewGrant records an authorization.

func (Grant) Covers

func (g Grant) Covers(runtime, taskName, runID string) bool

Covers reports whether this grant authorizes a runtime for a given task/run.

type Kind

type Kind string

Kind says what sort of thing a runtime is, which is mostly a statement about who pays and how.

const (
	// KindSubscription is a login the user already holds in another tool. Running
	// a task on one costs nothing beyond the subscription they already bought,
	// which is the entire reason to prefer it — and the entire reason it needs
	// explicit permission.
	KindSubscription Kind = "subscription"
	// KindHosted is memcode's own gateway: always available, metered per token.
	KindHosted Kind = "hosted"
)

type Need

type Need struct {
	// Model is a pinned catalog model, or "" / "auto" for no constraint.
	Model string
}

Need is what a task requires of whatever runs it.

type Policy

type Policy struct {
	Strategy Strategy
	// Allowed is the ordered preference list. Order is meaningful.
	Allowed []string
	// Model is a catalog model id, or "auto".
	Model string
	// Fallback names runtimes that may be tried when the SELECTED runtime fails
	// as a runtime — not when the task fails. See Chain.
	Fallback []string
}

Policy is a task's runtime preference, as authored.

type Resolution

type Resolution struct {
	// What was asked for.
	RequestedStrategy Strategy
	RequestedModel    string
	// What it resolved to.
	Runtime          string
	Model            string
	CredentialSource string
	// Which permission was relied on. Empty for the hosted gateway, which needs
	// none.
	AuthID    string
	AuthScope Scope
	// Chain is the ordered alternates for RUNTIME failure only.
	Chain []string
	// Why explains the choice in one line, for the run record.
	Why string
}

Resolution is the frozen decision, recorded on the run so it stays explainable long after the machine's state has changed.

func Resolve

func Resolve(p Policy, env Env) (Resolution, error)

Resolve picks the runtime for a task, deterministically.

The order is fixed and stated, because an unattended system that chooses differently on different days for reasons nobody can reconstruct is worse than one that chooses slightly wrong every time:

  1. an explicit runtime, if authorized and capable
  2. the authorized, capable entries of Allowed, in the user's order
  3. the hosted gateway, if the policy permits it
  4. nothing — blocked, with the unauthorized candidates named

type Runtime

type Runtime struct {
	ID      string
	Display string
	Kind    Kind
	// CredentialSource is the value MEMCODE_CREDENTIAL_SOURCE takes to select
	// this backend, empty for the hosted gateway.
	CredentialSource string
	// Vendor is whose models it serves, for the capability check. Empty means
	// "any" — the hosted gateway serves the whole catalog.
	Vendor string
	// DefaultModel serves a task that asked for `auto`.
	DefaultModel string
	// ZeroMarginalCost is true when a run adds nothing to a bill. This is what
	// "prefer a subscription" actually means, and it is deliberately NOT called
	// "cheapest": cheapest implies a cost comparison across latency, quality,
	// context limits and quota that nobody can make honestly from here.
	ZeroMarginalCost bool
	// Available reports presence on this machine. A pure check — it must never
	// activate anything.
	Available func() bool
}

Runtime is an execution backend a task can run on.

func All

func All() []Runtime

All returns every known runtime, in preference-neutral registry order.

func Get

func Get(id string) (Runtime, bool)

Get looks up a runtime by id.

func (Runtime) CanServe

func (r Runtime) CanServe(n Need) bool

CanServe reports whether this runtime satisfies a task's needs.

The check is deliberately narrow: a task that pinned a specific model needs a runtime whose vendor actually serves it, and everything else is served by anyone. Inventing finer capability claims would mean asserting things about latency, quota and tool support that cannot be verified from here.

func (Runtime) ResolveModel

func (r Runtime) ResolveModel(n Need) string

ResolveModel picks the model this runtime will actually serve.

type Scope

type Scope string

Scope is how far one grant reaches.

const (
	// ScopeRun authorizes exactly one execution. Of limited use to a scheduler,
	// which is precisely why it is worth supporting: an interactive "just this
	// once" must not silently become standing permission.
	ScopeRun Scope = "run"
	// ScopeTask authorizes one named task, forever. The useful default for
	// unattended work: this job may use my subscription, others may not.
	ScopeTask Scope = "task"
	// ScopeAll authorizes every autonomous task.
	ScopeAll Scope = "all"
)

type Strategy

type Strategy string

Strategy is how a task wants its runtime chosen.

const (
	// StrategyPreferAuthorized walks Allowed in order and takes the first
	// authorized, capable, zero-marginal-cost runtime, falling through to the
	// hosted gateway when none qualifies.
	//
	// Note what this is NOT: a cost optimizer. "Cheapest" stops being meaningful
	// once latency, model quality, tool support, context limits and subscription
	// quota differ, and a hidden optimizer picking differently on different days
	// is unpredictable in exactly the place that most wants to be boring. The
	// order in Allowed is the user's preference, and it is obeyed.
	StrategyPreferAuthorized Strategy = "prefer_authorized_subscription"
	// StrategyHosted always uses memcode's gateway, whatever else is available.
	StrategyHosted Strategy = "hosted_only"
	// StrategyExplicit requires the first entry in Allowed and refuses to
	// substitute. A task that must run on Codex says so and fails loudly rather
	// than quietly running somewhere else.
	StrategyExplicit Strategy = "explicit"
)

func Strategies

func Strategies() []Strategy

Strategies lists the valid strategies, for error messages.

Jump to

Keyboard shortcuts

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