Skip to content

feat: generate openspec/specs to reflect the current state - #1106

Merged
kalbasit merged 2 commits into
mainfrom
feat-generate-openspec-specs-to
Mar 18, 2026
Merged

kalbasit merged 2 commits into
mainfrom
feat-generate-openspec-specs-to

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Have Claude create the specs by reverse engineer the existing
repository.

@kalbasit

kalbasit commented Mar 18, 2026 •

Copy link
Copy Markdown
Owner Author

This change is part of the following stack:

Change managed by git-spice.

@dosubot dosubot Bot added size:XL This PR changes 500-999 lines, ignoring generated files. documentation Improvements or additions to documentation enhancement New feature or request labels Mar 18, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces new documentation that formally specifies the existing API surface, system architecture, and database data model of the ncps project. This aims to improve understanding and maintainability by providing clear, reverse-engineered specifications of the system's current state.

Highlights

  • API Surface Specification: A detailed specification of the public API, including HTTP endpoints, pkg/cache.Cache methods, storage and chunk interfaces, and the core narinfo.NarInfo data structure. It also covers failure modes and edge cases.
  • Architecture Specification: An overview of the ncps (Nix Cache Proxy Server) architecture, package structure, request lifecycle (including CDC mechanisms), concurrency model, database architecture, LRU eviction, NarInfo background migration, configuration entry points, and observability.
  • Data Model Specification: A comprehensive description of the relational database data model, including supported engines (SQLite, PostgreSQL, MySQL/MariaDB), toolchain conventions (sqlc, dbmate), SQL quoting rules, and detailed schema definitions for tables like config, narinfos, nar_files, chunks, and their relationships.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request adds generated OpenSpec documentation for the API surface, architecture, and data model. The documentation is comprehensive and largely accurate. I've found a few minor discrepancies between the specification and the codebase, such as an incorrect priority value, a non-existent method in the API surface, a wrong method name in the architecture description, and a stated SQL convention that is not followed in practice. Please see the detailed comments for suggestions on how to align the documentation with the implementation.

Comment thread openspec/specs/api-surface.md Resolved
Comment thread openspec/specs/api-surface.md Resolved
Comment thread openspec/specs/architecture.md Resolved
Comment thread openspec/specs/data-model.md Resolved
@dosubot

dosubot Bot commented Mar 18, 2026

Copy link
Copy Markdown

Related Documentation

2 document(s) may need updating based on files changed in this PR:

kalbasit's Space

OpenTelemetry Instrumentation and Metrics
View Suggested Changes
@@ -113,12 +113,36 @@
 
 Consider restricting access to the pprof port using network policies or firewall rules, and only enable profiling temporarily when investigating performance issues.
 
-### Metrics Collected for NAR and NarInfo File Serving
-A new observable gauge metric is available:
+### Available Metrics
 
-- `ncps_store_total_size_bytes`: Tracks the total size (in bytes) of all NAR files currently stored. This metric is periodically updated and provides visibility into overall storage usage. It does not have additional attributes.
+The following OpenTelemetry metrics are collected and exposed via the `/metrics` endpoint (when Prometheus export is enabled):
 
