Close a removed server's unmatched-stop episodes and retry a failed startup reconcile #476
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
DsThakurRawat
Fixes #475
Follow-up to #473. Both gaps from the issue, plus the comment cleanup.
1. Startup reconcile is retried after a database failure
ReconcileRemovedServersandReconcileConfiguredAgenciesnow return whether the pass finished (list, load and every close write).reconcileUnmatchedStopsAtStartuppasses that up, and:refreshHistoryclearshistoryStartupPendingonly when the pass succeeds, so the next refresh tries again.ReconcileHistoryAtStartupsetshistoryStartupPendingwhen its own pass fails, so in a--config-urldeployment the first refresh picks it up. With--config-filethere 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
FlushServeronly savedlast_reported_atand dropped the server from memory. It is replaced byCloseServer, which closes the open episodes asagency_removedagainst each agency's last complete observation (samecloseRemovedAgenciesLocked(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 asFlushAgenciesdoes for removed agencies.flushRemovedServerHistorycallsCloseServerfor base URLs that are gone, then runsReconcileRemovedServerstoo. That covers a server-scoped server that nothing has recorded since the restart: it has no in-memory entry, soTrackedServerURLsdoesn't list it, but the database does. A failed close also marks the entry unloaded (asRecordOBAMetricsdoes), 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
refreshHistoryandFlushHistorytake the lock too, and that a refresh holding it can make a tick skip with "previous cycle still running".docs/historical-investigation-schema.mdhad a line saying a removed server's unmatched episodes only getlast_reported_atsaved. I updated it to say they close asagency_removed.Testing
go vet ./...,gofmt, andgo test -count=1 ./...withWATCHDOG_TEST_DATABASE_URLset (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 nextOnConfigUpdatedcloses it asagency_removed. It fails on main (episodes closed after the next refresh = 0, want 1).TestConfigRefreshFlushWaitsForHistoryRecording/server: now also assertsagency_removedfor the whole-server case. It fails with the old flush (close_reason = <nil>).TestCloseServer_ClosesAsAgencyRemoved(replacesTestLastReported_SavedOnServerRemoval): closed asagency_removed,last_reported_atkept, 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 asagency_removed. Fails without the refresh-time reconcile.TestLastReported_CloseServerAfterFailedWrite(wasFlushServerAfterFailedWrite): the later in-memorylast_reported_atsurvives 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 -->