JuliusBrussee/caveman · error

prefix replacement put

Error message

prefix replacement put: %w

What it means

RememberReplacement persists a prefix replacement into the SQLite prefix_replacements table via an INSERT ... ON CONFLICT upsert. When the database Exec fails (lock, I/O error, schema problem), the error is wrapped as 'prefix replacement put'. The caller gets no handle and the replacement is not recorded.

Solutions

  1. Read the wrapped %w error to identify the underlying SQLite failure (lock vs schema vs disk)
  2. Ensure no other process holds a write lock on the database; retry after the writer exits
  3. Verify the schema includes prefix_replacements (re-run migrations or recreate the store)
  4. Check disk space and file permissions on the database path
Defensive patterns

Strategy: try-catch

Try / catch

handle, err := store.RememberReplacement(key, handleStr, repl)
if err != nil {
    var sqliteErr *sqlite.Error
    if errors.As(err, &sqliteErr) || strings.Contains(err.Error(), "prefix replacement put") {
        log.Printf("prefix cache write failed (locked or schema issue): %v", err)
        return nil, err // degrade: proxy can proceed without caching
    }
    return nil, err
}

Prevention

When it happens

Trigger: Any db.Exec failure during the upsert: database file locked by another process, disk full/corrupt, missing prefix_replacements table (old schema), or read-only database file.

Common situations: Concurrent proxy processes contending for the SQLite lock; an upgraded binary running against a database created by an older version without the table; disk quota exhausted on the host.

Related errors


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

Appendix: source

Thrown at proxy/internal/store/prefix_cache.go:55

// RememberReplacement durably records original→replacement and returns the
// authoritative bytes for that original. Storage is first-write-wins: if another
// in-flight request already stored a replacement for the same block, that one is
// returned and the caller forwards it, so two requests can never put two different
// prefixes on the wire for one logical message.
func (s *Store) RememberReplacement(scope string, original, replacement []byte, handle string) ([]byte, error) {
	if scope == "" || len(original) == 0 || len(replacement) == 0 || handle == "" {
		return nil, errors.New("prefix replacement: incomplete entry")
	}
	key := prefixCacheKey(scope, original)
	now := prefixCacheNow()
	if _, err := s.db.Exec(
		`INSERT INTO prefix_replacements (original_sha256, handle, replacement, created_at, last_used_at)
		 VALUES (?,?,?,?,?)
		 ON CONFLICT(original_sha256) DO UPDATE SET last_used_at=excluded.last_used_at`,
		key, handle, replacement, now, now,
	); err != nil {
		return nil, fmt.Errorf("prefix replacement put: %w", err)
	}
	stored, _, ok := s.readReplacement(key)
	if !ok {
		return nil, errors.New("prefix replacement: entry unreadable after write")
	}
	s.evictPrefixReplacements()
	return stored, nil
}

func (s *Store) readReplacement(key string) ([]byte, string, bool) {
	var handle string
	var replacement []byte
	row := s.db.QueryRow(`SELECT handle, replacement FROM prefix_replacements WHERE original_sha256 = ?`, key)
	switch err := row.Scan(&handle, &replacement); {
	case errors.Is(err, sql.ErrNoRows):
		return nil, "", false
	case err != nil:
		// NOT a miss: the entry may well exist. The caller still has to fail safe and

View on GitHub (pinned to 3ee70a1026)