@astrofoundry/pi-astro 0.40.41 โ†’ 0.40.42

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.
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  escalationText,
3
+ hostRestartText,
3
4
  observe,
4
5
  parseSnapshot,
5
6
  readWatchState,
@@ -98,6 +99,10 @@ export class ArcaneWatch {
98
99
  const now = this.deps.now();
99
100
  const seen = observe(state, snapshot, now, this.deps.nasOutage(now));
100
101
  for (const note of seen.notes) this.deps.log(`arcane watch: ${note}`);
102
+ for (const e of seen.hostRestarts) {
103
+ this.deps.log(`arcane watch: ${e.count} of ${e.total} containers restarted between ${e.from} and ${e.to}`);
104
+ await this.post(s.channelId, { content: hostRestartText(e, s.timeZone) });
105
+ }
101
106
  for (const u of seen.unhealthy) {
102
107
  const posted = await this.post(s.channelId, { content: unhealthyText(u, owners, s.timeZone, now), mentionUsers: owners });
103
108
  if (posted) this.deps.log(`arcane watch: ${u.name} ${u.problem} after a redeploy`);
@@ -4,7 +4,7 @@ import { join } from "node:path";
4
4
  import { afterEach, beforeEach, describe, expect, it } from "vitest";
5
5
  import type { WatchedContainer } from "../../specialists/lib/container-watch.ts";
6
6
  import { ArcaneWatch } from "./arcane-watch-monitor.ts";
7
- import { emptyWatchState, escalationText, healthProblem, observe, parseSnapshot, readWatchState, restartInvestigationTask, unhealthyText, type WatchState } from "./arcane-watch.ts";
7
+ import { emptyWatchState, escalationText, healthProblem, hostRestartText, observe, parseSnapshot, readWatchState, restartInvestigationTask, unhealthyText, type WatchState } from "./arcane-watch.ts";
8
8
  import type { DiscordConfig } from "./config.ts";
9
9
  import type { CreateMessage } from "./rest.ts";
10
10
  import type { InvestigationRequest } from "./security-monitor.ts";
@@ -34,7 +34,7 @@ describe("container watch rules", () => {
34
34
  it("only records a baseline the first time", () => {
35
35
  const state = emptyWatchState();
36
36
  const out = observe(state, [ct({ startedAt: iso(T0 - MIN) })], T0, false);
37
- expect(out).toEqual({ escalations: [], unhealthy: [], notes: [] });
37
+ expect(out).toEqual({ escalations: [], unhealthy: [], hostRestarts: [], notes: [] });
38
38
  expect(state.containers.syncthing).toEqual({ id: "a", imageId: "sha256:1", startedAt: iso(T0 - MIN), seenAt: iso(T0) });
39
39
  });
40
40
 
@@ -70,6 +70,33 @@ describe("container watch rules", () => {
70
70
  expect(state.restarts).toEqual({});
71
71
  });
72
72
 
73
+ it("counts a restart of most containers as one host event, also when two checks split it", () => {
74
+ const four = ["a", "b", "c", "d"].map((name) => ct({ name, id: name }));
75
+ const state = seenAt(four, T0);
76
+ const early = iso(T0 + 4 * MIN + 50_000);
77
+ observe(state, [ct({ name: "a", id: "a", startedAt: early }), ...four.slice(1)], T0 + 5 * MIN, false);
78
+ expect(state.restarts).toEqual({ a: [early] });
79
+ const together = iso(T0 + 5 * MIN + 10_000);
80
+ const out = observe(state, [ct({ name: "a", id: "a", startedAt: early }), ct({ name: "b", id: "b", startedAt: together }), ct({ name: "c", id: "c", startedAt: together }), four[3]], T0 + 5 * MIN + 40_000, false);
81
+ expect(out.hostRestarts).toEqual([{ count: 2, total: 4, from: together, to: together }]);
82
+ expect(state.restarts).toEqual({});
83
+ const late = iso(T0 + 5 * MIN + 45_000);
84
+ const after = observe(state, [ct({ name: "a", id: "a", startedAt: early }), ct({ name: "b", id: "b", startedAt: together }), ct({ name: "c", id: "c", startedAt: together }), ct({ name: "d", id: "d", startedAt: late })], T0 + 10 * MIN, false);
85
+ expect(after.hostRestarts).toEqual([]);
86
+ expect(after.notes).toEqual(["d restarted with a host-wide restart"]);
87
+ expect(state.restarts).toEqual({});
88
+ const alone = iso(T0 + 19 * MIN);
89
+ observe(state, [ct({ name: "a", id: "a", startedAt: alone }), ct({ name: "b", id: "b", startedAt: together }), ct({ name: "c", id: "c", startedAt: together }), ct({ name: "d", id: "d", startedAt: late })], T0 + 20 * MIN, false);
90
+ expect(state.restarts).toEqual({ a: [alone] });
91
+ });
92
+
93
+ it("forgets host events after a day", () => {
94
+ const state = seenAt([ct()], T0);
95
+ state.hostRestarts = [{ from: iso(T0 - 25 * 60 * MIN), to: iso(T0 - 25 * 60 * MIN) }];
96
+ observe(state, [ct()], T0 + 5 * MIN, false);
97
+ expect(state.hostRestarts).toEqual([]);
98
+ });
99
+
73
100
  it("checks a redeployed container after five minutes and reports what is wrong", () => {
74
101
  const state = seenAt([ct({ name: "mealie", id: "old", imageId: "sha256:1", project: "mealie" })], T0);
75
102
  const started = T0 + MIN;
@@ -125,6 +152,12 @@ describe("container watch texts", () => {
125
152
  );
126
153
  });
127
154
 
155
+ it("names a host-wide restart without tagging anyone", () => {
156
+ expect(hostRestartText({ count: 46, total: 52, from: iso(T0), to: iso(T0 + 8_000) }, TZ)).toBe(
157
+ "๐Ÿ” **Host-wide restart** ยท 46 of 52 containers restarted at 22 Oct 10:00 CEST, so Docker or the Arcane VM restarted. They do not count as container restarts.",
158
+ );
159
+ });
160
+
128
161
  it("asks the specialist to read only, within the host's one-day window", () => {
129
162
  const now = T0 + 24 * 60 * MIN;
130
163
  const task = restartInvestigationTask({ name: "syncthing", restarts: [iso(T0 + MIN), iso(now - MIN)] }, now);
@@ -219,6 +252,22 @@ describe("container watch monitor", () => {
219
252
  expect(h.investigations[0].task).toContain("restarted 3 times");
220
253
  });
221
254
 
255
+ it("posts a host-wide restart once, untagged, and investigates nothing", async () => {
256
+ const h = harness();
257
+ const three = ["a", "b", "c"].map((name) => ct({ name, id: name }));
258
+ h.answer.next = () => JSON.stringify({ containers: three });
259
+ await h.watch.poll(SCHEDULE);
260
+ h.clock.now = T0 + 5 * MIN;
261
+ const started = iso(T0 + 4 * MIN);
262
+ h.answer.next = () => JSON.stringify({ containers: three.map((c) => ({ ...c, startedAt: started })) });
263
+ await h.watch.poll(SCHEDULE);
264
+ expect(h.messages).toHaveLength(1);
265
+ expect(h.messages[0].message.content).toContain("๐Ÿ” **Host-wide restart** ยท 3 of 3 containers");
266
+ expect(h.messages[0].message.mentionUsers).toBeUndefined();
267
+ expect(h.investigations).toEqual([]);
268
+ expect(readWatchState().restarts).toEqual({});
269
+ });
270
+
222
271
  it("warns once after 30 minutes of failed checks and says when they work again", async () => {
223
272
  const h = harness();
224
273
  h.answer.next = () => "not json";
@@ -17,6 +17,10 @@ export const HEALTH_GRACE_MS = 5 * MINUTE;
17
17
  const STARTING_LIMIT_MS = 15 * MINUTE;
18
18
  /** The only stack that mounts the NAS share (`nas_media` in the arcane repository's media/compose.yaml). */
19
19
  export const NAS_STACKS: readonly string[] = ["media"];
20
+ /** When at least this share of the containers restarted since the last check, Docker or the VM restarted them all: one host event, no container's count. */
21
+ export const HOST_RESTART_SHARE = 0.5;
22
+ /** Restarts this close to a host event belong to it, so an event that two checks split counts nowhere. */
23
+ const HOST_RESTART_MARGIN_MS = 2 * MINUTE;
20
24
 
21
25
  export interface WatchState {
22
26
  /** The last observation of each container by name. */
@@ -27,9 +31,18 @@ export interface WatchState {
27
31
  escalated: Record<string, string>;
28
32
  /** Redeployed containers whose health is checked once the grace time has passed. */
29
33
  pending: PendingHealth[];
34
+ /** Host events of the last 24 hours, from the first to the last restart they covered. */
35
+ hostRestarts: { from: string; to: string }[];
30
36
  poll: { failures: number; warned: boolean };
31
37
  }
32
38
 
39
+ export interface HostRestart {
40
+ count: number;
41
+ total: number;
42
+ from: string;
43
+ to: string;
44
+ }
45
+
33
46
  export interface PendingHealth {
34
47
  name: string;
35
48
  id: string;
@@ -56,12 +69,13 @@ export interface Unhealthy {
56
69
  export interface Observation {
57
70
  escalations: Escalation[];
58
71
  unhealthy: Unhealthy[];
59
- /** Log lines for what was seen but is expected: our operations and NAS outages. */
72
+ hostRestarts: HostRestart[];
73
+ /** Log lines for what was seen but is expected: our operations, NAS outages, and host events. */
60
74
  notes: string[];
61
75
  }
62
76
 
63
77
  export function emptyWatchState(): WatchState {
64
- return { containers: {}, restarts: {}, escalated: {}, pending: [], poll: { failures: 0, warned: false } };
78
+ return { containers: {}, restarts: {}, escalated: {}, pending: [], hostRestarts: [], poll: { failures: 0, warned: false } };
65
79
  }
66
80
 
67
81
  function statePath(): string {
@@ -109,14 +123,17 @@ export function healthProblem(c: WatchedContainer, sinceMs: number): string | nu
109
123
  /**
110
124
  * Compares a fresh snapshot with the last one: a new id is a redeploy, whose
111
125
  * health is checked after the grace time; a newer start time with the same id
112
- * is a restart. The first snapshot only records the baseline.
126
+ * is a restart. The first snapshot only records the baseline. When most
127
+ * containers restarted at once, that is one host event and no container's
128
+ * restart.
113
129
  */
114
130
  export function observe(state: WatchState, snapshot: readonly WatchedContainer[], now: number, nasOutage: boolean): Observation {
115
- const out: Observation = { escalations: [], unhealthy: [], notes: [] };
131
+ const out: Observation = { escalations: [], unhealthy: [], hostRestarts: [], notes: [] };
116
132
  const baseline = Object.keys(state.containers).length === 0;
117
133
  const at = new Date(now).toISOString();
118
134
  const nasExpected = (c: { project?: string }) => nasOutage && c.project !== undefined && NAS_STACKS.includes(c.project);
119
135
  const current = new Map(snapshot.map((c) => [c.name, c]));
136
+ const restarted: { name: string; startedAt: string }[] = [];
120
137
  for (const c of snapshot) {
121
138
  const prev = state.containers[c.name];
122
139
  if (prev && !baseline) {
@@ -127,13 +144,28 @@ export function observe(state: WatchState, snapshot: readonly WatchedContainer[]
127
144
  } else if (c.startedAt && c.startedAt !== prev.startedAt && Date.parse(c.startedAt) > Date.parse(prev.seenAt)) {
128
145
  if (c.operation) out.notes.push(`${c.name} restarted during ${operationText(c.operation)}`);
129
146
  else if (nasExpected(c)) out.notes.push(`${c.name} restarted during a NAS outage`);
130
- else state.restarts[c.name] = [...(state.restarts[c.name] ?? []), c.startedAt];
147
+ else restarted.push({ name: c.name, startedAt: c.startedAt });
131
148
  }
132
149
  }
133
150
  const startedAt = c.startedAt ?? (prev?.id === c.id ? prev.startedAt : null);
134
151
  state.containers[c.name] = { id: c.id, imageId: c.imageId, startedAt, seenAt: at };
135
152
  }
136
153
  for (const name of Object.keys(state.containers)) if (!current.has(name)) delete state.containers[name];
154
+ state.hostRestarts = state.hostRestarts.filter((e) => Date.parse(e.to) >= now - DAY);
155
+ if (restarted.length >= Math.max(2, Math.ceil(snapshot.length * HOST_RESTART_SHARE))) {
156
+ const times = restarted.map((r) => Date.parse(r.startedAt));
157
+ const event = { from: new Date(Math.min(...times)).toISOString(), to: new Date(Math.max(...times)).toISOString() };
158
+ state.hostRestarts.push(event);
159
+ out.hostRestarts.push({ count: restarted.length, total: snapshot.length, ...event });
160
+ restarted.length = 0;
161
+ }
162
+ const inHostRestart = (t: string) => state.hostRestarts.some((e) => Date.parse(t) >= Date.parse(e.from) - HOST_RESTART_MARGIN_MS && Date.parse(t) <= Date.parse(e.to) + HOST_RESTART_MARGIN_MS);
163
+ for (const r of restarted) {
164
+ if (inHostRestart(r.startedAt)) out.notes.push(`${r.name} restarted with a host-wide restart`);
165
+ else state.restarts[r.name] = [...(state.restarts[r.name] ?? []), r.startedAt];
166
+ }
167
+ // A check that saw only the first restarts of an event counted them before the event was known.
168
+ for (const [name, times] of Object.entries(state.restarts)) state.restarts[name] = times.filter((t) => !inHostRestart(t));
137
169
  for (const [name, times] of Object.entries(state.restarts)) {
138
170
  const recent = times.filter((t) => Date.parse(t) >= now - DAY);
139
171
  if (recent.length === 0) delete state.restarts[name];
@@ -186,6 +218,11 @@ export function escalationText(e: Escalation, owners: readonly string[], timeZon
186
218
  ].join("\n");
187
219
  }
188
220
 
221
+ /** Untagged: Docker or the VM restarting is planned maintenance far more often than a fault. */
222
+ export function hostRestartText(e: HostRestart, timeZone: string): string {
223
+ return `๐Ÿ” **Host-wide restart** ยท ${e.count} of ${e.total} containers restarted at ${clockText(e.from, timeZone)}, so Docker or the Arcane VM restarted. They do not count as container restarts.`;
224
+ }
225
+
189
226
  export function unhealthyText(u: Unhealthy, owners: readonly string[], timeZone: string, now: number): string {
190
227
  const minutes = Math.round((now - Date.parse(u.since)) / MINUTE);
191
228
  return [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrofoundry/pi-astro",
3
- "version": "0.40.41",
3
+ "version": "0.40.42",
4
4
  "description": "Personal pi customizations (extensions, subagents, skills, prompts, themes) for the pi coding agent.",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -53,7 +53,7 @@ Wrappers ship as TypeScript and run under Node 24 type stripping: erasable synta
53
53
  - `inference` reaches Nexus through one forced-command key. The Europa check is HTTP only until Windows OpenSSH forced commands are verified.
54
54
  - `REFUSED_PREFIXES` protects only the wrapper itself (`config`, `auth`, `self-update`, `completion`). The GitOps-first rule for managed projects lives in the skill and the agent prompt, not in code, because unmanaged projects and emergencies need the direct CLI.
55
55
  - The wrapper's repository commands are the `git` group; `gitops` must stay free because it is an `arcane-cli` command group (server-side syncs).
56
- - Every passthrough call writes a private per-call copy of `arcanecli.yml` with `jwt_token` set, because some `arcane-cli` commands validate the config file before reading `ARCANE_TOKEN`. The copy is removed after the call.
56
+ - Every passthrough call writes a private per-call copy of `arcanecli.yml` with `jwt_token` set, because some `arcane-cli` commands validate the config file before reading `ARCANE_TOKEN`. The copy is removed after the call. arcane-cli prints a warning on stderr that `jwt_token` and `refresh_token` in the config stop working in v3 (in the bridge log since 2026-10-23); `cliFailure` therefore names the exit code and the filtered stdout as well, because a failure can carry nothing else on stderr (the media update on 2026-11-02).
57
57
  - Every wrapper answers `--help` without reading config, so agents can discover subcommands before anything else is set up.
58
58
  - `entry/specialist-install.sh` runs as root, so it runs only `/usr/bin`, `/bin`, `/usr/sbin`, `/sbin` and the Node it installed, never Homebrew or anything else the main user can write; it checks that `/usr/local/libexec/specialist-node` (and each `specialist-<tool>` folder) and its parents belong to root and are writable by nobody else. Arguments are checked before the root test (tests exercise that part). Downloads go to a root-only temporary directory and are verified before use: Node against `SHASUMS256.txt` and the LTS flag in `index.json`, npm tarballs against the registry's `sha512` integrity, arcane-cli against the release's checksums file. Inside `/Users/specialist` it works only as `specialist` (`sudo -u specialist -H`), from a staging directory root hands over, so a link the specialist plants cannot redirect a root write. Each component switches by rename after a check (every installed wrapper's `--help`, `--version` of the new binary) and restores the previous copy when the check fails. Older versions are refused; `rollback` swaps current and previous. `python`, `gcloud`, `bw`, and `jq` follow the same rules: version folders under `/usr/local/libexec/specialist-<tool>/` behind the links `current` and `previous`, sealed `root:wheel` and not writable by others, smoke-tested as `specialist` before `current` moves; `python` refuses a switch that breaks the current gcloud. Consumers use the `current` paths: `provision.sh` for `bw` and `jq`, `edge.json` for `gcloud` and `gcloudPython`. Nothing a wrapper runs with secrets may come from Homebrew.
59
59
  - `main()` waits for stdout and stderr to flush before `process.exit`: on a pipe, exiting right after a write drops everything past the 64 KiB buffer, which cut large answers such as `projects list --all` until 2026-09-25 (`lib/flush.test.ts`).
@@ -212,9 +212,15 @@ async function passthrough(config: ArcaneConfig, args: string[]): Promise<number
212
212
  });
213
213
  }
214
214
 
215
+ /** arcane-cli can fail with only a deprecation warning on stderr; the exit code and stdout then carry the cause. */
216
+ export function cliFailure(args: readonly string[], result: RunResult): string {
217
+ const stdout = filterJsonText(result.stdout, (v) => stripKeys(v, ARCANE_DENIED_KEYS)).replace(/\s+/g, " ").trim().slice(0, 500);
218
+ return [`${args.join(" ")}: exited ${result.code}`, result.stderr.trim(), stdout && `stdout: ${stdout}`].filter((part) => part.length > 0).join("; ");
219
+ }
220
+
215
221
  async function cliJson(cli: Cli, args: string[]): Promise<unknown> {
216
222
  const result = await cli(["--output", "json", ...args]);
217
- if (result.code !== 0) throw new ServiceError(`${args.join(" ")}: ${result.stderr.trim() || `exited ${result.code}`}`);
223
+ if (result.code !== 0) throw new ServiceError(cliFailure(args, result));
218
224
  try {
219
225
  return JSON.parse(result.stdout);
220
226
  } catch {
@@ -2,7 +2,7 @@ import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync
2
2
  import { tmpdir } from "node:os";
3
3
  import { join } from "node:path";
4
4
  import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5
- import { refusedPrefix, command as arcaneCommand } from "./arcane/run.ts";
5
+ import { cliFailure, refusedPrefix, command as arcaneCommand } from "./arcane/run.ts";
6
6
  import { buildBackupRemote, command as backupCommand } from "./backup/run.ts";
7
7
  import { buildNodeRemote, parseJoinArgs, parseParams, parseRecordFilters, technitiumRequest, technitiumTarget, command as dnsCommand } from "./dns/run.ts";
8
8
  import { buildVpsRemote, command as edgeCommand, isTransientSshFailure, PUBLIC_ROUTES, routeProbe } from "./edge/run.ts";
@@ -27,6 +27,14 @@ describe("arcane wrapper", () => {
27
27
  expect(refusedPrefix(["containers", "logs", "c1"])).toBeNull();
28
28
  });
29
29
 
30
+ it("names the exit code and stdout when arcane-cli fails with only a warning", () => {
31
+ const warning = "jwt_token and refresh_token in /tmp/x/arcanecli.yml are deprecated and stop working in v3.";
32
+ expect(cliFailure(["images", "updates", "check", "lscr.io/linuxserver/bazarr:latest"], { code: 1, stderr: `${warning}\n`, stdout: '{"success":false,"error":"too many requests","env":["SECRET=1"]}' })).toBe(
33
+ `images updates check lscr.io/linuxserver/bazarr:latest: exited 1; ${warning}; stdout: { "success": false, "error": "too many requests" }`,
34
+ );
35
+ expect(cliFailure(["version"], { code: 2, stderr: "", stdout: "" })).toBe("version: exited 2");
36
+ });
37
+
30
38
  it("keeps gitops writes inside the checkout", () => {
31
39
  const dir = "/repo";
32
40
  expect(safeRepoPath(dir, "mealie/compose.yaml")).toBe("/repo/mealie/compose.yaml");