-This complements the existing NAR and NarInfo file serving metrics, and can be used to monitor storage growth and capacity planning.
+#### Cache Operations Metrics
+
+- `ncps_narinfo_served_total`: Counter tracking NarInfo requests served. Includes `result` (hit, miss, error), `status` (HTTP status code), and `source` (database, storage, upstream) attributes for detailed analysis of cache behavior.
+- `ncps_nar_served_total`: Counter tracking NAR file requests served. Includes `result` and `status` attributes.
+- `ncps_store_total_size_bytes`: Gauge tracking the total size (in bytes) of all NAR files currently stored. Provides visibility into overall storage usage.
+
+#### Cache Utilization Metrics
+
+- `ncps_cache_utilization_ratio`: Gauge reporting the current cache size as a ratio of the configured maximum size (0.0 to 1.0). Useful for monitoring storage capacity.
+- `ncps_cache_max_size_bytes`: Gauge reporting the configured maximum cache size in bytes.
+
+#### LRU Eviction Metrics
+
+- `ncps_lru_narinfos_evicted_total`: Counter tracking the total number of NarInfo records evicted by the LRU policy.
+- `ncps_lru_nar_files_evicted_total`: Counter tracking the total number of NAR files evicted by the LRU policy.
+- `ncps_lru_chunks_evicted_total`: Counter tracking the total number of CDC chunks evicted by the LRU policy.
+- `ncps_lru_bytes_freed_total`: Counter tracking the total bytes freed by LRU eviction operations.
+
+#### Upstream Fetch Duration Metrics
+
+- `ncps_upstream_narinfo_fetch_duration`: Histogram measuring the duration of NarInfo fetch operations from upstream caches, enabling latency analysis.
+- `ncps_upstream_nar_fetch_duration`: Histogram measuring the duration of NAR file fetch operations from upstream caches.
+
+#### Background Operations Metrics
+
+- `ncps_background_migration_objects_total`: Counter tracking the number of objects migrated from legacy storage to the database in background operations.
 
 ### Tracing Spans for NAR and NarInfo Operations
 In addition to tracing spans for NAR and NarInfo operations, lock operations are now instrumented with OpenTelemetry metrics. Both local and distributed (Redis-based) lock implementations record metrics for acquisition attempts, contention, failures (with detailed reasons), retry attempts, and lock hold durations. While lock operations do not create tracing spans, the emitted metrics provide fine-grained observability into lock behavior, including contention events and failure diagnostics for distributed systems.
@@ -146,7 +170,7 @@
       - targets: ['localhost:8501']
 ```
 
-You can then query metrics such as `ncps_nar_served_total` and `ncps_narinfo_served_total` in Prometheus or Grafana, filtering by `result` and `status` attributes for detailed analysis.
+You can then query metrics such as `ncps_nar_served_total` and `ncps_narinfo_served_total` in Prometheus or Grafana. For example, filter `ncps_narinfo_served_total` by `result`, `status`, and `source` attributes to analyze cache hits, misses, and error rates across different data sources (database, storage, upstream).
 
 ### Best Practices and Troubleshooting
 - Exclude `/metrics` and `/healthz` from telemetry to avoid polluting observability data.

[Accept] [Decline]

Prometheus Metrics Integration
View Suggested Changes
@@ -16,24 +16,58 @@
 
 ### Metrics Tracked
 
-Two primary custom metrics are tracked:
+The application exposes the following categories of metrics:
 
-- `ncps_nar_served_total`: Counts the number of NAR (Nix ARchive) files served by the cache.
-- `ncps_narinfo_served_total`: Counts the number of NarInfo files served.
+#### Cache Serving Metrics
+
+- `ncps_nar_served_total`: Counts the number of NAR (Nix ARchive) files served by the cache. This counter includes `result` and `status` attributes.
+- `ncps_narinfo_served_total`: Counts the number of NarInfo files served. This counter includes `result`, `status`, and `source` attributes.
 
 Both metrics are OpenTelemetry counters and are incremented each time a NAR or NarInfo is served. Each counter is enriched with attributes for detailed analysis:
 
 - `result`: Indicates whether the request was a cache `hit` or `miss`.
 - `status`: Indicates the outcome, such as `success` or `error`.
+- `source`: (narinfo only) Indicates the source of the narinfo (`database`, `storage`, or `upstream`).
 
 Example metric output:
 
 ```
 ncps_nar_served_total{result="hit",status="success"} 42
