← Back to Pull requests

Close a removed server's unmatched-stop episodes and retry a failed startup reconcile #476

Closed
unknown closed this pull request without merging on Nov 29, 2026 16:46 UTC
View on GitHub
unknown unknown commented on Nov 29, 2026 07:49 UTC
Author

Fixes #475

Follow-up to #473. Both gaps from the issue, plus the comment cleanup.

1. Startup reconcile is retried after a database failure

ReconcileRemovedServers and ReconcileConfiguredAgencies now return whether the pass finished (list, load and every close write). reconcileUnmatchedStopsAtStartup passes that up, and:

  • refreshHistory clears historyStartupPending only when the pass succeeds, so the next refresh tries again.

  • ReconcileHistoryAtStartup sets historyStartupPending when its own pass fails, so in a --config-url deployment the first refresh picks it up. With --config-file there is no refresh loop, so that case still waits for a restart, same as today.
  • Re-running the pass is safe: it only closes episodes that are still open.

    2. Live server removal closes episodes like the startup pass

    FlushServer only saved last_reported_at and dropped the server from memory. It is replaced by CloseServer, which closes the open episodes as agency_removed against each agency's last complete observation (same closeRemovedAgenciesLocked(entry, nil) path the startup pass uses). If the entry was left unloaded by a failed write it reloads first. If the close write fails, the entry stays in memory and the row stays open, so the next config update retries, same as FlushAgencies does for removed agencies.

    flushRemovedServerHistory calls CloseServer for base URLs that are gone, then runs ReconcileRemovedServers too. That covers a server-scoped server that nothing has recorded since the restart: it has no in-memory entry, so TrackedServerURLs doesn't list it, but the database does. A failed close also marks the entry unloaded (as RecordOBAMetrics does), since the commit may have landed anyway. A live removal and a removal across a restart now end up the same in PostgreSQL.

    3. collectMu comment

    It now says refreshHistory and FlushHistory take the lock too, and that a refresh holding it can make a tick skip with "previous cycle still running".

    docs/historical-investigation-schema.md had a line saying a removed server's unmatched episodes only get last_reported_at saved. I updated it to say they close as agency_removed.

    Testing

    go vet ./..., gofmt, and go test -count=1 ./... with WATCHDOG_TEST_DATABASE_URL set (PostgreSQL 17 locally) all pass. My local golangci-lint is built with go1.26 and won't load the go1.27 config, so CI is the lint check here.

    New or changed tests:

  • TestReconcileHistoryAtStartup_RetriesAfterDatabaseFailure (new): the startup pass runs on a canceled context, the departed server's episode stays open, and the next OnConfigUpdated closes it as agency_removed. It fails on main (episodes closed after the next refresh = 0, want 1).

  • TestConfigRefreshFlushWaitsForHistoryRecording/server: now also asserts agency_removed for the whole-server case. It fails with the old flush (close_reason = <nil>).

  • TestCloseServer_ClosesAsAgencyRemoved (replaces TestLastReported_SavedOnServerRemoval): closed as agency_removed, last_reported_at kept, server no longer tracked.

  • TestCloseServer_RetriesAfterFailedWrite (new): a failed close keeps the server tracked, the row open and the entry unloaded, and the retry closes it.

  • TestConfigRefresh_ClosesUnpolledRemovedServer (new): a server-scoped server with an open episode and no in-memory entry is removed by a refresh and closes as agency_removed. Fails without the refresh-time reconcile.

  • TestLastReported_CloseServerAfterFailedWrite (was FlushServerAfterFailedWrite): the later in-memory last_reported_at survives the reload and is what the close writes.
  • <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    Bug Fixes
    Historical episodes now close with the appropriate reason when an agency, server, or feed is removed.
    If history reconciliation or a save fails, affected episodes remain available for a later retry instead of being prematurely cleared.
    Removing a configured server now closes its episodes even when the server was not being tracked in memory.
    Documentation
    Clarified that closed episodes are retained for 90 days, and that open episodes for configured sources are retained for 90 days after their last observation.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 554 lines (+415 / -139) across 6 files Reviewer: DsThakurRawat Review duration: 537m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 554 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.378
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #476"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.1% (MEDIUM) · Higher = larger blast radius
    48.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19% · Safe to merge
    19%