Skip to content

Commit 2576d84

Browse files
authored
fix(cli): surface live channel probe results (#117410)
1 parent 20ac22f commit 2576d84

3 files changed

Lines changed: 232 additions & 131 deletions

File tree

‎src/commands/health-format.test.ts‎

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import { describe, expect, it } from "vitest";
2+
import type { HealthSummary } from "../gateway/health/types.js";
3+
import { formatHealthChannelLines } from "./health-format.js";
4+
5+
const createHealthSummary = (
6+
params: Pick<HealthSummary, "channels" | "channelOrder" | "channelLabels">,
7+
): HealthSummary => ({
8+
ok: true,
9+
ts: 0,
10+
durationMs: 0,
11+
heartbeatSeconds: 60,
12+
defaultAgentId: "main",
13+
agents: [],
14+
sessions: { path: "/tmp/sessions.json", count: 0, recent: [] },
15+
...params,
16+
});
17+
18+
describe("formatHealthChannelLines", () => {
19+
it("formats per-account probe timings", () => {
20+
const summary = createHealthSummary({
21+
channels: {
22+
telegram: {
23+
accountId: "main",
24+
configured: true,
25+
probe: { ok: true, elapsedMs: 196, bot: { username: "pinguini_ugi_bot" } },
26+
accounts: {
27+
main: {
28+
accountId: "main",
29+
configured: true,
30+
probe: { ok: true, elapsedMs: 196, bot: { username: "pinguini_ugi_bot" } },
31+
},
32+
flurry: {
33+
accountId: "flurry",
34+
configured: true,
35+
probe: { ok: true, elapsedMs: 190, bot: { username: "flurry_ugi_bot" } },
36+
},
37+
poe: {
38+
accountId: "poe",
39+
configured: true,
40+
probe: { ok: true, elapsedMs: 188, bot: { username: "poe_ugi_bot" } },
41+
},
42+
},
43+
},
44+
},
45+
channelOrder: ["telegram"],
46+
channelLabels: { telegram: "Telegram" },
47+
});
48+
49+
expect(formatHealthChannelLines(summary, { accountMode: "all" })).toStrictEqual([
50+
"Telegram: ok (@pinguini_ugi_bot:main:196ms, @flurry_ugi_bot:flurry:190ms, @poe_ugi_bot:poe:188ms)",
51+
]);
52+
});
53+
54+
it("formats statusState without inferring from linked", () => {
55+
const summary = createHealthSummary({
56+
channels: {
57+
whatsapp: {
58+
accountId: "default",
59+
statusState: "unstable",
60+
configured: true,
61+
},
62+
},
63+
channelOrder: ["whatsapp"],
64+
channelLabels: { whatsapp: "WhatsApp" },
65+
});
66+
67+
expect(formatHealthChannelLines(summary)).toStrictEqual(["WhatsApp: auth stabilizing"]);
68+
});
69+
70+
it.each([
71+
[
72+
"fresh probe failure over passive healthy state",
73+
{ healthState: "healthy", probe: { ok: false, error: "sync rejected" } },
74+
"failed (unknown) - sync rejected",
75+
],
76+
[
77+
"fresh probe success over passive healthy state",
78+
{ healthState: "healthy", probe: { ok: true, elapsedMs: 12 } },
79+
"ok (12ms)",
80+
],
81+
[
82+
"blocked lifecycle over a failed probe",
83+
{ linked: true, healthState: "blocked", probe: { ok: false, error: "sync rejected" } },
84+
"blocked",
85+
],
86+
[
87+
"unknown degraded lifecycle over a successful probe",
88+
{ healthState: "degraded", probe: { ok: true, elapsedMs: 12 } },
89+
"degraded",
90+
],
91+
[
92+
"unstable authentication over passive healthy state and a failed probe",
93+
{
94+
healthState: "healthy",
95+
statusState: "unstable",
96+
probe: { ok: false, error: "sync rejected" },
97+
},
98+
"auth stabilizing",
99+
],
100+
[
101+
"disabled status over stale passive healthy state",
102+
{ healthState: "healthy", statusState: "disabled" },
103+
"disabled",
104+
],
105+
[
106+
"unconfigured account over stale lifecycle and authentication state",
107+
{ configured: false, healthState: "blocked", statusState: "unstable" },
108+
"not configured",
109+
],
110+
[
111+
"fresh probe failure over configured status",
112+
{ statusState: "configured", probe: { ok: false, error: "credentials rejected" } },
113+
"failed (unknown) - credentials rejected",
114+
],
115+
[
116+
"fresh probe failure over affirmative linked state",
117+
{ linked: true, probe: { ok: false, error: "session rejected" } },
118+
"failed (unknown) - session rejected",
119+
],
120+
[
121+
"negative linked state over a failed probe",
122+
{ linked: false, probe: { ok: false, error: "session rejected" } },
123+
"not linked",
124+
],
125+
["passive healthy state without a probe", { healthState: "healthy" }, "healthy"],
126+
])("formats %s", (_name, account, expected) => {
127+
const summary = createHealthSummary({
128+
channels: {
129+
test: {
130+
accountId: "default",
131+
configured: true,
132+
...account,
133+
},
134+
},
135+
channelOrder: ["test"],
136+
channelLabels: { test: "Test" },
137+
});
138+
139+
expect(formatHealthChannelLines(summary)).toStrictEqual([`Test: ${expected}`]);
140+
});
141+
142+
it("surfaces a failed sibling probe over the selected account's passive healthy state", () => {
143+
const summary = createHealthSummary({
144+
channels: {
145+
matrix: {
146+
accountId: "main",
147+
configured: true,
148+
healthState: "healthy",
149+
probe: { ok: true, elapsedMs: 12 },
150+
accounts: {
151+
main: {
152+
accountId: "main",
153+
configured: true,
154+
healthState: "healthy",
155+
probe: { ok: true, elapsedMs: 12 },
156+
},
157+
alerts: {
158+
accountId: "alerts",
159+
configured: true,
160+
healthState: "healthy",
161+
probe: { ok: false, error: "sync rejected" },
162+
},
163+
},
164+
},
165+
},
166+
channelOrder: ["matrix"],
167+
channelLabels: { matrix: "Matrix" },
168+
});
169+
170+
expect(formatHealthChannelLines(summary, { accountMode: "all" })).toStrictEqual([
171+
"Matrix: failed (unknown) - sync rejected",
172+
]);
173+
});
174+
175+
it("formats iMessage probe failures as failed health lines", () => {
176+
const summary = createHealthSummary({
177+
channels: {
178+
imessage: {
179+
accountId: "default",
180+
configured: true,
181+
probe: {
182+
ok: false,
183+
error:
184+
"imsg cannot access ~/Library/Messages/chat.db. Grant Full Disk Access to the Gateway/launcher process and restart Gateway.",
185+
},
186+
},
187+
},
188+
channelOrder: ["imessage"],
189+
channelLabels: { imessage: "iMessage" },
190+
});
191+
192+
expect(formatHealthChannelLines(summary)).toContain(
193+
"iMessage: failed (unknown) - imsg cannot access ~/Library/Messages/chat.db. Grant Full Disk Access to the Gateway/launcher process and restart Gateway.",
194+
);
195+
});
196+
});

‎src/commands/health-format.ts‎

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -186,40 +186,32 @@ export const formatHealthChannelLines = (
186186
: [];
187187
const statusState =
188188
typeof selectedSummary.statusState === "string" ? selectedSummary.statusState : null;
189-
const healthState = selectedSummary.healthState;
190-
if (typeof healthState === "string" && healthState) {
191-
lines.push(`${label}: ${healthState}`);
192-
continue;
193-
}
194-
if (statusState) {
195-
if (statusState === "linked") {
196-
const authAgeMs =
197-
typeof selectedSummary.authAgeMs === "number" ? selectedSummary.authAgeMs : null;
198-
const authLabel = authAgeMs != null ? ` (auth age ${Math.round(authAgeMs / 60000)}m)` : "";
199-
lines.push(`${label}: ${formatChannelStatusState(statusState)}${authLabel}`);
200-
} else {
201-
lines.push(`${label}: ${formatChannelStatusState(statusState)}`);
202-
}
203-
continue;
204-
}
205-
189+
const healthState =
190+
typeof selectedSummary.healthState === "string" && selectedSummary.healthState
191+
? selectedSummary.healthState
192+
: null;
206193
const linked = typeof selectedSummary.linked === "boolean" ? selectedSummary.linked : null;
207-
if (linked !== null) {
208-
if (linked) {
209-
const authAgeMs =
210-
typeof selectedSummary.authAgeMs === "number" ? selectedSummary.authAgeMs : null;
211-
const authLabel = authAgeMs != null ? ` (auth age ${Math.round(authAgeMs / 60000)}m)` : "";
212-
lines.push(`${label}: linked${authLabel}`);
213-
} else {
214-
lines.push(`${label}: not linked`);
215-
}
216-
continue;
217-
}
218-
219194
const configured =
220195
typeof selectedSummary.configured === "boolean" ? selectedSummary.configured : null;
221-
if (configured === false) {
222-
lines.push(`${label}: not configured`);
196+
const inactiveState =
197+
statusState === "disabled" || statusState === "unconfigured"
198+
? formatChannelStatusState(statusState)
199+
: configured === false
200+
? "not configured"
201+
: null;
202+
// Explicit inactive/degraded facts outrank probes; passive success waits until after them.
203+
// Otherwise a live probe can be hidden behind stale "healthy", "linked", or "configured".
204+
const preProbeState = inactiveState
205+
? inactiveState
206+
: healthState && healthState !== "healthy"
207+
? healthState
208+
: statusState && statusState !== "linked" && statusState !== "configured"
209+
? formatChannelStatusState(statusState)
210+
: linked === false
211+
? "not linked"
212+
: null;
213+
if (preProbeState) {
214+
lines.push(`${label}: ${preProbeState}`);
223215
continue;
224216
}
225217

@@ -251,11 +243,19 @@ export const formatHealthChannelLines = (
251243
continue;
252244
}
253245

254-
if (configured === true) {
255-
lines.push(`${label}: configured`);
256-
continue;
257-
}
258-
lines.push(`${label}: unknown`);
246+
const authAgeMs =
247+
typeof selectedSummary.authAgeMs === "number" ? selectedSummary.authAgeMs : null;
248+
const authLabel = authAgeMs != null ? ` (auth age ${Math.round(authAgeMs / 60000)}m)` : "";
249+
const passiveState = healthState
250+
? healthState
251+
: statusState
252+
? `${formatChannelStatusState(statusState)}${statusState === "linked" ? authLabel : ""}`
253+
: linked === true
254+
? `linked${authLabel}`
255+
: configured === true
256+
? "configured"
257+
: "unknown";
258+
lines.push(`${label}: ${passiveState}`);
259259
}
260260
return lines;
261261
};

‎src/commands/health.test.ts‎

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
formatConfigReloadHealthLine,
1313
formatContextEngineHealthLine,
1414
formatDeliveryQueueHealthLine,
15-
formatHealthChannelLines,
1615
healthCommand,
1716
} from "./health.js";
1817

