Skip to content

feat: add phase tracking, histograms, and safe config exposure to observability contract - #57

Merged
rbtr merged 1 commit into
mainfrom
feat/observability-contract
Jul 22, 2026
Merged

rbtr merged 1 commit into
mainfrom
feat/observability-contract

Conversation

@rbtr

@rbtr rbtr commented Jul 22, 2026

Copy link
Copy Markdown
Owner

Summary

Extends the observability contract with three categories of additions, all backward-compatible.

1. Per-batch phase tracking

A durable phase field (and phaseSince timestamp) is added to the engine's activeBatch struct. Only phases observable between Reconcile ticks are modelled:

Phase Condition
waiting_gate Batch staged; gate test running (outcome == "").
waiting_merge Gate passed; releasing PRs to forge auto-merge.
bisecting Batch is a bisection sub-batch; gate test running.

Queue-level linger state is exposed separately via a linger_since timestamp (not a per-batch phase).

2. New JSON fields in /status

Three additive fields are added to each per-queue JSON object. active_batches and pending_batches remain [][]int and are unchanged. TestOldJSONShapeUnchanged is a regression test that unmarshal-and-checks both fields stay [][]int.

  • active_batch_states — [{prs: []int, phase: string, phase_since: RFC3339}]
  • linger_since — RFC3339; present while the queue is accumulating PRs in the linger window, absent otherwise
  • config — safe resolved configuration subset:
    {config_source, base, merge_style, max_batch, batch_linger, batch_target, bisect_fanout}

config_source is "repo" when a .shunt.yml was found, "default" otherwise.

Explicitly excluded from config: token, instance_url, webhook_secret, bot_email, bot_user, staging_branch, public_url, lease_holder_id, status_ctx.

3. Four new Prometheus histograms

Labels: {owner, repo, base}. No PR-number or other high-cardinality labels.

Histogram What it measures Buckets
shunt_linger_seconds Batch-linger window duration 1, 5, 10, 30, 60, 120, 300, 600, 1800 s
shunt_gate_seconds{outcome} Gate run duration 60, 300, 900, 1800, 3600, 7200, 21600, 43200, 86400 s
shunt_native_merge_seconds Forge auto-merge wait 10, 30, 60, 120, 300, 600, 1800 s
shunt_reconcile_seconds Per-queue Reconcile call 0.01, 0.05, 0.1, 0.25, 0.5, 1, 5, 10, 60 s

shunt_reconcile_seconds is only recorded on the lease-holding instance — the start capture and defer are placed after the !held early return so standby instances in HA deployments do not skew the histogram.

Backward compatibility

  • active_batches and pending_batches remain [][]int.
  • All new /status fields use omitempty; consumers that only read the old fields are unaffected.
  • No new SHUNT_* env vars — all additions are always-on read-only observability.
  • No new external Go dependencies.

Tests

  • TestOldJSONShapeUnchanged — unmarshals /status JSON and verifies active_batches/pending_batches are still [][]int
  • TestNewStatusFieldsAppear — verifies active_batch_states, linger_since, config appear; verifies sensitive fields are absent
  • TestPrometheusNewHistograms — verifies all 4 new histogram families appear in /metrics
  • TestPhaseTracking, TestWaitingMergePhase — phase transitions
  • TestLingerSinceExposedInStatus, TestEffectiveConfigExposedInStatus — status fields
  • TestGateDurationHistogram, TestNativeMergeDurationHistogram, TestReconcileDurationHistogram, TestLingerDurationHistogram — histogram recording
  • TestStatusSnapshotCopiesBatches extended — verifies ActiveBatchStates[0].PRs is isolated from caller mutations
  • make lint test build ✅ go test -race ./... ✅

Review notes

A code-review pass was run before commit. Two issues were found and fixed:

  1. Reconcile timer moved to after the lease check (standby instances in HA deployments no longer skew the histogram with sub-millisecond no-op observations).
  2. ObserveQueueStatus now deep-copies each ActiveBatchState.PRs slice (not just a shallow append), and TestStatusSnapshotCopiesBatches asserts isolation of the new field.

Docs

  • docs/design.md — new "Observability contract" section with phase table, histogram buckets, and safe config subset
  • README.md — Observability section updated with new histograms and new /status fields
  • CHANGELOG.md — [Unreleased] entry

Add durable per-batch phase tracking (waiting_gate, waiting_merge,
bisecting) to the engine's activeBatch struct, persisting across
Reconcile ticks. Expose phase detail, linger window, and safe resolved
config via new additive fields in the /status JSON response.

New fields (existing active_batches/pending_batches [][]int unchanged):
- active_batch_states: [{prs, phase, phase_since}] per active batch
- linger_since: RFC3339 timestamp while the queue is in the linger window
- config: {config_source, base, merge_style, max_batch, batch_linger,
  batch_target, bisect_fanout} — sensitive fields excluded

config_source is 'repo' when a .shunt.yml was found, 'default' otherwise.
Excluded from config: token, instance_url, webhook_secret, bot_email,
bot_user, staging_branch, public_url, lease_holder_id, status_ctx.

Add four new Prometheus histograms (labels: owner, repo, base):
- shunt_linger_seconds: batch-linger window duration
- shunt_gate_seconds{outcome}: gate run duration (labelled by outcome)
- shunt_native_merge_seconds: forge auto-merge wait duration
- shunt_reconcile_seconds: per-queue Reconcile call duration
  (only recorded on the lease-holding instance in HA deployments)

Bisect sub-batches are tracked via a bisectOrigins map so startNext()
can assign phase=bisecting without requiring a parameter change.

Gate duration is recorded in checkActive() when outcome is set; native
merge duration uses the existing releasedAt field in recordLanded();
reconcile duration uses a defer placed after the lease check.

Backward compatibility: active_batches and pending_batches remain [][]int.
New JSON fields use omitempty. TestOldJSONShapeUnchanged regression test
confirms the old shape is unaffected.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rbtr
rbtr merged commit 2c60bd9 into main Jul 22, 2026
4 checks passed
@rbtr
rbtr deleted the feat/observability-contract branch July 22, 2026 16:28
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.

1 participant