promptopt

package
v0.0.7 Latest Latest
Warning

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

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

Documentation

Overview

Package promptopt implements prompt A/B testing and optimization for cloop. Variants are named alternatives to role-specific system prompts. Outcomes are recorded per variant and the best-performing variant is selected via the lower bound of the Wilson score confidence interval (95 %).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllRoles

func AllRoles() []pm.AgentRole

AllRoles returns the list of roles that have at least one registered variant.

func RecordOutcome

func RecordOutcome(workDir, variantID string, success bool, latencyMs int) error

RecordOutcome appends an outcome for the given variant to the JSONL file.

Types

type OutcomeRecord

type OutcomeRecord struct {
	VariantID string    `json:"variant_id"`
	Role      string    `json:"role"`
	Success   bool      `json:"success"`
	LatencyMs int       `json:"latency_ms"`
	Ts        time.Time `json:"ts"`
}

OutcomeRecord is a single A/B outcome entry persisted in the variants JSONL file.

func LoadOutcomes

func LoadOutcomes(workDir string) ([]OutcomeRecord, error)

LoadOutcomes reads all outcome records from the JSONL file. Returns nil, nil when the file does not exist yet.

type Variant

type Variant struct {
	// ID is a short unique identifier (e.g. "backend-default", "backend-concise").
	ID string
	// Role is the task role this variant applies to (empty = generic).
	Role pm.AgentRole
	// Name is a human-readable label shown in `cloop perf`.
	Name string
	// SystemPrompt is the text prepended to the task execution prompt in place of
	// the role's default RoleSystemPrompt. Empty strings inherit the default.
	SystemPrompt string
}

Variant is a named prompt variant for a specific agent role.

func BestVariant

func BestVariant(workDir string, role pm.AgentRole) Variant

BestVariant returns the variant with the highest Wilson lower-bound score for the given role, using outcome data recorded in workDir. It falls back to the default variant when there is no data yet.

func LoadVariants

func LoadVariants(role pm.AgentRole) []Variant

LoadVariants returns all registered prompt variants for the given role. Pass an empty role string to get variants that apply to every task.

func NextBestVariant

func NextBestVariant(workDir string, role pm.AgentRole, currentVariantID string) Variant

NextBestVariant returns the next best variant that is NOT the currently active variantID. Used by the orchestrator when switching on heal attempts.

func VariantByID

func VariantByID(id string) (Variant, bool)

VariantByID returns the variant with the given ID, or the zero Variant if not found.

type VariantStat

type VariantStat struct {
	Variant    Variant
	Trials     int
	Successes  int
	Failures   int
	AvgLatency int64   // milliseconds
	WinRate    float64 // 0.0 – 1.0
	Wilson     float64 // Wilson lower-bound score (used for ranking)
}

VariantStat is the public view of aggregated statistics for one variant.

func RoleStats

func RoleStats(workDir string, role pm.AgentRole) ([]*VariantStat, error)

RoleStats returns per-variant statistics for a single role, sorted by Wilson score.

Jump to

Keyboard shortcuts

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