feat: structured disconnect attribution for agent logs (#25191)

Implements
[PLAT-60](https://linear.app/codercom/issue/PLAT-60/enhance-disconnect-logs-with-structured-reason-attribution):
adds structured disconnect attribution to disconnect logs throughout the
agent and tailnet packages.

Every disconnect log site now carries structured slog fields. All
existing logs remain; existing messages are preserved with the fields
added alongside.

New fields on disconnect log lines:

- `connect_type` — which layer disconnected: `server_to_agent`,
`agent_to_client`, or `client_to_server`
- `disconnect_reason` — categorical reason: `graceful`, `network_error`,
`server_shutdown`, etc.
- `disconnect_expected` — whether the disconnect is normal operation
(`true`) or should be investigated (`false`)
- `disconnect_initiator` — who started it: `client`, `agent`, `server`,
or `network` (control-plane sites only)
- `disconnect_detail` — free-form supplemental info (where useful)

## What's covered

**Control plane (`server_to_agent`):** coordination RPC, DERP map
subscriber, agent runLoop, agent Close, `BasicCoordination.Close`,
`Controller.run`.

**Data plane (`agent_to_client`):** SSH sessions, reconnecting PTY,
JetBrains port-forwarding.

<details>
<summary>Control-plane sites</summary>

| Site | Reason | Initiator |
|---|---|---|
| `agent/agent.go` `runLoop` EOF | `network_error` | `network` |
| `agent/agent.go` `runCoordinator` deferred exit | `server_shutdown` /
`graceful` / `network_error` | `agent` / `server` / `network` |
| `agent/agent.go` `runDERPMapSubscriber` deferred exit | same (shared
`classifyCoordinatorRPCExit`) | same |
| `agent/agent.go` `Close` shutdown timeout | `server_shutdown` + detail
| `agent` |
| `agent/agent.go` `Close` clean coord disconnect | `server_shutdown` |
`agent` |
| `tailnet/controllers.go` `BasicCoordination.Close` | `graceful` or
`network_error` | `c.initiator` |
| `tailnet/controllers.go` `Controller.run` `net.ErrClosed` |
`network_error` | `network` |

</details>

<details>
<summary>Data-plane sites</summary>

| Site | Reason | Notes |
|---|---|---|
| `agent/agentssh/agentssh.go` SSH session closed | free-form
(`graceful`, `process exited with error status: N`, etc.) | Also sets
`closeCause("normal exit")` for clean exits so coderd's
`connection_log.DisconnectReason` is no longer empty |
| `agent/reconnectingpty/server.go` PTY closed | `server_shutdown`,
error string, or `graceful` | |
| `agent/agentssh/jetbrainstrack.go` channel closed | `normal close` or
error string | Previously passed empty reason |

</details>

<details>
<summary>Bug fix</summary>

The deferred `disconnected from coordination RPC` log no longer fires
when the initial `Coordinate()` RPC call fails before any connection is
established.

</details>

Refs PLAT-60.

---

_This PR was prepared by Coder Agents on behalf of @Emyrk._
**Manually QA'd a lot of common disconnects**

---------

Co-authored-by: Coder Agents <noreply@coder.com>
This commit is contained in:
Steven Masley
2026-05-19 09:47:03 -05:00
committed by GitHub
co-authored by Coder Agents
parent c9c933a300
commit 1afc6d4fd0
10 changed files with 595 additions and 26 deletions
+55
View File
@@ -2998,6 +2998,18 @@ export interface ChatWorkspaceTTLResponse {
*/
export const CoderDesktopTelemetryHeader = "Coder-Desktop-Telemetry";
// From codersdk/disconnect.go
export type ConnectionDirection =
| "agent_to_client"
| "client_to_server"
| "server_to_agent";
export const ConnectionDirections: ConnectionDirection[] = [
"agent_to_client",
"client_to_server",
"server_to_agent",
];
// From codersdk/insights.go
/**
* ConnectionLatency shows the latency for a connection.
@@ -3089,6 +3101,11 @@ export interface ConnectionLogsRequest extends Pagination {
readonly q?: string;
}
// From codersdk/disconnect.go
export type ConnectionMethod = "derp" | "direct" | "";
export const ConnectionMethods: ConnectionMethod[] = ["derp", "direct", ""];
// From codersdk/connectionlog.go
export type ConnectionType =
| "jetbrains"
@@ -4032,6 +4049,44 @@ export const DiagnosticSeverityStrings: DiagnosticSeverityString[] = [
"warning",
];
// From codersdk/disconnect.go
export type DisconnectInitiator =
| "agent"
| "client"
| "network"
| "server"
| "";
export const DisconnectInitiators: DisconnectInitiator[] = [
"agent",
"client",
"network",
"server",
"",
];
// From codersdk/disconnect.go
export type DisconnectReason =
| "client_closed"
| "control_plane_lost"
| "graceful"
| "network_error"
| "protocol_error"
| "server_shutdown"
| ""
| "workspace_stopped";
export const DisconnectReasons: DisconnectReason[] = [
"client_closed",
"control_plane_lost",
"graceful",
"network_error",
"protocol_error",
"server_shutdown",
"",
"workspace_stopped",
];
// From codersdk/workspaceagents.go
export type DisplayApp =
| "port_forwarding_helper"