// PEP-267: pinned versions of the agent CLIs (Claude Code, Codex, OpenCode) and what actually runs. // The pin is infra/agent-versions.json; the watchdog compares it with the service's CLIs and the // runner image (runner/agent-versions.ts) and keeps the snapshot here for the superadmin. // glossary-ignore-file: читает суперадмин (инженер), не продакт import { readFileSync } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import type { Db } from "@peppa/db"; import { peppaRoot } from "@peppa/shared"; import { getSetting, setSetting } from "./settings.ts"; export type AgentCli = "claude" | "codex" | "opencode"; export const AGENT_CLIS: Array<{ cli: AgentCli; name: string; bin: string; binEnv: string; npm: string; buildArg: string }> = [ { cli: "claude", name: "Claude Code", bin: "claude", binEnv: "PEPPA_CLAUDE_BIN", npm: "@anthropic-ai/claude-code", buildArg: "CLAUDE_CODE_VERSION" }, { cli: "codex", name: "Codex", bin: "codex", binEnv: "PEPPA_CODEX_BIN", npm: "@openai/codex", buildArg: "CODEX_VERSION" }, { cli: "opencode", name: "OpenCode", bin: "opencode", binEnv: "PEPPA_OPENCODE_BIN", npm: "opencode-ai", buildArg: "OPENCODE_VERSION" }, ]; /** * Every run of a CLI by the factory carries this: no CLI updates itself. Codex has no env for it — * `-c check_for_update_on_startup=false` in its launch (runner/providers.ts). */ export const NO_AUTOUPDATE_ENV: Readonly> = { DISABLE_AUTOUPDATER: "1", OPENCODE_DISABLE_AUTOUPDATE: "1" }; export type AgentVersions = Partial>; export function agentVersionsFile(root?: string): string { return process.env.PEPPA_AGENT_VERSIONS_FILE ?? join(root ?? peppaRoot(resolve(dirname(fileURLToPath(import.meta.url)), "../../..")), "infra/agent-versions.json"); } /** The pinned versions; an unreadable file — empty (nothing to compare with). */ export function pinnedAgentVersions(file = agentVersionsFile()): AgentVersions { try { const raw = JSON.parse(readFileSync(file, "utf8")) as Record; const out: AgentVersions = {}; for (const { cli } of AGENT_CLIS) if (typeof raw[cli] === "string" && raw[cli]) out[cli] = raw[cli] as string; return out; } catch { return {}; } } /** «2.1.330 (Claude Code)», «codex-cli 0.171.0», «1.19.6» → the version; nothing like it — null. */ export function parseCliVersion(output: string): string | null { return /\b(\d+\.\d+\.\d+(?:-[0-9A-Za-z.]+)?)\b/.exec(output)?.[1] ?? null; } export type VersionPlace = "service" | "runner"; export interface AgentVersionsSnapshot { at: string; pinned: AgentVersions; /** CLIs of the platform service itself (AI roles, subscription sign-in, agents without a container). */ service: AgentVersions; /** The runner image the agents run in (its labels); null — no image or no docker here. */ runner: AgentVersions | null; } export interface VersionDrift { place: VersionPlace; cli: AgentCli; pinned: string; actual: string; } const KEY = "agent.versions"; export const getAgentVersions = (db: Db) => getSetting(db, KEY); export async function saveAgentVersions(db: Db, s: AgentVersionsSnapshot): Promise { await setSetting(db, KEY, s); } /** Versions that differ from the pin (a CLI that is not installed is not a drift). */ export function versionDrifts(s: Pick): VersionDrift[] { const out: VersionDrift[] = []; for (const place of ["service", "runner"] as const) { const got = s[place]; if (!got) continue; for (const { cli } of AGENT_CLIS) { const pinned = s.pinned[cli]; const actual = got[cli]; if (pinned && actual && actual !== pinned) out.push({ place, cli, pinned, actual }); } } return out; } /** Lines for `platform_status` / `peppa status`. */ export function describeAgentVersions(s: AgentVersionsSnapshot | null, pinned = pinnedAgentVersions()): string[] { const out = [`Agent CLI versions${s ? ` (checked ${s.at.slice(0, 16).replace("T", " ")})` : " (not checked yet)"}:`]; const drifts = s ? versionDrifts(s) : []; for (const { cli, name } of AGENT_CLIS) { const where = [`pinned ${pinned[cli] ?? "—"}`]; if (s) where.push(`service ${s.service[cli] ?? "—"}`, `runner image ${s.runner ? (s.runner[cli] ?? "—") : "n/a"}`); const bad = drifts.filter((d) => d.cli === cli); out.push(`- ${name}: ${where.join(", ")}${bad.length ? ` — DRIFT in ${bad.map((d) => d.place).join(", ")}` : ""}`); } return out; }