@@ -505,100 +504,6 @@ describe("healthCommand", () => {
505504
]);
506505
expect(runtime.error).not.toHaveBeenCalled();
507506
});
508-
509-
it("formats per-account probe timings", () => {
510-
const summary = createHealthSummary({
511-
channels: {
512-
telegram: {
513-
accountId: "main",
514-
configured: true,
515-
probe: { ok: true, elapsedMs: 196, bot: { username: "pinguini_ugi_bot" } },
516-
accounts: {
517-
main: {
518-
accountId: "main",
519-
configured: true,
520-
probe: { ok: true, elapsedMs: 196, bot: { username: "pinguini_ugi_bot" } },
521-
},
522-
flurry: {
523-
accountId: "flurry",
524-
configured: true,
525-
probe: { ok: true, elapsedMs: 190, bot: { username: "flurry_ugi_bot" } },
526-
},
527-
poe: {
528-
accountId: "poe",
529-
configured: true,
530-
probe: { ok: true, elapsedMs: 188, bot: { username: "poe_ugi_bot" } },
531-
},
532-
},
533-
},
534-
},
535-
channelOrder: ["telegram"],
536-
channelLabels: { telegram: "Telegram" },
537-
});
538-
539-
const lines = formatHealthChannelLines(summary, { accountMode: "all" });
540-
expect(lines).toStrictEqual([
541-
"Telegram: ok (@pinguini_ugi_bot:main:196ms, @flurry_ugi_bot:flurry:190ms, @poe_ugi_bot:poe:188ms)",
542-
]);
543-
});
544-
545-
it("formats statusState without inferring from linked", () => {
546-
const summary = createHealthSummary({
547-
channels: {
548-
whatsapp: {
549-
accountId: "default",
550-
statusState: "unstable",
551-
configured: true,
552-
},
553-
},
554-
channelOrder: ["whatsapp"],
555-
channelLabels: { whatsapp: "WhatsApp" },
556-
});
557-
558-
const lines = formatHealthChannelLines(summary, { accountMode: "default" });
559-
expect(lines).toStrictEqual(["WhatsApp: auth stabilizing"]);
560-
});
561-
562-
it("formats derived channel health before linked state", () => {
563-
const summary = createHealthSummary({
564-
channels: {
565-
slack: {
566-
accountId: "default",
567-
configured: true,
568-
linked: true,
569-
lifecycle: "blocked",
570-
healthState: "blocked",
571-
},
572-
},
573-
channelOrder: ["slack"],
574-
channelLabels: { slack: "Slack" },
575-
});
576-
577-
expect(formatHealthChannelLines(summary)).toStrictEqual(["Slack: blocked"]);
578-
});
579-
580-
it("formats iMessage probe failures as failed health lines", () => {
581-
const summary = createHealthSummary({
582-
channels: {
583-
imessage: {
584-
accountId: "default",
585-
configured: true,
586-
probe: {
587-
ok: false,
588-
error:
589-
"imsg cannot access ~/Library/Messages/chat.db. Grant Full Disk Access to the Gateway/launcher process and restart Gateway.",
590-
},
591-
},
592-
},
593-
channelOrder: ["imessage"],
594-
channelLabels: { imessage: "iMessage" },
595-
});
596-
597-
const lines = formatHealthChannelLines(summary, { accountMode: "default" });
598-
expect(lines).toContain(
599-
"iMessage: failed (unknown) - imsg cannot access ~/Library/Messages/chat.db. Grant Full Disk Access to the Gateway/launcher process and restart Gateway.",
600-
);
601-
});
602507
});
603508

604509
describe("formatContextEngineHealthLine", () => {

0 commit comments

Comments
 (0)