Skip to content

Commit 9dc4080

Browse files
committed
fix(channels): preserve authored health state
1 parent a1b2629 commit 9dc4080

10 files changed

Lines changed: 81 additions & 18 deletions

File tree

‎docs/.generated/plugin-sdk-api-baseline.sha256‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bba5540be7cf9613a163663decdb2affe2af9bbd3ad7914989ab186f9c2abec1 module/channel
4545
0ceb4378709eb2d92a62a275f87fa04e18f77df8f942a9a0acef81019ebc1e24 module/channel-secret-runtime
4646
7c90157a95bc0523fc66b1f78ce140f7ec7dbf809dfa3a480244efc01f972754 module/channel-send-result
4747
fb123c1b557ed2527e335f13c3d6de41ab0c3305151001cf2b1075d1da8034c5 module/channel-setup
48-
139c347054962d583f2fc14071c37f49f5853f1a6f164b3b8820ac1c2facee19 module/channel-status
48+
aeac9bb8127faf636dde79772a577d9cb003660ef3d1df03ddf606101020c402 module/channel-status
4949
d904e33114f056022ef9fec184edcfe37c30b539b268e28b0ff485f0a02b9c59 module/channel-streaming
5050
14f0103adb14627b662fbe9fdd5ed08596ed702a250853e8beb8bb8dd1361588 module/cli-argv
5151
c89ec1b194b76f67a6f4dd108dccf460da6065646cba31374c8aa748f23a39e4 module/collection-runtime
@@ -63,7 +63,7 @@ c1ea9510dfda047609a99d5d2cd1f1560f5d469a36e6b695766213d695c25b0f module/convers
6363
f70c93d28053ca2e8353e45e6515ce7acef188097c6117d1545965d0699c8004 module/device-bootstrap
6464
6215d3af5923bf5a616d73062534968b69f448e3e30adc64ae9caebdd1a46d71 module/diagnostic-runtime
6565
ea81ef06956c1bc0853fa00afbbc2b5a4019116aaf8a436e1b27d06f7a2c9e88 module/directory-runtime
66-
a99d65194c814b022bf7b93c70a3fb1b7b88854458956b197a280691322591e1 module/discord
66+
e8adcff47c1b677cd2c01a2130fe4d226ac30ab3c88a3ceb4df5a8660316c4a9 module/discord
6767
f65408d85477bb362ebe6ed9148c1bb6b9eb7839733e5e3119f4ff8f1cfd0567 module/error-runtime
6868
b013053a61e7d9be3d0c683c02baf57fa7e4393ec54e0df6a46ab0f2fe2348fd module/extension-shared
6969
ceacad83db01c66e7be6aa21a291597020f13f737b697690eae7d47098e6499a module/gateway-method-runtime
@@ -132,7 +132,7 @@ ae469f32799380e6b045abaefefee6eb3f00d714ffbf36b6eeef5025dc529472 module/speech-
132132
9e521fe9073dfd1a6a6855f909fa6befe8613e18403f0a65faaba973a8b630c1 module/ssrf-policy
133133
f85d5be0f635de6a77bbd9224a37c373998e098a8dcf2048ddc2c9ac735b47ef module/ssrf-runtime
134134
ff35f9f74d35d37a2eb6126b57f3dc5a4d580b6222fe3a34c9368779ad32eab7 module/state-paths
135-
27194174f2742bd688100c9ea7df119fc58c0ff24ecbb281360497bc2eef13e5 module/status-helpers
135+
44cc071d9ba2baa26d6f33ba9407867fb07c33384f49d3e3d5210232022c1b5f module/status-helpers
136136
537047854c21ad20ea0572f8019503cbda3bbafece8194a28c30b0639bde2fde module/string-coerce-runtime
137137
1b5b9a4532db991fce807ba736b550de27aa5f9de2e1f508a8f4f09cae76ea29 module/telegram-account
138138
110944726884fca94f38c9c329b5950629438b9a719f4782c4beeade8bd67746 module/temp-path

‎extensions/whatsapp/src/status-issues.test.ts‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Whatsapp tests cover status issues plugin behavior.
2+
import { buildRuntimeAccountStatusSnapshot } from "openclaw/plugin-sdk/status-helpers";
23
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
34
import { collectWhatsAppStatusIssues } from "./status-issues.js";
45

@@ -32,26 +33,52 @@ describe("collectWhatsAppStatusIssues", () => {
3233
]);
3334
});
3435

