import {
  installFactoryWrapper
} from "./chunk-KJ2GJLPQ.js";

// src/backends/factory/setup.ts
var SENTINEL_NAME = ".claude-agents-factory-installed";
async function prepareFactoryOnSandbox(sandboxName, provider) {
  await installFactoryWrapper(sandboxName, provider);
  const script = [
    "set -euo pipefail",
    `SENTINEL="$HOME/${SENTINEL_NAME}"`,
    'if [ -f "$SENTINEL" ]; then exit 0; fi',
    "npm install -g @factory/cli",
    "PREFIX=$(npm config get prefix)",
    'if [ "$PREFIX" != "/usr/local" ]; then ln -sf "$PREFIX/bin/droid" /usr/local/bin/droid; fi',
    "/usr/local/bin/droid --version || $PREFIX/bin/droid --version",
    'touch "$SENTINEL"'
  ].join(" && ");
  const result = await provider.exec(sandboxName, ["bash", "-c", script], {
    timeoutMs: 5 * 6e4
  });
  if (result.exit_code !== 0) {
    throw new Error(
      `factory install failed (${result.exit_code}): ${result.stderr.slice(0, 500)}`
    );
  }
}

export {
  prepareFactoryOnSandbox
};
