refactor: centralize account bindings + health probes
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { resolveDefaultAgentId } from "../agents/agent-scope.js";
|
||||
import { resolveChannelDefaultAccountId } from "../channels/plugins/helpers.js";
|
||||
import { getChannelPlugin, listChannelPlugins } from "../channels/plugins/index.js";
|
||||
import type { ChannelAccountSnapshot } from "../channels/plugins/types.js";
|
||||
@@ -7,11 +8,20 @@ import { loadSessionStore, resolveStorePath } from "../config/sessions.js";
|
||||
import { buildGatewayConnectionDetails, callGateway } from "../gateway/call.js";
|
||||
import { info } from "../globals.js";
|
||||
import { formatErrorMessage } from "../infra/errors.js";
|
||||
import {
|
||||
type HeartbeatSummary,
|
||||
resolveHeartbeatSummaryForAgent,
|
||||
} from "../infra/heartbeat-runner.js";
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
import {
|
||||
buildChannelAccountBindings,
|
||||
resolvePreferredAccountId,
|
||||
} from "../routing/bindings.js";
|
||||
import { normalizeAgentId } from "../routing/session-key.js";
|
||||
import { theme } from "../terminal/theme.js";
|
||||
import { resolveHeartbeatSeconds } from "../web/reconnect.js";
|
||||
|
||||
export type ChannelHealthSummary = {
|
||||
export type ChannelAccountHealthSummary = {
|
||||
accountId: string;
|
||||
configured?: boolean;
|
||||
linked?: boolean;
|
||||
authAgeMs?: number | null;
|
||||
@@ -20,6 +30,20 @@ export type ChannelHealthSummary = {
|
||||
[key: string]: unknown;
|
||||
};
|
||||
|
||||
export type ChannelHealthSummary = ChannelAccountHealthSummary & {
|
||||
accounts?: Record<string, ChannelAccountHealthSummary>;
|
||||
};
|
||||
|
||||
export type AgentHeartbeatSummary = HeartbeatSummary;
|
||||
|
||||
export type AgentHealthSummary = {
|
||||
agentId: string;
|
||||
name?: string;
|
||||
isDefault: boolean;
|
||||
heartbeat: AgentHeartbeatSummary;
|
||||
sessions: HealthSummary["sessions"];
|
||||
};
|
||||
|
||||
export type HealthSummary = {
|
||||
/**
|
||||
* Convenience top-level flag for UIs (e.g. WebChat) that only need a binary
|
||||
@@ -32,7 +56,10 @@ export type HealthSummary = {
|
||||
channels: Record<string, ChannelHealthSummary>;
|
||||
channelOrder: string[];
|
||||
channelLabels: Record<string, string>;
|
||||
/** Legacy: default agent heartbeat seconds (rounded). */
|
||||
heartbeatSeconds: number;
|
||||
defaultAgentId: string;
|
||||
agents: AgentHealthSummary[];
|
||||
sessions: {
|
||||
path: string;
|
||||
count: number;
|
||||
@@ -46,6 +73,82 @@ export type HealthSummary = {
|
||||
|
||||
const DEFAULT_TIMEOUT_MS = 10_000;
|
||||
|
||||
const debugHealth = (...args: unknown[]) => {
|
||||
if (process.env.CLAWDBOT_DEBUG_HEALTH === "1") {
|
||||
console.warn("[health:debug]", ...args);
|
||||
}
|
||||
};
|
||||
|
||||
const formatDurationParts = (ms: number): string => {
|
||||
if (!Number.isFinite(ms)) return "unknown";
|
||||
if (ms < 1000) return `${Math.max(0, Math.round(ms))}ms`;
|
||||
const units: Array<{ label: string; size: number }> = [
|
||||
{ label: "w", size: 7 * 24 * 60 * 60 * 1000 },
|
||||
{ label: "d", size: 24 * 60 * 60 * 1000 },
|
||||
{ label: "h", size: 60 * 60 * 1000 },
|
||||
{ label: "m", size: 60 * 1000 },
|
||||
{ label: "s", size: 1000 },
|
||||
];
|
||||
let remaining = Math.max(0, Math.floor(ms));
|
||||
const parts: string[] = [];
|
||||
for (const unit of units) {
|
||||
const value = Math.floor(remaining / unit.size);
|
||||
if (value > 0) {
|
||||
parts.push(`${value}${unit.label}`);
|
||||
remaining -= value * unit.size;
|
||||
}
|
||||
}
|
||||
if (parts.length === 0) return "0s";
|
||||
return parts.join(" ");
|
||||
};
|
||||
|
||||
const resolveHeartbeatSummary = (cfg: ReturnType<typeof loadConfig>, agentId: string) =>
|
||||
resolveHeartbeatSummaryForAgent(cfg, agentId);
|
||||
|
||||
const resolveAgentOrder = (cfg: ReturnType<typeof loadConfig>) => {
|
||||
const defaultAgentId = resolveDefaultAgentId(cfg);
|
||||
const entries = Array.isArray(cfg.agents?.list) ? cfg.agents.list : [];
|
||||
const seen = new Set<string>();
|
||||
const ordered: Array<{ id: string; name?: string }> = [];
|
||||
|
||||
for (const entry of entries) {
|
||||
if (!entry || typeof entry !== "object") continue;
|
||||
if (typeof entry.id !== "string" || !entry.id.trim()) continue;
|
||||
const id = normalizeAgentId(entry.id);
|
||||
if (!id || seen.has(id)) continue;
|
||||
seen.add(id);
|
||||
ordered.push({ id, name: typeof entry.name === "string" ? entry.name : undefined });
|
||||
}
|
||||
|
||||
if (!seen.has(defaultAgentId)) {
|
||||
ordered.unshift({ id: defaultAgentId });
|
||||
}
|
||||
|
||||
if (ordered.length === 0) {
|
||||
ordered.push({ id: defaultAgentId });
|
||||
}
|
||||
|
||||
return { defaultAgentId, ordered };
|
||||
};
|
||||
|
||||
const buildSessionSummary = (storePath: string) => {
|
||||
const store = loadSessionStore(storePath);
|
||||
const sessions = Object.entries(store)
|
||||
.filter(([key]) => key !== "global" && key !== "unknown")
|
||||
.map(([key, entry]) => ({ key, updatedAt: entry?.updatedAt ?? 0 }))
|
||||
.sort((a, b) => b.updatedAt - a.updatedAt);
|
||||
const recent = sessions.slice(0, 5).map((s) => ({
|
||||
key: s.key,
|
||||
updatedAt: s.updatedAt || null,
|
||||
age: s.updatedAt ? Date.now() - s.updatedAt : null,
|
||||
}));
|
||||
return {
|
||||
path: storePath,
|
||||
count: sessions.length,
|
||||
recent,
|
||||
} satisfies HealthSummary["sessions"];
|
||||
};
|
||||
|
||||
const isAccountEnabled = (account: unknown): boolean => {
|
||||
if (!account || typeof account !== "object") return true;
|
||||
const enabled = (account as { enabled?: boolean }).enabled;
|
||||
@@ -55,7 +158,10 @@ const isAccountEnabled = (account: unknown): boolean => {
|
||||
const asRecord = (value: unknown): Record<string, unknown> | null =>
|
||||
value && typeof value === "object" ? (value as Record<string, unknown>) : null;
|
||||
|
||||
const formatProbeLine = (probe: unknown): string | null => {
|
||||
const formatProbeLine = (
|
||||
probe: unknown,
|
||||
opts: { botUsernames?: string[] } = {},
|
||||
): string | null => {
|
||||
const record = asRecord(probe);
|
||||
if (!record) return null;
|
||||
const ok = typeof record.ok === "boolean" ? record.ok : undefined;
|
||||
@@ -68,9 +174,17 @@ const formatProbeLine = (probe: unknown): string | null => {
|
||||
const webhook = asRecord(record.webhook);
|
||||
const webhookUrl = webhook && typeof webhook.url === "string" ? webhook.url : null;
|
||||
|
||||
const usernames = new Set<string>();
|
||||
if (botUsername) usernames.add(botUsername);
|
||||
for (const extra of opts.botUsernames ?? []) {
|
||||
if (extra) usernames.add(extra);
|
||||
}
|
||||
|
||||
if (ok) {
|
||||
let label = "ok";
|
||||
if (botUsername) label += ` (@${botUsername})`;
|
||||
if (usernames.size > 0) {
|
||||
label += ` (@${Array.from(usernames).join(", @")})`;
|
||||
}
|
||||
if (elapsedMs != null) label += ` (${elapsedMs}ms)`;
|
||||
if (webhookUrl) label += ` - webhook ${webhookUrl}`;
|
||||
return label;
|
||||
@@ -80,6 +194,29 @@ const formatProbeLine = (probe: unknown): string | null => {
|
||||
return label;
|
||||
};
|
||||
|
||||
const formatAccountProbeTiming = (summary: ChannelAccountHealthSummary): string | null => {
|
||||
const probe = asRecord(summary.probe);
|
||||
if (!probe) return null;
|
||||
const elapsedMs = typeof probe.elapsedMs === "number" ? Math.round(probe.elapsedMs) : null;
|
||||
const ok = typeof probe.ok === "boolean" ? probe.ok : null;
|
||||
if (elapsedMs == null && ok !== true) return null;
|
||||
|
||||
const accountId = summary.accountId || "default";
|
||||
const botRecord = asRecord(probe.bot);
|
||||
const botUsername = botRecord && typeof botRecord.username === "string" ? botRecord.username : null;
|
||||
const handle = botUsername ? `@${botUsername}` : accountId;
|
||||
const timing = elapsedMs != null ? `${elapsedMs}ms` : "ok";
|
||||
|
||||
return `${handle}:${accountId}:${timing}`;
|
||||
};
|
||||
|
||||
const isProbeFailure = (summary: ChannelAccountHealthSummary): boolean => {
|
||||
const probe = asRecord(summary.probe);
|
||||
if (!probe) return false;
|
||||
const ok = typeof probe.ok === "boolean" ? probe.ok : null;
|
||||
return ok === false;
|
||||
};
|
||||
|
||||
function styleHealthChannelLine(line: string): string {
|
||||
const colon = line.indexOf(":");
|
||||
if (colon === -1) return line;
|
||||
@@ -102,10 +239,17 @@ function styleHealthChannelLine(line: string): string {
|
||||
return line;
|
||||
}
|
||||
|
||||
export const formatHealthChannelLines = (summary: HealthSummary): string[] => {
|
||||
export const formatHealthChannelLines = (
|
||||
summary: HealthSummary,
|
||||
opts: {
|
||||
accountMode?: "default" | "all";
|
||||
accountIdsByChannel?: Record<string, string[] | undefined>;
|
||||
} = {},
|
||||
): string[] => {
|
||||
const channels = summary.channels ?? {};
|
||||
const channelOrder =
|
||||
summary.channelOrder?.length > 0 ? summary.channelOrder : Object.keys(channels);
|
||||
const accountMode = opts.accountMode ?? "default";
|
||||
|
||||
const lines: string[] = [];
|
||||
for (const channelId of channelOrder) {
|
||||
@@ -113,11 +257,36 @@ export const formatHealthChannelLines = (summary: HealthSummary): string[] => {
|
||||
if (!channelSummary) continue;
|
||||
const plugin = getChannelPlugin(channelId as never);
|
||||
const label = summary.channelLabels?.[channelId] ?? plugin?.meta.label ?? channelId;
|
||||
const linked = typeof channelSummary.linked === "boolean" ? channelSummary.linked : null;
|
||||
const accountSummaries = channelSummary.accounts ?? {};
|
||||
const accountIds = opts.accountIdsByChannel?.[channelId];
|
||||
const filteredSummaries =
|
||||
accountIds && accountIds.length > 0
|
||||
? accountIds
|
||||
.map((accountId) => accountSummaries[accountId])
|
||||
.filter((entry): entry is ChannelAccountHealthSummary => Boolean(entry))
|
||||
: undefined;
|
||||
const listSummaries =
|
||||
accountMode === "all"
|
||||
? Object.values(accountSummaries)
|
||||
: filteredSummaries ?? (channelSummary.accounts ? Object.values(accountSummaries) : []);
|
||||
const baseSummary =
|
||||
filteredSummaries && filteredSummaries.length > 0
|
||||
? filteredSummaries[0]
|
||||
: channelSummary;
|
||||
const botUsernames = listSummaries
|
||||
? listSummaries
|
||||
.map((account) => {
|
||||
const probeRecord = asRecord(account.probe);
|
||||
const bot = probeRecord ? asRecord(probeRecord.bot) : null;
|
||||
return bot && typeof bot.username === "string" ? bot.username : null;
|
||||
})
|
||||
.filter((value): value is string => Boolean(value))
|
||||
: [];
|
||||
const linked = typeof baseSummary.linked === "boolean" ? baseSummary.linked : null;
|
||||
if (linked !== null) {
|
||||
if (linked) {
|
||||
const authAgeMs =
|
||||
typeof channelSummary.authAgeMs === "number" ? channelSummary.authAgeMs : null;
|
||||
typeof baseSummary.authAgeMs === "number" ? baseSummary.authAgeMs : null;
|
||||
const authLabel = authAgeMs != null ? ` (auth age ${Math.round(authAgeMs / 60000)}m)` : "";
|
||||
lines.push(`${label}: linked${authLabel}`);
|
||||
} else {
|
||||
@@ -126,14 +295,33 @@ export const formatHealthChannelLines = (summary: HealthSummary): string[] => {
|
||||
continue;
|
||||
}
|
||||
|
||||
const configured =
|
||||
typeof channelSummary.configured === "boolean" ? channelSummary.configured : null;
|
||||
const configured = typeof baseSummary.configured === "boolean" ? baseSummary.configured : null;
|
||||
if (configured === false) {
|
||||
lines.push(`${label}: not configured`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const probeLine = formatProbeLine(channelSummary.probe);
|
||||
const accountTimings =
|
||||
accountMode === "all"
|
||||
? listSummaries
|
||||
.map((account) => formatAccountProbeTiming(account))
|
||||
.filter((value): value is string => Boolean(value))
|
||||
: [];
|
||||
const failedSummary = listSummaries.find((summary) => isProbeFailure(summary));
|
||||
if (failedSummary) {
|
||||
const failureLine = formatProbeLine(failedSummary.probe, { botUsernames });
|
||||
if (failureLine) {
|
||||
lines.push(`${label}: ${failureLine}`);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (accountTimings.length > 0) {
|
||||
lines.push(`${label}: ok (${accountTimings.join(", ")})`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const probeLine = formatProbeLine(baseSummary.probe, { botUsernames });
|
||||
if (probeLine) {
|
||||
lines.push(`${label}: ${probeLine}`);
|
||||
continue;
|
||||
@@ -154,18 +342,28 @@ export async function getHealthSnapshot(params?: {
|
||||
}): Promise<HealthSummary> {
|
||||
const timeoutMs = params?.timeoutMs;
|
||||
const cfg = loadConfig();
|
||||
const heartbeatSeconds = resolveHeartbeatSeconds(cfg, undefined);
|
||||
const storePath = resolveStorePath(cfg.session?.store);
|
||||
const store = loadSessionStore(storePath);
|
||||
const sessions = Object.entries(store)
|
||||
.filter(([key]) => key !== "global" && key !== "unknown")
|
||||
.map(([key, entry]) => ({ key, updatedAt: entry?.updatedAt ?? 0 }))
|
||||
.sort((a, b) => b.updatedAt - a.updatedAt);
|
||||
const recent = sessions.slice(0, 5).map((s) => ({
|
||||
key: s.key,
|
||||
updatedAt: s.updatedAt || null,
|
||||
age: s.updatedAt ? Date.now() - s.updatedAt : null,
|
||||
}));
|
||||
const { defaultAgentId, ordered } = resolveAgentOrder(cfg);
|
||||
const channelBindings = buildChannelAccountBindings(cfg);
|
||||
const sessionCache = new Map<string, HealthSummary["sessions"]>();
|
||||
const agents: AgentHealthSummary[] = ordered.map((entry) => {
|
||||
const storePath = resolveStorePath(cfg.session?.store, { agentId: entry.id });
|
||||
const sessions = sessionCache.get(storePath) ?? buildSessionSummary(storePath);
|
||||
sessionCache.set(storePath, sessions);
|
||||
return {
|
||||
agentId: entry.id,
|
||||
name: entry.name,
|
||||
isDefault: entry.id === defaultAgentId,
|
||||
heartbeat: resolveHeartbeatSummary(cfg, entry.id),
|
||||
sessions,
|
||||
} satisfies AgentHealthSummary;
|
||||
});
|
||||
const defaultAgent = agents.find((agent) => agent.isDefault) ?? agents[0];
|
||||
const heartbeatSeconds = defaultAgent?.heartbeat.everyMs
|
||||
? Math.round(defaultAgent.heartbeat.everyMs / 1000)
|
||||
: 0;
|
||||
const sessions =
|
||||
defaultAgent?.sessions ??
|
||||
buildSessionSummary(resolveStorePath(cfg.session?.store, { agentId: defaultAgentId }));
|
||||
|
||||
const start = Date.now();
|
||||
const cappedTimeout = Math.max(1000, timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
||||
@@ -182,59 +380,110 @@ export async function getHealthSnapshot(params?: {
|
||||
cfg,
|
||||
accountIds,
|
||||
});
|
||||
const account = plugin.config.resolveAccount(cfg, defaultAccountId);
|
||||
const enabled = plugin.config.isEnabled
|
||||
? plugin.config.isEnabled(account, cfg)
|
||||
: isAccountEnabled(account);
|
||||
const configured = plugin.config.isConfigured
|
||||
? await plugin.config.isConfigured(account, cfg)
|
||||
: true;
|
||||
const boundAccounts = channelBindings.get(plugin.id)?.get(defaultAgentId) ?? [];
|
||||
const preferredAccountId = resolvePreferredAccountId({
|
||||
accountIds,
|
||||
defaultAccountId,
|
||||
boundAccounts,
|
||||
});
|
||||
const boundAccountIdsAll = Array.from(
|
||||
new Set(Array.from(channelBindings.get(plugin.id)?.values() ?? []).flatMap((ids) => ids)),
|
||||
);
|
||||
const accountIdsToProbe = Array.from(
|
||||
new Set(
|
||||
[preferredAccountId, defaultAccountId, ...accountIds, ...boundAccountIdsAll].filter(
|
||||
(value) => value && value.trim(),
|
||||
),
|
||||
),
|
||||
);
|
||||
debugHealth("channel", {
|
||||
id: plugin.id,
|
||||
accountIds,
|
||||
defaultAccountId,
|
||||
boundAccounts,
|
||||
preferredAccountId,
|
||||
accountIdsToProbe,
|
||||
});
|
||||
const accountSummaries: Record<string, ChannelAccountHealthSummary> = {};
|
||||
|
||||
let probe: unknown;
|
||||
let lastProbeAt: number | null = null;
|
||||
if (enabled && configured && doProbe && plugin.status?.probeAccount) {
|
||||
try {
|
||||
probe = await plugin.status.probeAccount({
|
||||
account,
|
||||
timeoutMs: cappedTimeout,
|
||||
cfg,
|
||||
});
|
||||
lastProbeAt = Date.now();
|
||||
} catch (err) {
|
||||
probe = { ok: false, error: formatErrorMessage(err) };
|
||||
lastProbeAt = Date.now();
|
||||
for (const accountId of accountIdsToProbe) {
|
||||
const account = plugin.config.resolveAccount(cfg, accountId);
|
||||
const enabled = plugin.config.isEnabled
|
||||
? plugin.config.isEnabled(account, cfg)
|
||||
: isAccountEnabled(account);
|
||||
const configured = plugin.config.isConfigured
|
||||
? await plugin.config.isConfigured(account, cfg)
|
||||
: true;
|
||||
|
||||
let probe: unknown;
|
||||
let lastProbeAt: number | null = null;
|
||||
if (enabled && configured && doProbe && plugin.status?.probeAccount) {
|
||||
try {
|
||||
probe = await plugin.status.probeAccount({
|
||||
account,
|
||||
timeoutMs: cappedTimeout,
|
||||
cfg,
|
||||
});
|
||||
lastProbeAt = Date.now();
|
||||
} catch (err) {
|
||||
probe = { ok: false, error: formatErrorMessage(err) };
|
||||
lastProbeAt = Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
const probeRecord = probe && typeof probe === "object" ? (probe as Record<string, unknown>) : null;
|
||||
const bot =
|
||||
probeRecord && typeof probeRecord.bot === "object"
|
||||
? (probeRecord.bot as { username?: string | null })
|
||||
: null;
|
||||
if (bot?.username) {
|
||||
debugHealth("probe.bot", { channel: plugin.id, accountId, username: bot.username });
|
||||
}
|
||||
|
||||
const snapshot: ChannelAccountSnapshot = {
|
||||
accountId,
|
||||
enabled,
|
||||
configured,
|
||||
};
|
||||
if (probe !== undefined) snapshot.probe = probe;
|
||||
if (lastProbeAt) snapshot.lastProbeAt = lastProbeAt;
|
||||
|
||||
const summary = plugin.status?.buildChannelSummary
|
||||
? await plugin.status.buildChannelSummary({
|
||||
account,
|
||||
cfg,
|
||||
defaultAccountId: accountId,
|
||||
snapshot,
|
||||
})
|
||||
: undefined;
|
||||
const record =
|
||||
summary && typeof summary === "object"
|
||||
? (summary as ChannelAccountHealthSummary)
|
||||
: ({
|
||||
accountId,
|
||||
configured,
|
||||
probe,
|
||||
lastProbeAt,
|
||||
} satisfies ChannelAccountHealthSummary);
|
||||
if (record.configured === undefined) record.configured = configured;
|
||||
if (record.lastProbeAt === undefined && lastProbeAt) {
|
||||
record.lastProbeAt = lastProbeAt;
|
||||
}
|
||||
record.accountId = accountId;
|
||||
accountSummaries[accountId] = record;
|
||||
}
|
||||
|
||||
const snapshot: ChannelAccountSnapshot = {
|
||||
accountId: defaultAccountId,
|
||||
enabled,
|
||||
configured,
|
||||
};
|
||||
if (probe !== undefined) snapshot.probe = probe;
|
||||
if (lastProbeAt) snapshot.lastProbeAt = lastProbeAt;
|
||||
|
||||
const summary = plugin.status?.buildChannelSummary
|
||||
? await plugin.status.buildChannelSummary({
|
||||
account,
|
||||
cfg,
|
||||
defaultAccountId,
|
||||
snapshot,
|
||||
})
|
||||
: undefined;
|
||||
const record =
|
||||
summary && typeof summary === "object"
|
||||
? (summary as ChannelHealthSummary)
|
||||
: ({
|
||||
configured,
|
||||
probe,
|
||||
lastProbeAt,
|
||||
} satisfies ChannelHealthSummary);
|
||||
if (record.configured === undefined) record.configured = configured;
|
||||
if (record.lastProbeAt === undefined && lastProbeAt) {
|
||||
record.lastProbeAt = lastProbeAt;
|
||||
const defaultSummary =
|
||||
accountSummaries[preferredAccountId] ??
|
||||
accountSummaries[defaultAccountId] ??
|
||||
accountSummaries[accountIdsToProbe[0] ?? preferredAccountId];
|
||||
const fallbackSummary = defaultSummary ?? accountSummaries[Object.keys(accountSummaries)[0]];
|
||||
if (fallbackSummary) {
|
||||
channels[plugin.id] = {
|
||||
...fallbackSummary,
|
||||
accounts: accountSummaries,
|
||||
} satisfies ChannelHealthSummary;
|
||||
}
|
||||
channels[plugin.id] = record;
|
||||
}
|
||||
|
||||
const summary: HealthSummary = {
|
||||
@@ -245,10 +494,12 @@ export async function getHealthSnapshot(params?: {
|
||||
channelOrder,
|
||||
channelLabels,
|
||||
heartbeatSeconds,
|
||||
defaultAgentId,
|
||||
agents,
|
||||
sessions: {
|
||||
path: storePath,
|
||||
count: sessions.length,
|
||||
recent,
|
||||
path: sessions.path,
|
||||
count: sessions.count,
|
||||
recent: sessions.recent,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -269,6 +520,7 @@ export async function healthCommand(
|
||||
async () =>
|
||||
await callGateway<HealthSummary>({
|
||||
method: "health",
|
||||
params: opts.verbose ? { probe: true } : undefined,
|
||||
timeoutMs: opts.timeoutMs,
|
||||
}),
|
||||
);
|
||||
@@ -278,6 +530,7 @@ export async function healthCommand(
|
||||
if (opts.json) {
|
||||
runtime.log(JSON.stringify(summary, null, 2));
|
||||
} else {
|
||||
const debugEnabled = process.env.CLAWDBOT_DEBUG_HEALTH === "1";
|
||||
if (opts.verbose) {
|
||||
const details = buildGatewayConnectionDetails();
|
||||
runtime.log(info("Gateway connection:"));
|
||||
@@ -285,21 +538,133 @@ export async function healthCommand(
|
||||
runtime.log(` ${line}`);
|
||||
}
|
||||
}
|
||||
for (const line of formatHealthChannelLines(summary)) {
|
||||
const cfg = loadConfig();
|
||||
const localAgents = resolveAgentOrder(cfg);
|
||||
const defaultAgentId = summary.defaultAgentId ?? localAgents.defaultAgentId;
|
||||
const agents = Array.isArray(summary.agents) ? summary.agents : [];
|
||||
const fallbackAgents = localAgents.ordered.map((entry) => {
|
||||
const storePath = resolveStorePath(cfg.session?.store, { agentId: entry.id });
|
||||
return {
|
||||
agentId: entry.id,
|
||||
name: entry.name,
|
||||
isDefault: entry.id === localAgents.defaultAgentId,
|
||||
heartbeat: resolveHeartbeatSummary(cfg, entry.id),
|
||||
sessions: buildSessionSummary(storePath),
|
||||
} satisfies AgentHealthSummary;
|
||||
});
|
||||
const resolvedAgents = agents.length > 0 ? agents : fallbackAgents;
|
||||
const displayAgents = opts.verbose
|
||||
? resolvedAgents
|
||||
: resolvedAgents.filter((agent) => agent.agentId === defaultAgentId);
|
||||
const channelBindings = buildChannelAccountBindings(cfg);
|
||||
if (debugEnabled) {
|
||||
runtime.log(info("[debug] local channel accounts"));
|
||||
for (const plugin of listChannelPlugins()) {
|
||||
const accountIds = plugin.config.listAccountIds(cfg);
|
||||
const defaultAccountId = resolveChannelDefaultAccountId({
|
||||
plugin,
|
||||
cfg,
|
||||
accountIds,
|
||||
});
|
||||
runtime.log(
|
||||
` ${plugin.id}: accounts=${accountIds.join(", ") || "(none)"} default=${defaultAccountId}`,
|
||||
);
|
||||
for (const accountId of accountIds) {
|
||||
const account = plugin.config.resolveAccount(cfg, accountId);
|
||||
const record = asRecord(account);
|
||||
const tokenSource =
|
||||
record && typeof record.tokenSource === "string" ? record.tokenSource : undefined;
|
||||
const configured = plugin.config.isConfigured
|
||||
? await plugin.config.isConfigured(account, cfg)
|
||||
: true;
|
||||
runtime.log(
|
||||
` - ${accountId}: configured=${configured}${tokenSource ? ` tokenSource=${tokenSource}` : ""}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
runtime.log(info("[debug] bindings map"));
|
||||
for (const [channelId, byAgent] of channelBindings.entries()) {
|
||||
const entries = Array.from(byAgent.entries()).map(
|
||||
([agentId, ids]) => `${agentId}=[${ids.join(", ")}]`,
|
||||
);
|
||||
runtime.log(` ${channelId}: ${entries.join(" ")}`);
|
||||
}
|
||||
runtime.log(info("[debug] gateway channel probes"));
|
||||
for (const [channelId, channelSummary] of Object.entries(summary.channels ?? {})) {
|
||||
const accounts = channelSummary.accounts ?? {};
|
||||
const probes = Object.entries(accounts).map(([accountId, accountSummary]) => {
|
||||
const probe = asRecord(accountSummary.probe);
|
||||
const bot = probe ? asRecord(probe.bot) : null;
|
||||
const username = bot && typeof bot.username === "string" ? bot.username : null;
|
||||
return `${accountId}=${username ?? "(no bot)"}`;
|
||||
});
|
||||
runtime.log(` ${channelId}: ${probes.join(", ") || "(none)"}`);
|
||||
}
|
||||
}
|
||||
const channelAccountFallbacks = Object.fromEntries(
|
||||
listChannelPlugins().map((plugin) => {
|
||||
const accountIds = plugin.config.listAccountIds(cfg);
|
||||
const defaultAccountId = resolveChannelDefaultAccountId({
|
||||
plugin,
|
||||
cfg,
|
||||
accountIds,
|
||||
});
|
||||
const preferred = resolvePreferredAccountId({
|
||||
accountIds,
|
||||
defaultAccountId,
|
||||
boundAccounts: channelBindings.get(plugin.id)?.get(defaultAgentId) ?? [],
|
||||
});
|
||||
return [plugin.id, [preferred] as string[]] as const;
|
||||
}),
|
||||
);
|
||||
const accountIdsByChannel = (() => {
|
||||
const entries = displayAgents.length > 0 ? displayAgents : resolvedAgents;
|
||||
const byChannel: Record<string, string[]> = {};
|
||||
for (const [channelId, byAgent] of channelBindings.entries()) {
|
||||
const accountIds: string[] = [];
|
||||
for (const agent of entries) {
|
||||
const ids = byAgent.get(agent.agentId) ?? [];
|
||||
for (const id of ids) {
|
||||
if (!accountIds.includes(id)) accountIds.push(id);
|
||||
}
|
||||
}
|
||||
if (accountIds.length > 0) byChannel[channelId] = accountIds;
|
||||
}
|
||||
for (const [channelId, fallbackIds] of Object.entries(channelAccountFallbacks)) {
|
||||
if (!byChannel[channelId] || byChannel[channelId].length === 0) {
|
||||
byChannel[channelId] = fallbackIds;
|
||||
}
|
||||
}
|
||||
return byChannel;
|
||||
})();
|
||||
const channelLines = Object.keys(accountIdsByChannel).length > 0
|
||||
? formatHealthChannelLines(summary, {
|
||||
accountMode: opts.verbose ? "all" : "default",
|
||||
accountIdsByChannel,
|
||||
})
|
||||
: formatHealthChannelLines(summary, {
|
||||
accountMode: opts.verbose ? "all" : "default",
|
||||
});
|
||||
for (const line of channelLines) {
|
||||
runtime.log(styleHealthChannelLine(line));
|
||||
}
|
||||
const cfg = loadConfig();
|
||||
for (const plugin of listChannelPlugins()) {
|
||||
const channelSummary = summary.channels?.[plugin.id];
|
||||
if (!channelSummary || channelSummary.linked !== true) continue;
|
||||
if (!plugin.status?.logSelfId) continue;
|
||||
const boundAccounts = channelBindings.get(plugin.id)?.get(defaultAgentId) ?? [];
|
||||
const accountIds = plugin.config.listAccountIds(cfg);
|
||||
const defaultAccountId = resolveChannelDefaultAccountId({
|
||||
plugin,
|
||||
cfg,
|
||||
accountIds,
|
||||
});
|
||||
const account = plugin.config.resolveAccount(cfg, defaultAccountId);
|
||||
const accountId = resolvePreferredAccountId({
|
||||
accountIds,
|
||||
defaultAccountId,
|
||||
boundAccounts,
|
||||
});
|
||||
const account = plugin.config.resolveAccount(cfg, accountId);
|
||||
plugin.status.logSelfId({
|
||||
account,
|
||||
cfg,
|
||||
@@ -308,16 +673,45 @@ export async function healthCommand(
|
||||
});
|
||||
}
|
||||
|
||||
runtime.log(info(`Heartbeat interval: ${summary.heartbeatSeconds}s`));
|
||||
runtime.log(
|
||||
info(`Session store: ${summary.sessions.path} (${summary.sessions.count} entries)`),
|
||||
);
|
||||
if (summary.sessions.recent.length > 0) {
|
||||
runtime.log("Recent sessions:");
|
||||
for (const r of summary.sessions.recent) {
|
||||
if (resolvedAgents.length > 0) {
|
||||
const agentLabels = resolvedAgents.map((agent) =>
|
||||
agent.isDefault ? `${agent.agentId} (default)` : agent.agentId,
|
||||
);
|
||||
runtime.log(info(`Agents: ${agentLabels.join(", ")}`));
|
||||
}
|
||||
const heartbeatParts = displayAgents
|
||||
.map((agent) => {
|
||||
const everyMs = agent.heartbeat?.everyMs;
|
||||
const label = everyMs ? formatDurationParts(everyMs) : "disabled";
|
||||
return `${label} (${agent.agentId})`;
|
||||
})
|
||||
.filter(Boolean);
|
||||
if (heartbeatParts.length > 0) {
|
||||
runtime.log(info(`Heartbeat interval: ${heartbeatParts.join(", ")}`));
|
||||
}
|
||||
if (displayAgents.length === 0) {
|
||||
runtime.log(info(`Session store: ${summary.sessions.path} (${summary.sessions.count} entries)`));
|
||||
if (summary.sessions.recent.length > 0) {
|
||||
for (const r of summary.sessions.recent) {
|
||||
runtime.log(
|
||||
`- ${r.key} (${r.updatedAt ? `${Math.round((Date.now() - r.updatedAt) / 60000)}m ago` : "no activity"})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (const agent of displayAgents) {
|
||||
runtime.log(
|
||||
`- ${r.key} (${r.updatedAt ? `${Math.round((Date.now() - r.updatedAt) / 60000)}m ago` : "no activity"})`,
|
||||
info(
|
||||
`Session store (${agent.agentId}): ${agent.sessions.path} (${agent.sessions.count} entries)`,
|
||||
),
|
||||
);
|
||||
if (agent.sessions.recent.length > 0) {
|
||||
for (const r of agent.sessions.recent) {
|
||||
runtime.log(
|
||||
`- ${r.key} (${r.updatedAt ? `${Math.round((Date.now() - r.updatedAt) / 60000)}m ago` : "no activity"})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,6 +97,7 @@ export async function statusCommand(
|
||||
async () =>
|
||||
await callGateway<HealthSummary>({
|
||||
method: "health",
|
||||
params: { probe: true },
|
||||
timeoutMs: opts.timeoutMs,
|
||||
}),
|
||||
)
|
||||
@@ -211,6 +212,22 @@ export async function statusCommand(
|
||||
|
||||
const probesValue = health ? ok("enabled") : muted("skipped (use --deep)");
|
||||
|
||||
const heartbeatValue = (() => {
|
||||
const parts = summary.heartbeat.agents
|
||||
.map((agent) => {
|
||||
if (!agent.enabled || !agent.everyMs) return `disabled (${agent.agentId})`;
|
||||
const everyLabel = agent.every;
|
||||
return `${everyLabel} (${agent.agentId})`;
|
||||
})
|
||||
.filter(Boolean);
|
||||
return parts.length > 0 ? parts.join(", ") : "disabled";
|
||||
})();
|
||||
|
||||
const storeLabel =
|
||||
summary.sessions.paths.length > 1
|
||||
? `${summary.sessions.paths.length} stores`
|
||||
: summary.sessions.paths[0] ?? "unknown";
|
||||
|
||||
const overviewRows = [
|
||||
{ Item: "Dashboard", Value: dashboard },
|
||||
{ Item: "OS", Value: `${osSummary.label} · node ${process.versions.node}` },
|
||||
@@ -232,10 +249,10 @@ export async function statusCommand(
|
||||
{ Item: "Agents", Value: agentsValue },
|
||||
{ Item: "Probes", Value: probesValue },
|
||||
{ Item: "Events", Value: eventsValue },
|
||||
{ Item: "Heartbeat", Value: `${summary.heartbeatSeconds}s` },
|
||||
{ Item: "Heartbeat", Value: heartbeatValue },
|
||||
{
|
||||
Item: "Sessions",
|
||||
Value: `${summary.sessions.count} active · default ${defaults.model ?? "unknown"}${defaultCtx} · store ${summary.sessions.path}`,
|
||||
Value: `${summary.sessions.count} active · default ${defaults.model ?? "unknown"}${defaultCtx} · ${storeLabel}`,
|
||||
},
|
||||
];
|
||||
|
||||
@@ -396,7 +413,7 @@ export async function statusCommand(
|
||||
Detail: `${health.durationMs}ms`,
|
||||
});
|
||||
|
||||
for (const line of formatHealthChannelLines(health)) {
|
||||
for (const line of formatHealthChannelLines(health, { accountMode: "all" })) {
|
||||
const colon = line.indexOf(":");
|
||||
if (colon === -1) continue;
|
||||
const item = line.slice(0, colon).trim();
|
||||
|
||||
@@ -8,11 +8,13 @@ import {
|
||||
resolveStorePath,
|
||||
type SessionEntry,
|
||||
} from "../config/sessions.js";
|
||||
import { listAgentsForGateway } from "../gateway/session-utils.js";
|
||||
import { buildChannelSummary } from "../infra/channel-summary.js";
|
||||
import { resolveHeartbeatSummaryForAgent } from "../infra/heartbeat-runner.js";
|
||||
import { peekSystemEvents } from "../infra/system-events.js";
|
||||
import { resolveHeartbeatSeconds } from "../web/reconnect.js";
|
||||
import { parseAgentSessionKey } from "../routing/session-key.js";
|
||||
import { resolveLinkChannelContext } from "./status.link-channel.js";
|
||||
import type { SessionStatus, StatusSummary } from "./status.types.js";
|
||||
import type { HeartbeatStatus, SessionStatus, StatusSummary } from "./status.types.js";
|
||||
|
||||
const classifyKey = (key: string, entry?: SessionEntry): SessionStatus["kind"] => {
|
||||
if (key === "global") return "global";
|
||||
@@ -24,7 +26,8 @@ const classifyKey = (key: string, entry?: SessionEntry): SessionStatus["kind"] =
|
||||
return "direct";
|
||||
};
|
||||
|
||||
const buildFlags = (entry: SessionEntry): string[] => {
|
||||
const buildFlags = (entry?: SessionEntry): string[] => {
|
||||
if (!entry) return [];
|
||||
const flags: string[] = [];
|
||||
const think = entry?.thinkingLevel;
|
||||
if (typeof think === "string" && think.length > 0) flags.push(`think:${think}`);
|
||||
@@ -44,7 +47,16 @@ const buildFlags = (entry: SessionEntry): string[] => {
|
||||
export async function getStatusSummary(): Promise<StatusSummary> {
|
||||
const cfg = loadConfig();
|
||||
const linkContext = await resolveLinkChannelContext(cfg);
|
||||
const heartbeatSeconds = resolveHeartbeatSeconds(cfg, undefined);
|
||||
const agentList = listAgentsForGateway(cfg);
|
||||
const heartbeatAgents: HeartbeatStatus[] = agentList.agents.map((agent) => {
|
||||
const summary = resolveHeartbeatSummaryForAgent(cfg, agent.id);
|
||||
return {
|
||||
agentId: agent.id,
|
||||
enabled: summary.enabled,
|
||||
every: summary.every,
|
||||
everyMs: summary.everyMs,
|
||||
} satisfies HeartbeatStatus;
|
||||
});
|
||||
const channelSummary = await buildChannelSummary(cfg, {
|
||||
colorize: true,
|
||||
includeAllowFrom: true,
|
||||
@@ -63,50 +75,82 @@ export async function getStatusSummary(): Promise<StatusSummary> {
|
||||
lookupContextTokens(configModel) ??
|
||||
DEFAULT_CONTEXT_TOKENS;
|
||||
|
||||
const storePath = resolveStorePath(cfg.session?.store);
|
||||
const store = loadSessionStore(storePath);
|
||||
const now = Date.now();
|
||||
const sessions = Object.entries(store)
|
||||
.filter(([key]) => key !== "global" && key !== "unknown")
|
||||
.map(([key, entry]) => {
|
||||
const updatedAt = entry?.updatedAt ?? null;
|
||||
const age = updatedAt ? now - updatedAt : null;
|
||||
const model = entry?.model ?? configModel ?? null;
|
||||
const contextTokens =
|
||||
entry?.contextTokens ?? lookupContextTokens(model) ?? configContextTokens ?? null;
|
||||
const input = entry?.inputTokens ?? 0;
|
||||
const output = entry?.outputTokens ?? 0;
|
||||
const total = entry?.totalTokens ?? input + output;
|
||||
const remaining = contextTokens != null ? Math.max(0, contextTokens - total) : null;
|
||||
const pct =
|
||||
contextTokens && contextTokens > 0
|
||||
? Math.min(999, Math.round((total / contextTokens) * 100))
|
||||
: null;
|
||||
const storeCache = new Map<string, Record<string, SessionEntry | undefined>>();
|
||||
const loadStore = (storePath: string) => {
|
||||
const cached = storeCache.get(storePath);
|
||||
if (cached) return cached;
|
||||
const store = loadSessionStore(storePath);
|
||||
storeCache.set(storePath, store);
|
||||
return store;
|
||||
};
|
||||
const buildSessionRows = (
|
||||
store: Record<string, SessionEntry | undefined>,
|
||||
opts: { agentIdOverride?: string } = {},
|
||||
) =>
|
||||
Object.entries(store)
|
||||
.filter(([key]) => key !== "global" && key !== "unknown")
|
||||
.map(([key, entry]) => {
|
||||
const updatedAt = entry?.updatedAt ?? null;
|
||||
const age = updatedAt ? now - updatedAt : null;
|
||||
const model = entry?.model ?? configModel ?? null;
|
||||
const contextTokens =
|
||||
entry?.contextTokens ?? lookupContextTokens(model) ?? configContextTokens ?? null;
|
||||
const input = entry?.inputTokens ?? 0;
|
||||
const output = entry?.outputTokens ?? 0;
|
||||
const total = entry?.totalTokens ?? input + output;
|
||||
const remaining = contextTokens != null ? Math.max(0, contextTokens - total) : null;
|
||||
const pct =
|
||||
contextTokens && contextTokens > 0
|
||||
? Math.min(999, Math.round((total / contextTokens) * 100))
|
||||
: null;
|
||||
const parsedAgentId = parseAgentSessionKey(key)?.agentId;
|
||||
const agentId = opts.agentIdOverride ?? parsedAgentId;
|
||||
|
||||
return {
|
||||
key,
|
||||
kind: classifyKey(key, entry),
|
||||
sessionId: entry?.sessionId,
|
||||
updatedAt,
|
||||
age,
|
||||
thinkingLevel: entry?.thinkingLevel,
|
||||
verboseLevel: entry?.verboseLevel,
|
||||
reasoningLevel: entry?.reasoningLevel,
|
||||
elevatedLevel: entry?.elevatedLevel,
|
||||
systemSent: entry?.systemSent,
|
||||
abortedLastRun: entry?.abortedLastRun,
|
||||
inputTokens: entry?.inputTokens,
|
||||
outputTokens: entry?.outputTokens,
|
||||
totalTokens: total ?? null,
|
||||
remainingTokens: remaining,
|
||||
percentUsed: pct,
|
||||
model,
|
||||
contextTokens,
|
||||
flags: buildFlags(entry),
|
||||
} satisfies SessionStatus;
|
||||
})
|
||||
return {
|
||||
agentId,
|
||||
key,
|
||||
kind: classifyKey(key, entry),
|
||||
sessionId: entry?.sessionId,
|
||||
updatedAt,
|
||||
age,
|
||||
thinkingLevel: entry?.thinkingLevel,
|
||||
verboseLevel: entry?.verboseLevel,
|
||||
reasoningLevel: entry?.reasoningLevel,
|
||||
elevatedLevel: entry?.elevatedLevel,
|
||||
systemSent: entry?.systemSent,
|
||||
abortedLastRun: entry?.abortedLastRun,
|
||||
inputTokens: entry?.inputTokens,
|
||||
outputTokens: entry?.outputTokens,
|
||||
totalTokens: total ?? null,
|
||||
remainingTokens: remaining,
|
||||
percentUsed: pct,
|
||||
model,
|
||||
contextTokens,
|
||||
flags: buildFlags(entry),
|
||||
} satisfies SessionStatus;
|
||||
})
|
||||
.sort((a, b) => (b.updatedAt ?? 0) - (a.updatedAt ?? 0));
|
||||
|
||||
const paths = new Set<string>();
|
||||
const byAgent = agentList.agents.map((agent) => {
|
||||
const storePath = resolveStorePath(cfg.session?.store, { agentId: agent.id });
|
||||
paths.add(storePath);
|
||||
const store = loadStore(storePath);
|
||||
const sessions = buildSessionRows(store, { agentIdOverride: agent.id });
|
||||
return {
|
||||
agentId: agent.id,
|
||||
path: storePath,
|
||||
count: sessions.length,
|
||||
recent: sessions.slice(0, 10),
|
||||
};
|
||||
});
|
||||
|
||||
const allSessions = Array.from(paths)
|
||||
.flatMap((storePath) => buildSessionRows(loadStore(storePath)))
|
||||
.sort((a, b) => (b.updatedAt ?? 0) - (a.updatedAt ?? 0));
|
||||
const recent = sessions.slice(0, 5);
|
||||
const recent = allSessions.slice(0, 10);
|
||||
const totalSessions = allSessions.length;
|
||||
|
||||
return {
|
||||
linkChannel: linkContext
|
||||
@@ -117,17 +161,21 @@ export async function getStatusSummary(): Promise<StatusSummary> {
|
||||
authAgeMs: linkContext.authAgeMs,
|
||||
}
|
||||
: undefined,
|
||||
heartbeatSeconds,
|
||||
heartbeat: {
|
||||
defaultAgentId: agentList.defaultId,
|
||||
agents: heartbeatAgents,
|
||||
},
|
||||
channelSummary,
|
||||
queuedSystemEvents,
|
||||
sessions: {
|
||||
path: storePath,
|
||||
count: sessions.length,
|
||||
paths: Array.from(paths),
|
||||
count: totalSessions,
|
||||
defaults: {
|
||||
model: configModel ?? null,
|
||||
contextTokens: configContextTokens ?? null,
|
||||
},
|
||||
recent,
|
||||
byAgent,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { ChannelId } from "../channels/plugins/types.js";
|
||||
|
||||
export type SessionStatus = {
|
||||
agentId?: string;
|
||||
key: string;
|
||||
kind: "direct" | "group" | "global" | "unknown";
|
||||
sessionId?: string;
|
||||
@@ -22,6 +23,13 @@ export type SessionStatus = {
|
||||
flags: string[];
|
||||
};
|
||||
|
||||
export type HeartbeatStatus = {
|
||||
agentId: string;
|
||||
enabled: boolean;
|
||||
every: string;
|
||||
everyMs: number | null;
|
||||
};
|
||||
|
||||
export type StatusSummary = {
|
||||
linkChannel?: {
|
||||
id: ChannelId;
|
||||
@@ -29,13 +37,22 @@ export type StatusSummary = {
|
||||
linked: boolean;
|
||||
authAgeMs: number | null;
|
||||
};
|
||||
heartbeatSeconds: number;
|
||||
heartbeat: {
|
||||
defaultAgentId: string;
|
||||
agents: HeartbeatStatus[];
|
||||
};
|
||||
channelSummary: string[];
|
||||
queuedSystemEvents: string[];
|
||||
sessions: {
|
||||
path: string;
|
||||
paths: string[];
|
||||
count: number;
|
||||
defaults: { model: string | null; contextTokens: number | null };
|
||||
recent: SessionStatus[];
|
||||
byAgent: Array<{
|
||||
agentId: string;
|
||||
path: string;
|
||||
count: number;
|
||||
recent: SessionStatus[];
|
||||
}>;
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user