{"record":{"id":"6eddd0b91876cef5","repo":"JuliusBrussee/caveman","slug":"replacement-memory-s-already-exists","errorCode":null,"errorMessage":"replacement memory %s already exists","messagePattern":"replacement memory (.+?) already exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem/store.go","lineNumber":233,"sourceCode":"\tdefer tx.Rollback()\n\n\tvar old Memory\n\tif err := scanMemory(tx.QueryRow(memorySelect+` WHERE id = ? AND valid_until IS NULL`, oldID), &old); err != nil {\n\t\tif err == sql.ErrNoRows {\n\t\t\treturn Memory{}, fmt.Errorf(\"memory %s is not current\", oldID)\n\t\t}\n\t\treturn Memory{}, fmt.Errorf(\"read current memory: %w\", err)\n\t}\n\tif old.Text == newText {\n\t\treturn Memory{}, fmt.Errorf(\"replacement text is identical to current memory\")\n\t}\n\tnewID := memID(newText)\n\tvar exists int\n\tif err := tx.QueryRow(`SELECT COUNT(*) FROM memories WHERE id = ?`, newID).Scan(&exists); err != nil {\n\t\treturn Memory{}, fmt.Errorf(\"check replacement memory: %w\", err)\n\t}\n\tif exists > 0 {\n\t\treturn Memory{}, fmt.Errorf(\"replacement memory %s already exists\", newID)\n\t}\n\tnow := time.Now().UTC().Format(time.RFC3339Nano)\n\tif _, err := tx.Exec(\n\t\t`INSERT INTO memories\n\t\t   (id, text, created_at, valid_from, supersedes)\n\t\t VALUES (?, ?, ?, ?, ?)`,\n\t\tnewID, newText, now, now, oldID,\n\t); err != nil {\n\t\treturn Memory{}, fmt.Errorf(\"insert replacement memory: %w\", err)\n\t}\n\tres, err := tx.Exec(\n\t\t`UPDATE memories\n\t\t    SET valid_until = ?, superseded_by = ?\n\t\t  WHERE id = ? AND valid_until IS NULL`,\n\t\tnow, newID, oldID,\n\t)\n\tif err != nil {\n\t\treturn Memory{}, fmt.Errorf(\"expire old memory: %w\", err)","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/mem/store.go#L215-L251","documentation":"Supersede rejects the operation because a memory with the replacement text's ID (memID(newText)) already exists in the store. IDs are content-derived, so this means the exact same text was already remembered; creating another row would duplicate it.","triggerScenarios":"Calling Store.Supersede(oldID, newText) where newText was already stored (its memID exists in the memories table), including when newText is an older/historical version of the same lineage.","commonSituations":"Trying to roll a memory back to text that already exists as a previous version in the chain; re-superseding with text that was remembered in a separate call; two clients concurrently superseding to the same content.","solutions":["Provide genuinely new replacement text that has not been stored before.","Check whether the desired text already exists (search/History) before calling Supersede.","If reverting to a previous version, use an approach that expires the current version rather than inserting a duplicate."],"exampleFix":"// before\n_, err := store.Supersede(id, existingText) // panics: already exists\n// after\nnewText := strings.TrimSpace(userInput)\nif newText != existingText {\n\t_, err := store.Supersede(id, newText)\n\treturn err\n}","handlingStrategy":"validation","validationCode":"newID := memID(newText) // same derivation the store uses\nif knownIDs[newID] { return fmt.Errorf(\"text already stored\") }","typeGuard":null,"tryCatchPattern":"_, err := store.Supersede(id, newText)\nif err != nil && strings.Contains(err.Error(), \"already exists\") {\n\treturn fmt.Errorf(\"%q already stored; provide new text\", newText)\n}","preventionTips":["Ensure replacement text differs from all previously stored versions","Check History(id) before attempting a revert-style supersede","Coordinate concurrent clients so two don't supersede to identical text"],"tags":["duplicate","validation","content-addressed"],"backgroundTag":"file-already-exists","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"}