@capacitor/cli9.1.04.7 kB
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isAgentDriven = exports.isCI = exports.detectRunContext = exports.CONVENTION_PATTERN = exports.CONVENTION_DENYLIST = void 0;
const utils_terminal_1 = require("@ionic/utils-terminal");
const net_1 = require("net");
const declared_1 = require("./declared");
const signals_1 = require("./signals");
const unknown_1 = require("./unknown");
const version_1 = require("./version");
var unknown_2 = require("./unknown");
Object.defineProperty(exports, "CONVENTION_DENYLIST", { enumerable: true, get: function () { return unknown_2.CONVENTION_DENYLIST; } });
Object.defineProperty(exports, "CONVENTION_PATTERN", { enumerable: true, get: function () { return unknown_2.CONVENTION_PATTERN; } });
/**
* Any single signal happens legitimately — a `cat` pager, a missing TTY, a
* local proxy. Three together, with no terminal anywhere, is what separates a
* machine-driven run from a developer with an unusual setup.
*/
const HEURISTIC_THRESHOLD = 3;
/**
* Classify this invocation as human, CI, or agent-driven from the environment
* alone — no process spawn, no filesystem read.
*
* Every input is injectable and nothing is captured at module load, so tests
* can drive the whole thing. This is also why `@ionic/utils-terminal`'s
* `TERMINAL_INFO` isn't used: it snapshots the real process when the module is
* first imported and freezes it.
*/
function detectRunContext(options = {}) {
var _a, _b, _c, _d;
const env = (_a = options.env) !== null && _a !== void 0 ? _a : process.env;
const streams = (_b = options.streams) !== null && _b !== void 0 ? _b : readStreamStates();
const cwd = (_c = options.cwd) !== null && _c !== void 0 ? _c : process.cwd();
const execPath = (_d = options.execPath) !== null && _d !== void 0 ? _d : process.execPath;
const scan = (0, signals_1.scanSignals)(env, streams, cwd, execPath);
const surface = (0, signals_1.deriveSurface)(env, scan);
// CI is classified but never delivered, so nothing below needs to run.
if (isCI(env)) {
return { report: false, surface, confidence: 'none' };
}
const declared = (0, declared_1.detectDeclared)(env);
if (declared) {
return {
report: true,
agentId: declared.id,
agentVersion: (0, version_1.detectAgentVersion)(env, declared.id),
surface,
confidence: 'declared',
sessionVar: declared.sessionVar,
};
}
// Neither path below may report a run with a terminal attached, which makes
// "someone at a terminal is never reported as an agent" a guarantee rather
// than a likelihood. The convention rule needs the gate most: it matches on
// the shape of a variable name alone, which desktop sessions and shells also
// produce, yet returns the stronger `declared` confidence.
const headless = scan.headless.includes(signals_1.NO_TTY_SIGNAL);
const detectedBy = headless ? (0, unknown_1.detectUnknownHarness)(env) : undefined;
if (detectedBy) {
return {
report: true,
agentId: 'unknown',
agentDetectedBy: detectedBy,
surface,
confidence: 'declared',
};
}
// This path exists for harnesses that strip their environment before
// invoking the CLI.
const weight = headless ? scan.headless.length + scan.sandbox.length : 0;
if (weight >= HEURISTIC_THRESHOLD) {
return {
report: true,
agentId: 'unknown',
surface,
confidence: 'heuristic',
};
}
return { report: true, surface, confidence: 'none' };
}
exports.detectRunContext = detectRunContext;
/**
* Whether this is a build server. Reuses the CLI's existing variable list
* rather than a copy of it, so the gate can't drift from `isInteractive()`.
*/
function isCI(env) {
return utils_terminal_1.CI_ENVIRONMENT_VARIABLES.some((name) => Boolean(env[name]));
}
exports.isCI = isCI;
/**
* Whether something other than a person invoked the CLI. True for an
* unrecognised harness as well as a named one — both mean there is nobody
* waiting on the terminal.
*/
function isAgentDriven(runContext) {
return Boolean(runContext.agentId);
}
exports.isAgentDriven = isAgentDriven;
function readStreamStates() {
const { stdin, stdout, stderr } = process;
return {
tty: [Boolean(stdin.isTTY), Boolean(stdout.isTTY), Boolean(stderr.isTTY)],
socket: [isPipe(stdin), isPipe(stdout), isPipe(stderr)],
};
}
/**
* `tty.ReadStream` extends `net.Socket`, so a real terminal has to be excluded
* rather than just testing the prototype.
*/
function isPipe(stream) {
return stream instanceof net_1.Socket && !stream.isTTY;
}
Shown as plain text from the published tarball, after apm checked it against the version's checksum. Nothing in it runs on this page.