JuliusBrussee/caveman · error

replacement memory already exists

Error message

replacement memory %s already exists

What it means

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.

Solutions

  1. Provide genuinely new replacement text that has not been stored before.
  2. Check whether the desired text already exists (search/History) before calling Supersede.
  3. If reverting to a previous version, use an approach that expires the current version rather than inserting a duplicate.

Example fix

// before
_, err := store.Supersede(id, existingText) // panics: already exists
// after
newText := strings.TrimSpace(userInput)
if newText != existingText {
	_, err := store.Supersede(id, newText)
	return err
}
Defensive patterns

Strategy: validation

Validate before calling

newID := memID(newText) // same derivation the store uses
if knownIDs[newID] { return fmt.Errorf("text already stored") }

Try / catch

_, err := store.Supersede(id, newText)
if err != nil && strings.Contains(err.Error(), "already exists") {
	return fmt.Errorf("%q already stored; provide new text", newText)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


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

Appendix: source

Thrown at mem/store.go:233

	defer tx.Rollback()

	var old Memory
	if err := scanMemory(tx.QueryRow(memorySelect+` WHERE id = ? AND valid_until IS NULL`, oldID), &old); err != nil {
		if err == sql.ErrNoRows {
			return Memory{}, fmt.Errorf("memory %s is not current", oldID)
		}
		return Memory{}, fmt.Errorf("read current memory: %w", err)
	}
	if old.Text == newText {
		return Memory{}, fmt.Errorf("replacement text is identical to current memory")
	}
	newID := memID(newText)
	var exists int
	if err := tx.QueryRow(`SELECT COUNT(*) FROM memories WHERE id = ?`, newID).Scan(&exists); err != nil {
		return Memory{}, fmt.Errorf("check replacement memory: %w", err)
	}
	if exists > 0 {
		return Memory{}, fmt.Errorf("replacement memory %s already exists", newID)
	}
	now := time.Now().UTC().Format(time.RFC3339Nano)
	if _, err := tx.Exec(
		`INSERT INTO memories
		   (id, text, created_at, valid_from, supersedes)
		 VALUES (?, ?, ?, ?, ?)`,
		newID, newText, now, now, oldID,
	); err != nil {
		return Memory{}, fmt.Errorf("insert replacement memory: %w", err)
	}
	res, err := tx.Exec(
		`UPDATE memories
		    SET valid_until = ?, superseded_by = ?
		  WHERE id = ? AND valid_until IS NULL`,
		now, newID, oldID,
	)
	if err != nil {
		return Memory{}, fmt.Errorf("expire old memory: %w", err)

View on GitHub (pinned to 3ee70a1026)