JuliusBrussee/caveman · error

start experiment

Error message

start experiment %q: %w

What it means

This wraps a failure of the INSERT INTO experiments SQL statement inside StartExperiment. Any SQLite/driver error (constraint violation, locked database, schema mismatch) is reported under this prefix with the experiment label for context.

Solutions

  1. Read the wrapped error (%w) for the exact SQL failure cause
  2. Check for an existing experiment with the same label (UNIQUE collision) and reuse or rename it
  3. Run schema migrations if the database predates the experiments table
  4. Retry on SQLITE_BUSY or ensure connections close to release locks
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-check label uniqueness if schema enforces UNIQUE
var count int
err := db.QueryRow(`SELECT COUNT(*) FROM experiments WHERE label = ?`, label).Scan(&count)
if err == nil && count > 0 { return errors.New("experiment label already exists") }

Try / catch

exp, err := store.StartExperiment(label, sinkID, fixKind, note)
if err != nil {
    if strings.Contains(err.Error(), "UNIQUE constraint") { /* reuse or rename */ }
    var busy bool
    if strings.Contains(err.Error(), "SQLITE_BUSY") { busy = true /* retry with backoff */ }
    return fmt.Errorf("start experiment failed: %w", err)
}

Prevention

When it happens

Trigger: Calling StartExperiment when the experiments table is missing or has an incompatible schema, a UNIQUE constraint on label collides with an existing experiment, or the database file is locked/corrupt.

Common situations: Old database files from a previous schema version, starting two experiments with the same label, or concurrent writers holding the SQLite lock.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/1b668b2e9a8445d3. Report an issue: GitHub.

Appendix: source

Thrown at proxy/internal/store/learn_experiment.go:99

	SavedUSD   *float64              `json:"saved_usd_per_session,omitempty"`
	Currency   string                `json:"currency,omitempty"`
	Attributed LearnAttribution      `json:"attribution"`
	Caveats    []string              `json:"caveats"`
}

func (s *Store) StartExperiment(label, sinkID, fixKind, note string) (Experiment, error) {
	label = strings.TrimSpace(label)
	if label == "" || len(label) > 128 {
		return Experiment{}, fmt.Errorf("experiment label must be 1-128 bytes")
	}
	if len(note) > 4096 {
		return Experiment{}, fmt.Errorf("note exceeds 4096 bytes")
	}
	now := learnOutcomeClock().UTC().Format(time.RFC3339)
	result, err := s.db.Exec(`INSERT INTO experiments (label, sink_id, fix_kind, note, created_at)
		VALUES (?, ?, ?, ?, ?)`, label, sinkID, fixKind, note, now)
	if err != nil {
		return Experiment{}, fmt.Errorf("start experiment %q: %w", label, err)
	}
	id, err := result.LastInsertId()
	if err != nil {
		return Experiment{}, fmt.Errorf("read experiment id: %w", err)
	}
	if _, err := s.db.Exec(`INSERT INTO experiment_arms (experiment_id, arm, started_at)
		VALUES (?, ?, ?)`, id, experimentArmOn, now); err != nil {
		return Experiment{}, fmt.Errorf("open first arm: %w", err)
	}
	return s.LoadExperiment(label)
}

// SwitchExperimentArm closes the open arm and opens the other one. Switching to
// the arm already running is a no-op rather than an error: it keeps a repeated
// call from fragmenting the interval history.
func (s *Store) SwitchExperimentArm(label, arm string) (Experiment, error) {
	arm = strings.ToLower(strings.TrimSpace(arm))
	if arm != experimentArmOn && arm != experimentArmOff {

View on GitHub (pinned to 3ee70a1026)