Documentation
¶
Overview ¶
Package aggregator provides an example of how to use BuildSnapshotMetadata in the aggregator's publish cycle.
This example demonstrates the integration point where corpus stats are read and made available for snapshot construction.
Package aggregator provides snapshot publishing functionality for the commitgraph leaderboard.
This file implements the histogram sum invariant assertion that runs during the publish cycle to block publishes with histogram mismatches.
Package aggregator provides a production-ready example of the publish cycle with histogram invariant assertion integration.
This demonstrates the complete flow including: - Database connection setup - Metadata building - Assertion checking - Failure handling with appropriate error codes - Bypass mode for emergencies
Package aggregator provides snapshot publishing functionality for the commitgraph leaderboard.
The aggregator runs every 15 minutes and publishes a full ranked list as a Parquet snapshot. This package handles the metadata construction for the snapshot, including corpus stats, percentile distributions, and window information.
Index ¶
- func Example_mockPublishCycle()
- func Example_publishCycle()
- func Example_publishCycleWithBypass()
- func Example_publishCycleWithFailingAssertion()
- func Example_publishCycleWithPassingAssertion()
- func Example_publishExitCodes()
- func IsAssertionFailedError(err error) bool
- type AssertionFailedError
- type AssertionOptions
- type AssertionResult
- type CorpusTotals
- type HistogramViolation
- type PercentileDistribution
- type PublishResult
- type SnapshotMetadata
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Example_mockPublishCycle ¶
func Example_mockPublishCycle()
Example_mockPublishCycle shows a minimal publish cycle with mocked data for testing the integration point without a database.
func Example_publishCycle ¶
func Example_publishCycle()
Example_publishCycle demonstrates how to integrate BuildSnapshotMetadata and the histogram invariant assertion into the aggregator's 15-minute publish cycle.
This is a conceptual example showing the clear integration point where: 1. Database connection is established 2. BuildSnapshotMetadata is called (reads corpus_stats and percentile_distribution) 3. RunHistogramAssertion is called (blocks publish on histogram mismatches) 4. Metadata is used in snapshot construction (only if assertion passes)
func Example_publishCycleWithBypass ¶
func Example_publishCycleWithBypass()
Example_publishCycleWithBypass demonstrates emergency bypass usage.
func Example_publishCycleWithFailingAssertion ¶
func Example_publishCycleWithFailingAssertion()
Example_publishCycleWithFailingAssertion demonstrates handling assertion failure.
func Example_publishCycleWithPassingAssertion ¶
func Example_publishCycleWithPassingAssertion()
Example_publishCycleWithPassingAssertion demonstrates a successful publish cycle.
func Example_publishExitCodes ¶
func Example_publishExitCodes()
Example_publishExitCodes demonstrates returning appropriate exit codes.
func IsAssertionFailedError ¶
IsAssertionFailedError checks if an error is an AssertionFailedError.
Types ¶
type AssertionFailedError ¶
type AssertionFailedError struct {
ViolationCount int
Violations []HistogramViolation
Message string
}
AssertionFailedError is returned when the histogram invariant assertion fails. This is a distinct error type that allows callers to distinguish assertion failures from other errors (like database connection failures).
func (*AssertionFailedError) Error ¶
func (e *AssertionFailedError) Error() string
Error implements the error interface.
type AssertionOptions ¶
type AssertionOptions struct {
// Bypass allows the assertion to be bypassed for emergencies.
// When true, the assertion still runs but failures don't block publish.
Bypass bool
// BypassReason must be provided when Bypass is true for audit trail.
BypassReason string
}
AssertionOptions configures how the assertion runs.
type AssertionResult ¶
type AssertionResult struct {
// Passed indicates whether the assertion passed (no violations)
Passed bool
// ViolationCount is the number of users with histogram mismatches
ViolationCount int
// Violations contains detailed information about each violation
Violations []HistogramViolation
// Bypassed indicates whether the assertion was bypassed
Bypassed bool
// BypassReason contains the audit trail for why the assertion was bypassed
BypassReason string
// Timestamp when the assertion was run
Timestamp time.Time
}
AssertionResult represents the result of running the histogram invariant assertion.
func RunHistogramAssertion ¶
func RunHistogramAssertion(ctx context.Context, db *sql.DB, opts AssertionOptions) (*AssertionResult, error)
RunHistogramAssertion executes the histogram sum invariant assertion.
This verifies that for every user, the sum of the 30-element daily_ai_commits histogram array equals the ai_commits_30d scalar field. Both values are derived from the same source data over the same 30-day window, so any divergence indicates a bug in the histogram aggregation or ranking query.
The assertion runs the SQL query from migrations/invariant_7_histogram_reconciles.sql which returns rows where SUM(daily_ai_commits) != ai_commits_30d.
Returns:
- AssertionResult with detailed results
- error if the query execution fails (not assertion failure, query error)
Usage in publish cycle:
metadata, err := BuildSnapshotMetadata(ctx, db)
if err != nil {
return fmt.Errorf("build metadata: %w", err)
}
// Run histogram invariant assertion BEFORE publishing
assertionResult, err := RunHistogramAssertion(ctx, db, AssertionOptions{})
if err != nil {
return fmt.Errorf("run histogram assertion: %w", err)
}
if !assertionResult.Passed {
return fmt.Errorf("histogram assertion failed: %d violations, blocking publish",
assertionResult.ViolationCount)
}
// Assertion passed - proceed with publish
func (*AssertionResult) FormatViolationLog ¶
func (r *AssertionResult) FormatViolationLog() string
FormatViolationLog returns a formatted log message for the violations. This is useful for diagnostic output when the assertion fails.
func (*AssertionResult) ShouldBlockPublish ¶
func (r *AssertionResult) ShouldBlockPublish() bool
ShouldBlockPublish returns true if the assertion result should block publishing. This is true when the assertion failed AND bypass was not enabled.
type CorpusTotals ¶
type CorpusTotals struct {
// Commits is the total AI commits in corpus
Commits int64
// Developers is the total developers with AI commits
Developers int64
// Repositories is the total repositories with AI commits
Repositories int64
}
CorpusTotals contains the three corpus-wide totals from corpus_stats.
type HistogramViolation ¶
type HistogramViolation struct {
// CanonicalLogin is the user's canonical login (after alias merge)
CanonicalLogin string
// AICommits30d is the total commits in the 30-day window from the ranking query
AICommits30d int
// HistogramSum is the sum of the 30-element daily_ai_commits array
HistogramSum int
// Difference is the discrepancy (ai_commits_30d - histogram_sum)
Difference int
}
HistogramViolation represents a single row where the histogram sum doesn't match ai_commits_30d.
type PercentileDistribution ¶
type PercentileDistribution struct {
// P50 is the median (50th percentile) of ai_commits_30d
P50 float64
// P75 is the 75th percentile (third quartile)
P75 float64
// P90 is the 90th percentile (top 10% threshold)
P90 float64
// P95 is the 95th percentile (top 5% threshold)
P95 float64
// P99 is the 99th percentile (top 1% threshold)
P99 float64
}
PercentileDistribution contains the percentile distribution of ai_commits_30d.
type PublishResult ¶
type PublishResult struct {
// Success indicates whether the publish succeeded
Success bool
// Blocked indicates whether the publish was blocked by assertion failure
Blocked bool
// AssertionResult contains the histogram assertion results
AssertionResult *AssertionResult
// SnapshotMetadata contains the snapshot metadata
SnapshotMetadata *SnapshotMetadata
// Error contains any error that occurred
Error error
}
PublishResult represents the result of a publish cycle.
func RunPublishCycle ¶
func RunPublishCycle(ctx context.Context, dsn string, opts AssertionOptions) *PublishResult
RunPublishCycle executes the complete publish cycle with assertion checking.
This function demonstrates the production flow where: 1. Database connection is established 2. Snapshot metadata is built (corpus stats, percentiles) 3. Histogram invariant assertion is run 4. Publish proceeds only if assertion passes (or bypass is enabled) 5. Appropriate error codes are returned for different failure modes
Returns PublishResult with detailed status.
func RunPublishCycleWithBypass ¶
func RunPublishCycleWithBypass(ctx context.Context, dsn, bypassReason string) *PublishResult
RunPublishCycleWithBypass executes the publish cycle with bypass enabled. This should only be used in emergencies with a clear audit trail.
The bypass reason is logged and included in the assertion result for auditing.
type SnapshotMetadata ¶
type SnapshotMetadata struct {
// WindowStart is the first day of the 30-day histogram window.
// All rows in the snapshot share the same window: current_date - 29
WindowStart time.Time
// Totals contains corpus-wide scalar totals from corpus_stats table
Totals CorpusTotals
// Percentiles contains the percentile distribution of ai_commits_30d
// across all ranked users
Percentiles PercentileDistribution
// GeneratedAt is when this snapshot was created
GeneratedAt time.Time
}
SnapshotMetadata contains the file-level metadata for a published snapshot.
These fields are stored once per snapshot file (in Parquet metadata/footer), not per row. All rows share the same window_start and totals.
func BuildSnapshotMetadata ¶
BuildSnapshotMetadata constructs the snapshot metadata by reading corpus stats and percentile distribution from the database.
This function implements the integration point for the aggregator's publish cycle. It is called after the database is accessible and before the snapshot is constructed, ensuring the stats are available when we build the snapshot.
Parameters:
- ctx: Context for database queries
- db: Database connection (PostgreSQL)
Returns:
- *SnapshotMetadata: Complete snapshot metadata with totals, percentiles, and window info
- error: Database error if any query fails
Integration flow: 1. Establish database connection (caller's responsibility) 2. Call BuildSnapshotMetadata to read stats and construct metadata 3. Use metadata when constructing the Parquet snapshot
Example usage:
ctx := context.Background()
db, err := sql.Open("postgres", dsn)
if err != nil {
return fmt.Errorf("failed to connect to database: %w", err)
}
defer db.Close()
metadata, err := aggregator.BuildSnapshotMetadata(ctx, db)
if err != nil {
return fmt.Errorf("failed to build snapshot metadata: %w", err)
}
// Use metadata in snapshot construction
fmt.Printf("Publishing snapshot with %d total commits\n", metadata.Totals.Commits)