Skip to content

NarInfoDiskCache: Support arbitrary nix-cache-info fields#525

Merged
edolstra merged 2 commits into
mainfrom
nar-info-disk-cache-generic-fields
Jun 26, 2026
Merged

NarInfoDiskCache: Support arbitrary nix-cache-info fields#525
edolstra merged 2 commits into
mainfrom
nar-info-disk-cache-generic-fields

Conversation

@edolstra

@edolstra edolstra commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Motivation

This replaces the WantMassQuery and Priority SQLite columns with a generic JSON field storing arbitrary nix-cache-info fields. This allows fields to be added in the future without having to change the database schema.

Extracted from #523. This will also be useful for #488 which adds some nix-cache-info fields as well.

Context

Summary by CodeRabbit

  • New Features

    • Binary cache metadata now preserves additional nix-cache-info fields, improving compatibility with caches that provide extra information.
  • Bug Fixes

    • Cache settings are parsed and applied more consistently during startup.
    • Existing cache metadata is reused more reliably when cache details change.
  • Chores

    • The local binary-cache disk cache format was updated to store the new generalized metadata structure (new schema version).

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: dd3b8734-22b6-4b35-954d-fa809cc7e221

📥 Commits

Reviewing files that changed from the base of the PR and between 156eb3a and 6108e4d.

📒 Files selected for processing (6)
  • src/libstore-tests/nar-info-disk-cache.cc
  • src/libstore/binary-cache-store.cc
  • src/libstore/http-binary-cache-store.cc
  • src/libstore/include/nix/store/binary-cache-store.hh
  • src/libstore/include/nix/store/nar-info-disk-cache.hh
  • src/libstore/nar-info-disk-cache.cc
🚧 Files skipped from review as they are similar to previous changes (6)
  • src/libstore-tests/nar-info-disk-cache.cc
  • src/libstore/include/nix/store/binary-cache-store.hh
  • src/libstore/http-binary-cache-store.cc
  • src/libstore/include/nix/store/nar-info-disk-cache.hh
  • src/libstore/binary-cache-store.cc
  • src/libstore/nar-info-disk-cache.cc

📝 Walkthrough

Walkthrough

Cache metadata now flows as string maps. The store parses nix-cache-info into fields, applies recognized settings separately, persists the map in SQLite, and updates HTTP cache initialization and disk-cache tests to use the new shape.

Changes

Binary cache fields migration

Layer / File(s) Summary
Cache metadata contracts
src/libstore/include/nix/store/nar-info-disk-cache.hh, src/libstore/include/nix/store/binary-cache-store.hh
CacheInfo now stores cache metadata in fields, and BinaryCacheStore declares helpers for parsing and applying nix-cache-info maps.
Parse and apply cache info
src/libstore/binary-cache-store.cc
BinaryCacheStore::init() now parses nix-cache-info into a map, preserves unknown entries, and applies WantMassQuery and Priority through a separate helper.
SQLite cache fields
src/libstore/nar-info-disk-cache.cc, src/libstore-tests/nar-info-disk-cache.cc
The SQLite cache stores JSON fields in BinaryCaches, bumps the cache database filename to v2, and the disk-cache test creates and checks cache metadata through fields maps.
HTTP cache initialization
src/libstore/http-binary-cache-store.cc
HttpBinaryCacheStore::init() applies cached fields on cache hits, parses nix-cache-info on misses, maps UploadToHTTP failures to a nix::Error, and creates new disk-cache entries from the parsed map.

Sequence Diagram(s)

sequenceDiagram
  participant HttpBinaryCacheStore
  participant NarInfoDiskCache
  participant BinaryCacheStore

  HttpBinaryCacheStore->>NarInfoDiskCache: upToDateCacheExists(cacheKey)
  alt cacheInfo present
    NarInfoDiskCache-->>HttpBinaryCacheStore: cacheInfo.fields
    HttpBinaryCacheStore->>BinaryCacheStore: applyCacheInfoFields(cacheInfo.fields)
  else cacheInfo missing
    HttpBinaryCacheStore->>BinaryCacheStore: parseNixCacheInfo()
    BinaryCacheStore-->>HttpBinaryCacheStore: fields map
    HttpBinaryCacheStore->>BinaryCacheStore: applyCacheInfoFields(fields)
    HttpBinaryCacheStore->>NarInfoDiskCache: createCache(cacheKey, {.fields = fields})
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Poem

I hop through caches, quick and bright,
With fields of maps tucked in just right.
I nibble nix-cache-info by moon,
Then thump a happy SQLite tune.
My whiskers twitch — cache carrots grow.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: NarInfoDiskCache now supports arbitrary nix-cache-info fields.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch nar-info-disk-cache-generic-fields

Comment @coderabbitai help to get the list of available commands.

@edolstra edolstra force-pushed the nar-info-disk-cache-generic-fields branch from e1d8e51 to 156eb3a Compare June 26, 2026 18:08
cole-h
cole-h previously approved these changes Jun 26, 2026
@github-actions

github-actions Bot commented Jun 26, 2026

