#!/usr/bin/env node
import {
  createProgram,
  installCommandTelemetry,
  printFirstRunWelcome
} from "../chunk-35FI23SO.js";
import "../chunk-KUVPSUG4.js";
import "../chunk-DLBMYLYR.js";
import "../chunk-MKPRB4PL.js";
import "../chunk-NP2ZI6E3.js";
import "../chunk-5KTTKXQN.js";
import "../chunk-CQ553GZO.js";
import "../chunk-GISMXMVL.js";
import "../chunk-WWACIIBA.js";
import "../chunk-TEFCFC4H.js";
import "../chunk-D2LZUJLP.js";
import "../chunk-GUIX2TMZ.js";
import "../chunk-KET4QQZB.js";
import "../chunk-L3CEN42B.js";
import "../chunk-YG4ZBGEH.js";
import {
  dispatchSkillsFromGit
} from "../chunk-3LPOTTPX.js";
import "../chunk-YEBUFRTT.js";
import "../chunk-EJPR5VCT.js";
import "../chunk-IOW3MW2K.js";
import "../chunk-GTXUUGFG.js";
import "../chunk-4NFV5UZO.js";
import "../chunk-KTWKW6KJ.js";
import "../chunk-7PGYJO3L.js";
import "../chunk-LM4F5KBK.js";
import "../chunk-6CHNY3NJ.js";
import "../chunk-MT2MMBUA.js";
import "../chunk-5PPV4WYY.js";
import "../chunk-KWD6FF7M.js";
import "../chunk-EPUKTTJZ.js";
import "../chunk-W6Y7ZW3Y.js";
import "../chunk-27AJKSQY.js";
import {
  findConfigFile,
  loadConfig
} from "../chunk-PW3U4YJV.js";
import "../chunk-3ISINLYT.js";
import "../chunk-6B6UN6SG.js";
import "../chunk-ZCZD7FHU.js";
import {
  CLI_VERSION
} from "../chunk-BM3PWGXQ.js";
import {
  getUpdateNotification,
  init_dist,
  isUpdateCheckEnabled,
  readCheckState,
  shouldRunCheck,
  spawnBackgroundCheck
} from "../chunk-4DWOMGUM.js";
import "../chunk-RL4VMEXL.js";
import "../chunk-BMM3GXV6.js";
import "../chunk-ZP5E7YET.js";
import "../chunk-HIK6OEUF.js";
import "../chunk-EBJQ6N4M.js";
import {
  handleError
} from "../chunk-3WGJMBKH.js";
import "../chunk-KFQGP6VL.js";

// src/bin/harness.ts
import "dotenv/config";

// src/bin/update-check-hooks.ts
init_dist();
var DEFAULT_INTERVAL_MS = 864e5;
var cachedConfigInterval = null;
function readConfigInterval() {
  if (cachedConfigInterval !== null) return cachedConfigInterval;
  try {
    const findResult = findConfigFile();
    if (!findResult.ok) {
      cachedConfigInterval = void 0;
      return void 0;
    }
    const configResult = loadConfig(findResult.value);
    if (!configResult.ok) {
      cachedConfigInterval = void 0;
      return void 0;
    }
    const val = configResult.value.updateCheckInterval;
    cachedConfigInterval = val;
    return val;
  } catch {
    cachedConfigInterval = void 0;
    return void 0;
  }
}
function runUpdateCheckAtStartup() {
  try {
    const configInterval = readConfigInterval();
    if (!isUpdateCheckEnabled(configInterval)) return;
    const state = readCheckState();
    const interval = configInterval ?? DEFAULT_INTERVAL_MS;
    if (!shouldRunCheck(state, interval)) return;
    spawnBackgroundCheck(CLI_VERSION);
  } catch {
  }
}
function printUpdateNotification() {
  try {
    const configInterval = readConfigInterval();
    if (!isUpdateCheckEnabled(configInterval)) return;
    const message = getUpdateNotification(CLI_VERSION);
    if (message) {
      process.stderr.write(`
${message}
`);
    }
  } catch {
  }
}

// src/skill/dispatch-session.ts
import { execSync } from "child_process";
import fs from "fs";
import path from "path";
var LAST_HEAD_FILE = ".harness/dispatch-last-head.txt";
function resolveGitRoot(projectPath) {
  try {
    return execSync("git rev-parse --show-toplevel", {
      cwd: projectPath,
      encoding: "utf8",
      stdio: ["pipe", "pipe", "pipe"]
    }).trim();
  } catch {
    return path.resolve(projectPath);
  }
}
function getCurrentHead(projectPath) {
  try {
    return execSync("git rev-parse HEAD", {
      cwd: projectPath,
      encoding: "utf8",
      stdio: ["pipe", "pipe", "pipe"]
    }).trim();
  } catch {
    return null;
  }
}
function readLastHead(projectPath) {
  const root = resolveGitRoot(projectPath);
  const filePath = path.join(root, LAST_HEAD_FILE);
  try {
    return fs.readFileSync(filePath, "utf8").trim();
  } catch {
    return null;
  }
}
function writeLastHead(projectPath, head) {
  const root = resolveGitRoot(projectPath);
  const filePath = path.join(root, LAST_HEAD_FILE);
  const dir = path.dirname(filePath);
  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir, { recursive: true });
  }
  fs.writeFileSync(filePath, head + "\n", "utf8");
}
function detectHeadDelta(projectPath) {
  const currentHead = getCurrentHead(projectPath);
  if (!currentHead) return null;
  const lastHead = readLastHead(projectPath);
  if (lastHead === null) {
    writeLastHead(projectPath, currentHead);
    return null;
  }
  if (currentHead === lastHead) return null;
  return currentHead;
}
async function sessionStartDispatch(projectPath) {
  const currentHead = detectHeadDelta(projectPath);
  if (!currentHead) {
    return { dispatched: false };
  }
  try {
    const result = await dispatchSkillsFromGit(projectPath, {});
    writeLastHead(projectPath, currentHead);
    return { dispatched: true, result, currentHead };
  } catch {
    writeLastHead(projectPath, currentHead);
    return { dispatched: false };
  }
}
function formatDispatchBanner(result) {
  if (!result.dispatched || !result.result || result.result.skills.length === 0) {
    return null;
  }
  const { context, skills } = result.result;
  const lines = [
    `Skill dispatch (${context.changeType} change, ${context.domains.length} domain(s)):`
  ];
  for (const skill of skills.slice(0, 3)) {
    const parallel = skill.parallelSafe ? " [parallel-safe]" : "";
    lines.push(
      `  ${skill.urgency === "critical" ? "!" : "-"} ${skill.name} (${skill.estimatedImpact} impact)${parallel}`
    );
  }
  if (skills.length > 3) {
    lines.push(`  ... and ${skills.length - 3} more`);
  }
  return lines.join("\n");
}

// src/bin/harness.ts
async function main() {
  printFirstRunWelcome();
  runUpdateCheckAtStartup();
  const dispatchPromise = sessionStartDispatch(process.cwd()).catch(
    () => ({ dispatched: false })
  );
  const program = createProgram();
  installCommandTelemetry(program, process.cwd());
  try {
    await program.parseAsync(process.argv);
  } catch (error) {
    handleError(error);
  }
  printUpdateNotification();
  const dispatchResult = await dispatchPromise;
  const banner = formatDispatchBanner(dispatchResult);
  if (banner) {
    console.error(`
${banner}
`);
  }
}
void main();
