@astrofoundry/pi-astro 0.37.1 → 0.37.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -71,7 +71,9 @@ import {
71
71
  } from "./tools-report.ts";
72
72
  import { type Quote, type QuotedMessage, withQuotes } from "./quote.ts";
73
73
  import { DiscordRest, type MessageComponent } from "./rest.ts";
74
+ import { ArcaneWatch } from "./arcane-watch-monitor.ts";
74
75
  import { type InvestigationRequest, SecurityMonitor } from "./security-monitor.ts";
76
+ import { nasOutage, readSecurityState } from "./security.ts";
75
77
 
76
78
  const REACTION = { running: "⏳", waiting: "🔒", done: "✅", failed: "❌" } as const;
77
79
  /** Answers longer than this go as a file attachment with a short summary. */
@@ -222,6 +224,7 @@ class Bridge {
222
224
  private readonly ctx: ExtensionContext;
223
225
  private readonly token: string;
224
226
  private readonly security: SecurityMonitor;
227
+ private readonly arcaneWatch: ArcaneWatch;
225
228
 
226
229
  constructor(ctx: ExtensionContext, config: DiscordConfig, token: string) {
227
230
  this.ctx = ctx;
@@ -235,7 +238,17 @@ class Bridge {
235
238
  runEntry: (service, caller, args, timeoutMs) => this.runEntry(service, caller, args, timeoutMs),
236
239
  config: () => this.config,
237
240
  log,
238
- investigate: (request) => this.investigateSecurity(request),
241
+ investigate: (request) => this.investigateIn("security", request),
242
+ stopping: () => this.stopping,
243
+ now: () => Date.now(),
244
+ });
245
+ this.arcaneWatch = new ArcaneWatch({
246
+ rest: this.rest,
247
+ runEntry: (service, caller, args, timeoutMs) => this.runEntry(service, caller, args, timeoutMs),
248
+ config: () => this.config,
249
+ log,
250
+ investigate: (request) => this.investigateIn("arcane", request),
251
+ nasOutage: (now) => nasOutage(readSecurityState().nas, now),
239
252
  stopping: () => this.stopping,
240
253
  now: () => Date.now(),
241
254
  });
@@ -346,10 +359,12 @@ class Bridge {
346
359
  void this.maybeReports();
347
360
  void this.processRestarts();
348
361
  this.security.tick();
362
+ this.arcaneWatch.tick();
349
363
  }, 60_000);
350
364
  this.reportTimer.unref();
351
365
  void this.maybeReports();
352
366
  this.security.tick();
367
+ this.arcaneWatch.tick();
353
368
  log(`connected as ${me.username} (${me.id}); channels ${Object.keys(this.config.channels).join(", ")}; specialists ${this.specialists().names.join(", ")}`);
354
369
  }
355
370
 
@@ -1118,29 +1133,30 @@ class Bridge {
1118
1133
  }
1119
1134
 
1120
1135
  /**
1121
- * Investigate on a security alert: astro.security in the alert's thread,
1122
- * queued behind any other security task, as a fresh conversation that the
1136
+ * An investigation in a thread: astro.security on a security alert, or
1137
+ * astro.arcane on a container that keeps restarting. It waits behind any
1138
+ * other task of that specialist and starts a fresh conversation that the
1123
1139
  * owner can continue by writing in the thread.
1124
1140
  */
