Help us shape the future of Go! Take the 2026 Go Developer Survey

delegation

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package delegation runs an explicit bounded local-first task policy. It can escalate once after a known complete durable answer fails declared checks; it never retries uncertain calls, intercepts an agent session, or picks models.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrQualityFailed = errors.New("delegated task did not satisfy every declared quality check")
	ErrStopped       = errors.New("delegated task stopped without escalating; inspect its recorded outcome")
)

Functions

func NewOutcomeRunID added in v0.9.0

func NewOutcomeRunID() (string, error)

func OutcomeRecord added in v0.9.0

func OutcomeRecord(request Request, result Result, runID string) outcomes.Run

func RecordOutcome added in v0.9.0

func RecordOutcome(ctx context.Context, broker inference.Broker, request Request, result *Result, runID string)

func RequestDigest added in v0.13.0

func RequestDigest(request Request) string

RequestDigest returns the canonical identity used by Router results. Callers must Validate the request before using the digest as an approval binding. Outcome tracking is excluded, matching the existing generation identity.

func ShadowPartition added in v0.14.0

func ShadowPartition(request Request, tier Tier, runtimeID string) (string, error)

ShadowPartition binds every approved non-question part of the task. Bindings must declare business variables embedded in the question; a partition alone is not a proof that two free-form questions have equivalent answers.

func SpecForTier added in v0.13.0

func SpecForTier(request Request, tierID string, allowPaid bool) (inference.Spec, error)

SpecForTier validates the entire approved request and returns the exact derived spec for a declared tier. It uses the Router's unchanged task and revision identities; it performs no inference or provider discovery.

func Validate

func Validate(request Request, allowPaid bool) error

Validate resolves all tier plans and checks before any factory/provider call. A configured hosted tier is rejected unless this caller explicitly opts in, even when the local tier could produce a passing answer.

Types

type Attempt

type Attempt struct {
	BudgetDiagnostic *inference.BudgetDiagnostic `json:"budget_diagnostic,omitempty"`
	TierID           string                      `json:"tier_id"`
	Result           inference.Result            `json:"result"` // Response.Text is always stripped.
	Checks           []evaluation.CheckResult    `json:"checks"`
	QualityPassed    bool                        `json:"quality_passed"`
	OutputSHA256     string                      `json:"output_sha256,omitempty"`
	ErrorCategory    string                      `json:"error_category,omitempty"`
}

type OutcomeTracking added in v0.4.0

type OutcomeTracking struct {
	Status  string            `json:"status"` // recorded or failed
	RunID   string            `json:"run_id"`
	Task    outcomes.Task     `json:"task"`
	Receipt *outcomes.Receipt `json:"receipt,omitempty"`
	Record  *outcomes.Run     `json:"record,omitempty"`
}

OutcomeTracking is separate from answer quality. Registration failure never changes a checked answer or authorizes another model call. Record is returned on failure so the exact metadata can be reconciled without regenerating text.

type Policy

type Policy struct {
	Local      Tier  `json:"local"`
	Escalation *Tier `json:"escalation,omitempty"`
}

type Request

type Request struct {
	TrackOutcome   bool               `json:"track_outcome,omitempty"` // Opt-in shared accounting; excluded from generation identity.
	Spec           inference.Spec     `json:"spec"`
	Policy         Policy             `json:"policy"`
	Checks         []evaluation.Check `json:"checks"`
	SemanticShadow *semantic.Config   `json:"semantic_shadow,omitempty"` // Advisory observation; excluded from generation identity.
}

Request.Spec supplies approved task, revision, prompt and context only. Every model and resource setting comes from the explicit tier templates.

func Load

func Load(reader io.Reader) (Request, error)

Load reads at most 1MiB of request JSON. Authorization is deliberately not a JSON field: only the caller can enable paid escalation on Router.AllowPaid.

type Result

type Result struct {
	SemanticShadow   *semantic.Observation       `json:"semantic_shadow,omitempty"`
	BudgetDiagnostic *inference.BudgetDiagnostic `json:"budget_diagnostic,omitempty"`
	OutcomeTracking  *OutcomeTracking            `json:"outcome_tracking,omitempty"`
	RequestSHA256    string                      `json:"request_sha256"`
	Attempts         []Attempt                   `json:"attempts"`
	SelectedTier     string                      `json:"selected_tier,omitempty"`
	Response         *inference.Response         `json:"response,omitempty"` // Only a passing answer is exposed.
	Status           string                      `json:"status"`             // completed, quality_failed, stopped
}

type Router

type Router struct {
	Factory   RunnerFactory
	AllowPaid bool // Caller permission, never read from request JSON or environment.
	Shadow    *ShadowObserver
}

func (Router) Run

func (router Router) Run(ctx context.Context, request Request) (Result, error)

Run executes at most two explicitly configured tiers. A second tier is reached only after error-free, known complete and durable output fails the declared checks. Every provider/accounting error stops the whole policy. Repeated calls retain the same request/tier identity and the Runner's reuse behavior; the Router never changes a revision to force another generation.

type Runner

type Runner interface {
	Run(context.Context, inference.Spec) (inference.Result, error)
}

type RunnerFactory

type RunnerFactory func(Tier) (Runner, error)

type ShadowObserver added in v0.14.0

type ShadowObserver struct {
	Broker          inference.Broker
	Index           semantic.Index
	EmbedderFactory func(context.Context, semantic.Config) (semantic.Embedder, error)
	Resources       admission.Source // nil lets a Go caller provide its own admission.
	// contains filtered or unexported fields
}

ShadowObserver is optional, local-only opportunity measurement. One observer admits one bounded observation at a time and retains no waiting task history. Failures never change a generated answer, its accounting, or its claims.

type Tier

type Tier struct {
	ID   string         `json:"id"`
	Spec inference.Spec `json:"spec"`
}

Tier.Spec must omit task, revision, instructions, input and context. Hosted escalation requires explicit prices and positive per-call and daily budgets.

Jump to

Keyboard shortcuts

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