import {
  appendRunEvent,
  appendStageHeartbeat,
  createRunState,
  setRunStatus
} from "./chunk-EMWF2575.js";
import {
  noImplementationModeFor
} from "./chunk-X7SQTCIQ.js";
import {
  createRunResult
} from "./chunk-VY4Y5U57.js";
import {
  __export
} from "./chunk-MLKGABMK.js";

// src/runner.ts
var runner_exports = {};
__export(runner_exports, {
  runRiddleProof: () => runRiddleProof
});
function errorDetails(error) {
  if (error instanceof Error) {
    return {
      name: error.name,
      message: error.message
    };
  }
  return { message: String(error) };
}
function adapterBlocker(code, message, checkpoint, details) {
  return {
    code,
    message,
    checkpoint,
    details
  };
}
function blockRun(input) {
  input.state.blocker = input.blocker;
  appendRunEvent(input.state, {
    kind: "run.blocked",
    checkpoint: input.blocker.checkpoint,
    stage: input.stage,
    summary: input.blocker.message,
    details: {
      code: input.blocker.code,
      ...input.blocker.details
    }
  });
  setRunStatus(input.state, "blocked");
  return createRunResult({
    state: input.state,
    status: "blocked",
    last_summary: input.blocker.message,
    evidence_bundle: input.evidence_bundle,
    raw: input.raw
  });
}
function shouldIterate(assessment) {
  const nextStage = assessment.continue_with_stage || assessment.recommended_stage;
  return nextStage === "implement" || nextStage === "author";
}
async function notifyIfConfigured(input) {
  if (!input.notification) return input.result;
  try {
    const notification = await input.notification.notify({
      state: input.state,
      result: input.result
    });
    input.state.notification = notification;
    appendRunEvent(input.state, {
      kind: "notification.completed",
      checkpoint: "notification_completed",
      stage: "notify",
      summary: "Integration notification completed."
    });
  } catch (error) {
    appendRunEvent(input.state, {
      kind: "notification.failed",
      checkpoint: "notification_failed",
      stage: "notify",
      summary: "Integration notification failed.",
      details: errorDetails(error)
    });
  }
  return createRunResult({
    state: input.state,
    status: input.state.status,
    last_summary: input.result.last_summary,
    evidence_bundle: input.result.evidence_bundle,
    raw: input.result.raw
  });
}
async function runRiddleProof(input) {
  const state = input.state || createRunState({
    request: input.request,
    state_path: input.state_path,
    worktree_path: input.workdir
  });
  const adapters = input.adapters || {};
  const maxIterations = Math.max(1, Math.trunc(input.max_iterations ?? 1));
  appendRunEvent(state, {
    kind: "run.started",
    checkpoint: "run_started",
    stage: "setup",
    summary: "Riddle Proof run started.",
    details: {
      run_id: state.run_id,
      state_path: state.state_path,
      worktree_path: state.worktree_path,
      branch: state.branch,
      max_iterations: maxIterations,
      ship_mode: state.request.ship_mode || "none"
    }
  });
  appendStageHeartbeat(state, {
    stage: "setup",
    summary: "Setup stage is active.",
    details: {
      run_id: state.run_id,
      state_path: state.state_path,
      worktree_path: state.worktree_path,
      branch: state.branch
    }
  });
  let workdir = input.workdir;
  let evidenceContext;
  if (adapters.preflight) {
    appendRunEvent(state, {
      kind: "preflight.started",
      checkpoint: "preflight_started",
      stage: "preflight",
      summary: "Riddle Proof preflight adapter started."
    });
    try {
      const preflight = await adapters.preflight.preflight({ request: state.request, state });
      if (!preflight.ok) {
        return blockRun({
          state,
          stage: "preflight",
          blocker: adapterBlocker(
            "preflight_failed",
            "The preflight adapter found blocking tool or model configuration issues.",
            "preflight_failed",
            {
              blockers: preflight.blockers,
              warnings: preflight.warnings,
              degraded_capabilities: preflight.degraded_capabilities
            }
          ),
          raw: { preflight }
        });
      }
      appendRunEvent(state, {
        kind: "preflight.completed",
        checkpoint: "preflight_completed",
        stage: "preflight",
        summary: "Riddle Proof preflight adapter completed.",
        details: {
          warnings: preflight.warnings,
          degraded_capabilities: preflight.degraded_capabilities
        }
      });
    } catch (error) {
      return blockRun({
        state,
        stage: "preflight",
        blocker: adapterBlocker("preflight_exception", "The preflight adapter threw an exception.", "preflight_failed", errorDetails(error))
      });
    }
  }
  if (adapters.setup) {
    appendRunEvent(state, {
      kind: "setup.started",
      checkpoint: "setup_started",
      stage: "setup",
      summary: "Riddle Proof setup adapter started."
    });
    try {
      const setup = await adapters.setup.setup({ request: state.request, state });
      if (!setup.ok) {
        return blockRun({
          state,
          stage: "setup",
          blocker: adapterBlocker(
            "setup_failed",
            "The setup adapter did not complete successfully.",
            "setup_failed",
            { blockers: setup.blockers }
          ),
          raw: { setup }
        });
      }
      workdir = setup.worktree_path || setup.workdir || workdir;
      state.worktree_path = setup.worktree_path || setup.workdir || state.worktree_path;
      state.branch = setup.branch || state.branch;
      evidenceContext = setup.evidence_context;
      appendRunEvent(state, {
        kind: "setup.completed",
        checkpoint: "setup_completed",
        stage: "setup",
        summary: "Riddle Proof setup adapter completed.",
        details: {
          has_workdir: Boolean(workdir),
          worktree_path: state.worktree_path,
          branch: state.branch,
          cleanup_policy: setup.cleanup_policy,
          has_evidence_context: Boolean(evidenceContext)
        }
      });
    } catch (error) {
      return blockRun({
        state,
        stage: "setup",
        blocker: adapterBlocker("setup_exception", "The setup adapter threw an exception.", "setup_failed", errorDetails(error))
      });
    }
  }
  const changeRequest = state.request.change_request?.trim();
  const noImplementationMode = noImplementationModeFor(state.request);
  if (!changeRequest) {
    return blockRun({
      state,
      stage: "setup",
      blocker: adapterBlocker("change_request_required", "A change request is required before implementation.", "request_invalid")
    });
  }
  if (!noImplementationMode && !workdir) {
    return blockRun({
      state,
      stage: "setup",
      blocker: adapterBlocker("workdir_not_configured", "A workdir or setup adapter result is required before implementation.", "setup_required")
    });
  }
  if (!noImplementationMode && !adapters.implementation) {
    return blockRun({
      state,
      stage: "implement",
      blocker: adapterBlocker("implementation_adapter_not_configured", "An implementation adapter is required to change code.", "implementation_required")
    });
  }
  if (!adapters.proof) {
    return blockRun({
      state,
      stage: "prove",
      blocker: adapterBlocker("proof_adapter_not_configured", "A proof adapter is required to capture evidence.", "proof_required")
    });
  }
  if (!adapters.judge) {
    return blockRun({
      state,
      stage: "verify",
      blocker: adapterBlocker("judge_adapter_not_configured", "A judge adapter is required to assess proof.", "judge_required")
    });
  }
  let implementation;
  let evidenceBundle;
  let assessment;
  for (let attempt = 0; attempt < maxIterations; attempt += 1) {
    state.iterations += 1;
    if (noImplementationMode) {
      state.implementation_status = "not_required";
      appendRunEvent(state, {
        kind: "implementation.skipped",
        checkpoint: "implementation_not_required",
        stage: "implement",
        summary: "Implementation stage skipped because audit/no-diff mode disables code changes.",
        details: { iteration: state.iterations }
      });
    } else {
      appendStageHeartbeat(state, {
        stage: "implement",
        summary: "Implementation stage is active.",
        details: { iteration: state.iterations }
      });
      appendRunEvent(state, {
        kind: "implementation.started",
        checkpoint: "implementation_started",
        stage: "implement",
        summary: "Implementation adapter started.",
        details: { iteration: state.iterations }
      });
      try {
        implementation = await adapters.implementation.implement({
          workdir,
          change_request: changeRequest,
          evidence_context: evidenceContext,
          state
        });
      } catch (error) {
        return blockRun({
          state,
          stage: "implement",
          blocker: adapterBlocker("implementation_exception", "The implementation adapter threw an exception.", "implementation_failed", errorDetails(error)),
          evidence_bundle: evidenceBundle
        });
      }
      if (!implementation.ok) {
        return blockRun({
          state,
          stage: "implement",
          blocker: adapterBlocker(
            "implementation_failed",
            "The implementation adapter did not complete successfully.",
            "implementation_failed",
            { blockers: implementation.blockers }
          ),
          evidence_bundle: evidenceBundle,
          raw: { implementation }
        });
      }
      appendRunEvent(state, {
        kind: "implementation.completed",
        checkpoint: "implementation_completed",
        stage: "implement",
        summary: "Implementation adapter completed.",
        details: {
          changed_files: implementation.changed_files,
          tests_run: implementation.tests_run
        }
      });
    }
    appendStageHeartbeat(state, {
      stage: "prove",
      summary: "Proof capture stage is active.",
      details: {
        verification_mode: state.request.verification_mode
      }
    });
    appendRunEvent(state, {
      kind: "proof.started",
      checkpoint: "proof_started",
      stage: "prove",
      summary: "Proof adapter started."
    });
    try {
      const proof = await adapters.proof.prove({
        state,
        implementation,
        evidence_context: evidenceContext
      });
      if (!proof.ok || !proof.evidence_bundle) {
        return blockRun({
          state,
          stage: "prove",
          blocker: adapterBlocker(
            "proof_failed",
            "The proof adapter did not produce a usable evidence bundle.",
            "proof_failed",
            { blockers: proof.blockers }
          ),
          raw: { proof }
        });
      }
      evidenceBundle = proof.evidence_bundle;
      appendRunEvent(state, {
        kind: "proof.completed",
        checkpoint: "proof_completed",
        stage: "prove",
        summary: "Proof adapter completed.",
        details: {
          verification_mode: evidenceBundle.verification_mode,
          artifact_count: evidenceBundle.artifacts?.length ?? 0
        }
      });
    } catch (error) {
      return blockRun({
        state,
        stage: "prove",
        blocker: adapterBlocker("proof_exception", "The proof adapter threw an exception.", "proof_failed", errorDetails(error))
      });
    }
    appendRunEvent(state, {
      kind: "judge.started",
      checkpoint: "judge_started",
      stage: "verify",
      summary: "Judge adapter started."
    });
    appendStageHeartbeat(state, {
      stage: "verify",
      summary: "Verification stage is active.",
      details: {
        verification_mode: evidenceBundle.verification_mode
      }
    });
    try {
      assessment = await adapters.judge.assessProof({ state, evidence_bundle: evidenceBundle });
      state.proof_decision = assessment.decision;
      appendRunEvent(state, {
        kind: "judge.completed",
        checkpoint: "judge_completed",
        stage: "verify",
        summary: assessment.summary,
        details: {
          decision: assessment.decision,
          recommended_stage: assessment.recommended_stage,
          continue_with_stage: assessment.continue_with_stage,
          reasons: assessment.reasons
        }
      });
    } catch (error) {
      return blockRun({
        state,
        stage: "verify",
        blocker: adapterBlocker("judge_exception", "The judge adapter threw an exception.", "judge_failed", errorDetails(error)),
        evidence_bundle: evidenceBundle
      });
    }
    if (assessment.decision === "ready_to_ship") break;
    if (attempt + 1 < maxIterations && shouldIterate(assessment)) {
      evidenceContext = evidenceBundle;
      appendRunEvent(state, {
        kind: "run.iterating",
        checkpoint: "iteration_requested",
        stage: "implement",
        summary: "Judge requested another implementation iteration.",
        details: {
          decision: assessment.decision,
          next_iteration: state.iterations + 1
        }
      });
      continue;
    }
    return blockRun({
      state,
      stage: "verify",
      blocker: adapterBlocker(
        "proof_not_ready",
        assessment.summary || "Proof is not ready to ship.",
        "judge_completed",
        {
          decision: assessment.decision,
          recommended_stage: assessment.recommended_stage,
          continue_with_stage: assessment.continue_with_stage,
          reasons: assessment.reasons
        }
      ),
      evidence_bundle: evidenceBundle,
      raw: { assessment }
    });
  }
  if (!assessment || !evidenceBundle) {
    return blockRun({
      state,
      stage: "verify",
      blocker: adapterBlocker("runner_incomplete", "The runner ended without proof assessment.", "runner_incomplete")
    });
  }
  if (state.request.ship_mode !== "ship") {
    setRunStatus(state, "ready_to_ship");
    const result2 = createRunResult({
      state,
      status: "ready_to_ship",
      last_summary: assessment.summary,
      evidence_bundle: evidenceBundle,
      raw: { implementation, assessment }
    });
    return notifyIfConfigured({ state, result: result2, notification: adapters.notification });
  }
  if (!adapters.ship) {
    return blockRun({
      state,
      stage: "ship",
      blocker: adapterBlocker("ship_adapter_not_configured", "A ship adapter is required when ship_mode is ship.", "ship_required"),
      evidence_bundle: evidenceBundle,
      raw: { implementation, assessment }
    });
  }
  appendRunEvent(state, {
    kind: "ship.started",
    checkpoint: "ship_started",
    stage: "ship",
    summary: "Ship adapter started."
  });
  appendStageHeartbeat(state, {
    stage: "ship",
    summary: "Ship stage is active."
  });
  let metadata;
  try {
    metadata = await adapters.ship.ship({ state, assessment });
  } catch (error) {
    return blockRun({
      state,
      stage: "ship",
      blocker: adapterBlocker("ship_exception", "The ship adapter threw an exception.", "ship_failed", errorDetails(error)),
      evidence_bundle: evidenceBundle,
      raw: { implementation, assessment }
    });
  }
  appendRunEvent(state, {
    kind: "ship.completed",
    checkpoint: "ship_completed",
    stage: "ship",
    summary: "Ship adapter completed.",
    details: {
      pr_url: metadata.pr_url,
      marked_ready: metadata.marked_ready,
      finalized: metadata.finalized
    }
  });
  setRunStatus(state, "shipped");
  const result = createRunResult({
    state,
    status: "shipped",
    last_summary: "Riddle Proof shipped.",
    metadata,
    evidence_bundle: evidenceBundle,
    raw: { implementation, assessment }
  });
  return notifyIfConfigured({ state, result, notification: adapters.notification });
}

export {
  runRiddleProof,
  runner_exports
};
