JuliusBrussee/caveman · error

read experiment id

Error message

read experiment id: %w

What it means

After inserting the experiment row, StartExperiment calls result.LastInsertId() to obtain the new row id; this error wraps a failure of that driver call. It means the row was likely inserted but its generated id could not be read.

Solutions

  1. Inspect the wrapped error from LastInsertId for the driver-specific cause
  2. Use a standard, maintained SQLite driver (e.g. mattn/go-sqlite3 or modernc.org/sqlite) that supports LastInsertId
  3. As a fallback, SELECT the id by the (unique) label after the insert

Example fix

// before
id, err := result.LastInsertId()
// after
id, err := result.LastInsertId()
if err != nil {
    row := s.db.QueryRow(`SELECT id FROM experiments WHERE label = ?`, label)
    err = row.Scan(&id)
}
Defensive patterns

Strategy: fallback

Try / catch

exp, err := store.StartExperiment(label, sinkID, fixKind, note)
if err != nil && strings.Contains(err.Error(), "read experiment id") {
    // fall back to loading by label
    exp, err = store.LoadExperiment(label)
}

Prevention

When it happens

Trigger: The SQLite driver fails to report LastInsertId — typically when the driver/connection configuration does not support it or the driver returns an unexpected error after the insert.

Common situations: Using a driver or connection mode where last-insert-id is unavailable, or driver bugs after unusual insert outcomes.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


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

Appendix: source

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

}

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 {
		return Experiment{}, fmt.Errorf("arm must be %q or %q", experimentArmOn, experimentArmOff)
	}
	experiment, err := s.LoadExperiment(label)
	if err != nil {

View on GitHub (pinned to 3ee70a1026)