35-
it("reports revoked cached credentials as logged out with relink guidance", () => {
36-
const issues = collectWhatsAppStatusIssues([
37-
{
38-
accountId: "default",
39-
enabled: true,
36+
it.each([
37+
{
38+
name: "logged-out",
39+
runtime: {
40+
running: false,
41+
connected: false,
4042
linked: false,
4143
healthState: "logged-out",
4244
lastError: "status=401",
4345
},
44-
]);
45-
46-
expect(issues).toEqual([
47-
{
46+
expected: {
4847
channel: "whatsapp",
4948
accountId: "default",
5049
kind: "auth",
5150
message: "Session logged out: status=401",
5251
fix: "Run: openclaw channels login (scan QR on the gateway host).",
5352
},
53+
},
54+
{
55+
name: "conflict",
56+
runtime: {
57+
running: false,
58+
connected: false,
59+
linked: true,
60+
reconnectAttempts: 2,
61+
healthState: "conflict",
62+
lastError: "status=440",
63+
},
64+
expected: {
65+
channel: "whatsapp",
66+
accountId: "default",
67+
kind: "runtime",
68+
message: "Linked but session conflict (reconnectAttempts=2): status=440",
69+
fix: "Run: openclaw doctor (or restart the gateway). If it persists, relink via channels login and check logs.",
70+
},
71+
},
72+
])("preserves projected $name guidance", ({ runtime, expected }) => {
73+
const issues = collectWhatsAppStatusIssues([
74+
{
75+
accountId: "default",
76+
enabled: true,
77+
...buildRuntimeAccountStatusSnapshot({ runtime }),
78+
},
5479
]);
80+
81+
expect(issues).toEqual([expected]);
5582
});
5683

5784
it("reports auth reads that are still stabilizing", () => {

‎src/channels/account-snapshot-fields.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,13 @@ describe("projectSafeChannelAccountSnapshotFields", () => {
159159
expect(withoutFlag).not.toHaveProperty("terminalDisconnect");
160160
});
161161

162-
it("projects recorded lifecycle without forwarding channel-authored healthState", () => {
162+
it("projects recorded lifecycle alongside channel-authored healthState", () => {
163163
expect(
164164
projectSafeChannelAccountSnapshotFields({
165165
lifecycle: "blocked",
166166
healthState: "degraded",
167167
}),
168-
).toEqual({ lifecycle: "blocked" });
168+
).toEqual({ healthState: "degraded", lifecycle: "blocked" });
169169
expect(projectSafeChannelAccountSnapshotFields({ lifecycle: "unknown" })).toEqual({});
170170
});
171171

‎src/channels/account-snapshot-fields.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ export function projectSafeChannelAccountSnapshotFields(
250250
"lastTransportActivityAt",
251251
asFiniteNumber(record.lastTransportActivityAt),
252252
);
253-
setSnapshotField(snapshot, "statusState", normalizeOptionalString(record.statusState));
253+
for (const key of ["statusState", "healthState"] as const) {
254+
setSnapshotField(snapshot, key, normalizeOptionalString(record[key]));
255+
}
254256
const lifecycle = record.lifecycle;
255257
if (
256258
lifecycle === "starting" ||

‎src/commands/health.snapshot.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,31 +695,41 @@ describe("collectGatewayHealthSnapshot", () => {
695695
channels: {
696696
telegram: {
697697
accountId: "default",
698+
running: true,
698699
connected: true,
699700
lastConnectedAt: 123,
701+
healthState: "reconnecting",
700702
},
701703
},
702704
channelAccounts: {},
703705
},
704706
});
705707
const telegram = snap.channels.telegram as {
708+
running?: boolean;
706709
connected?: boolean;
707710
lastConnectedAt?: number;
711+
healthState?: string;
708712
probe?: { ok?: boolean; bot?: { username?: string } };
709713
accounts?: Record<
710714
string,
711715
{
716+
running?: boolean;
712717
connected?: boolean;
713718
lastConnectedAt?: number;
719+
healthState?: string;
714720
probe?: { ok?: boolean; bot?: { username?: string } };
715721
}
716722
>;
717723
};
718724

725+
expect(telegram.running).toBe(true);
719726
expect(telegram.connected).toBe(true);
720727
expect(telegram.lastConnectedAt).toBe(123);
728+
expect(telegram.healthState).toBe("reconnecting");
721729
expect(telegram.probe?.bot?.username).toBe("runtime_bot");
730+
expect(telegram.accounts?.default?.running).toBe(true);
722731
expect(telegram.accounts?.default?.connected).toBe(true);
732+
expect(telegram.accounts?.default?.healthState).toBe("reconnecting");
723733
expect(telegram.accounts?.default?.probe?.ok).toBe(true);
724734
});
725735

‎src/gateway/health/collector.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,9 @@ export async function collectGatewayHealthSnapshot(params: {
336336
staleEventThresholdMs: DEFAULT_CHANNEL_STALE_EVENT_THRESHOLD_MS,
337337
channelConnectGraceMs: DEFAULT_CHANNEL_CONNECT_GRACE_MS,
338338
});
339-
snapshot.healthState = health.healthy ? undefined : health.reason;
339+
if (!health.healthy) {
340+
snapshot.healthState = health.reason;
341+
}
340342

341343
const summary = plugin.status?.buildChannelSummary
342344
? await plugin.status.buildChannelSummary({
@@ -352,7 +354,6 @@ export async function collectGatewayHealthSnapshot(params: {
352354
? ({ ...snapshot, ...summary } as ChannelAccountHealthSummary)
353355
: ({ ...snapshot, accountId, configured } satisfies ChannelAccountHealthSummary),
354356
);
355-
record.healthState = health.healthy ? undefined : health.reason;
356357
if (record.configured === undefined) {
357358
record.configured = configured;
358359
}

‎src/gateway/server-methods/channels.status.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ describe("channelsHandlers channels.status", () => {
463463
configured: true,
464464
running: true,
465465
connected: true,
466+
healthState: "stale",
466467
lastStartAt: now - 60 * 60_000,
467468
lastTransportActivityAt: now - 40 * 60_000,
468469
});
@@ -502,6 +503,22 @@ describe("channelsHandlers channels.status", () => {
502503
expect(firstChannelAccount(payload, "whatsapp").healthState).toBe("stale-socket");
503504
});
504505

506+
it("preserves channel-authored health state when shared health is healthy", async () => {
507+
mocks.applyPluginAutoEnable.mockReturnValue({ config: { autoEnabled: true }, changes: [] });
508+
mocks.buildChannelAccountSnapshot.mockResolvedValue({
509+
accountId: "default",
510+
enabled: true,
511+
configured: true,
512+
running: true,
513+
connected: true,
514+
healthState: "reconnecting",
515+
});
516+
517+
const payload = await runChannelsStatus({ probe: false, timeoutMs: 2000 });
518+
519+
expect(firstChannelAccount(payload, "whatsapp").healthState).toBe("reconnecting");
520+
});
521+
505522
it("derives blocked health from recorded lifecycle", async () => {
506523
mocks.applyPluginAutoEnable.mockReturnValue({ config: { autoEnabled: true }, changes: [] });
507524
mocks.buildChannelAccountSnapshot.mockResolvedValue({

‎src/gateway/server-methods/channels.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,9 @@ export const channelsHandlers: GatewayRequestHandlers = {
481481
staleEventThresholdMs: DEFAULT_CHANNEL_STALE_EVENT_THRESHOLD_MS,
482482
channelConnectGraceMs: DEFAULT_CHANNEL_CONNECT_GRACE_MS,
483483
});
484-
snapshot.healthState = health.healthy ? undefined : health.reason;
484+
if (!health.healthy) {
485+
snapshot.healthState = health.reason;
486+
}
485487
return { accountId, account, snapshot };
486488
};
487489

‎src/plugin-sdk/status-helpers.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ describe("buildRuntimeAccountStatusSnapshot", () => {
411411
lastDisconnect: { at: 12, error: "boom" },
412412
lastEventAt: 13,
413413
lastTransportActivityAt: 14,
414+
healthState: "reconnecting",
414415
lifecycle: "recovering" as const,
415416
ingressUnavailable: true as const,
416417
busy: true,
@@ -431,6 +432,7 @@ describe("buildRuntimeAccountStatusSnapshot", () => {
431432
lastDisconnect: { at: 12, error: "boom" },
432433
lastEventAt: 13,
433434
lastTransportActivityAt: 14,
435+
healthState: "reconnecting",
434436
lifecycle: "recovering",
435437
ingressUnavailable: true,
436438
busy: true,

‎src/plugin-sdk/status-helpers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type RuntimeLifecycleSnapshot = {
4242
| null;
4343
lastEventAt?: number | null;
4444
lastTransportActivityAt?: number | null;
45+
healthState?: string | null;
4546
lifecycle?: ChannelAccountSnapshot["lifecycle"] | null;
4647
ingressUnavailable?: true | null;
4748
terminalDisconnect?: boolean | null;
@@ -402,6 +403,7 @@ export function buildRuntimeAccountStatusSnapshot<TExtra extends StatusSnapshotE
402403
...(typeof runtime?.lastTransportActivityAt === "number"
403404
? { lastTransportActivityAt: runtime.lastTransportActivityAt }
404405
: {}),
406+
...(typeof runtime?.healthState === "string" ? { healthState: runtime.healthState } : {}),
405407
...(runtime?.lifecycle ? { lifecycle: runtime.lifecycle } : {}),
406408
// Absence means unknown; only a recorded ingress failure crosses the projection.
407409
...(runtime?.ingressUnavailable === true ? { ingressUnavailable: true as const } : {}),

0 commit comments

Comments
 (0)