husteno-o feat: AI reliability, LRU cache, Prometheus metrics, CI logs, dependency alerts · 9/10/2026
9fbbdd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { cache } from "@devpulse/cache";
import { NotFoundError, ValidationError } from "@devpulse/core";
import { db, monitorResults, monitors } from "@devpulse/database";
import { inspectSslCertificate, testHttpRequest, validateSafeUrl } from "@devpulse/devtools";
import { logger } from "@devpulse/logger";
import { and, desc, eq } from "drizzle-orm";
export interface MonitorCheckOutcome {
monitorId: string;
guildId: string;
name: string;
url: string;
statusCode: number | null;
responseTimeMs: number;
isHealthy: boolean;
sslValid?: boolean;
sslExpiresAt?: Date;
statusChanged: boolean;
errorMessage?: string;
}
export class MonitoringService {
async addMonitor(
guildId: string,
params: {
name?: string;
url: string;
method?: "GET" | "HEAD";
intervalSeconds?: number;
expectedStatus?: number;
sslCheckEnabled?: boolean;
},
) {
const safeUrl = await validateSafeUrl(params.url);
const urlStr = safeUrl.toString();
const name = params.name?.trim() || safeUrl.hostname;
// Check count limit
const existing = await db.query.monitors.findMany({
where: eq(monitors.guildId, guildId),
});
if (existing.length >= 20) {
throw new ValidationError("Maximum limit of 20 monitors reached for this server.");
}
const id = crypto.randomUUID();
const newMonitor = {
id,
guildId,
name,
url: urlStr,
method: params.method || "GET",
intervalSeconds: params.intervalSeconds || 60,
expectedStatus: params.expectedStatus || 200,
sslCheckEnabled: params.sslCheckEnabled ?? true,
isActive: true,
isHealthy: true,
};
await db.insert(monitors).values(newMonitor);
// Run initial check immediately
const checkOutcome = await this.checkMonitor(id);
return {
id,
name,
url: urlStr,
initialCheck: checkOutcome,
};
}
async listMonitors(guildId: string) {
return db.query.monitors.findMany({
where: eq(monitors.guildId, guildId),
orderBy: [desc(monitors.createdAt)],
});
}
async getMonitorStatus(guildId: string, monitorId: string) {
const monitor = await db.query.monitors.findFirst({
where: and(eq(monitors.id, monitorId), eq(monitors.guildId, guildId)),
});
if (!monitor) {
throw new NotFoundError("Monitor", monitorId);
}
const history = await db.query.monitorResults.findMany({
where: eq(monitorResults.monitorId, monitorId),
orderBy: [desc(monitorResults.checkedAt)],
limit: 10,
});
return {
monitor,
history,
};
}
async removeMonitor(guildId: string, monitorId: string) {
const monitor = await db.query.monitors.findFirst({
where: and(eq(monitors.id, monitorId), eq(monitors.guildId, guildId)),
});
if (!monitor) {
throw new NotFoundError("Monitor", monitorId);
}
await db.delete(monitors).where(eq(monitors.id, monitorId));
return true;
}
async checkMonitor(monitorId: string): Promise<MonitorCheckOutcome> {
const monitor = await db.query.monitors.findFirst({
where: eq(monitors.id, monitorId),
});
if (!monitor) {
throw new NotFoundError("Monitor", monitorId);
}
const startTime = Date.now();
let statusCode: number | null = null;
let isHealthy = false;
let errorMessage: string | undefined;
let sslValid: boolean | undefined;
let sslExpiresAt: Date | undefined;
try {
const httpRes = await testHttpRequest(monitor.url, monitor.method as "GET" | "HEAD");
statusCode = httpRes.statusCode;
isHealthy = statusCode === monitor.expectedStatus;
if (!isHealthy) {
errorMessage = `Expected HTTP ${monitor.expectedStatus}, received HTTP ${statusCode}`;
}
if (monitor.sslCheckEnabled && monitor.url.startsWith("https://")) {
const domain = new URL(monitor.url).hostname;
const cert = await inspectSslCertificate(domain).catch(() => null);
if (cert) {
sslValid = cert.isValid;
sslExpiresAt = new Date(cert.validTo);
if (!cert.isValid || cert.daysRemaining < 3) {
isHealthy = false;
errorMessage = errorMessage
? `${errorMessage} | SSL certificate expired or invalid`
: `SSL certificate invalid (expires in ${cert.daysRemaining} days)`;
}
}
}
} catch (err: unknown) {
isHealthy = false;
errorMessage =
(err instanceof Error ? err.message : String(err)) || "Network / connection timeout";
}
const responseTimeMs = Date.now() - startTime;
const previousHealthyState = monitor.isHealthy;
const statusChanged = previousHealthyState !== isHealthy;
// Update monitor table
await db
.update(monitors)
.set({
lastCheckedAt: new Date(Date.now()),
lastStatus: statusCode,
lastResponseTimeMs: responseTimeMs,
lastError: errorMessage || null,
isHealthy,
updatedAt: new Date(Date.now()),
})
.where(eq(monitors.id, monitorId));
// Save monitor result
await db.insert(monitorResults).values({
id: crypto.randomUUID(),
monitorId,
guildId: monitor.guildId,
statusCode,
responseTimeMs,
isHealthy,
sslValid,
sslExpiresAt,
errorMessage: errorMessage || null,
checkedAt: new Date(Date.now()),
});
return {
monitorId,
guildId: monitor.guildId,
name: monitor.name,
url: monitor.url,
statusCode,
responseTimeMs,
isHealthy,
sslValid,
sslExpiresAt,
statusChanged,
errorMessage,
};
}
async runScheduledChecks(): Promise<MonitorCheckOutcome[]> {
const lockAcquired = await cache.acquireLock("scheduler:monitors", 50);
if (!lockAcquired) {
logger.debug("Another instance is currently running monitor checks");
return [];
}
try {
const activeMonitors = await db.query.monitors.findMany({
where: eq(monitors.isActive, true),
});
const now = Date.now();
const dueMonitors = activeMonitors.filter((m) => {
if (!m.lastCheckedAt) return true;
const lastMs = new Date(m.lastCheckedAt).getTime();
return now - lastMs >= m.intervalSeconds * 1000;
});
logger.info({ dueCount: dueMonitors.length }, "Running due monitor checks");
const outcomes: MonitorCheckOutcome[] = [];
for (const m of dueMonitors) {
try {
const res = await this.checkMonitor(m.id);
outcomes.push(res);
} catch (err: unknown) {
logger.error({ err, monitorId: m.id }, "Error during scheduled monitor check");
}
}
return outcomes;
} finally {
await cache.releaseLock("scheduler:monitors");
}
}
}
export const monitoringService = new MonitoringService();
class MetricsCollector {
private commandCounts = new Map<string, { count: number; totalMs: number; errors: number }>();
private cacheHits = 0;
private cacheMisses = 0;
private aiRequests = 0;
private aiErrors = 0;
private githubApiCalls = 0;
private githubApiErrors = 0;
private startedAt = Date.now();
recordCommand(name: string, durationMs: number, success: boolean): void {
const existing = this.commandCounts.get(name) || { count: 0, totalMs: 0, errors: 0 };
existing.count++;
existing.totalMs += durationMs;
if (!success) existing.errors++;
this.commandCounts.set(name, existing);
}
recordCacheHit(): void { this.cacheHits++; }
recordCacheMiss(): void { this.cacheMisses++; }
recordAiRequest(): void { this.aiRequests++; }
recordAiError(): void { this.aiErrors++; }
recordGithubApiCall(): void { this.githubApiCalls++; }
recordGithubApiError(): void { this.githubApiErrors++; }
getSnapshot(): Record<string, unknown> {
const commands: Record<string, unknown> = {};
for (const [name, data] of this.commandCounts) {
commands[name] = {
total: data.count,
avgMs: Math.round(data.totalMs / data.count),
errors: data.errors,
errorRate: data.count > 0 ? `${((data.errors / data.count) * 100).toFixed(1)}%` : "0%",
};
}
const uptimeMs = Date.now() - this.startedAt;
return {
uptimeSeconds: Math.floor(uptimeMs / 1000),
uptimeHuman: `${Math.floor(uptimeMs / 3600000)}h ${Math.floor((uptimeMs % 3600000) / 60000)}m`,
commands,
commandSummary: {
totalCommands: Array.from(this.commandCounts.values()).reduce((s, c) => s + c.count, 0),
totalErrors: Array.from(this.commandCounts.values()).reduce((s, c) => s + c.errors, 0),
avgLatencyMs: Math.round(
Array.from(this.commandCounts.values()).reduce((s, c) => s + c.totalMs, 0) /
Math.max(Array.from(this.commandCounts.values()).reduce((s, c) => s + c.count, 0), 1),
),
},
cache: {
hits: this.cacheHits,
misses: this.cacheMisses,
hitRate: this.cacheHits + this.cacheMisses > 0
? `${((this.cacheHits / (this.cacheHits + this.cacheMisses)) * 100).toFixed(1)}%`
: "N/A",
},
ai: {
requests: this.aiRequests,
errors: this.aiErrors,
errorRate: this.aiRequests > 0
? `${((this.aiErrors / this.aiRequests) * 100).toFixed(1)}%`
: "0%",
},
github: {
apiCalls: this.githubApiCalls,
apiErrors: this.githubApiErrors,
},
};
}
getPrometheusMetrics(): string {
const lines: string[] = [];
lines.push("# HELP gitbot_commands_total Total number of commands executed");
lines.push("# TYPE gitbot_commands_total counter");
for (const [name, data] of this.commandCounts) {
lines.push(`gitbot_commands_total{command="${name}"} ${data.count}`);
}
lines.push("# HELP gitbot_command_duration_ms Average command duration");
lines.push("# TYPE gitbot_command_duration_ms gauge");
for (const [name, data] of this.commandCounts) {
lines.push(`gitbot_command_duration_ms{command="${name}"} ${Math.round(data.totalMs / data.count)}`);
}
lines.push("# HELP gitbot_command_errors_total Total command errors");
lines.push("# TYPE gitbot_command_errors_total counter");
for (const [name, data] of this.commandCounts) {
lines.push(`gitbot_command_errors_total{command="${name}"} ${data.errors}`);
}
lines.push(`# HELP gitbot_cache_hits_total Cache hits`);
lines.push(`# TYPE gitbot_cache_hits_total counter`);
lines.push(`gitbot_cache_hits_total ${this.cacheHits}`);
lines.push(`# HELP gitbot_cache_misses_total Cache misses`);
lines.push(`# TYPE gitbot_cache_misses_total counter`);
lines.push(`gitbot_cache_misses_total ${this.cacheMisses}`);
lines.push(`# HELP gitbot_uptime_seconds Uptime in seconds`);
lines.push(`# TYPE gitbot_uptime_seconds gauge`);
lines.push(`gitbot_uptime_seconds ${Math.floor((Date.now() - this.startedAt) / 1000)}`);
return lines.join("\n");
}
}
export const metricsCollector = new MetricsCollector();