{"record":{"id":"41b077cdb45c71ae","repo":"JuliusBrussee/caveman","slug":"prefix-replacement-put-w","errorCode":null,"errorMessage":"prefix replacement put: %w","messagePattern":"prefix replacement put: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/internal/store/prefix_cache.go","lineNumber":55,"sourceCode":"\n// RememberReplacement durably records original→replacement and returns the\n// authoritative bytes for that original. Storage is first-write-wins: if another\n// in-flight request already stored a replacement for the same block, that one is\n// returned and the caller forwards it, so two requests can never put two different\n// prefixes on the wire for one logical message.\nfunc (s *Store) RememberReplacement(scope string, original, replacement []byte, handle string) ([]byte, error) {\n\tif scope == \"\" || len(original) == 0 || len(replacement) == 0 || handle == \"\" {\n\t\treturn nil, errors.New(\"prefix replacement: incomplete entry\")\n\t}\n\tkey := prefixCacheKey(scope, original)\n\tnow := prefixCacheNow()\n\tif _, err := s.db.Exec(\n\t\t`INSERT INTO prefix_replacements (original_sha256, handle, replacement, created_at, last_used_at)\n\t\t VALUES (?,?,?,?,?)\n\t\t ON CONFLICT(original_sha256) DO UPDATE SET last_used_at=excluded.last_used_at`,\n\t\tkey, handle, replacement, now, now,\n\t); err != nil {\n\t\treturn nil, fmt.Errorf(\"prefix replacement put: %w\", err)\n\t}\n\tstored, _, ok := s.readReplacement(key)\n\tif !ok {\n\t\treturn nil, errors.New(\"prefix replacement: entry unreadable after write\")\n\t}\n\ts.evictPrefixReplacements()\n\treturn stored, nil\n}\n\nfunc (s *Store) readReplacement(key string) ([]byte, string, bool) {\n\tvar handle string\n\tvar replacement []byte\n\trow := s.db.QueryRow(`SELECT handle, replacement FROM prefix_replacements WHERE original_sha256 = ?`, key)\n\tswitch err := row.Scan(&handle, &replacement); {\n\tcase errors.Is(err, sql.ErrNoRows):\n\t\treturn nil, \"\", false\n\tcase err != nil:\n\t\t// NOT a miss: the entry may well exist. The caller still has to fail safe and","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/proxy/internal/store/prefix_cache.go#L37-L73","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the wrapped %w error to identify the underlying SQLite failure (lock vs schema vs disk)","Ensure no other process holds a write lock on the database; retry after the writer exits","Verify the schema includes prefix_replacements (re-run migrations or recreate the store)","Check disk space and file permissions on the database path"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"handle, err := store.RememberReplacement(key, handleStr, repl)\nif err != nil {\n    var sqliteErr *sqlite.Error\n    if errors.As(err, &sqliteErr) || strings.Contains(err.Error(), \"prefix replacement put\") {\n        log.Printf(\"prefix cache write failed (locked or schema issue): %v\", err)\n        return nil, err // degrade: proxy can proceed without caching\n    }\n    return nil, err\n}","preventionTips":["Configure a busy_timeout on the SQLite connection to tolerate brief lock contention","Run migrations so prefix_replacements exists before writes","Monitor disk space on the database volume","Avoid running multiple writer processes against the same store file"],"tags":["sqlite","persistence","cache"],"backgroundTag":"database-write-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"}