/** * HU-5272: names (never values) of the current environment's variables that an * agent started by kj would not receive, so a dropped login is visible instead * of silent. Informational: it never fails. */ import { buildAgentEnv } from "../utils/role-env.js"; import { resolveRole } from "../config/role-resolver.js"; import { STRATEGY } from "./types.js"; const PANEL_ROLES = ["coder", "reviewer", "solomon"]; /** * @param {Record} env * @param {string[]} [passthrough] * @param {string|null} [agent] * @returns {string[]} sorted names dropped by that agent's allowlist */ export function droppedAgentVars(env, passthrough = [], agent = null) { const kept = buildAgentEnv(env, { agent, passthrough }); return Object.keys(env).filter((name) => !Object.hasOwn(kept, name)).toSorted(); } /** * @param {object} config loaded kj config * @returns {string[]} distinct providers of the panel roles, in role order */ export function panelAgents(config) { const providers = PANEL_ROLES.map((role) => resolveRole(config, role).provider).filter(Boolean); return [...new Set(providers)]; } function describeDropped(byAgent) { const groups = byAgent.filter(({ names }) => names.length > 0); if (groups.length === 0) return "every variable of this environment reaches the panel agents"; const list = groups.map(({ agent, names }) => `${agent}: ${names.join(", ")}`).join("; "); return `not passed to agents: ${list} (add one with security.env_passthrough)`; } export function createAgentEnvCheck({ env = process.env } = {}) { return { name: "agent-env", label: "Agent environment", strategy: STRATEGY.NONE, async detect({ config } = {}) { if (config?.security?.env_allowlist === false) { return { ok: true, severity: "info", detail: "env allowlist off: agents receive the whole environment" }; } const agents = panelAgents(config); if (agents.length === 0) { return { ok: true, severity: "info", detail: "no panel agent configured (roles.coder, roles.reviewer, roles.solomon)" }; } const passthrough = config?.security?.env_passthrough || []; const byAgent = agents.map((agent) => ({ agent, names: droppedAgentVars(env, passthrough, agent) })); return { ok: true, severity: "info", detail: describeDropped(byAgent) }; }, }; } export function getAgentEnvChecks(opts) { return [createAgentEnvCheck(opts)]; }