import { detectMux as realDetectMux } from "../mux/index.ts"; import { emitDiagnostic } from "../diagnostics/diagnostics.ts"; const warnedInvalidValues = new Set(); // Indirection: the auto-mode branch reads through a mutable reference so // tests can swap in a stub detector. Production callers still resolve // `realDetectMux`; keeping this module-scoped preserves zero-arg call sites. let detectMuxImpl: () => boolean = realDetectMux; export type BackendKind = "pane" | "headless"; export function selectBackend(): BackendKind { const raw = (process.env.PI_SUBAGENT_MODE ?? "auto").toLowerCase(); if (raw === "pane") return "pane"; if (raw === "headless") return "headless"; if (raw !== "auto" && !warnedInvalidValues.has(raw)) { warnedInvalidValues.add(raw); emitDiagnostic({ code: "invalid-subagent-mode", audience: { human: true }, message: `[subagents] invalid PI_SUBAGENT_MODE="${raw}"; ` + `using auto (valid: pane | headless | auto)\n`, }); } return detectMuxImpl() ? "pane" : "headless"; } export const __test__ = { resetWarnedValues(): void { warnedInvalidValues.clear(); }, setDetectMux(fn: () => boolean): void { detectMuxImpl = fn; }, restoreDetectMux(): void { detectMuxImpl = realDetectMux; }, };