1125
- private async investigateSecurity(request: InvestigationRequest): Promise<boolean> {
1126
- const agent = this.specialists().agents.find((a) => a.name === "astro.security");
1141
+ private async investigateIn(specialist: "security" | "arcane", request: InvestigationRequest): Promise<boolean> {
1142
+ const agent = this.specialists().agents.find((a) => a.name === `astro.${specialist}`);
1127
1143
  if (!agent) {
1128
- await this.rest.createMessage(request.threadId, { content: "❌ astro.security is not configured on this host." }).catch(() => undefined);
1144
+ await this.rest.createMessage(request.threadId, { content: `❌ astro.${specialist} is not configured on this host.` }).catch(() => undefined);
1129
1145
  return false;
1130
1146
  }
1131
1147
  const now = Date.now();
1132
- const conversation = resolveConversation(this.conversations, { channelId: request.threadId, specialist: "security", fresh: true, now }, () => newConversationId("security", now));
1148
+ const conversation = resolveConversation(this.conversations, { channelId: request.threadId, specialist, fresh: true, now }, () => newConversationId(specialist, now));
1133
1149
  this.conversations = startConversation(this.conversations, conversation.key, conversation.id, now);
1134
1150
  saveState(this.conversations);
1135
- const task: ActiveTask = { specialist: "security", channelId: request.threadId, messageId: request.starterId, userId: request.userId, startedAt: now, conversation, images: [] };
1151
+ const task: ActiveTask = { specialist, channelId: request.threadId, messageId: request.starterId, userId: request.userId, startedAt: now, conversation, images: [] };
1136
1152
  let failed = true;
1137
- const previous = this.queues.get("security") ?? Promise.resolve();
1153
+ const previous = this.queues.get(specialist) ?? Promise.resolve();
1138
1154
  const next = previous
1139
1155
  .then(async () => {
1140
1156
  failed = await this.runTask(agent, request.task, task);
1141
1157
  })
1142
- .catch((err) => log(`security investigation failed: ${err instanceof Error ? err.message : String(err)}`));
1143
- this.queues.set("security", next);
1158
+ .catch((err) => log(`${specialist} investigation failed: ${err instanceof Error ? err.message : String(err)}`));
1159
+ this.queues.set(specialist, next);
1144
1160
  await next;
1145
1161
  return !failed;
1146
1162
  }
@@ -84,7 +84,7 @@ function harness(): Harness {
84
84
  rest,
85
85
  runEntry: async (service, _caller, args) => {
86
86
  calls.push([service, ...args]);
87
- const answer = answers.get(`${service} ${args[0]}`);
87
+ const answer = answers.get(`${service} ${args.slice(0, 3).join(" ")}`) ?? answers.get(`${service} ${args[0]}`);
88
88
  if (!answer) throw new Error(`no answer for ${service} ${args.join(" ")}`);
89
89
  return answer();
90
90
  },
@@ -295,8 +295,12 @@ describe("security monitor", () => {
295
295
  h.answers.set("security wazuh-vuln-events", () =>
296
296
  JSON.stringify({ days: 2, count: 1, events: [{ timestamp: "2026-10-20T20:00:00.000+0000", agent: "dmz", cve: "CVE-1", status: "Active", package: "openssl", version: "3.5.1", severity: "High", score: 7.5, condition: "Package less than 3.5.2" }] }),
297
297
  );
298
+ h.answers.set("arcane images vulnerabilities summary", () => JSON.stringify({ totalImages: 57, scannedImages: 57, summary: { critical: 1 } }));
299
+ h.answers.set("arcane images vulnerabilities list", () => JSON.stringify({ data: [{ vulnerabilityId: "CVE-7", pkgName: "zlib", fixedVersion: "1.3.1", ignored: false, imageName: "nginx:latest" }] }));
298
300
  await h.monitor.dailyReport(SCHEDULE, "2026-10-21");
299
301
  const text = h.rest.messages[0].message.content;
302
+ expect(text).toContain("Arcane images · 1 critical CVE with a fix in 1 image: nginx:latest: CVE-7 (zlib); no newer image offered yet");
303
+ expect(h.calls).toContainEqual(["arcane", "images", "vulnerabilities", "list", "--severity", "critical", "--all"]);
300
304
  expect(text.split("\n")[0]).toBe("**Security Daily Report:** quiet day (24 h to 06:05 CEST)");
301
305
  expect(text).toContain("CrowdSec · 2 bans, all front door SNI scanning");
302
306
  expect(text).toContain("Vulnerabilities · 1 new CVE on 1 host: CVE-1 (openssl, High 7.5) on dmz · fixed versions exist");
@@ -1,4 +1,5 @@
1
1
  import { chunkMessage } from "./chunk.ts";
2
+ import { readPostedArcane } from "./daily-report.ts";
2
3
  import { type DiscordConfig, isOwner, type ReportSchedule, reportSchedule } from "./config.ts";
3
4
  import { KeyedQueue } from "./media.ts";
4
5
  import { MONDAY, writeReportState, zonedWeekday } from "./reports.ts";
@@ -19,15 +20,20 @@ import {
19
20
  dailyNeedsOwner,
20
21
  dailyText,
21
22
  healthChange,
23
+ type ImageOffer,
24
+ imageOffers,
25
+ type ImageVulnFacts,
22
26
  investigationTask,
23
27
  isAcked,
24
28
  MAX_POSTS_PER_CHECK,
25
29
  needsOrigin,
30
+ noteNas,
26
31
  noticeText,
27
32
  ORIGIN_RETRY_MS,
28
33
  type OriginVerdict,
29
34
  originArgs,
30
35
  parseAttention,
36
+ parseImageVulns,
31
37
  parseOrigin,
32
38
  parseSecurityButton,
33
39
  type PatchingFacts,
@@ -272,6 +278,7 @@ export class SecurityMonitor {
272
278
  for (const e of fresh) {
273
279
  if (state.seen[e.id] !== undefined) continue;
274
280
  state.seen[e.id] = e.timestamp;
281
+ noteNas(state, e);
275
282
  if (needsOrigin(e)) settle(e, new Date(now).toISOString());
276
283
  else ready.push(e);
277
284
  }
@@ -429,6 +436,16 @@ export class SecurityMonitor {
429
436
  });
430
437
  }
431
438
 
439
+ /** Arcane's Trivy results (critical, with a fix) and the stacks of the newest Arcane Daily Report that would replace those images. */
440
+ private async imageFacts(): Promise<{ images: ImageVulnFacts | null; offers: Map<string, ImageOffer>; arcaneChannelId?: string }> {
441
+ const summary = await this.read("images vulnerabilities summary", "arcane", ["images", "vulnerabilities", "summary"], (v) => (v && typeof v === "object" ? v : null));
442
+ const list = summary ? await this.read("images vulnerabilities list", "arcane", ["images", "vulnerabilities", "list", "--severity", "critical", "--all"], (v) => (v && typeof v === "object" ? v : null), 5 * MINUTE) : null;
443
+ const images = summary && list ? parseImageVulns(summary, list) : null;
444
+ if (summary && list && !images) this.deps.log("security report: images vulnerabilities returned an unexpected answer");
445
+ const arcaneChannelId = reportSchedule(this.deps.config(), "arcane")?.channelId;
446
+ return { images, offers: imageOffers(readPostedArcane()?.stacks ?? []), ...(arcaneChannelId ? { arcaneChannelId } : {}) };
447
+ }
448
+
432
449
  private async postReport(channelId: string, text: string, mention: boolean): Promise<string> {
433
450
  const owners = this.owners();
434
451
  let first = "";
@@ -451,11 +468,12 @@ export class SecurityMonitor {
451
468
  const events = await this.read("wazuh-events", "security", ["wazuh-events", "1440", String(WATCH_LEVEL)], parseEvents);
452
469
  const attention = await this.read("attention", "security", ["attention"], parseAttention, 2 * MINUTE);
453
470
  const vulns = await this.vulnerabilities(now);
471
+ const images = await this.imageFacts();
454
472
  await this.fillOrigins(events);
455
473
  const state = readSecurityState();
456
474
  const wazuh = wazuhDigest(events, state.origins, (e) => acked(state, e));
457
475
  if (this.deps.stopping()) return;
458
- const data = { until: clockText(new Date(now).toISOString(), s.timeZone).replace(/^\d+ \w+ /, ""), crowdsec, wazuh, found: vulns?.found ?? null, closed: vulns?.closed ?? null, attention };
476
+ const data = { until: clockText(new Date(now).toISOString(), s.timeZone).replace(/^\d+ \w+ /, ""), crowdsec, wazuh, found: vulns?.found ?? null, closed: vulns?.closed ?? null, attention, ...images };
459
477
  writeReportState("security", { date, status: "sending" });
460
478
  const id = await this.postReport(s.channelId, dailyText(data, this.owners()), dailyNeedsOwner(data));
461
479
  writeReportState("security", { date, status: "sent", messageId: id });
@@ -475,6 +493,7 @@ export class SecurityMonitor {
475
493
  const resticText = await this.readText("restic-check-status", "backup", ["restic-check-status"]);
476
494
  // Monday has no daily report, so the weekly one moves the vulnerability record forward.
477
495
  const day = await this.vulnerabilities(now);
496
+ const images = await this.imageFacts();
478
497
  await this.fillOrigins(events);
479
498
  const { state, previousBans } = await this.update((st) => {
480
499
  const last = st.weeks[st.weeks.length - 1];
@@ -500,6 +519,7 @@ export class SecurityMonitor {
500
519
  attention,
501
520
  crl: events ? crlFromEvents(events, now) : ("unknown" as const),
502
521
  backups: archives ? { guests: archives, restic: resticText === null ? null : resticStatus(resticText) } : null,
522
+ ...images,
503
523
  last24h: {
504
524
  bans: lastDay?.count ?? null,
505
525
  wazuh: lastDayEvents?.length ?? null,
@@ -33,6 +33,14 @@ import {
33
33
  type PostedAlert,
34
34
  pruneState,
35
35
  type ReportedVuln,
36
+ type ImageOffer,
37
+ imageOffers,
38
+ imageVulnLine,
39
+ type ImageVulnFacts,
40
+ offeredImages,
41
+ parseImageVulns,
42
+ nasOutage,
43
+ noteNas,
36
44
  repeatText,
37
45
  resticStatus,
38
46
  type VulnEvent,
@@ -357,11 +365,73 @@ describe("vulnerabilities", () => {
357
365
  });
358
366
  });
359
367
 
368
+ describe("NAS outage record", () => {
369
+ it("keeps the newest loss and return and calls the share out until half an hour after it returns", () => {
370
+ const state = emptyState();
371
+ noteNas(state, event({ rule: "100012", timestamp: "2026-10-17T22:47:00.000+0000" }));
372
+ noteNas(state, event({ rule: "100012", timestamp: "2026-10-17T20:00:00.000+0000" }));
373
+ noteNas(state, event({ rule: "5712", timestamp: "2026-10-18T09:00:00.000+0000" }));
374
+ expect(state.nas).toEqual({ lostAt: "2026-10-17T22:47:00.000+0000", restoredAt: null });
375
+ expect(nasOutage(state.nas, Date.parse("2026-10-18T03:00:00Z"))).toBe(true);
376
+ noteNas(state, event({ rule: "100013", timestamp: "2026-10-18T06:25:00.000+0000" }));
377
+ expect(nasOutage(state.nas, Date.parse("2026-10-18T06:50:00Z"))).toBe(true);
378
+ expect(nasOutage(state.nas, Date.parse("2026-10-18T07:00:00Z"))).toBe(false);
379
+ expect(nasOutage(emptyState().nas, Date.parse("2026-10-18T07:00:00Z"))).toBe(false);
380
+ });
381
+ });
382
+
383
+ describe("Arcane image scans", () => {
384
+ const item = (over: Record<string, unknown> = {}) => ({ vulnerabilityId: "CVE-2026-1", pkgName: "openssl", installedVersion: "3.0.1", fixedVersion: "3.0.2", severity: "CRITICAL", ignored: false, imageName: "ghcr.io/mealie-recipes/mealie:latest", ...over });
385
+ const stacks = [
386
+ { stack: "mealie", lines: [{ ref: "ghcr.io/mealie-recipes/mealie:latest" }], actionable: true, state: "offered" },
387
+ { stack: "supabase", lines: [{ ref: "supabase/postgres:15.8.1.085" }], actionable: false, state: "offered", held: "coordinated release: review first" },
388
+ { stack: "vault", lines: [{ ref: "vaultwarden/server:latest" }], actionable: true, state: "updated" },
389
+ ];
390
+
391
+ it("keeps critical findings that have a fix and are not ignored", () => {
392
+ const facts = parseImageVulns({ totalImages: 57, scannedImages: 3 }, { data: [item(), item({ vulnerabilityId: "CVE-2", ignored: true }), item({ vulnerabilityId: "CVE-3", fixedVersion: "" }), { nonsense: true }] });
393
+ expect(facts).toEqual({ scanned: 3, total: 57, critical: [{ id: "CVE-2026-1", pkg: "openssl", image: "ghcr.io/mealie-recipes/mealie:latest" }] });
394
+ expect(parseImageVulns({ totalImages: 57 }, { data: [] })).toBeNull();
395
+ expect(parseImageVulns({ totalImages: 1, scannedImages: 1 }, {})).toBeNull();
396
+ });
397
+
398
+ it("matches images to stacks by repository, whatever the tag or registry spelling", () => {
399
+ const offers = imageOffers(stacks);
400
+ expect(offers.get("ghcr.io/mealie-recipes/mealie")).toEqual({ stack: "mealie", offered: true });
401
+ expect(offers.get("docker.io/supabase/postgres")).toEqual({ stack: "supabase", offered: false, held: "coordinated release: review first" });
402
+ expect(offers.get("docker.io/vaultwarden/server")).toEqual({ stack: "vault", offered: false });
403
+ expect(imageOffers([{ stack: "x", lines: [{}], actionable: true, state: "offered" }]).size).toBe(0);
404
+ });
405
+
406
+ it("points each image at its stack's Update button, a hold, or nothing yet", () => {
407
+ const offers = imageOffers(stacks);
408
+ const facts: ImageVulnFacts = {
409
+ scanned: 57,
410
+ total: 57,
411
+ critical: [
412
+ { id: "CVE-1", pkg: "openssl", image: "ghcr.io/mealie-recipes/mealie:latest" },
413
+ { id: "CVE-2", pkg: "zlib", image: "ghcr.io/mealie-recipes/mealie:latest" },
414
+ { id: "CVE-3", pkg: "glibc", image: "ghcr.io/mealie-recipes/mealie:latest" },
415
+ { id: "CVE-4", pkg: "libpq", image: "supabase/postgres:15.8.1.085" },
416
+ { id: "CVE-5", pkg: "curl", image: "nginx:latest" },
417
+ ],
418
+ };
419
+ expect(imageVulnLine(facts, offers, "1549873938140499999")).toBe(
420
+ "Arcane images · 5 critical CVEs with a fix in 3 images: ghcr.io/mealie-recipes/mealie:latest: CVE-1 (openssl), CVE-2 (zlib), +1 more; update offered: Update on mealie in the Arcane Daily Report (<#1549873938140499999>) · nginx:latest: CVE-5 (curl); no newer image offered yet · supabase/postgres:15.8.1.085: CVE-4 (libpq); supabase is held: coordinated release: review first",
421
+ );
422
+ expect(offeredImages(facts, offers)).toEqual(["mealie"]);
423
+ expect(offeredImages(null, offers)).toEqual([]);
424
+ expect(imageVulnLine(null, offers)).toBe("Arcane images · the check failed");
425
+ expect(imageVulnLine({ scanned: 0, total: 57, critical: [] }, offers)).toBe("Arcane images · no Trivy scan yet (57 images)");
426
+ expect(imageVulnLine({ ...facts, critical: facts.critical.slice(4) }, new Map(), undefined, 0)).toBe("Arcane images · 1 critical CVE with a fix in 1 image: · +1 more images");
427
+ });
428
+ });
429
+
360
430
  describe("daily report", () => {
361
431
  const attention = parseAttention({ checked_at: "x", bouncer: { count: 1, oldest_pull_seconds: 6 }, wazuh_agents: { total: 8, active: 8 }, health_issues: [] });
362
432
 
363
433
  it("reads quiet day and tags nobody when nothing is open", () => {
364
- const data = { until: "06:00 CEST", crowdsec: summary(), wazuh: wazuhDigest([], {}, () => false), found: [], closed: [], attention };
434
+ const data = { until: "06:00 CEST", crowdsec: summary(), wazuh: wazuhDigest([], {}, () => false), found: [], closed: [], attention, images: { scanned: 57, total: 57, critical: [] }, offers: new Map() };
365
435
  expect(dailyText(data, [OWNER])).toBe(
366
436
  [
367
437
  "**Security Daily Report:** quiet day (24 h to 06:00 CEST)",
@@ -369,15 +439,17 @@ describe("daily report", () => {
369
439
  "Wazuh · no alerts at level 10+",
370
440
  "Vulnerabilities · nothing new",
371
441
  "Patches · none",
442
+ "Arcane images · no critical CVE with a fix (57 of 57 images scanned)",
372
443
  "Health · 8/8 Wazuh agents active · bouncer pulled 6 s ago",
373
444
  ].join("\n"),
374
445
  );
375
446
  expect(dailyNeedsOwner(data)).toBe(false);
447
+ expect(dailyNeedsOwner({ ...data, images: null })).toBe(true);
376
448
  });
377
449
 
378
450
  it("names urgent alerts and failed checks in the headline and tags the owner while something is open", () => {
379
451
  const wazuh = wazuhDigest([event({ rule: "5712", level: 12, description: "brute force" })], {}, () => false);
380
- const data = { until: "06:00 CEST", crowdsec: null, wazuh, found: [], closed: [], attention };
452
+ const data = { until: "06:00 CEST", crowdsec: null, wazuh, found: [], closed: [], attention, images: { scanned: 57, total: 57, critical: [] }, offers: new Map() };
381
453
  const text = dailyText(data, [OWNER]);
382
454
  expect(text.split("\n")[0]).toBe("**Security Daily Report:** 1 urgent alert, 1 check failed (24 h to 06:00 CEST)");
383
455
  expect(text.endsWith(`<@${OWNER}>`)).toBe(true);
@@ -401,6 +473,8 @@ describe("weekly report", () => {
401
473
  crl: null,
402
474
  backups: { guests: [{ vmid: "100", name: "arcane", last: Date.parse("2026-10-20T00:30:00Z") / 1000 }], restic: { result: "success", at: "18 Oct 04:00" } },
403
475
  last24h: { bans: 16, wazuh: 0, urgent: 0, found: 0, closed: 2, health: 0 },
476
+ images: { scanned: 57, total: 57, critical: [] } as ImageVulnFacts | null,
477
+ offers: new Map<string, ImageOffer>(),
404
478
  };
405
479
 
406
480
  it("follows the approved layout and says when nothing needs the owner", () => {
@@ -413,6 +487,7 @@ describe("weekly report", () => {
413
487
  "Wazuh · no alerts at level 10+",
414
488
  "Vulnerabilities · 0 new · 0 closed by patches · 0 open with a fixed version",
415
489
  "Patching · 14 package installs on 2 hosts · 0 CVEs closed · agent restarts: frontdoor-1337 19 Oct · reboot pending: none",
490
+ "Arcane images · no critical CVE with a fix (57 of 57 images scanned)",
416
491
  "Health · 8/8 Wazuh agents active · bouncer pulled 4 s ago · RADIUS CRL: no renewal warning (it warns 30 days ahead)",
417
492
  "Backups · Proxmox: the only guest backed up within 48 h, last 20 Oct 02:30 CEST · restic check: success 18 Oct 04:00",
418
493
  "Needs you · nothing",
@@ -431,6 +506,10 @@ describe("weekly report", () => {
431
506
  "Needs you · 1) 1 unexplained deployment change this week; Investigate on the alert 2) reboot dmz to load installed updates 3) 1 CVE with a fixed version still open after 3 days (dmz); unattended-upgrades may be stuck 4) renew the RADIUS CRL (wifi-device.sh renew-crl), 23 days left 5) no Proxmox backup within 48 h for observability 6) the restic check ended with exit-code",
432
507
  );
433
508
  expect(out.text).toContain("RADIUS CRL: renew within 23 days (warned 19 Oct)");
509
+ const offers = imageOffers([{ stack: "mealie", lines: [{ ref: "ghcr.io/mealie-recipes/mealie:latest" }], actionable: true, state: "offered" }]);
510
+ const withImages = weeklyText({ ...base, offers, images: { scanned: 57, total: 57, critical: [{ id: "CVE-9", pkg: "openssl", image: "ghcr.io/mealie-recipes/mealie:1" }] } }, [OWNER]);
511
+ expect(withImages.text).toContain("Needs you · 1) update mealie in the Arcane Daily Report: the newer image replaces one with a critical CVE");
512
+ expect(weeklyText({ ...base, images: null }, [OWNER]).text).toContain("1) 1 check of this report failed");
434
513
  expect(out.text.endsWith(`<@${OWNER}>`)).toBe(true);
435
514
  });
436
515
 
@@ -2,6 +2,7 @@ import { readFileSync, renameSync, writeFileSync } from "node:fs";
2
2
  import { join } from "node:path";
3
3
  import type { ChangeOrigin } from "../../specialists/lib/change-origin.ts";
4
4
  import type { AlertSummary, VulnEvent, WazuhEvent } from "../../specialists/security/run.ts";
5
+ import { parseImageRef } from "../../specialists/lib/registry.ts";
5
6
  import { configDir } from "./config.ts";
6
7
  import { escapeDiscord } from "./media.ts";
7
8
  import type { MessageComponent } from "./rest.ts";
@@ -124,6 +125,8 @@ export interface SecurityState {
124
125
  vulns: { reported: Record<string, ReportedVuln>; processedUntil: string | null; since: string | null };
125
126
  /** Ban totals of earlier weekly reports: CrowdSec keeps alerts for 7 days, so last week exists only here. */
126
127
  weeks: { date: string; bans: number }[];
128
+ /** The newest NAS MEDIA loss (rule 100012) and return (100013); the Arcane container watch expects media restarts around them. */
129
+ nas: { lostAt: string | null; restoredAt: string | null };
127
130
  }
128
131
 
129
132
  export function emptyState(): SecurityState {
@@ -139,6 +142,7 @@ export function emptyState(): SecurityState {
139
142
  poll: { failures: 0, warned: false },
140
143
  vulns: { reported: {}, processedUntil: null, since: null },
141
144
  weeks: [],
145
+ nas: { lostAt: null, restoredAt: null },
142
146
  };
143
147
  }
144
148
 
@@ -164,6 +168,23 @@ export function writeSecurityState(state: SecurityState): void {
164
168
 
165
169
  const DAY = 86_400_000;
166
170
 
171
+ /** Keeps the newest NAS loss and return the checks have seen. */
172
+ export function noteNas(state: SecurityState, e: WazuhEvent): void {
173
+ const key = e.rule === "100012" ? "lostAt" : e.rule === "100013" ? "restoredAt" : null;
174
+ if (key && (state.nas[key] === null || Date.parse(e.timestamp) > Date.parse(state.nas[key] as string))) state.nas[key] = e.timestamp;
175
+ }
176
+
177
+ /** The media watchdog restarts the media containers once access returns, within a few minutes. */
178
+ const NAS_SETTLE_MS = 30 * 60_000;
179
+
180
+ /** Whether the NAS share is lost now, or came back within the last half hour. */
181
+ export function nasOutage(nas: SecurityState["nas"], now: number): boolean {
182
+ const lost = nas.lostAt ? Date.parse(nas.lostAt) : null;
183
+ const restored = nas.restoredAt ? Date.parse(nas.restoredAt) : null;
184
+ if (lost !== null && (restored === null || restored < lost)) return true;
185
+ return restored !== null && now - restored < NAS_SETTLE_MS;
186
+ }
187
+
167
188
  /** Drops what no check, report, or button can need any more. */
168
189
  export function pruneState(state: SecurityState, now: number): void {
169
190
  const keep = (iso: string, days: number) => Date.parse(iso) >= now - days * DAY;
@@ -545,6 +566,98 @@ export function patchLine(closed: readonly VulnEvent[] | null, label = "Patches"
545
566
  return `${label} · ${shown.join(" · ")}${more}`;
546
567
  }
547
568
 
569
+ // ---------------------------------------------------------------- Arcane image scans
570
+
571
+ /** One critical finding of Arcane's Trivy scan that has a fixed version (`images vulnerabilities list`). */
572
+ export interface ImageVuln {
573
+ id: string;
574
+ pkg: string;
575
+ image: string;
576
+ }
577
+
578
+ export interface ImageVulnFacts {
579
+ scanned: number;
580
+ total: number;
581
+ critical: ImageVuln[];
582
+ }
583
+
584
+ /** What the newest Arcane Daily Report offers for the stack that runs an image. */
585
+ export interface ImageOffer {
586
+ stack: string;
587
+ /** The stack has an Update button that still applies. */
588
+ offered: boolean;
589
+ held?: string;
590
+ }
591
+
592
+ /** `images vulnerabilities summary` plus `list --severity critical --all`; ignored findings and findings without a fix are left out. */
593
+ export function parseImageVulns(summary: unknown, list: unknown): ImageVulnFacts | null {
594
+ const s = summary as { totalImages?: unknown; scannedImages?: unknown } | null;
595
+ const data = (list as { data?: unknown } | null)?.data;
596
+ if (!s || typeof s.totalImages !== "number" || typeof s.scannedImages !== "number" || !Array.isArray(data)) return null;
597
+ const critical: ImageVuln[] = [];
598
+ for (const v of data as Record<string, unknown>[]) {
599
+ if (typeof v.vulnerabilityId !== "string" || typeof v.imageName !== "string" || v.ignored === true) continue;
600
+ if (typeof v.fixedVersion !== "string" || v.fixedVersion.length === 0) continue;
601
+ critical.push({ id: v.vulnerabilityId, pkg: typeof v.pkgName === "string" ? v.pkgName : "?", image: v.imageName });
602
+ }
603
+ return { scanned: s.scannedImages, total: s.totalImages, critical };
604
+ }
605
+
606
+ function repositoryOf(ref: string): string | null {
607
+ try {
608
+ const r = parseImageRef(ref);
609
+ return `${r.registry}/${r.repository}`;
610
+ } catch {
611
+ return null;
612
+ }
613
+ }
614
+
615
+ /** Image repository to the stack that runs it, from the stacks the newest Arcane Daily Report shows. */
616
+ export function imageOffers(stacks: readonly { stack: string; lines: { ref?: string }[]; actionable: boolean; state: string; held?: string }[]): Map<string, ImageOffer> {
617
+ const out = new Map<string, ImageOffer>();
618
+ for (const s of stacks) {
619
+ for (const l of s.lines) {
620
+ const repo = l.ref ? repositoryOf(l.ref) : null;
621
+ if (repo) out.set(repo, { stack: s.stack, offered: s.actionable && s.state === "offered", ...(s.held ? { held: s.held } : {}) });
622
+ }
623
+ }
624
+ return out;
625
+ }
626
+
627
+ function offerText(offer: ImageOffer | undefined, arcaneChannelId: string | undefined): string {
628
+ if (offer?.offered) return `update offered: Update on ${escapeDiscord(offer.stack)} in the Arcane Daily Report${arcaneChannelId ? ` (<#${arcaneChannelId}>)` : ""}`;
629
+ if (offer?.held) return `${escapeDiscord(offer.stack)} is held: ${escapeDiscord(offer.held)}`;
630
+ return "no newer image offered yet";
631
+ }
632
+
633
+ /** Images with critical CVEs that an update in the newest Arcane Daily Report would replace. */
634
+ export function offeredImages(facts: ImageVulnFacts | null, offers: ReadonlyMap<string, ImageOffer>): string[] {
635
+ if (!facts) return [];
636
+ const stacks = new Set<string>();
637
+ for (const v of facts.critical) {
638
+ const offer = offers.get(repositoryOf(v.image) ?? "");
639
+ if (offer?.offered) stacks.add(offer.stack);
640
+ }
641
+ return [...stacks].sort();
642
+ }
643
+
644
+ export function imageVulnLine(facts: ImageVulnFacts | null, offers: ReadonlyMap<string, ImageOffer>, arcaneChannelId?: string, maxShown = 3): string {
645
+ if (!facts) return "Arcane images · the check failed";
646
+ if (facts.scanned === 0) return `Arcane images · no Trivy scan yet (${facts.total} images)`;
647
+ if (facts.critical.length === 0) return `Arcane images · no critical CVE with a fix (${facts.scanned} of ${facts.total} images scanned)`;
648
+ const byImage = new Map<string, ImageVuln[]>();
649
+ for (const v of facts.critical) byImage.set(v.image, [...(byImage.get(v.image) ?? []), v]);
650
+ const groups = [...byImage.entries()].sort((x, y) => y[1].length - x[1].length || x[0].localeCompare(y[0]));
651
+ const shown = groups.slice(0, maxShown).map(([image, vulns]) => {
652
+ const named = vulns.slice(0, 2).map((v) => `${escapeDiscord(v.id)} (${escapeDiscord(v.pkg)})`);
653
+ const rest = vulns.length > 2 ? `, +${vulns.length - 2} more` : "";
654
+ return `${escapeDiscord(image)}: ${named.join(", ")}${rest}; ${offerText(offers.get(repositoryOf(image) ?? ""), arcaneChannelId)}`;
655
+ });
656
+ const more = groups.length > maxShown ? ` · +${groups.length - maxShown} more images` : "";
657
+ const n = facts.critical.length;
658
+ return `Arcane images · ${n} critical CVE${n === 1 ? "" : "s"} with a fix in ${groups.length} image${groups.length === 1 ? "" : "s"}: ${shown.join(" · ")}${more}`;
659
+ }
660
+
548
661
  // ---------------------------------------------------------------- reports
549
662
 
550
663
  export interface DailyData {
@@ -555,6 +668,10 @@ export interface DailyData {
555
668
  found: VulnEvent[] | null;
556
669
  closed: VulnEvent[] | null;
557
670
  attention: Attention | null;
671
+ images: ImageVulnFacts | null;
672
+ offers: ReadonlyMap<string, ImageOffer>;
673
+ /** The Arcane Daily Report's channel, linked from offered updates. */
674
+ arcaneChannelId?: string;
558
675
  }
559
676
 
560
677
  function headline(urgent: number, unexplained: number, health: number, failed: number): string {
@@ -568,7 +685,7 @@ function headline(urgent: number, unexplained: number, health: number, failed: n
568
685
  }
569
686
 
570
687
  function failures(d: DailyData): number {
571
- return [d.crowdsec === null, d.wazuh.failed, d.found === null, d.attention === null].filter(Boolean).length;
688
+ return [d.crowdsec === null, d.wazuh.failed, d.found === null, d.attention === null, d.images === null].filter(Boolean).length;
572
689
  }
573
690
 
574
691
  /** Owners are tagged only while something is open: a health issue or a check that failed. */
@@ -578,7 +695,7 @@ export function dailyNeedsOwner(d: DailyData): boolean {
578
695
 
579
696
  export function dailyText(d: DailyData, owners: readonly string[]): string {
580
697
  const head = `**${DAILY_PREFIX}** ${headline(d.wazuh.urgent, d.wazuh.unexplained.length, d.attention?.issues.length ?? 0, failures(d))} (24 h to ${d.until})`;
581
- const lines = [head, crowdsecDailyLine(d.crowdsec), d.wazuh.line, vulnLine(d.found), patchLine(d.closed), healthLine(d.attention)];
698
+ const lines = [head, crowdsecDailyLine(d.crowdsec), d.wazuh.line, vulnLine(d.found), patchLine(d.closed), imageVulnLine(d.images, d.offers, d.arcaneChannelId), healthLine(d.attention)];
582
699
  if (dailyNeedsOwner(d)) lines.push(mentionLine(owners));
583
700
  return lines.join("\n");
584
701
  }
@@ -613,6 +730,9 @@ export interface WeeklyData {
613
730
  attention: Attention | null;
614
731
  crl: { days: number; warned: string } | null | "unknown";
615
732
  backups: BackupFacts | null;
733
+ images: ImageVulnFacts | null;
734
+ offers: ReadonlyMap<string, ImageOffer>;
735
+ arcaneChannelId?: string;
616
736
  last24h: { bans: number | null; wazuh: number | null; urgent: number; found: number | null; closed: number | null; health: number | null };
617
737
  }
618
738
 
@@ -688,13 +808,15 @@ export function needsYou(d: WeeklyData): string[] {
688
808
  if (d.patching && d.patching.rebootPending.length > 0) items.push(`reboot ${d.patching.rebootPending.map(escapeDiscord).join(", ")} to load installed updates`);
689
809
  const stuck = d.openWithFix.filter((v) => d.now - Date.parse(v.since) > 3 * DAY);
690
810
  if (stuck.length > 0) items.push(`${stuck.length} CVE${stuck.length === 1 ? "" : "s"} with a fixed version still open after 3 days (${[...new Set(stuck.map((v) => v.agent))].map(escapeDiscord).join(", ")}); unattended-upgrades may be stuck`);
811
+ const offered = offeredImages(d.images, d.offers);
812
+ if (offered.length > 0) items.push(`update ${offered.map(escapeDiscord).join(", ")} in the Arcane Daily Report: the newer image replaces one with a critical CVE`);
691
813
  if (d.crl !== null && d.crl !== "unknown") items.push(`renew the RADIUS CRL (wifi-device.sh renew-crl), ${d.crl.days} days left`);
692
814
  if (d.backups) {
693
815
  const stale = d.backups.guests.filter((g) => d.now / 1000 - g.last > STALE_BACKUP_S);
694
816
  if (stale.length > 0) items.push(`no Proxmox backup within 48 h for ${stale.map((g) => escapeDiscord(g.name)).join(", ")}`);
695
817
  if (d.backups.restic && d.backups.restic.result !== "success") items.push(`the restic check ended with ${escapeDiscord(d.backups.restic.result)}`);
696
818
  }
697
- const failed = [d.crowdsec === null, d.wazuh.failed, d.found === null, d.patching === null, d.attention === null, d.backups === null].filter(Boolean).length;
819
+ const failed = [d.crowdsec === null, d.wazuh.failed, d.found === null, d.patching === null, d.attention === null, d.backups === null, d.images === null].filter(Boolean).length;
698
820
  if (failed > 0) items.push(`${failed} check${failed === 1 ? "" : "s"} of this report failed; the bridge log on Cortex has the reason`);
699
821
  return items;
700
822
  }
@@ -719,6 +841,7 @@ export function weeklyText(d: WeeklyData, owners: readonly string[]): { text: st
719
841
  d.wazuh.line,
720
842
  `Vulnerabilities · ${found} · ${closed} · ${fixable} open with a fixed version${tracking}`,
721
843
  patchingLine(d.patching, d.closed, d.timeZone),
844
+ imageVulnLine(d.images, d.offers, d.arcaneChannelId),
722
845
  `${healthLine(d.attention)} · ${crlText(d.crl, d.timeZone)}`,
723
846
  backupLine(d.backups, d.now, d.timeZone),
724
847
  `Needs you · ${items.length === 0 ? "nothing" : items.map((t, i) => `${i + 1}) ${t}`).join(" ")}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrofoundry/pi-astro",
3
- "version": "0.37.1",
3
+ "version": "0.37.3",
4
4
  "description": "Personal pi customizations (extensions, subagents, skills, prompts, themes) for the pi coding agent.",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -89,6 +89,8 @@ Public routes (Supabase, Grimoire, Mealie URLs) are `astro.edge`'s `route-probe`
89
89
 
90
90
  `["change-origin", "<path>", "<sha256|->", "<utc>"]` answers whether a deployment-file change Wazuh saw on the Arcane VM (`/opt/docker/<project>/compose.yaml`, `docker-compose.yml`, or `pre-deploy.sh`; rule 100020) is ours. `known` when the new content (its SHA-256 from the alert's `syscheck.sha256_after`), or `-` for a deletion, equals a commit of `<project>/<file>` at origin, any author, CI included; or when our `stack-update`, `manager-upgrade`, `manager-rollback`, or `host db-restore` on that stack ran then (the specialists' audit log). Otherwise `unknown`, with the newest commit touching the file. Arcane syncs the files verbatim, so a GitOps deploy always matches. The Discord bridge calls it for every such alert; an `unknown` change pages the owner.
91
91
 
92
+ `["container-watch"]` lists every container with its state, health, exit code, and uptime; containers started or created in the last 30 minutes also carry the start time, the Compose project, and the `operation` (our stack-update, manager-upgrade, manager-rollback, or host db-restore on that stack) whose run covers the start. The Discord bridge polls it every five minutes to detect restarts and redeploys; when it asks you to investigate a container that keeps restarting, read only and propose the fix.
93
+
92
94
  `["project-status"]` (or with a project name) lists projects with their GitOps sync: commit, state, hook state, without error text. `["deployment", "check"|"wait", "<project>", "--commit", "<sha>", "--container", "NAME=sha256:ID", ...]` compares the sync and each container's project, image ID, state, and health; `wait` polls every 10 s up to `--timeout` (default 600 s). Read `status` in the answer.
93
95
 
94
96
  ## Output
@@ -25,7 +25,7 @@ The `security` tool is read-only. Every call is a fixed command on the guest; `[
25
25
  | `wazuh-log <lines>` | manager container log |
26
26
  | `remote-hosts` | `host/program.log` names in the archive |
27
27
  | `remote-log <host> <program> <lines>` | tail of one archived log, e.g. `remote-log frontdoor-1337 nginx-stream 200` |
28
- | `journal <unit> <since>` | `crowdsec`, `rsyslog`, `nftables`, `docker`, `security-attention`, `update-home-whitelist`, `ssh` |
28
+ | `journal <unit> <since>` | `crowdsec`, `rsyslog`, `nftables`, `docker`, `update-home-whitelist`, `ssh` |
29
29
 
30
30
  ```json
31
31
  { "args": ["attention"] }
@@ -6,6 +6,7 @@ import { buildHostRemote, compareAudit, HOST_COMMANDS, type AuditFile } from "..
6
6
  import { type CommitVersion, coveringOperation, judgeChange, parseChangeQuery, readTail } from "../lib/change-origin.ts";
7
7
  import { failedChecks, MANAGER_COMPOSE, MANAGER_IMAGE, normalizeVersion, olderThan, pinCompose, projectSync, syncReady, utcStamp, type VerifyCheck } from "../lib/arcane-manager.ts";
8
8
  import { readConfig } from "../lib/config.ts";
9
+ import { isRecent, listContainer, type WatchedContainer, withDetail } from "../lib/container-watch.ts";
9
10
  import { containerFacts, deploymentChecks, listRows, parseDeploymentArgs, syncFacts, type DeploymentExpectation } from "../lib/deployment.ts";
10
11
  import { ServiceError, UsageError } from "../lib/errors.ts";
11
12
  import { withLock } from "../lib/lock.ts";
@@ -116,6 +117,9 @@ ${Object.entries(HOST_COMMANDS)
116
117
  docker-compose.yml, pre-deploy.sh) is ours: known when the new content, or the deletion,
117
118
  is a commit of <project>/<file> in the GitOps repository, or when our stack-update,
118
119
  manager-upgrade, manager-rollback, or host db-restore on that stack ran then (audit log)
120
+ container-watch every container's state, health, exit code, and uptime; for those started or created in
121
+ the last 30 minutes also the start time, the Compose project, and our stack-update,
122
+ manager-upgrade, manager-rollback, or host db-restore whose run covers the start (JSON)
119
123
  release-notes <stack>[/<service>]
120
124
  GitHub release notes between the running and the latest version of one service's image,
121
125
  found through the image's org.opencontainers.image.source label
@@ -303,6 +307,29 @@ async function changeOrigin(config: ArcaneConfig, args: string[]): Promise<numbe
303
307
  return 0;
304
308
  }
305
309
 
310
+ /** Container state for the Discord bridge's restart and redeploy checks: one list, plus details only for recent starts. */
311
+ async function containerWatch(config: ArcaneConfig, args: string[]): Promise<number> {
312
+ if (args.length > 0) throw new UsageError("container-watch takes no arguments");
313
+ let audit: string;
314
+ try {
315
+ audit = readTail(auditLogPath(), AUDIT_TAIL_BYTES);
316
+ } catch {
317
+ audit = "";
318
+ }
319
+ const now = Date.now();
320
+ const containers = await withCli(config, async (cli) => {
321
+ const out: WatchedContainer[] = [];
322
+ for (const row of listRows(await cliJson(cli, ["containers", "list", "--all"]))) {
323
+ const c = listContainer(row);
324
+ if (!c) continue;
325
+ out.push(isRecent(c, now) ? withDetail(c, await cliJson(cli, ["containers", "get", c.id]), audit) : c);
326
+ }
327
+ return out;
328
+ });
329
+ printJson({ checkedAt: new Date(now).toISOString(), containers });
330
+ return 0;
331
+ }
332
+
306
333
  // ---------------------------------------------------------------- Arcane VM
307
334
 
308
335
  const NAME = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,127}$/;
@@ -742,6 +769,7 @@ export async function command(args: string[]): Promise<number> {
742
769
  if (args[0] === "update-report") return updateReport(config, args.slice(1));
743
770
  if (args[0] === "release-notes") return releaseNotes(config, args.slice(1));
744
771
  if (args[0] === "change-origin") return changeOrigin(config, args.slice(1));
772
+ if (args[0] === "container-watch") return containerWatch(config, args.slice(1));
745
773
  if (args[0] === "host") return hostCommand(config, args.slice(1));
746
774
  if (args[0] === "project-status") return projectStatus(config, args.slice(1));
747
775
  if (args[0] === "deployment") return deployment(config, args.slice(1));
@@ -47,7 +47,7 @@ case "$name" in
47
47
  sudo -n tail -n "$3" "/var/log/remote/$1/$2.log" ;;
48
48
  journal)
49
49
  unit="${rest%% *}"; since="${rest#* }"
50
- case "$unit" in crowdsec|rsyslog|nftables|docker|security-attention|update-home-whitelist|ssh) ;; *) echo "bad unit" >&2; exit 2 ;; esac
50
+ case "$unit" in crowdsec|rsyslog|nftables|docker|update-home-whitelist|ssh) ;; *) echo "bad unit" >&2; exit 2 ;; esac
51
51
  since_ok "$since" || { echo "bad since" >&2; exit 2; }
52
52
  sudo -n journalctl -u "$unit" --since "$since" --no-pager -n 200 ;;
53
53
  *) echo "refused: $cmd" >&2; exit 2 ;;