JuliusBrussee/caveman · error

read back memory

Error message

read back memory: %w

What it means

After inserting, Remember reads the row back with the standard memorySelect query to return the stored Memory. This error wraps any failure of that SELECT/scan (e.g. sql.ErrNoRows, scan type mismatch, db error). It is an internal consistency check: the row that was just written could not be read back.

Solutions

  1. Check the wrapped cause; if sql.ErrNoRows, another writer may have removed the row — inspect concurrent modifications.
  2. Verify the memories table schema matches what this binary's migrateMemorySchema produces (partial migration suspect).
  3. Retry after transient lock errors.
  4. If scan mismatches persist, rebuild the db with the current version's migration.
Defensive patterns

Strategy: try-catch

Try / catch

m, err := store.Remember(text)
if err != nil {
    if strings.Contains(err.Error(), "read back memory") {
        log.Printf("insert ok but read-back failed; verify schema/migration: %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling Remember when the read-back SELECT fails due to db lock/errors, or a scan/type mismatch between memorySelect's column layout and the Memory struct, or (rarely) the row vanishing between insert and select under concurrent modification.

Common situations: Schema drift — an older/newer table shape than the code expects after a partial migration; concurrent writers interfering; transient SQLITE_BUSY on the query.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at mem/store.go:190

	}
	id := memID(text)
	created := time.Now().UTC().Format(time.RFC3339Nano)
	// INSERT OR IGNORE keeps the original created_at on a repeat remember.
	// RetryOnBusy: under a fan-out of cavemem processes, a queued writer can
	// outlast a single busy_timeout (Windows file locking is the slowest case).
	if err := ccr.RetryOnBusy(func() error {
		_, e := s.db.Exec(
			`INSERT OR IGNORE INTO memories (id, text, created_at, valid_from) VALUES (?, ?, ?, ?)`,
			id, text, created, created,
		)
		return e
	}); err != nil {
		return Memory{}, fmt.Errorf("store memory: %w", err)
	}
	var stored Memory
	row := s.db.QueryRow(memorySelect+` WHERE id = ?`, id)
	if err := scanMemory(row, &stored); err != nil {
		return Memory{}, fmt.Errorf("read back memory: %w", err)
	}
	if stored.ValidUntil != nil {
		return Memory{}, fmt.Errorf("memory %s exists only as an expired historical version and cannot be re-remembered as current", stored.ID)
	}
	return stored, nil
}

// Supersede atomically replaces one current memory with a new current version.
// The old row remains durable for history and byte-exact audit, but normal recall
// excludes it. An already-expired or unknown id fails closed.
func (s *Store) Supersede(oldID, newText string) (Memory, error) {
	if strings.TrimSpace(oldID) == "" {
		return Memory{}, fmt.Errorf("supersede requires old memory id")
	}
	if strings.TrimSpace(newText) == "" {
		return Memory{}, fmt.Errorf("cannot supersede with empty text")
	}
	if err := validateMemorySize(newText); err != nil {

View on GitHub (pinned to 3ee70a1026)