{"record":{"id":"760f242020e98696","repo":"JuliusBrussee/caveman","slug":"read-experiment-id-w","errorCode":null,"errorMessage":"read experiment id: %w","messagePattern":"read experiment id: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/internal/store/learn_experiment.go","lineNumber":103,"sourceCode":"}\n\nfunc (s *Store) StartExperiment(label, sinkID, fixKind, note string) (Experiment, error) {\n\tlabel = strings.TrimSpace(label)\n\tif label == \"\" || len(label) > 128 {\n\t\treturn Experiment{}, fmt.Errorf(\"experiment label must be 1-128 bytes\")\n\t}\n\tif len(note) > 4096 {\n\t\treturn Experiment{}, fmt.Errorf(\"note exceeds 4096 bytes\")\n\t}\n\tnow := learnOutcomeClock().UTC().Format(time.RFC3339)\n\tresult, err := s.db.Exec(`INSERT INTO experiments (label, sink_id, fix_kind, note, created_at)\n\t\tVALUES (?, ?, ?, ?, ?)`, label, sinkID, fixKind, note, now)\n\tif err != nil {\n\t\treturn Experiment{}, fmt.Errorf(\"start experiment %q: %w\", label, err)\n\t}\n\tid, err := result.LastInsertId()\n\tif err != nil {\n\t\treturn Experiment{}, fmt.Errorf(\"read experiment id: %w\", err)\n\t}\n\tif _, err := s.db.Exec(`INSERT INTO experiment_arms (experiment_id, arm, started_at)\n\t\tVALUES (?, ?, ?)`, id, experimentArmOn, now); err != nil {\n\t\treturn Experiment{}, fmt.Errorf(\"open first arm: %w\", err)\n\t}\n\treturn s.LoadExperiment(label)\n}\n\n// SwitchExperimentArm closes the open arm and opens the other one. Switching to\n// the arm already running is a no-op rather than an error: it keeps a repeated\n// call from fragmenting the interval history.\nfunc (s *Store) SwitchExperimentArm(label, arm string) (Experiment, error) {\n\tarm = strings.ToLower(strings.TrimSpace(arm))\n\tif arm != experimentArmOn && arm != experimentArmOff {\n\t\treturn Experiment{}, fmt.Errorf(\"arm must be %q or %q\", experimentArmOn, experimentArmOff)\n\t}\n\texperiment, err := s.LoadExperiment(label)\n\tif err != nil {","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/proxy/internal/store/learn_experiment.go#L85-L121","documentation":"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.","triggerScenarios":"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.","commonSituations":"Using a driver or connection mode where last-insert-id is unavailable, or driver bugs after unusual insert outcomes.","solutions":["Inspect the wrapped error from LastInsertId for the driver-specific cause","Use a standard, maintained SQLite driver (e.g. mattn/go-sqlite3 or modernc.org/sqlite) that supports LastInsertId","As a fallback, SELECT the id by the (unique) label after the insert"],"exampleFix":"// before\nid, err := result.LastInsertId()\n// after\nid, err := result.LastInsertId()\nif err != nil {\n    row := s.db.QueryRow(`SELECT id FROM experiments WHERE label = ?`, label)\n    err = row.Scan(&id)\n}","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"exp, err := store.StartExperiment(label, sinkID, fixKind, note)\nif err != nil && strings.Contains(err.Error(), \"read experiment id\") {\n    // fall back to loading by label\n    exp, err = store.LoadExperiment(label)\n}","preventionTips":["Use a driver that reliably supports LastInsertId","Keep sqlite drivers updated","Verify insert success before reading the id (already done by error check)","Prefer LoadExperiment-by-label as an idempotent recovery path"],"tags":["database","sqlite","last-insert-id"],"backgroundTag":"database-query-failed","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}