← Back to Pull requests

fix(agent): drop a silent controller connection and reconnect #1290

Closed
unknown closed this pull request without merging on Nov 21, 2026 11:58 UTC
View on GitHub
unknown unknown commented on Nov 15, 2026 07:54 UTC
Author

Fixes #1285.

Problem

The controller pings every 15 seconds and drops an agent after 45 seconds without a pong (heartbeat_timeout). The agent has no check in the other direction: it only answers pings and its socket has no keepalive. When the network path breaks silently (VPN restart, mesh path change), the controller drops the session while the agent keeps a half-open connection that the operating system still reports as established. The agent never reconnects and every backup on it fails with Failed to send backup command to agent until the container is restarted.

We hit this on three agents in production over three days. In each case ss inside the agent container showed the connection to the controller as ESTAB, while the reverse proxy in front of the controller had no connection from that host.

Fix

  • The agent session records when it last received a message from the controller. A watchdog checks every 15 seconds and, after 90 seconds of silence (six missed pings, twice the controller's own timeout), logs a warning with the session diagnostics and drops the connection. The existing onclose path then reconnects.

  • The connection is dropped with terminate(). A close handshake never completes on a dead connection: with Bun 1.4.4, ws.close() against a silent peer fired no close event within 8 seconds, while terminate() fired it immediately with code 1006. close() stays as a fallback when terminate is not available.

  • The outbound send failure path uses the same helper, for the same reason.
  • No protocol change: the agent reacts to the existing heartbeat.ping messages, so it works with any controller of the same protocol version.

    Tests

  • New tests in apps/agent/src/__tests__/controller-session.test.ts: a silent controller connection is terminated after the timeout, and a connection that keeps receiving pings is kept.

  • bun fmt --check, bun check and the agent test suite pass (219 passed, 1 skipped).

  • Built on top of v0.44.2 for our agents, where we are rolling it out.
  • <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    Bug Fixes
    Silent controller connections are now terminated after 90 seconds without messages, helping prevent inactive sessions from remaining connected.
    Active connections that continue sending heartbeats remain connected.
    When timer processing is delayed, a brief grace period helps avoid ending a connection if a heartbeat arrives shortly afterward.
    * Failed message delivery now terminates the connection more reliably, allowing the session to close cleanly.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 342 lines (+256 / -86) across 4 files Reviewer: nicotsx Review duration: 8883m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 342 lines diff across 4 files
    0.42
    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.349
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1290"
    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: 42.3% (MEDIUM) · Higher = larger blast radius
    42.3%
    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: 16.7% · Safe to merge
    16.7%