/**
 * `olam flywheel ping` — no-op subcommand for cold-start telemetry.
 *
 * Establishes the measurement baseline OQ12 references: before porting any
 * subcommand from atlas-toolbox to olam, we need to know what cold-start
 * overhead the shim → olam binary path adds vs the current bash-invoked
 * scripts. Operators run `time olam flywheel ping` on a representative
 * machine + record the result.
 *
 * Branch decision (per plan A2):
 *   ≤200ms total invocation  → full-port for A3+ subcommands (drop hybrid hedge S2)
 *   200-500ms                → full-port with follow-up perf-investigation tagged
 *   >500ms                   → escalate to hybrid model (heavy helpers ported;
 *                              emit-breadcrumb stays as native script delegating)
 *
 * Telemetry baseline written to ~/.local/share/olam/flywheel-baseline.json.
 * Permanent telemetry surface for `olam doctor` (promoted in task A13).
 */
import { mkdirSync, writeFileSync } from 'node:fs';
import { homedir } from 'node:os';
import { dirname, join } from 'node:path';
const COLD_START_BUDGET_GOOD_MS = 200;
const COLD_START_BUDGET_FAIR_MS = 500;
function classifyColdStart(coldStartMs) {
    if (coldStartMs <= COLD_START_BUDGET_GOOD_MS)
        return 'good';
    if (coldStartMs <= COLD_START_BUDGET_FAIR_MS)
        return 'fair';
    return 'over-budget';
}
function readOlamVersion() {
    try {
        // packageVersion is injected at build time by the bundle script;
        // fall back to env when running via `npm run dev`.
        return process.env.npm_package_version ?? '0.0.0-dev';
    }
    catch {
        return '0.0.0-dev';
    }
}
function writeBaseline(record) {
    const baselinePath = join(homedir(), '.local', 'share', 'olam', 'flywheel-baseline.json');
    mkdirSync(dirname(baselinePath), { recursive: true });
    writeFileSync(baselinePath, JSON.stringify(record, null, 2) + '\n', 'utf8');
    return baselinePath;
}
export function registerFlywheelPing(parent) {
    parent
        .command('ping')
        .description('No-op cold-start telemetry surface (measures + records flywheel CLI baseline)')
        .option('--json', 'emit baseline record as JSON instead of human-readable status')
        .option('--no-record-baseline', 'do not write ~/.local/share/olam/flywheel-baseline.json (default: record)')
        .action((opts) => {
        // We measure from the Node process load tick. uptime() returns seconds since
        // the process started, including module-resolution + commander parsing —
        // exactly the operator-visible cold-start surface we want to measure.
        const coldStartMs = Math.round(process.uptime() * 1000);
        const record = {
            cold_start_ms: coldStartMs,
            olam_version: readOlamVersion(),
            measured_at: new Date().toISOString(),
            classification: classifyColdStart(coldStartMs),
        };
        let baselinePath;
        if (opts.recordBaseline !== false) {
            baselinePath = writeBaseline(record);
        }
        if (opts.json) {
            process.stdout.write(JSON.stringify(record) + '\n');
        }
        else {
            process.stdout.write(`flywheel ping: cold_start=${record.cold_start_ms}ms classification=${record.classification} version=${record.olam_version}\n`);
            if (baselinePath !== undefined) {
                process.stdout.write(`baseline recorded: ${baselinePath}\n`);
            }
        }
    });
}
//# sourceMappingURL=ping.js.map