96 lines
3.8 KiB
TypeScript
96 lines
3.8 KiB
TypeScript
// PEP-267: once an hour the watchdog compares the pinned agent CLI versions (infra/agent-versions.json)
|
|
// with what actually runs — the service's own CLIs (`<bin> --version`: AI roles, subscription sign-in,
|
|
// agents without a container) and the runner image (its labels `quest.telecom.peppa.cli.<cli>`).
|
|
// A version that changed by itself — one message to the superadmin per version.
|
|
// glossary-ignore-file: сообщения читает администратор платформы
|
|
import { execFile } from "node:child_process";
|
|
import { promisify } from "node:util";
|
|
import {
|
|
AGENT_CLIS,
|
|
alertAdmins,
|
|
getAgentVersions,
|
|
NO_AUTOUPDATE_ENV,
|
|
parseCliVersion,
|
|
pinnedAgentVersions,
|
|
saveAgentVersions,
|
|
versionDrifts,
|
|
type AgentVersions,
|
|
type AgentVersionsSnapshot,
|
|
} from "@peppa/core";
|
|
import type { Db } from "@peppa/db";
|
|
import { m } from "@peppa/shared/i18n";
|
|
import { runnerImage } from "./sandbox.ts";
|
|
|
|
const run = promisify(execFile);
|
|
const HOUR_MS = 3600_000;
|
|
const docker = () => process.env.PEPPA_DOCKER_BIN ?? "docker";
|
|
|
|
/** `<bin> --version` of each CLI here; a CLI that is not installed is left out. */
|
|
export async function serviceAgentVersions(): Promise<AgentVersions> {
|
|
const out: AgentVersions = {};
|
|
await Promise.all(
|
|
AGENT_CLIS.map(async ({ cli, bin, binEnv }) => {
|
|
try {
|
|
const r = await run(process.env[binEnv] ?? bin, ["--version"], { timeout: 20_000, env: { ...process.env, ...NO_AUTOUPDATE_ENV } });
|
|
const v = parseCliVersion(`${r.stdout}\n${r.stderr}`);
|
|
if (v) out[cli] = v;
|
|
} catch {
|
|
/* not installed here */
|
|
}
|
|
}),
|
|
);
|
|
return out;
|
|
}
|
|
|
|
/** Versions the runner image was built with (its labels); no docker or no image — null. */
|
|
export async function runnerImageVersions(image = runnerImage()): Promise<AgentVersions | null> {
|
|
try {
|
|
const r = await run(docker(), ["image", "inspect", "-f", "{{json .Config.Labels}}", image], { timeout: 20_000 });
|
|
const labels = JSON.parse(r.stdout.trim() || "{}") as Record<string, string> | null;
|
|
const out: AgentVersions = {};
|
|
for (const { cli } of AGENT_CLIS) {
|
|
const v = labels?.[`quest.telecom.peppa.cli.${cli}`];
|
|
if (v) out[cli] = v;
|
|
}
|
|
return out;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function checkAgentVersions(db: Db, o: { now?: Date; log?: (s: string) => void } = {}): Promise<AgentVersionsSnapshot> {
|
|
const now = o.now ?? new Date();
|
|
const snap: AgentVersionsSnapshot = {
|
|
at: now.toISOString(),
|
|
pinned: pinnedAgentVersions(),
|
|
service: await serviceAgentVersions(),
|
|
runner: await runnerImageVersions(),
|
|
};
|
|
await saveAgentVersions(db, snap);
|
|
for (const d of versionDrifts(snap)) {
|
|
const name = AGENT_CLIS.find((c) => c.cli === d.cli)!.name;
|
|
const place = d.place === "runner" ? m("в образе агентов") : m("в сервисе FORGE");
|
|
await alertAdmins(
|
|
db,
|
|
`agent-version:${d.place}:${d.cli}:${d.actual}`,
|
|
m("{name} {place}: версия {actual} вместо закреплённой {pinned}. Вернуть — scripts/runner-image.sh --rollback (образ агентов) или выкатка платформы; новую версию — только через scripts/runner-image.sh --try.", {
|
|
name,
|
|
place,
|
|
actual: d.actual,
|
|
pinned: d.pinned,
|
|
}),
|
|
o.log ?? (() => {}),
|
|
);
|
|
}
|
|
return snap;
|
|
}
|
|
|
|
/** Watchdog tick: once an hour. */
|
|
export async function agentVersionsTick(db: Db, o: { now?: Date; log?: (s: string) => void } = {}): Promise<AgentVersionsSnapshot | null> {
|
|
if (process.env.PEPPA_AGENT_VERSIONS === "off") return null;
|
|
const now = o.now ?? new Date();
|
|
const last = await getAgentVersions(db);
|
|
if (last && now.getTime() - Date.parse(last.at) < HOUR_MS) return null;
|
|
return checkAgentVersions(db, { now, log: o.log });
|
|
}
|