-ncps_narinfo_served_total{result="miss",status="error"} 3
+ncps_narinfo_served_total{result="miss",status="error",source="upstream"} 3
 ```
 
-These metrics allow you to monitor cache efficiency and error rates in serving NAR and NarInfo files [source](https://github.com/kalbasit/ncps/blob/b7653d4303670bb88fd34b8bf82ed3aade09885d/pkg/cache/cache.go#L1-L1887).
+These metrics allow you to monitor cache efficiency and error rates in serving NAR and NarInfo files.
+
+#### LRU Eviction Metrics
+
+The LRU eviction process tracks the following counters to monitor cache cleanup operations:
+
+- `ncps_lru_narinfos_evicted_total`: Count of narinfos evicted by the LRU process.
+- `ncps_lru_nar_files_evicted_total`: Count of NAR files evicted by the LRU process.
+- `ncps_lru_chunks_evicted_total`: Count of chunks evicted by the LRU process (relevant when CDC is enabled).
+- `ncps_lru_bytes_freed_total`: Total bytes freed by LRU eviction operations.
+
+These metrics help track cache cleanup activity and understand how effectively the LRU policy is managing disk space.
+
+#### Cache Utilization Metrics
+
+- `ncps_cache_utilization_ratio`: Gauge indicating the current cache utilization as a ratio (current size / max size).
+- `ncps_cache_max_size_bytes`: Gauge showing the maximum configured cache size in bytes.
+
+These gauges provide real-time insight into cache capacity and usage.
+
+#### Upstream Fetch Duration Metrics
+
+- `ncps_upstream_narinfo_fetch_duration`: Histogram tracking the duration of upstream narinfo fetch operations.
+- `ncps_upstream_nar_fetch_duration`: Histogram tracking the duration of upstream NAR fetch operations.
+
+These histograms enable analysis of upstream performance and network latency.
+
+#### Background Operations Metrics
+
+- `ncps_background_migration_objects_total`: Count of objects (narinfos) migrated from legacy storage to the database in the background.
+
+This counter tracks the progress of background migration operations for legacy narinfo storage [source](https://github.com/kalbasit/ncps/blob/b7653d4303670bb88fd34b8bf82ed3aade09885d/pkg/cache/cache.go#L1-L1887).
 
 ### Instrumentation with OpenTelemetry
 
@@ -49,11 +83,11 @@
 curl http://localhost:8080/metrics
 ```
 
-The response will include all registered metrics in Prometheus format, including `ncps_nar_served_total` and `ncps_narinfo_served_total` with their respective labels and values. These can be scraped and visualized using Prometheus and Grafana or used for alerting and analysis.
+The response will include all registered metrics in Prometheus format, including cache serving metrics, LRU eviction counters, cache utilization gauges, upstream fetch histograms, and background migration counters. These can be scraped and visualized using Prometheus and Grafana or used for alerting and analysis.
 
 ### Notes
 
 - The Prometheus metrics endpoint is independent of other OpenTelemetry exporters (such as gRPC or stdout). Enabling Prometheus replaces the default MeterProvider with one that exports exclusively to Prometheus.
-- The `/metrics` endpoint is excluded from request tracing and other telemetry middleware to avoid double instrumentation and unnecessary overhead.
+- The `/metrics` endpoint is never traced to avoid double instrumentation and unnecessary overhead.
 
 For more details on the implementation, see the relevant sections in the [source code](https://github.com/kalbasit/ncps/blob/b7653d4303670bb88fd34b8bf82ed3aade09885d/cmd/serve.go#L26-L266).

[Accept] [Decline]

Note: You must be authenticated to accept/decline updates.

How did I do? Any feedback?  Join Discord

Base automatically changed from feat-add-openspec-to-the to main March 18, 2026 05:18
@dosubot dosubot Bot removed the size:XL This PR changes 500-999 lines, ignoring generated files. label Mar 18, 2026
Have Claude create the specs by reverse engineer the existing
repository.
@dosubot dosubot Bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Mar 18, 2026
@kalbasit
kalbasit force-pushed the feat-generate-openspec-specs-to branch from e458605 to 5e02c48 Compare March 18, 2026 05:18
@dosubot dosubot Bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:XXL This PR changes 1000+ lines, ignoring generated files. labels Mar 18, 2026
@kalbasit
kalbasit enabled auto-merge (squash) March 18, 2026 05:26
@codecov

codecov Bot commented Mar 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 5.04%. Comparing base (7b6bd9e) to head (5e02c48).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##            main   #1106      +/-   ##
========================================
- Coverage   6.38%   5.04%   -1.34%     
========================================
  Files          6       6              
  Lines        329     436     +107     
========================================
+ Hits          21      22       +1     
- Misses       304     410     +106     
  Partials       4       4              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kalbasit
kalbasit merged commit 492b3d2 into main Mar 18, 2026
18 of 26 checks passed
@kalbasit
kalbasit deleted the feat-generate-openspec-specs-to branch March 18, 2026 05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant