@astrofoundry/pi-astro 0.40.64 → 0.40.66

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.
@@ -11,7 +11,7 @@ import { containerFacts, deploymentChecks, listRows, parseDeploymentArgs, syncFa
11
11
  import { ServiceError, UsageError } from "../lib/errors.ts";
12
12
  import { withLock } from "../lib/lock.ts";
13
13
  import { main } from "../lib/main.ts";
14
- import { buildUpdateReport, checkEntries, parseTag, rateLimited, resolveRef, stackServices, type CheckEntry, type UpdateRow } from "../lib/image-updates.ts";
14
+ import { buildUpdateReport, checkEntries, parseTag, rateLimited, resolveRef, stackServices, type CheckEntry, type ReportSource, type UpdateRow } from "../lib/image-updates.ts";
15
15
  import { printJson, printRaw } from "../lib/output.ts";
16
16
  import { auditLogPath, specialistsHome } from "../lib/paths.ts";
17
17
  import { run, type RunResult } from "../lib/proc.ts";
@@ -102,7 +102,7 @@ ${Object.entries(HOST_COMMANDS)
102
102
  .filter(([, c]) => !c.internal)
103
103
  .map(([, c]) => ` ${c.help}`)
104
104
  .join("\n")}
105
- update-report [--all] fresh image update check for every stack, with running and latest release versions
105
+ update-report [--all] fresh image update check for every stack of every environment, with running and latest release versions
106
106
  from the registries; JSON with a ready table (--all also lists up-to-date services) and
107
107
  the held stacks and services of ${HOLDS_FILE}. Images the registry throttled (toomanyrequests) are
108
108
  checked again after 5 and 20 seconds. Checks only: never pulls images or redeploys.
@@ -123,7 +123,7 @@ ${Object.entries(HOST_COMMANDS)
123
123
  docker-compose.yml, pre-deploy.sh) is ours: known when the new content, or the deletion,
124
124
  is a commit of <project>/<file> in the GitOps repository, or when our stack-update,
125
125
  manager-upgrade, manager-rollback, or host db-restore on that stack ran then (audit log)
126
- container-watch every container's state, health, exit code, and uptime; for those started or created in
126
+ container-watch every container's state in every environment, health, exit code, and uptime; for those started or created in
127
127
  the last 30 minutes also the start time, the Compose project, and our stack-update,
128
128
  manager-upgrade, manager-rollback, or host db-restore whose run covers the start (JSON)
129
129
  release-notes <stack>[/<service>]
@@ -187,12 +187,16 @@ function writeCallConfig(config: ArcaneConfig, token: string): string {
187
187
 
188
188
  type Cli = (args: string[], timeoutMs?: number) => Promise<{ stdout: string; stderr: string; code: number }>;
189
189
 
190
+ /** The Arcane environment the calls of this invocation target; unset is the manager's own Docker host. Stack commands set it to their stack's environment. */
191
+ let targetEnv: string | undefined;
192
+
190
193
  /** One federated token and one private config copy for every arcane-cli call of this invocation. */
191
- async function withCli<T>(config: ArcaneConfig, fn: (cli: Cli) => Promise<T>): Promise<T> {
194
+ async function withCli<T>(config: ArcaneConfig, fn: (cli: Cli) => Promise<T>, env: string | undefined = targetEnv): Promise<T> {
192
195
  const token = await federatedToken(config);
193
196
  const callConfig = writeCallConfig(config, token);
197
+ const at = env === undefined ? [] : ["--env", env];
194
198
  try {
195
- return await fn((args, timeoutMs = TIMEOUT_MS) => run(config.arcaneCli, ["--config", callConfig, "--no-color", ...args], { env: { ARCANE_TOKEN: token }, timeoutMs }));
199
+ return await fn((args, timeoutMs = TIMEOUT_MS) => run(config.arcaneCli, ["--config", callConfig, "--no-color", ...at, ...args], { env: { ARCANE_TOKEN: token }, timeoutMs }));
196
200
  } finally {
197
201
  rmSync(dirname(callConfig), { recursive: true, force: true });
198
202
  }
@@ -228,30 +232,102 @@ async function cliJson(cli: Cli, args: string[]): Promise<unknown> {
228
232
  }
229
233
  }
230
234
 
235
+ /** The manager's own Docker host, which arcane-cli reaches without --env. */
236
+ const LOCAL_ENV = "0";
237
+
238
+ interface ArcaneEnv {
239
+ id: string;
240
+ name: string;
241
+ }
242
+
243
+ /** Every enabled environment, the manager's own first. */
244
+ async function arcaneEnvs(cli: Cli): Promise<ArcaneEnv[]> {
245
+ const envs = listRows(await cliJson(cli, ["environments", "list"]))
246
+ .filter((e) => e.enabled !== false && typeof e.id === "string" && typeof e.name === "string")
247
+ .map((e) => ({ id: e.id as string, name: e.name as string }));
248
+ return envs.sort((a, b) => Number(b.id === LOCAL_ENV) - Number(a.id === LOCAL_ENV) || a.name.localeCompare(b.name));
249
+ }
250
+
251
+ /** arcane-cli arguments that point one call at an environment. */
252
+ function envArgs(env: ArcaneEnv): string[] {
253
+ return env.id === LOCAL_ENV ? [] : ["--env", env.id];
254
+ }
255
+
256
+ /** The remote environment that runs this stack, or undefined for the manager's own host; stack names are unique across environments. */
257
+ async function stackEnv(config: ArcaneConfig, stack: string): Promise<string | undefined> {
258
+ return withCli(
259
+ config,
260
+ async (cli) => {
261
+ const found: ArcaneEnv[] = [];
262
+ for (const env of await arcaneEnvs(cli)) {
263
+ const projects = listRows(await cliJson(cli, [...envArgs(env), "projects", "list", "--all"]));
264
+ if (projects.some((p) => p.name === stack)) found.push(env);
265
+ }
266
+ if (found.length > 1) throw new ServiceError(`${stack} is a project in several environments: ${found.map((e) => e.name).join(", ")}`);
267
+ return found[0] === undefined || found[0].id === LOCAL_ENV ? undefined : found[0].id;
268
+ },
269
+ undefined,
270
+ );
271
+ }
272
+
231
273
  /** Pauses before each new check of the images the registry throttled. */
232
274
  const THROTTLE_RETRY_WAITS_MS = [5000, 20000];
233
275
 
234
276
  /** A fresh registry check first, then the stack list, so every result the report uses is from this run. */
277
+ async function freshSource(cli: Cli, env: ArcaneEnv): Promise<ReportSource> {
278
+ const at = envArgs(env);
279
+ const check = checkEntries(await cliJson(cli, [...at, "images", "updates", "check", "--all"]));
280
+ for (const wait of THROTTLE_RETRY_WAITS_MS) {
281
+ const limited = rateLimited(check);
282
+ if (limited.length === 0) break;
283
+ await new Promise((r) => setTimeout(r, wait));
284
+ for (const ref of limited) {
285
+ // A retry that fails again keeps the throttled entry for the next round or the report.
286
+ const again = await cliJson(cli, [...at, "images", "updates", "check", ref]).catch(() => null);
287
+ if (again && typeof again === "object" && "hasUpdate" in again) check[ref] = again as CheckEntry;
288
+ }
289
+ }
290
+ const projects = await cliJson(cli, [...at, "projects", "list", "--all"]);
291
+ return { ...(env.id === LOCAL_ENV ? {} : { environment: env.name }), check, projects };
292
+ }
293
+
294
+ /** Every environment's stacks in one report; a remote environment that fails is named in environmentErrors instead of failing the report. */
235
295
  async function updateReport(config: ArcaneConfig, args: string[]): Promise<number> {
236
296
  const extra = args.filter((a) => a !== "--all");
237
297
  if (extra.length > 0) throw new UsageError(`update-report takes only --all, got: ${extra.join(" ")}`);
238
298
  const startedAt = Date.now();
239
299
  const holds = await holdsFromOrigin(config);
240
300
  return withCli(config, async (cli) => {
241
- const check = checkEntries(await cliJson(cli, ["images", "updates", "check", "--all"]));
242
- for (const wait of THROTTLE_RETRY_WAITS_MS) {
243
- const limited = rateLimited(check);
244
- if (limited.length === 0) break;
245
- await new Promise((r) => setTimeout(r, wait));
246
- for (const ref of limited) {
247
- // A retry that fails again keeps the throttled entry for the next round or the report.
248
- const again = await cliJson(cli, ["images", "updates", "check", ref]).catch(() => null);
249
- if (again && typeof again === "object" && "hasUpdate" in again) check[ref] = again as CheckEntry;
301
+ const sources: ReportSource[] = [];
302
+ const environmentErrors: { environment: string; error: string }[] = [];
303
+ // Each remote agent should run the manager's version; manager-upgrade moves only the manager.
304
+ const agentsBehind: { environment: string; agent: string; manager: string }[] = [];
305
+ const versionOf = async (env: ArcaneEnv): Promise<string> => {
306
+ const answer = await cliJson(cli, ["environments", "version", env.id]);
307
+ const current = typeof answer === "object" && answer !== null ? (answer as { currentVersion?: unknown }).currentVersion : undefined;
308
+ return typeof current === "string" ? current : "?";
309
+ };
310
+ const envs = await arcaneEnvs(cli);
311
+ const manager = await versionOf(envs[0]);
312
+ for (const env of envs) {
313
+ try {
314
+ sources.push(await freshSource(cli, env));
315
+ if (env.id === LOCAL_ENV) continue;
316
+ const agent = await versionOf(env);
317
+ if (agent !== manager) agentsBehind.push({ environment: env.name, agent, manager });
318
+ } catch (e) {
319
+ if (env.id === LOCAL_ENV) throw e;
320
+ environmentErrors.push({ environment: env.name, error: e instanceof Error ? e.message : String(e) });
250
321
  }
251
322
  }
252
- const projects = await cliJson(cli, ["projects", "list", "--all"]);
253
- const report = await buildUpdateReport({ check, projects, registry: new HttpRegistry(), startedAt, all: args.includes("--all") });
254
- printJson({ ...report, holds: holds.holds, ...(holds.error ? { holdsError: holds.error } : {}) });
323
+ const report = await buildUpdateReport({ sources, registry: new HttpRegistry(), startedAt, all: args.includes("--all") });
324
+ printJson({
325
+ ...report,
326
+ holds: holds.holds,
327
+ ...(holds.error ? { holdsError: holds.error } : {}),
328
+ ...(environmentErrors.length > 0 ? { environmentErrors } : {}),
329
+ ...(agentsBehind.length > 0 ? { agentsBehind } : {}),
330
+ });
255
331
  return 0;
256
332
  });
257
333
  }
@@ -274,6 +350,7 @@ async function releaseNotes(config: ArcaneConfig, args: string[]): Promise<numbe
274
350
  if (args.length !== 1 || !/^[A-Za-z0-9_.-]+(\/[A-Za-z0-9_.-]+)?$/.test(args[0])) throw new UsageError("release-notes <stack>[/<service>]");
275
351
  const [stack, service] = args[0].split("/");
276
352
  const startedAt = Date.now();
353
+ targetEnv = await stackEnv(config, stack);
277
354
  return withCli(config, async (cli) => {
278
355
  const all = stackServices(await cliJson(cli, ["projects", "list", "--all"]));
279
356
  const matches = all.filter((s) => s.stack === stack && (service === undefined || s.service === service));
@@ -342,16 +419,26 @@ async function containerWatch(config: ArcaneConfig, args: string[]): Promise<num
342
419
  audit = "";
343
420
  }
344
421
  const now = Date.now();
422
+ const environmentErrors: { environment: string; error: string }[] = [];
345
423
  const containers = await withCli(config, async (cli) => {
346
- const out: WatchedContainer[] = [];
347
- for (const row of listRows(await cliJson(cli, ["containers", "list", "--all"]))) {
348
- const c = listContainer(row);
349
- if (!c) continue;
350
- out.push(isRecent(c, now) ? withDetail(c, await cliJson(cli, ["containers", "get", c.id]), audit) : c);
424
+ const out: (WatchedContainer & { environment?: string })[] = [];
425
+ for (const env of await arcaneEnvs(cli)) {
426
+ const at = envArgs(env);
427
+ const where = env.id === LOCAL_ENV ? {} : { environment: env.name };
428
+ try {
429
+ for (const row of listRows(await cliJson(cli, [...at, "containers", "list", "--all"]))) {
430
+ const c = listContainer(row);
431
+ if (!c) continue;
432
+ out.push({ ...(isRecent(c, now) ? withDetail(c, await cliJson(cli, [...at, "containers", "get", c.id]), audit) : c), ...where });
433
+ }
434
+ } catch (e) {
435
+ if (env.id === LOCAL_ENV) throw e;
436
+ environmentErrors.push({ environment: env.name, error: e instanceof Error ? e.message : String(e) });
437
+ }
351
438
  }
352
439
  return out;
353
440
  });
354
- printJson({ checkedAt: new Date(now).toISOString(), containers });
441
+ printJson({ checkedAt: new Date(now).toISOString(), containers, ...(environmentErrors.length > 0 ? { environmentErrors } : {}) });
355
442
  return 0;
356
443
  }
357
444
 
@@ -407,13 +494,19 @@ async function hostCommand(config: ArcaneConfig, args: string[]): Promise<number
407
494
  async function projectStatus(config: ArcaneConfig, args: string[]): Promise<number> {
408
495
  if (args.length > 1 || (args[0] !== undefined && !NAME.test(args[0]))) throw new UsageError("project-status [name]");
409
496
  return withCli(config, async (cli) => {
410
- const projects = listRows(await cliJson(cli, ["projects", "list", "--all"]));
411
- const syncs = listRows(await cliJson(cli, ["gitops", "list", "--all"]));
497
+ const projects: Record<string, unknown>[] = [];
498
+ const syncs: Record<string, unknown>[] = [];
499
+ for (const env of await arcaneEnvs(cli)) {
500
+ const where = env.id === LOCAL_ENV ? {} : { environment: env.name };
501
+ projects.push(...listRows(await cliJson(cli, [...envArgs(env), "projects", "list", "--all"])).map((p) => ({ ...p, ...where })));
502
+ syncs.push(...listRows(await cliJson(cli, [...envArgs(env), "gitops", "list", "--all"])));
503
+ }
412
504
  const picked = args[0] === undefined ? projects : projects.filter((p) => p.name === args[0]);
413
505
  if (args[0] !== undefined && picked.length !== 1) throw new UsageError(`no single project named ${args[0]}`);
414
506
  printJson({
415
507
  projects: picked.map((p) => ({
416
508
  name: p.name,
509
+ ...(p.environment === undefined ? {} : { environment: p.environment }),
417
510
  status: p.status,
418
511
  running: p.runningCount,
419
512
  services: p.serviceCount,
@@ -448,6 +541,7 @@ async function deploymentSnapshot(config: ArcaneConfig, expect: DeploymentExpect
448
541
  /** One federated token per check, because a wait can outlive the token's five minutes. */
449
542
  async function deployment(config: ArcaneConfig, args: string[]): Promise<number> {
450
543
  const expect = parseDeploymentArgs(args);
544
+ targetEnv = await stackEnv(config, expect.project);
451
545
  const deadline = Date.now() + expect.timeoutS * 1000;
452
546
  for (;;) {
453
547
  const snapshot = await deploymentSnapshot(config, expect);
@@ -750,6 +844,7 @@ async function stackUpdate(config: ArcaneConfig, args: string[]): Promise<number
750
844
  const holdsPath = join(dir, HOLDS_FILE);
751
845
  const holds = existsSync(holdsPath) ? parseHolds(readFileSync(holdsPath, "utf-8")) : {};
752
846
  if (holds[stack]) throw new UsageError(`${stack} is held in ${HOLDS_FILE}: ${holds[stack]}`);
847
+ targetEnv = await stackEnv(config, stack);
753
848
 
754
849
  const before = await withCli(config, (cli) => stackState(cli, stack, Date.now()));
755
850
  if (before.stopped) throw new UsageError(`${stack} is stopped; a redeploy would start it, so start it on purpose first`);
@@ -852,6 +947,7 @@ async function stackRollback(config: ArcaneConfig, args: string[]): Promise<numb
852
947
  const repo = await repoStatus(spec, dir);
853
948
  if (repo.changed.length > 0) throw new ServiceError(`the arcane checkout has uncommitted changes: ${repo.changed.join(", ")}`);
854
949
  if (repo.ahead > 0) throw new ServiceError(`the arcane checkout is ${repo.ahead} commit(s) ahead of origin`);
950
+ targetEnv = await stackEnv(config, stack);
855
951
  const layout = await withCli(config, (cli) => stackLayout(cli, stack));
856
952
  if (layout.composePath === null) throw new ServiceError(`${stack} has no single GitOps sync with a Compose path`);
857
953
  const shown = (await git(spec, dir, ["show", "--name-only", "--format=%s", commit])).split("\n").filter(Boolean);
@@ -134,7 +134,7 @@ describe("buildUpdateReport", () => {
134
134
  "busybox:latest": entry("B", "B"),
135
135
  "ghcr.io/astrofoundry/app:main": entry("A1", "A2"),
136
136
  };
137
- const report = await buildUpdateReport({ check, projects, registry, startedAt: NOW, all: false });
137
+ const report = await buildUpdateReport({ sources: [{ check, projects }], registry, startedAt: NOW, all: false });
138
138
  expect(report.counts).toEqual({ update: 3, release: 0, rebuild: 0, unknown: 1, "up-to-date": 2 });
139
139
  expect(report.rows.map((r) => `${r.stack}/${r.service}:${r.status}`)).toEqual(["media/prowlarr:update", "ours/api:update", "ours/worker:update", "empty/empty:unknown"]);
140
140
  expect(report.table).toMatch(/^Fresh check 2026-09-25T18:30:00Z: 6 services in 6 stacks, 4 images\. 1 pinned private images \(1 services\) excluded\./);
@@ -142,13 +142,23 @@ describe("buildUpdateReport", () => {
142
142
  expect(report.rows.some((r) => r.stack === "pinned")).toBe(false);
143
143
  expect(report.table).toContain("media/prowlarr prowlarr 2.0.5.5160-ls123 2.1.0.5190-ls124 pull");
144
144
  expect(report.table).toContain("* empty: Arcane lists no images for this stack");
145
- const all = await buildUpdateReport({ check, projects, registry, startedAt: NOW, all: true });
145
+ const all = await buildUpdateReport({ sources: [{ check, projects }], registry, startedAt: NOW, all: true });
146
146
  expect(all.rows).toHaveLength(6);
147
147
  expect(all.table).toContain("* canary: stack is stopped");
148
148
  expect(all.rows.find((r) => r.stack === "canary")?.stopped).toBe(true);
149
149
  expect(all.rows.find((r) => r.stack === "media")?.stopped).toBeUndefined();
150
150
  });
151
151
 
152
+ it("resolves the same image on each environment's own check and names the remote environment", async () => {
153
+ const registry = new FakeRegistry({ "linuxserver/sonarr": { tags: ["4.0.17.2952-ls300"], digests: { "4.0.17.2952-ls300": "S2" } } });
154
+ const ref = "lscr.io/linuxserver/sonarr:latest";
155
+ const local = { check: { [ref]: entry("S2", "S2") }, projects: { data: [{ name: "media", status: "running", updateInfo: { serviceUpdates: { sonarr: { imageRef: ref } } } }] } };
156
+ const mira = { environment: "mira", check: { [ref]: entry("S1", "S2") }, projects: { data: [{ name: "media-mira", status: "running", updateInfo: { serviceUpdates: { sonarr: { imageRef: ref } } } }] } };
157
+ const report = await buildUpdateReport({ sources: [local, mira], registry, startedAt: NOW, all: true });
158
+ expect(report.rows.map((r) => `${r.stack}:${r.status}:${r.environment ?? "local"}`)).toEqual(["media-mira:update:mira", "media:up-to-date:local"]);
159
+ expect(report.images).toBe(1);
160
+ });
161
+
152
162
  it("fetches each repository's tags once", async () => {
153
163
  let lists = 0;
154
164
  const inner: Registry = {
@@ -30,6 +30,8 @@ export interface UpdateRow {
30
30
  latestDigest?: string;
31
31
  /** The stack is stopped: `stack-update` refuses it, because a redeploy would start it. */
32
32
  stopped?: true;
33
+ /** The Arcane remote environment that runs the stack; absent for the manager's own Docker host. */
34
+ environment?: string;
33
35
  }
34
36
 
35
37
  export interface UpdateReport {
@@ -357,22 +359,35 @@ export function cachingRegistry(inner: Registry): Registry {
357
359
  * Builds the update report: every service image of every stack, against a check
358
360
  * that ran moments before, with release versions read from the registries.
359
361
  */
360
- export async function buildUpdateReport(input: { check: unknown; projects: unknown; registry: Registry; startedAt: number; all: boolean }): Promise<UpdateReport> {
361
- const entries = checkEntries(input.check);
362
- const services = stackServices(input.projects);
363
- const refs = [...new Set(services.map((s) => s.ref).filter((r): r is string => r !== null))];
362
+ /** One Arcane environment's fresh registry check and stack list; `environment` names a remote one. */
363
+ export interface ReportSource {
364
+ environment?: string;
365
+ check: unknown;
366
+ projects: unknown;
367
+ }
368
+
369
+ export async function buildUpdateReport(input: { sources: ReportSource[]; registry: Registry; startedAt: number; all: boolean }): Promise<UpdateReport> {
370
+ // The same image reference runs a different build in each environment, so each one resolves on its own check.
371
+ const entries = input.sources.map((src) => checkEntries(src.check));
372
+ const services = input.sources.flatMap((src, i) => stackServices(src.projects).map((s) => ({ ...s, source: i })));
373
+ const keyOf = (source: number, ref: string) => `${source}\n${ref}`;
374
+ const keys = [...new Set(services.filter((s) => s.ref !== null).map((s) => keyOf(s.source, s.ref as string)))];
364
375
  const resolved = new Map<string, Resolution & { kind: RefKind }>();
365
376
  const registry = cachingRegistry(input.registry);
366
- await mapLimit(refs, CONCURRENCY, async (ref) => {
367
- resolved.set(ref, await resolveRef(ref, entries[ref], registry, input.startedAt));
377
+ await mapLimit(keys, CONCURRENCY, async (key) => {
378
+ const [source, ref] = [Number(key.slice(0, key.indexOf("\n"))), key.slice(key.indexOf("\n") + 1)];
379
+ resolved.set(key, await resolveRef(ref, entries[source][ref], registry, input.startedAt));
368
380
  });
369
- const privateImages = refs.filter((ref) => resolved.get(ref)?.private === true);
381
+ const refs = [...new Set(services.map((s) => s.ref).filter((r): r is string => r !== null))];
382
+ const privateImages = refs.filter((ref) => services.some((s) => s.ref === ref && resolved.get(keyOf(s.source, ref))?.private === true));
370
383
  const listed = services.filter((s) => s.ref === null || !privateImages.includes(s.ref));
371
384
  const rows: UpdateRow[] = listed.map((s) => {
372
- if (s.ref === null) return { stack: s.stack, service: s.service, ref: "", image: "?", kind: "floating", running: "?", latest: "?", status: "unknown", action: "", note: "Arcane lists no images for this stack" };
373
- const r = resolved.get(s.ref) as Resolution & { kind: RefKind };
385
+ const environment = input.sources[s.source].environment;
386
+ const where = environment === undefined ? {} : { environment };
387
+ if (s.ref === null) return { stack: s.stack, service: s.service, ref: "", image: "?", kind: "floating", running: "?", latest: "?", status: "unknown", action: "", note: "Arcane lists no images for this stack", ...where };
388
+ const r = resolved.get(keyOf(s.source, s.ref)) as Resolution & { kind: RefKind };
374
389
  const note = [r.note, s.stopped ? "stack is stopped" : ""].filter(Boolean).join("; ");
375
- return { stack: s.stack, service: s.service, ref: s.ref, image: shortImage(parseImageRef(s.ref)), kind: r.kind, running: r.running, latest: r.latest, status: r.status, action: r.action, note, ...(r.latestDigest ? { latestDigest: r.latestDigest } : {}), ...(s.stopped ? { stopped: true as const } : {}) };
390
+ return { stack: s.stack, service: s.service, ref: s.ref, image: shortImage(parseImageRef(s.ref)), kind: r.kind, running: r.running, latest: r.latest, status: r.status, action: r.action, note, ...(r.latestDigest ? { latestDigest: r.latestDigest } : {}), ...(s.stopped ? { stopped: true as const } : {}), ...where };
376
391
  });
377
392
  rows.sort((a, b) => STATUS_ORDER[a.status] - STATUS_ORDER[b.status] || a.stack.localeCompare(b.stack) || a.service.localeCompare(b.service));
378
393
  const counts: Record<UpdateStatus, number> = { update: 0, release: 0, rebuild: 0, unknown: 0, "up-to-date": 0 };
@@ -107,8 +107,13 @@ const HELP = `media specialist (trendarr: TMDB trending, OMDb ratings, Radarr an
107
107
  add-movies <imdbId...> add to Radarr monitored and start a search (at most ${MAX_ADD_IDS} IDs)
108
108
  add-series <imdbId...> add to Sonarr monitored and start a search (at most ${MAX_ADD_IDS} IDs)
109
109
  sabnzbd status|pause|resume read or toggle the SABnzbd queue pause state
110
+ queue Radarr and Sonarr queue records a person must handle (importPending, importBlocked,
111
+ failedPending): app, id, title, state, status, messages, added, size
112
+ queue-remove <radarr|sonarr> <id> --confirm
113
+ remove that record and delete its download from SABnzbd, without blocklisting the release
110
114
 
111
- Output is trendarr's JSON plus "warnings" (trendarr's stderr warnings, such as an unreachable Radarr).
115
+ Output is trendarr's JSON plus "warnings" (trendarr's stderr warnings, such as an unreachable Radarr);
116
+ queue and queue-remove call Radarr and Sonarr directly.
112
117
  An add exits 0 even when an ID fails: read each results[].status (added, already_exists, not_found, error).
113
118
  trendarr init and its configuration are operator tasks.`;
114
119
 
@@ -281,11 +286,95 @@ async function runTrendarr(config: MediaConfig, call: TrendarrCall): Promise<{ s
281
286
  }
282
287
  }
283
288
 
289
+ /** Queue states Radarr and Sonarr leave for a person: an import that waits, is blocked, or failed. */
290
+ const STUCK_STATES = ["importPending", "importBlocked", "failedPending"];
291
+ const APPS = ["radarr", "sonarr"] as const;
292
+ export type ArrApp = (typeof APPS)[number];
293
+
294
+ function isApp(value: string | undefined): value is ArrApp {
295
+ return value === "radarr" || value === "sonarr";
296
+ }
297
+
298
+ /** One queue record a person must handle; no API key or download path in it. */
299
+ export interface QueueRecord {
300
+ app: ArrApp;
301
+ id: number;
302
+ title: string;
303
+ state: string;
304
+ status: string;
305
+ messages: string[];
306
+ added: string | null;
307
+ size: number | null;
308
+ }
309
+
310
+ /** The records of one `/api/v3/queue` page that wait for a person. */
311
+ export function stuckRecords(app: ArrApp, page: unknown): QueueRecord[] {
312
+ const records = (page as { records?: unknown } | null)?.records;
313
+ if (!Array.isArray(records)) throw new ServiceError(`${app} returned no queue records`);
314
+ return (records as Record<string, unknown>[])
315
+ .filter((r) => typeof r.id === "number" && typeof r.trackedDownloadState === "string" && STUCK_STATES.includes(r.trackedDownloadState))
316
+ .map((r) => ({
317
+ app,
318
+ id: r.id as number,
319
+ title: typeof r.title === "string" ? r.title : "?",
320
+ state: r.trackedDownloadState as string,
321
+ status: typeof r.trackedDownloadStatus === "string" ? r.trackedDownloadStatus : "?",
322
+ messages: (Array.isArray(r.statusMessages) ? (r.statusMessages as { title?: unknown; messages?: unknown }[]) : []).flatMap((m) => {
323
+ const texts = Array.isArray(m.messages) ? m.messages.filter((t): t is string => typeof t === "string") : [];
324
+ return texts.length > 0 ? texts : typeof m.title === "string" ? [m.title] : [];
325
+ }),
326
+ added: typeof r.added === "string" ? r.added : null,
327
+ size: typeof r.size === "number" ? r.size : null,
328
+ }));
329
+ }
330
+
331
+ /** `queue-remove <radarr|sonarr> <id> --confirm`: the gate asks an owner, because the download is deleted. */
332
+ export function parseQueueRemove(args: string[]): { app: ArrApp; id: number } {
333
+ const usage = "queue-remove <radarr|sonarr> <id> --confirm";
334
+ const rest = args.filter((a) => a !== "--confirm");
335
+ if (rest.length !== 2 || !isApp(rest[0]) || !/^\d{1,10}$/.test(rest[1])) throw new UsageError(usage);
336
+ if (!args.includes("--confirm")) throw new UsageError(`${usage}: it deletes the download from SABnzbd, so it needs --confirm`);
337
+ return { app: rest[0], id: Number(rest[1]) };
338
+ }
339
+
340
+ async function arrRequest(config: MediaConfig, app: ArrApp, method: "GET" | "DELETE", path: string): Promise<unknown> {
341
+ const base = app === "radarr" ? config.radarrUrl : config.sonarrUrl;
342
+ const key = readSecret(SERVICE, app === "radarr" ? "RADARR_API_KEY" : "SONARR_API_KEY");
343
+ const res = await fetch(new URL(path, base), { method, headers: { "X-Api-Key": key, accept: "application/json" }, signal: AbortSignal.timeout(30_000) });
344
+ if (!res.ok) throw new ServiceError(`${app} answered ${res.status} to ${method} ${path.split("?")[0]}`);
345
+ return method === "GET" ? ((await res.json()) as unknown) : null;
346
+ }
347
+
348
+ /** Both apps' queue records that wait for a person; an app that does not answer becomes a warning. */
349
+ async function queueStatus(config: MediaConfig): Promise<{ records: QueueRecord[]; warnings: string[] }> {
350
+ const records: QueueRecord[] = [];
351
+ const warnings: string[] = [];
352
+ for (const app of APPS) {
353
+ try {
354
+ records.push(...stuckRecords(app, await arrRequest(config, app, "GET", "/api/v3/queue?page=1&pageSize=200")));
355
+ } catch (err) {
356
+ warnings.push(`${app}: ${err instanceof Error ? err.message.slice(0, 200) : String(err)}`);
357
+ }
358
+ }
359
+ return { records, warnings };
360
+ }
361
+
284
362
  export async function command(args: string[]): Promise<number> {
285
363
  if (args.length === 0 || args[0] === "--help" || args[0] === "help") {
286
364
  printRaw(HELP);
287
365
  return 0;
288
366
  }
367
+ if (args[0] === "queue") {
368
+ if (args.length !== 1) throw new UsageError("queue takes no arguments");
369
+ printJson(await queueStatus(readConfig<MediaConfig>(SERVICE, SHAPE)));
370
+ return 0;
371
+ }
372
+ if (args[0] === "queue-remove") {
373
+ const { app, id } = parseQueueRemove(args.slice(1));
374
+ await arrRequest(readConfig<MediaConfig>(SERVICE, SHAPE), app, "DELETE", `/api/v3/queue/${id}?removeFromClient=true&blocklist=false`);
375
+ printJson({ status: "removed", app, id });
376
+ return 0;
377
+ }
289
378
  const call = buildTrendarrCall(args);
290
379
  const config = readConfig<MediaConfig>(SERVICE, SHAPE);
291
380
  printJson(interpretRun(call, await runTrendarr(config, call)));
@@ -8,7 +8,7 @@ import { buildNodeRemote, parseJoinArgs, parseParams, parseRecordFilters, techni
8
8
  import { buildVpsRemote, command as edgeCommand, isTransientSshFailure, PUBLIC_ROUTES, routeProbe } from "./edge/run.ts";
9
9
  import { API_PREFIXES, buildDmzRemote, installedRelease, publishedSha256, validateApiPath, command as identityCommand } from "./identity/run.ts";
10
10
  import { buildInferenceRemote, command as inferenceCommand } from "./inference/run.ts";
11
- import { MAX_ADD_IDS, MAX_EXCLUDE_IDS, buildTrendarrCall, interpretRun, parseImdbIds, trendarrConfig, trendarrError, trendarrWarnings, command as mediaCommand } from "./media/run.ts";
11
+ import { MAX_ADD_IDS, MAX_EXCLUDE_IDS, buildTrendarrCall, interpretRun, parseImdbIds, parseQueueRemove, stuckRecords, trendarrConfig, trendarrError, trendarrWarnings, command as mediaCommand } from "./media/run.ts";
12
12
  import { ServiceError, UsageError } from "./lib/errors.ts";
13
13
  import { safeRepoPath, safeWritePath } from "./lib/repo.ts";
14
14
  import { appGetPath, appPolicyDelete, portForwardDelete, clientFixedIpWrite, syslogChanges, buildPulsarRemote, cloudPath, deviceNetworkWrite, NAT_PATH, natWrite, parseQuery, parseSiteArgs, redactUnifi, unifiPath, unifiWrite, command as networkCommand } from "./network/run.ts";
@@ -822,6 +822,24 @@ describe("media wrapper", () => {
822
822
  expect(() => parseImdbIds(Array.from({ length: MAX_ADD_IDS + 1 }, (_, i) => `tt${i + 1}`))).toThrow(/at most/);
823
823
  });
824
824
 
825
+ it("keeps the queue records a person must handle, without anything secret", () => {
826
+ const page = {
827
+ records: [
828
+ { id: 7, title: "Primetime.2026.REPACK.2160p", trackedDownloadState: "importPending", trackedDownloadStatus: "warning", statusMessages: [{ title: "Primetime.2026", messages: ["Not a Custom Format upgrade"] }, { title: "only a title", messages: [] }], added: "2026-11-03T16:03:52Z", size: 13406787332, outputPath: "/scratch/complete/x", downloadId: "SABnzbd_nzo_1" },
829
+ { id: 8, title: "Downloading", trackedDownloadState: "downloading", trackedDownloadStatus: "ok" },
830
+ { title: "no id", trackedDownloadState: "importBlocked" },
831
+ ],
832
+ };
833
+ expect(stuckRecords("radarr", page)).toEqual([
834
+ { app: "radarr", id: 7, title: "Primetime.2026.REPACK.2160p", state: "importPending", status: "warning", messages: ["Not a Custom Format upgrade", "only a title"], added: "2026-11-03T16:03:52Z", size: 13406787332 },
835
+ ]);
836
+ expect(() => stuckRecords("sonarr", { error: "x" })).toThrow(/no queue records/);
837
+ expect(parseQueueRemove(["sonarr", "42", "--confirm"])).toEqual({ app: "sonarr", id: 42 });
838
+ expect(() => parseQueueRemove(["radarr", "42"])).toThrow(/needs --confirm/);
839
+ expect(() => parseQueueRemove(["lidarr", "42", "--confirm"])).toThrow(/radarr\|sonarr/);
840
+ expect(() => parseQueueRemove(["radarr", "4x", "--confirm"])).toThrow(/radarr\|sonarr/);
841
+ });
842
+
825
843
  it("writes trendarr's configuration in its own schema", () => {
826
844
  const config = { trendarrCli: "/x/cli.js", radarrUrl: "http://r:7878", radarrQualityProfileId: 7, radarrRootFolderPath: "/media/movies", sonarrUrl: "http://s:8989", sonarrQualityProfileId: 7, sonarrRootFolderPath: "/media/tv", sonarrMonitor: "all", sabnzbdUrl: "http://z:8084", maxEntries: 10 };
827
845
  expect(trendarrConfig(config, { tmdbAccessToken: "t", omdbApiKey: "o", radarrApiKey: "r", sonarrApiKey: "s", sabnzbdApiKey: "z" })).toEqual({