Copy link
Copy Markdown

@github-actions github-actions Bot temporarily deployed to pull request June 26, 2026 18:14 Inactive

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/libstore/nar-info-disk-cache.cc (1)

194-194: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low value

Consider wrapping the fields JSON parse to add context on corruption.

nlohmann::json::parse(getStr(2)) throws on a malformed/legacy row, propagating an opaque error. Other readers here (e.g. lookupRealisation) wrap parsing in a try/catch that adds "while parsing the local disk cache". Mirroring that would improve diagnosability, though the v2 filename bump makes legacy rows unlikely.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/libstore/nar-info-disk-cache.cc` at line 194, Wrap the `fields` parsing
in `nar-info-disk-cache.cc` with the same kind of `try/catch` used by
`lookupRealisation` so malformed or legacy cache rows fail with added context.
Update the `queryCache.getStr(2)` JSON parse in the `fields` initializer to
rethrow with a message like “while parsing the local disk cache” and keep the
surrounding row handling consistent with the other cache readers.
src/libstore-tests/nar-info-disk-cache.cc (1)

120-126: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Stale commented-out block references the removed API.

This dead block uses the old positional createCache(..., wantMassQuery, prio) signature and r->wantMassQuery/r->priority, which no longer exist after this migration. Consider removing it or updating it to the .fields shape to avoid confusion.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/libstore-tests/nar-info-disk-cache.cc` around lines 120 - 126, Remove or
update the stale commented-out test block in NarInfo disk cache tests so it
matches the current cache API. The dead code still uses the old positional
createCache signature and the removed wantMassQuery/priority accessors; either
delete this block entirely or rewrite it to use the new fields-based shape,
keeping the references aligned with createCache and upToDateCacheExists.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/libstore/binary-cache-store.cc`:
- Around line 81-87: The Priority parsing in
BinaryCacheStore::applyCacheInfoFields currently uses std::stoi, which can throw
on malformed or out-of-range remote nix-cache-info data. Replace that conversion
with string2Int, and only call config.priority.setDefault when the value parses
successfully; otherwise skip the field and keep initialization non-throwing.
Keep the existing WantMassQuery handling unchanged.

In `@src/libstore/include/nix/store/binary-cache-store.hh`:
- Around line 101-113: Update the doc comments for parseNixCacheInfo() and
applyCacheInfoFields() so they match the behavior in binary-cache-store.cc:
parseNixCacheInfo() should be described as parsing nix-cache-info and returning
known plus non-standard fields without applying them, while
applyCacheInfoFields() should describe applying the supported fields actually
handled there. Also remove the GetNarInfosV1 mention from these comments and
align the “known fields” wording with the current implementation.

---

Nitpick comments:
In `@src/libstore-tests/nar-info-disk-cache.cc`:
- Around line 120-126: Remove or update the stale commented-out test block in
NarInfo disk cache tests so it matches the current cache API. The dead code
still uses the old positional createCache signature and the removed
wantMassQuery/priority accessors; either delete this block entirely or rewrite
it to use the new fields-based shape, keeping the references aligned with
createCache and upToDateCacheExists.

In `@src/libstore/nar-info-disk-cache.cc`:
- Line 194: Wrap the `fields` parsing in `nar-info-disk-cache.cc` with the same
kind of `try/catch` used by `lookupRealisation` so malformed or legacy cache
rows fail with added context. Update the `queryCache.getStr(2)` JSON parse in
the `fields` initializer to rethrow with a message like “while parsing the local
disk cache” and keep the surrounding row handling consistent with the other
cache readers.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: df4a3231-1b4f-4246-85a4-a4eae26a4439

📥 Commits

Reviewing files that changed from the base of the PR and between 3888c31 and 156eb3a.

📒 Files selected for processing (6)
  • src/libstore-tests/nar-info-disk-cache.cc
  • src/libstore/binary-cache-store.cc
  • src/libstore/http-binary-cache-store.cc
  • src/libstore/include/nix/store/binary-cache-store.hh
  • src/libstore/include/nix/store/nar-info-disk-cache.hh
  • src/libstore/nar-info-disk-cache.cc

Comment thread src/libstore/binary-cache-store.cc
Comment thread src/libstore/include/nix/store/binary-cache-store.hh
edolstra and others added 2 commits June 26, 2026 20:18
This replaces the WantMassQuery and Priority SQLite columns with a
generic JSON field storing arbitrary nix-cache-info fields. This
allows fields to be added in the future without having to change the
database schema.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@edolstra edolstra force-pushed the nar-info-disk-cache-generic-fields branch from 156eb3a to 6108e4d Compare June 26, 2026 18:21
@edolstra edolstra enabled auto-merge June 26, 2026 18:21
@github-actions github-actions Bot temporarily deployed to pull request June 26, 2026 18:30 Inactive
@edolstra edolstra added this pull request to the merge queue Jun 26, 2026
Merged via the queue into main with commit a565d89 Jun 26, 2026
61 of 64 checks passed
@edolstra edolstra deleted the nar-info-disk-cache-generic-fields branch June 26, 2026 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants