import {
  __export
} from "./chunk-MLKGABMK.js";

// src/proof-run-core.ts
var proof_run_core_exports = {};
__export(proof_run_core_exports, {
  BUNDLED_RIDDLE_PROOF_DIR: () => BUNDLED_RIDDLE_PROOF_DIR,
  CHECKPOINT_CONTRACT_VERSION: () => CHECKPOINT_CONTRACT_VERSION,
  RIDDLE_PROOF_DIR_CANDIDATES: () => RIDDLE_PROOF_DIR_CANDIDATES,
  WORKFLOW_STAGE_ORDER: () => WORKFLOW_STAGE_ORDER,
  buildCheckpointContract: () => buildCheckpointContract,
  buildSetupArgs: () => buildSetupArgs,
  canonicalProofAssessmentStageForDecision: () => canonicalProofAssessmentStageForDecision,
  checkpointContinueStage: () => checkpointContinueStage,
  clearStageDecisionRequest: () => clearStageDecisionRequest,
  ensureAction: () => ensureAction,
  ensureStageLoopState: () => ensureStageLoopState,
  invalidateVerifyEvidence: () => invalidateVerifyEvidence,
  mergeStateFromParams: () => mergeStateFromParams,
  noImplementationModeFor: () => noImplementationModeFor,
  normalizeProofAssessmentStageFields: () => normalizeProofAssessmentStageFields,
  previewModeFromWorkflowMode: () => previewModeFromWorkflowMode,
  proofAssessmentHardBlockersForState: () => proofAssessmentHardBlockersForState,
  readState: () => readState,
  recordStageAttempt: () => recordStageAttempt,
  requiredBaselineLabelsForState: () => requiredBaselineLabelsForState,
  resolveConfig: () => resolveConfig,
  resolveRiddleProofDir: () => resolveRiddleProofDir,
  setStageDecisionRequest: () => setStageDecisionRequest,
  summarizeState: () => summarizeState,
  validateShipGate: () => validateShipGate,
  visualDeltaForState: () => visualDeltaForState,
  visualDeltaRequiredForState: () => visualDeltaRequiredForState,
  visualDeltaShipGateReason: () => visualDeltaShipGateReason,
  workflowFile: () => workflowFile,
  writeState: () => writeState
});
import { readFileSync, existsSync, writeFileSync } from "fs";
import { randomUUID } from "crypto";
import path from "path";
import { fileURLToPath } from "url";
var WORKFLOW_STAGE_ORDER = ["setup", "recon", "author", "implement", "verify", "ship"];
function canonicalProofAssessmentStageForDecision(decision) {
  const normalized = typeof decision === "string" ? decision.trim() : "";
  if (normalized === "ready_to_ship") return "ship";
  if (normalized === "needs_richer_proof") return "author";
  if (normalized === "revise_capture") return "verify";
  if (normalized === "needs_recon") return "recon";
  if (normalized === "needs_implementation") return "implement";
  return null;
}
function normalizeProofAssessmentStageFields(assessment) {
  const canonicalStage = canonicalProofAssessmentStageForDecision(assessment.decision);
  if (!canonicalStage) return assessment;
  return {
    ...assessment,
    recommended_stage: canonicalStage,
    continue_with_stage: canonicalStage
  };
}
function normalizedMode(value) {
  return typeof value === "string" ? value.trim().toLowerCase() : "";
}
function previewModeFromWorkflowMode(value) {
  const mode = normalizedMode(value);
  return mode === "server" || mode === "static" ? mode : void 0;
}
function noImplementationModeInput(value) {
  return value && typeof value === "object" ? value : {};
}
function noImplementationModeFor(params, state) {
  const input = noImplementationModeInput(params);
  const stateInput = noImplementationModeInput(state);
  const mode = normalizedMode(input.mode ?? input.workflow_mode ?? stateInput.mode ?? stateInput.workflow_mode);
  const implementationMode = normalizedMode(input.implementation_mode ?? stateInput.implementation_mode);
  const requireDiff = input.require_diff ?? stateInput.require_diff;
  const allowCodeChanges = input.allow_code_changes ?? stateInput.allow_code_changes;
  return mode === "audit" || mode === "profile" || implementationMode === "none" || requireDiff === false || allowCodeChanges === false;
}
function canRunSetupWithoutRepo(params) {
  return noImplementationModeFor(params) && Boolean((params.prod_url || "").trim());
}
var CHECKPOINT_CONTRACT_VERSION = "riddle-proof-run.checkpoint.v1";
function currentDistDir() {
  const meta = typeof import.meta === "object" ? import.meta : {};
  if (typeof meta.url === "string" && meta.url) {
    return path.dirname(fileURLToPath(meta.url));
  }
  if (typeof __dirname === "string") return __dirname;
  return process.cwd();
}
var BUNDLED_RIDDLE_PROOF_DIR = path.resolve(
  currentDistDir(),
  "..",
  "runtime"
);
var RIDDLE_PROOF_DIR_CANDIDATES = [
  BUNDLED_RIDDLE_PROOF_DIR
];
function createWorkflowStatePath() {
  const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:.TZ]/g, "").slice(0, 14);
  return `/tmp/riddle-proof-state-${stamp}-${randomUUID().slice(0, 8)}.json`;
}
function argsPathForStatePath(statePath) {
  const base = path.basename(statePath);
  if (base.startsWith("riddle-proof-state")) {
    return path.join(path.dirname(statePath), base.replace("riddle-proof-state", "riddle-proof-args"));
  }
  return path.join(path.dirname(statePath), `${base}.args.json`);
}
function isRiddleProofSkillDir(candidate) {
  return existsSync(workflowFile(candidate, "setup"));
}
function resolveRiddleProofDir(config = {}) {
  if (config.riddleProofDir) return config.riddleProofDir;
  const candidates = [
    process.env.RIDDLE_PROOF_DIR,
    ...RIDDLE_PROOF_DIR_CANDIDATES
  ].filter((candidate) => Boolean(candidate));
  return candidates.find(isRiddleProofSkillDir) || candidates.find((candidate) => existsSync(candidate)) || RIDDLE_PROOF_DIR_CANDIDATES[0];
}
function resolveConfig(config = {}, params = {}) {
  const configuredStatePath = config.statePath || "";
  const shouldIsolateWorkflow = !params.state_path && !configuredStatePath && (params.action === "setup" || params.action === "run");
  const statePath = params.state_path || (shouldIsolateWorkflow ? createWorkflowStatePath() : configuredStatePath || "/tmp/riddle-proof-state.json");
  return {
    riddleProofDir: resolveRiddleProofDir(config),
    statePath,
    argsPath: argsPathForStatePath(statePath),
    defaultReviewer: config.defaultReviewer || "davisdiehl"
  };
}
function ensureAction(action) {
  if (["setup", "recon", "author", "implement", "verify", "ship", "status", "sync", "run"].includes(action)) {
    return action;
  }
  throw new Error(`Unsupported action: ${action}`);
}
function workflowFile(riddleProofDir, action) {
  return path.join(riddleProofDir, "pipelines", `riddle-proof-${action}.lobster`);
}
function asJsonString(value) {
  if (value === void 0 || value === null || value === "") return "";
  if (typeof value === "string") return value;
  return JSON.stringify(value);
}
function buildSetupArgs(params, config) {
  if (!params.repo && !canRunSetupWithoutRepo(params)) throw new Error("repo is required for setup/run");
  if (!params.change_request) throw new Error("change_request is required for setup/run");
  const commitMessage = (params.commit_message || params.change_request || "").trim();
  const captureScript = (params.capture_script || "").trim();
  const requestedReference = params.reference || (params.prod_url ? "both" : "before");
  if (!commitMessage) throw new Error("commit_message is required for setup/run");
  return {
    repo: params.repo,
    branch: params.branch || "",
    change_request: params.change_request,
    commit_message: commitMessage,
    prod_url: params.prod_url || "",
    capture_script: captureScript,
    success_criteria: params.success_criteria || "",
    assertions_json: params.assertions_json || asJsonString(params.assertions),
    verification_mode: params.verification_mode || "proof",
    resume_session: params.resume_session || "",
    target_image_url: params.target_image_url || "",
    target_image_hash: params.target_image_hash || "",
    viewport_matrix_json: params.viewport_matrix_json || asJsonString(params.viewport_matrix),
    deterministic_setup_json: params.deterministic_setup_json || asJsonString(params.deterministic_setup),
    reference: requestedReference,
    base_branch: params.base_branch || "main",
    before_ref: params.before_ref || "",
    allow_static_preview_fallback: params.allow_static_preview_fallback ? "true" : "",
    context: params.context || "",
    reviewer: params.reviewer || config.defaultReviewer,
    mode: previewModeFromWorkflowMode(params.mode) || "",
    implementation_mode: params.implementation_mode || "",
    require_diff: params.require_diff,
    allow_code_changes: params.allow_code_changes,
    build_command: params.build_command || "npm run build",
    build_output: params.build_output || "build",
    server_image: params.server_image || "node:20-slim",
    server_command: params.server_command || "npm start",
    server_port: String(params.server_port || 3e3),
    server_path: params.server_path || "",
    server_path_source: params.server_path ? "user" : "",
    use_auth: params.use_auth ? "true" : "",
    auth_localStorage_json: params.auth_localStorage_json || "",
    auth_cookies_json: params.auth_cookies_json || "",
    auth_headers_json: params.auth_headers_json || "",
    color_scheme: params.color_scheme || "",
    wait_for_selector: params.wait_for_selector || "",
    discord_channel: params.discord_channel || "",
    discord_thread_id: params.discord_thread_id || "",
    discord_message_id: params.discord_message_id || "",
    discord_source_url: params.discord_source_url || "",
    ship_mode: params.ship_mode || "",
    leave_draft: params.leave_draft ? "true" : ""
  };
}
function readState(statePath) {
  if (!existsSync(statePath)) {
    return null;
  }
  return JSON.parse(readFileSync(statePath, "utf-8"));
}
function writeState(statePath, state) {
  writeFileSync(statePath, JSON.stringify(state, null, 2));
}
function normalizeOptionalString(value) {
  return typeof value === "string" ? value.trim() : void 0;
}
var INTERACTION_VERIFICATION_MODES = /* @__PURE__ */ new Set(["interaction", "interactive", "user_flow", "user-flow", "workflow"]);
function normalizeRoutePath(value) {
  const raw = typeof value === "string" ? value.trim() : "";
  if (!raw) return "";
  try {
    const url = /^https?:\/\//i.test(raw) ? new URL(raw) : new URL(raw.startsWith("/") || raw.startsWith("?") || raw.startsWith("#") ? raw : `/${raw}`, "https://riddle-proof.local");
    const pathname = url.pathname.replace(/\/+$/, "") || "/";
    return `${pathname}${url.search}${url.hash}`;
  } catch {
    const hashSplit = raw.split("#");
    const beforeHash = hashSplit.shift() || "";
    const hash = hashSplit.length ? `#${hashSplit.join("#")}` : "";
    const querySplit = beforeHash.split("?");
    const rawPath = querySplit.shift() || "";
    const query = querySplit.length ? `?${querySplit.join("?")}` : "";
    const pathname = `/${rawPath}`.replace(/\/+/g, "/").replace(/\/+$/, "") || "/";
    return `${pathname}${query}${hash}`;
  }
}
function trimRouteCandidate(value) {
  return value.trim().replace(/[),.;\]}]+$/g, "");
}
function expectedTerminalRouteFromText(value) {
  const text = typeof value === "string" ? value.trim() : "";
  if (!text) return "";
  const routePattern = "(https?:\\/\\/[^\\s\"'<>`]+|\\/[^\\s\"'<>`]+)";
  const patterns = [
    new RegExp(`\\bexpected\\s+(?:terminal\\s+|final\\s+|after\\s+)?(?:url|route|path)\\s*(?:is|=|:)\\s*${routePattern}`, "i"),
    new RegExp(`\\b(?:terminal|final|after)\\s+(?:url|route|path)\\s*(?:is|=|:)\\s*${routePattern}`, "i"),
    new RegExp(`\\b(?:ends|end|ending|lands|land|landing)\\s+(?:at|on)\\s*${routePattern}`, "i")
  ];
  for (const pattern of patterns) {
    const match = text.match(pattern);
    if (!match) continue;
    const candidate = trimRouteCandidate(match[1] || "");
    const normalized = normalizeRoutePath(candidate);
    if (normalized) return normalized;
  }
  return "";
}
function requestedExpectedTerminalRouteForState(state) {
  return expectedTerminalRouteFromText(state.success_criteria) || expectedTerminalRouteFromText(state.change_request) || expectedTerminalRouteFromText(state.context) || expectedTerminalRouteFromText(state.assertions_json);
}
function isInteractionVerificationMode(value) {
  return INTERACTION_VERIFICATION_MODES.has(typeof value === "string" ? value.trim().toLowerCase() : "");
}
function stringRecordValue(record, key) {
  if (!record || typeof record !== "object") return "";
  const value = record[key];
  return typeof value === "string" ? value.trim() : "";
}
function appendStateWarning(state, key, warning) {
  const existing = Array.isArray(state[key]) ? state[key].filter((item) => typeof item === "string") : [];
  if (!existing.includes(warning)) state[key] = [...existing, warning];
}
function applyRequestedInteractionRouteContract(state) {
  if (!isInteractionVerificationMode(state.verification_mode)) return;
  const requestedTerminal = requestedExpectedTerminalRouteForState(state);
  if (!requestedTerminal) return;
  state.requested_expected_terminal_path = requestedTerminal;
  if (!stringRecordValue(state, "expected_terminal_path")) {
    state.expected_terminal_path = requestedTerminal;
  }
  const startPath = normalizeRoutePath(state.server_path) || normalizeRoutePath(state.expected_start_path) || "/";
  if (!stringRecordValue(state, "expected_start_path")) {
    state.expected_start_path = startPath;
  }
  const existingContract = state.interaction_contract && typeof state.interaction_contract === "object" ? state.interaction_contract : {};
  state.interaction_contract = {
    ...existingContract,
    start_path: stringRecordValue(existingContract, "start_path") || startPath,
    expected_terminal_path: stringRecordValue(existingContract, "expected_terminal_path") || requestedTerminal
  };
}
function interactionStartPathForAuthorPacket(state, parsed, refined) {
  return normalizeRoutePath(
    stringRecordValue(state, "expected_start_path") || stringRecordValue(refined, "expected_start_path") || stringRecordValue(parsed.interaction_contract, "start_path") || stringRecordValue(parsed.proof_contract, "start_path") || stringRecordValue(state, "server_path") || "/"
  ) || "/";
}
function authorPacketServerPath(state, parsed, refined, serverPath, expectedTerminalPath) {
  if (!isInteractionVerificationMode(state.verification_mode)) return serverPath;
  const startPath = interactionStartPathForAuthorPacket(state, parsed, refined);
  state.expected_start_path = startPath;
  if (expectedTerminalPath && normalizeRoutePath(serverPath) === normalizeRoutePath(expectedTerminalPath) && normalizeRoutePath(serverPath) !== startPath) {
    appendStateWarning(
      state,
      "author_warnings",
      "Supervisor packet refined_inputs.server_path matched the terminal interaction route; kept the recon start route for capture."
    );
    return startPath;
  }
  return serverPath;
}
function knownEnvironmentIssuesFromNotes(notes) {
  const text = notes.toLowerCase();
  const issues = [];
  if ((text.includes("erofs") || text.includes("read-only file system")) && text.includes("node_modules") && (text.includes(".vite-temp") || text.includes("vite.config"))) {
    issues.push({
      code: "vite_temp_config_erofs",
      source: "implementation_notes",
      severity: "environment",
      summary: "Focused build verification hit the known Vite temp-config EROFS issue in shared node_modules."
    });
  }
  return issues;
}
function guardProofEvidenceGlobalAssignments(script) {
  return script.replace(
    /^(\s*)(globalThis|window|self)\.__riddleProofEvidence\s*=\s*([^;\n]+);\s*$/gm,
    (_match, indent, root, expression) => `${indent}try { if (typeof ${root} !== "undefined" && ${root}) ${root}.__riddleProofEvidence = ${expression.trim()}; } catch {}`
  );
}
function normalizeCaptureScript(value) {
  const script = normalizeOptionalString(value) || "";
  return script ? guardProofEvidenceGlobalAssignments(script) : "";
}
function compactCaptureScriptForHeuristics(script) {
  return script.replace(/\/\*[\s\S]*?\*\//g, " ").replace(/(^|[^:])\/\/.*$/gm, "$1 ").replace(/\s+/g, " ").trim().toLowerCase();
}
function interactionCaptureScriptLooksPassive(script) {
  const text = compactCaptureScriptForHeuristics(script);
  if (!text) return true;
  const actionPatterns = [
    /\bpage\.(click|dblclick|tap|fill|press|type|check|uncheck|selectoption|dispatch(event)?|goto|reload)\s*\(/,
    /\blocator\s*\([^)]*\)\s*\.\s*(click|dblclick|tap|fill|press|type|check|uncheck|selectoption|dispatch(event)?)\s*\(/,
    /\b(getby(role|text|label|testid|placeholder|title)|getbyalttext)\s*\([^)]*\)\s*\.\s*(click|dblclick|tap|fill|press|type|check|uncheck|selectoption)\s*\(/,
    /\bkeyboard\s*\.\s*(press|type|inserttext)\s*\(/,
    /\bmouse\s*\.\s*(click|dblclick|down|up|move)\s*\(/,
    /\btouchscreen\s*\.\s*tap\s*\(/
  ];
  if (actionPatterns.some((pattern) => pattern.test(text))) return false;
  const evidencePatterns = [
    /\breturn\s+[{[]/,
    /\breturn\s+\w+/,
    /__riddleproofevidence/,
    /\bproof_evidence\b/,
    /\brouteexpectationsource\b/,
    /\bexpectedurl\b/,
    /\bassertions?\b/
  ];
  return !evidencePatterns.some((pattern) => pattern.test(text));
}
function setStructuredInteractionCaptureFailure(state, summary) {
  const existing = typeof state.structured_interaction_capture_failure_summary === "string" ? state.structured_interaction_capture_failure_summary.trim() : "";
  if (!existing) state.structured_interaction_capture_failure_summary = summary;
}
function appendProofSummaryLine(state, line) {
  const text = String(line || "").trim();
  if (!text) return;
  const existing = typeof state.proof_summary === "string" ? state.proof_summary.trim() : "";
  if (existing.includes(text)) return;
  state.proof_summary = existing ? `${existing}
${text}` : text;
}
function hasAuthoredProofPlan(state = {}) {
  return Boolean((state?.proof_plan || "").trim()) && Boolean((state?.capture_script || "").trim());
}
function syncAuthoringState(state = {}) {
  const reconReady = ["ready_for_proof_plan", "completed"].includes(state?.recon_status || "");
  const reconBlocked = ["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "");
  const reconExhausted = state?.recon_status === "exhausted";
  if (reconBlocked) {
    state.author_status = "needs_recon_judgment";
  } else if (reconExhausted) {
    state.author_status = "recon_exhausted";
  } else if (!reconReady) {
    state.author_status = state.author_status || "pending_recon";
  } else if (state.author_status === "ready" || state.proof_plan_status === "ready") {
    state.author_status = "ready";
  } else {
    state.author_status = "needs_authoring";
  }
  if (reconBlocked) {
    state.proof_plan_status = "needs_recon_judgment";
  } else if (reconExhausted) {
    state.proof_plan_status = "recon_exhausted";
  } else if (state.author_status === "ready" || state.proof_plan_status === "ready") {
    state.proof_plan_status = "ready";
  } else if (reconReady) {
    state.proof_plan_status = "needs_authoring";
  } else {
    state.proof_plan_status = state.proof_plan_status || "pending_recon";
  }
  state.author_request = state.author_request || {};
  state.author_summary = state.author_summary || "";
  return state;
}
function ensureStageLoopState(state = {}) {
  if (!state || typeof state !== "object") return state;
  syncAuthoringState(state);
  state.explicit_stage_gate = true;
  state.stage_attempts = state.stage_attempts || {};
  for (const stage of WORKFLOW_STAGE_ORDER) {
    const current = state.stage_attempts[stage] || {};
    state.stage_attempts[stage] = {
      count: Number(current.count || 0),
      last_status: current.last_status || null,
      last_checkpoint: current.last_checkpoint || null,
      last_summary: current.last_summary || null,
      last_attempted_at: current.last_attempted_at || null,
      history: Array.isArray(current.history) ? current.history : []
    };
  }
  state.stage_decision_request = state.stage_decision_request || {};
  state.active_checkpoint = state.active_checkpoint || null;
  state.active_checkpoint_stage = state.active_checkpoint_stage || null;
  state.last_requested_advance_stage = state.last_requested_advance_stage || null;
  return state;
}
function clearStageDecisionRequest(state = {}) {
  ensureStageLoopState(state);
  state.stage_decision_request = {};
  state.active_checkpoint = null;
  state.active_checkpoint_stage = null;
  return state;
}
function recordStageAttempt(state, stage, entry = {}) {
  ensureStageLoopState(state);
  const bucket = state.stage_attempts[stage];
  const attemptNumber = Number(bucket.count || 0) + 1;
  const item = {
    attempt: attemptNumber,
    stage,
    status: entry.status || "completed",
    checkpoint: entry.checkpoint || null,
    summary: entry.summary || null,
    requested_advance_stage: entry.requestedAdvanceStage || null,
    halted_for_approval: Boolean(entry.haltedForApproval),
    auto_approved: Boolean(entry.autoApproved),
    retryable: Boolean(entry.retryable),
    checkpoint_disposition: entry.checkpointDisposition || null,
    error: entry.error || null,
    details: entry.details || {},
    attempted_at: (/* @__PURE__ */ new Date()).toISOString()
  };
  bucket.count = attemptNumber;
  bucket.last_status = item.status;
  bucket.last_checkpoint = item.checkpoint;
  bucket.last_summary = item.summary;
  bucket.last_attempted_at = item.attempted_at;
  bucket.history = [...bucket.history, item].slice(-25);
  state.stage_attempts[stage] = bucket;
  state.last_requested_advance_stage = entry.requestedAdvanceStage || state.last_requested_advance_stage || null;
  return item;
}
function setStageDecisionRequest(state, request) {
  ensureStageLoopState(state);
  const continueWithStage = request.continueWithStage || request.recommendedAdvanceStage || null;
  state.active_checkpoint = request.checkpoint;
  state.active_checkpoint_stage = request.stage;
  state.stage_decision_request = {
    stage: request.stage,
    checkpoint: request.checkpoint,
    summary: request.summary,
    next_actions: request.nextActions || [],
    advance_options: request.advanceOptions || [],
    recommended_advance_stage: request.recommendedAdvanceStage || null,
    continue_from_checkpoint: Boolean(continueWithStage),
    continue_with_stage: continueWithStage,
    blocking: Boolean(request.blocking),
    details: request.details || {},
    checkpoint_contract: request.checkpointContract || null,
    updated_at: (/* @__PURE__ */ new Date()).toISOString()
  };
  return state.stage_decision_request;
}
function checkpointContinueStage(state) {
  ensureStageLoopState(state);
  const stage = state?.stage_decision_request?.continue_with_stage || state?.stage_decision_request?.recommended_advance_stage || null;
  return WORKFLOW_STAGE_ORDER.includes(stage) ? stage : null;
}
function invalidateVerifyEvidence(state = {}) {
  if (!state || typeof state !== "object") {
    return { invalidated: false };
  }
  const hadAfterCdn = typeof state.after_cdn === "string" && state.after_cdn.trim().length > 0;
  const hadVerifyResults = Boolean(state.verify_results && Object.keys(state.verify_results).length > 0);
  const hadMergeRecommendation = typeof state.merge_recommendation === "string" && state.merge_recommendation.trim().length > 0;
  const hadProofSummary = typeof state.proof_summary === "string" && state.proof_summary.trim().length > 0;
  const hadVerifyStatus = typeof state.verify_status === "string" && state.verify_status.trim().length > 0;
  const hadVerifySummary = typeof state.verify_summary === "string" && state.verify_summary.trim().length > 0;
  const hadVerifyDecisionRequest = Boolean(state.verify_decision_request && Object.keys(state.verify_decision_request).length > 0);
  const hadEvidenceNotes = Array.isArray(state.evidence_notes) && state.evidence_notes.length > 0;
  const hadProofAssessment = Boolean(state.proof_assessment && Object.keys(state.proof_assessment).length > 0);
  const hadProofAssessmentRequest = Boolean(state.proof_assessment_request && Object.keys(state.proof_assessment_request).length > 0);
  const invalidated = hadAfterCdn || hadVerifyResults || hadMergeRecommendation || hadProofSummary || hadVerifyStatus || hadVerifySummary || hadVerifyDecisionRequest || hadEvidenceNotes || hadProofAssessment || hadProofAssessmentRequest;
  if (invalidated) {
    state.after_cdn = "";
    state.verify_results = {};
    state.verify_status = "";
    state.verify_summary = "";
    state.verify_decision_request = {};
    state.merge_recommendation = null;
    state.proof_summary = "";
    state.evidence_notes = [];
    state.proof_assessment = {};
    state.proof_assessment_source = null;
    state.proof_assessment_request = {};
  }
  return {
    invalidated,
    hadAfterCdn,
    hadVerifyResults,
    hadMergeRecommendation,
    hadProofSummary,
    hadVerifyStatus,
    hadVerifySummary,
    hadVerifyDecisionRequest,
    hadEvidenceNotes,
    hadProofAssessment,
    hadProofAssessmentRequest
  };
}
function normalizedReference(state = {}) {
  const reference = String(state?.requested_reference || state?.reference || "before").trim();
  return reference || "before";
}
function normalizedProofAssessment(state = {}) {
  const proofAssessment = state?.proof_assessment || {};
  const source = String(proofAssessment?.source || state?.proof_assessment_source || "").trim().toLowerCase();
  const decision = String(proofAssessment?.decision || "").trim();
  return {
    decision: decision || null,
    source: source || null
  };
}
var VISUAL_FIRST_MODES = /* @__PURE__ */ new Set([
  "visual",
  "render",
  "ui",
  "layout",
  "screenshot",
  "canvas",
  "animation"
]);
function objectValue(value) {
  return value && typeof value === "object" && !Array.isArray(value) ? value : {};
}
function normalizedVerificationMode(state = {}) {
  const bundle = objectValue(state?.evidence_bundle);
  const bundleMode = String(bundle.verification_mode || "").trim().toLowerCase();
  if (bundleMode) return bundleMode;
  return String(state?.verification_mode || "proof").trim().toLowerCase() || "proof";
}
function visualDeltaRequiredForState(state = {}) {
  if (noImplementationModeFor(state) || visualDeltaNotApplicableForNoImplementation(state)) return false;
  const bundle = objectValue(state?.evidence_bundle);
  const contract = objectValue(bundle.artifact_contract);
  const required = objectValue(contract.required);
  if (required.visual_delta === false) return false;
  return required.visual_delta === true || VISUAL_FIRST_MODES.has(normalizedVerificationMode(state));
}
function visualDeltaForState(state = {}) {
  const bundle = objectValue(state?.evidence_bundle);
  const after = objectValue(bundle.after);
  const afterDelta = objectValue(after.visual_delta);
  if (Object.keys(afterDelta).length) return afterDelta;
  const request = objectValue(state?.proof_assessment_request);
  return objectValue(request.visual_delta);
}
function visualDeltaNotApplicableForNoImplementation(state = {}) {
  const visualDelta = visualDeltaForState(state);
  if (String(visualDelta.status || "").trim() !== "not_applicable") return false;
  const reason = String(visualDelta.reason || "").toLowerCase();
  return reason.includes("audit/no-diff") || reason.includes("does not require a before/after implementation delta");
}
function visualDeltaShipGateReason(state = {}) {
  if (!visualDeltaRequiredForState(state)) return null;
  const visualDelta = visualDeltaForState(state);
  if (visualDelta.status === "measured" && visualDelta.passed === true) return null;
  const status = String(visualDelta.status || "missing");
  if (status === "unmeasured") {
    const reason2 = String(visualDelta.reason || "").trim();
    return reason2 ? `visual_delta.status=unmeasured blocks ready_to_ship for visual/UI proof: ${reason2}` : "visual_delta.status=unmeasured blocks ready_to_ship for visual/UI proof";
  }
  if (status === "measured" && visualDelta.passed === false) {
    return "visual_delta.status=measured but visual_delta.passed=false blocks ready_to_ship for visual/UI proof";
  }
  const reason = String(visualDelta.reason || "").trim();
  if (reason) return `visual_delta.status=${status} blocks ready_to_ship for visual/UI proof: ${reason}`;
  return `visual_delta.status=${status} blocks ready_to_ship for visual/UI proof`;
}
function proofAssessmentHardBlockersForState(state = {}) {
  const request = objectValue(state?.proof_assessment_request);
  const blockers = [];
  const add = (value) => {
    if (typeof value !== "string") return;
    const trimmed = value.trim();
    if (trimmed && !blockers.includes(trimmed)) blockers.push(trimmed);
  };
  if (Array.isArray(request.hard_blockers)) {
    for (const blocker of request.hard_blockers) add(blocker);
  }
  add(state?.structured_interaction_capture_failure_summary);
  add(state?.structured_interaction_failure_summary);
  if (isInteractionVerificationMode(normalizedVerificationMode(state)) && !stateHasProofEvidence(state)) {
    add("interaction proof evidence is required before ready_to_ship; proof_evidence_present=false");
  }
  const mergeRecommendation = String(state?.merge_recommendation || "").trim();
  if (mergeRecommendation === "do-not-merge" && blockers.length) {
    add("merge_recommendation=do-not-merge because the proof bundle contains hard blockers.");
  }
  return blockers;
}
function visualDeltaEvidenceIssueCode(state = {}, blocker = "") {
  const visualDelta = visualDeltaForState(state || {});
  const status = String(visualDelta.status || "").trim();
  const reason = `${String(visualDelta.reason || "")}
${blocker}`.toLowerCase();
  if (status === "unmeasured") {
    if (reason.includes("fetch") || reason.includes("allowlist") || reason.includes("registered domain") || reason.includes("high risk") || reason.includes("comparator")) {
      return "comparator_fetch_blocked";
    }
    return "visual_delta_unmeasured";
  }
  if (status === "measured" && visualDelta.passed === false) return "semantic_proof_failed";
  if (visualDeltaRequiredForState(state || {})) return "visual_delta_unmeasured";
  return "semantic_proof_failed";
}
function requiredBaselineLabelsForState(state = {}) {
  const reference = normalizedReference(state);
  const labels = [];
  if (reference === "before" || reference === "both") labels.push("before");
  if (reference === "prod" || reference === "both") labels.push("prod");
  return labels;
}
function stateHasAfterEvidence(state = {}) {
  if (String(state?.after_cdn || "").trim()) return true;
  const bundle = objectValue(state?.evidence_bundle);
  const after = objectValue(bundle.after);
  const observation = objectValue(after.observation);
  const supporting = objectValue(after.supporting_artifacts);
  return Boolean(
    observation.valid === true && (supporting.has_structured_payload === true || stateHasProofEvidence(state) || observation.telemetry_ready === true)
  );
}
function stateHasProofEvidence(state = {}) {
  if (state?.proof_evidence_present === true) return true;
  if (state?.proof_evidence !== void 0 && state?.proof_evidence !== null) {
    if (typeof state.proof_evidence !== "object") return true;
    if (Object.keys(objectValue(state.proof_evidence)).length > 0) return true;
  }
  const bundle = objectValue(state?.evidence_bundle);
  const after = objectValue(bundle.after);
  const supporting = objectValue(after.supporting_artifacts);
  const request = objectValue(state?.proof_assessment_request);
  const structuredEvidence = objectValue(request.structured_evidence);
  return Boolean(
    supporting.proof_evidence_present === true || structuredEvidence.proof_evidence_present === true || Object.keys(objectValue(bundle.proof_evidence)).length > 0 || Object.keys(objectValue(after.proof_evidence)).length > 0
  );
}
function validateShipGate(state = {}) {
  const reference = normalizedReference(state);
  const prodUrl = String(state?.prod_url || "").trim();
  const beforeCdn = String(state?.before_cdn || "").trim();
  const prodCdn = String(state?.prod_cdn || "").trim();
  const afterCdn = String(state?.after_cdn || "").trim();
  const verifyStatus = String(state?.verify_status || "").trim();
  const proofAssessment = normalizedProofAssessment(state);
  const verificationMode = normalizedVerificationMode(state);
  const visualDelta = visualDeltaForState(state);
  const visualDeltaRequired = visualDeltaRequiredForState(state);
  const visualDeltaBlocker = visualDeltaShipGateReason(state);
  const hardBlockers = proofAssessmentHardBlockersForState(state);
  const reasons = [];
  if (!["before", "prod", "both"].includes(reference)) {
    reasons.push(`reference must be before, prod, or both; got ${reference}`);
  }
  const requiredBaselines = requiredBaselineLabelsForState(state);
  if (requiredBaselines.includes("before") && !beforeCdn) {
    reasons.push("before_cdn is required before ship");
  }
  if (requiredBaselines.includes("prod")) {
    if (!prodUrl) {
      reasons.push(`prod_url is required when reference=${reference}`);
    }
    if (!prodCdn) {
      reasons.push("prod_cdn is required before ship");
    }
  }
  if (!stateHasAfterEvidence(state)) {
    reasons.push("after_cdn is required before ship");
  }
  if (verifyStatus !== "evidence_captured") {
    reasons.push("verify_status must be evidence_captured before ship");
  }
  if (!["supervising_agent", "supervisor"].includes(proofAssessment.source || "")) {
    reasons.push("proof_assessment.source must be supervising_agent before ship");
  }
  if (proofAssessment.decision !== "ready_to_ship") {
    reasons.push("proof_assessment.decision must be ready_to_ship before ship");
  }
  if (visualDeltaBlocker) {
    reasons.push(visualDeltaBlocker);
  }
  for (const blocker of hardBlockers) {
    reasons.push(`proof hard blocker prevents ready_to_ship: ${blocker}`);
  }
  return {
    ok: reasons.length === 0,
    reasons,
    required_baselines: requiredBaselines,
    evidence: {
      reference,
      verification_mode: verificationMode || null,
      prod_url: prodUrl || null,
      before_cdn: beforeCdn || null,
      prod_cdn: prodCdn || null,
      after_cdn: afterCdn || null,
      verify_status: verifyStatus || null,
      proof_assessment_decision: proofAssessment.decision,
      proof_assessment_source: proofAssessment.source,
      visual_delta_required: visualDeltaRequired,
      visual_delta_status: typeof visualDelta.status === "string" ? visualDelta.status : null,
      visual_delta_passed: typeof visualDelta.passed === "boolean" ? visualDelta.passed : null,
      hard_blockers: hardBlockers
    }
  };
}
var CHECKPOINT_CONTRACT_SPECS = {
  setup_review: {
    purpose: "Inspect prepared workspace state before moving into recon."
  },
  recon_supervisor_judgment: {
    purpose: "Supervising agent judges the latest recon attempt and either retries recon, promotes baselines for authoring, or escalates.",
    accepted_inputs: [{
      name: "recon_assessment_json",
      type: "json",
      required: true,
      description: "JSON assessment with decision, summary, baseline_understanding, continue_with_stage, escalation_target, refined_inputs, and reasons."
    }],
    response_schema: {
      decision: ["retry_recon", "ready_for_author", "recon_stuck"],
      summary: "string",
      baseline_understanding: {
        reference: ["before", "prod", "both", "unknown"],
        target_route: "string",
        before_evidence_url: "string",
        visible_before_state: "string",
        relevant_elements: ["string"],
        requested_change: "string",
        proof_focus: "string",
        stop_condition: "string",
        quality_risks: ["string"]
      },
      continue_with_stage: ["recon", "author"],
      escalation_target: ["agent", "human"],
      refined_inputs: {
        server_path: "string",
        wait_for_selector: "string",
        reference: ["before", "prod", "both"]
      },
      reasons: ["string"],
      source: "supervising_agent"
    },
    required_state: ["recon_assessment_request", "recon_results.attempt_history"]
  },
  recon_review: {
    purpose: "Recon baselines were promoted and the workflow is ready for proof authoring."
  },
  recon_human_escalation: {
    purpose: "Recon is explicitly blocked for human direction after supervising-agent escalation."
  },
  author_supervisor_judgment: {
    purpose: "Supervising agent authors the proof packet from recon observations.",
    accepted_inputs: [{
      name: "author_packet_json",
      type: "json",
      required: true,
      description: "JSON proof packet with proof_plan, capture_script, optional refined_inputs, rationale, confidence, and summary."
    }],
    response_schema: {
      proof_plan: "string",
      capture_script: "string",
      baseline_understanding_used: {
        reference: ["before", "prod", "both", "unknown"],
        target_route: "string",
        before_evidence_url: "string",
        visible_before_state: "string",
        relevant_elements: ["string"],
        requested_change: "string",
        proof_focus: "string",
        stop_condition: "string",
        quality_risks: ["string"]
      },
      refined_inputs: {
        server_path: "string",
        wait_for_selector: "string",
        reference: ["before", "prod", "both"]
      },
      rationale: ["string"],
      confidence: "low | medium | high",
      summary: "string"
    },
    required_state: ["author_request", "recon_results.baselines"]
  },
  author_review: {
    purpose: "Proof packet is ready; choose whether to inspect, re-author, implement, or verify."
  },
  implement_changes_missing: {
    purpose: "Implementation stage did not detect material code changes.",
    accepted_inputs: [{
      name: "make_code_changes",
      type: "external_action",
      required: true,
      description: "Make the actual code changes in the after worktree, then resume the implement stage."
    }]
  },
  implement_review: {
    purpose: "Implementation changes were detected and stale verify evidence, if any, was invalidated."
  },
  implement_required: {
    purpose: "Verify cannot run until implementation changes are recorded.",
    accepted_inputs: [{
      name: "make_code_changes",
      type: "external_action",
      required: true,
      description: "Make the requested code change, then advance the run to implement."
    }]
  },
  verify_capture_retry: {
    purpose: "Verify capture was incomplete; the agent loop should revise capture authoring before shipping.",
    accepted_inputs: [{
      name: "author_packet_json",
      type: "json",
      description: "Optional revised proof packet if returning to author."
    }],
    required_state: ["verify_decision_request"]
  },
  verify_capture_blocked: {
    purpose: "Verify capture produced conclusive failed browser evidence and should stop instead of retrying proof authoring.",
    required_state: ["verify_decision_request"]
  },
  verify_supervisor_judgment: {
    purpose: "Supervising agent judges whether captured evidence proves the change is ready to ship.",
    accepted_inputs: [{
      name: "proof_assessment_json",
      type: "json",
      required: true,
      description: "JSON assessment with decision, summary, recommended_stage, continue_with_stage, escalation_target, and reasons."
    }],
    response_schema: {
      decision: ["ready_to_ship", "needs_richer_proof", "revise_capture"],
      summary: "string",
      recommended_stage: ["ship", "author", "implement", "recon", "verify"],
      continue_with_stage: ["ship", "author", "implement", "recon", "verify"],
      escalation_target: ["agent", "human"],
      reasons: ["string"],
      source: "supervising_agent"
    },
    required_state: ["before_cdn or prod_cdn", "after_cdn", "proof_assessment_request"],
    include_ship_gate: true
  },
  verify_human_escalation: {
    purpose: "Proof loop is explicitly escalated to the human after supervising-agent judgment."
  },
  verify_agent_retry: {
    purpose: "Supervising agent judged the proof insufficient and kept the workflow inside the internal loop.",
    accepted_inputs: [{
      name: "author_packet_json",
      type: "json",
      description: "Optional revised proof packet if the checkpoint resumes to author."
    }]
  },
  verify_ship_ready: {
    purpose: "Proof assessment is ready_to_ship and the next continuation should enter ship.",
    include_ship_gate: true
  },
  verify_required: {
    purpose: "Ship is blocked until verify captures usable after evidence.",
    include_ship_gate: true
  },
  verify_supervisor_judgment_required: {
    purpose: "Ship is blocked until the supervising agent judges the current evidence ready_to_ship.",
    accepted_inputs: [{
      name: "proof_assessment_json",
      type: "json",
      required: true,
      description: "JSON assessment that must use decision=ready_to_ship before ship can continue."
    }],
    include_ship_gate: true
  },
  ship_gate_blocked: {
    purpose: "Ship is blocked because required baseline, after evidence, or supervising-agent proof approval is missing.",
    include_ship_gate: true
  },
  ship_review: {
    purpose: "Ship completed; inspect the PR, proof comment, and CI status.",
    include_ship_gate: true
  }
};
function buildCheckpointContract(state, request) {
  const spec = CHECKPOINT_CONTRACT_SPECS[request.checkpoint] || {
    purpose: request.summary
  };
  const continueWithStage = request.continueWithStage || request.recommendedAdvanceStage || null;
  const payload = {
    version: CHECKPOINT_CONTRACT_VERSION,
    checkpoint: request.checkpoint,
    stage: request.stage,
    purpose: spec.purpose,
    summary: request.summary,
    blocking: Boolean(request.blocking),
    next_actions: request.nextActions || [],
    advance_options: request.advanceOptions || [],
    accepted_inputs: spec.accepted_inputs || [],
    response_schema: spec.response_schema || null,
    required_state: spec.required_state || [],
    resume: {
      action: "run",
      state_path: request.statePath || null,
      continue_from_checkpoint: Boolean(continueWithStage),
      continue_with_stage: continueWithStage
    }
  };
  if (spec.include_ship_gate) {
    payload.ship_gate = validateShipGate(state);
  }
  return payload;
}
function mergeStateFromParams(statePath, params) {
  const state = readState(statePath);
  if (!state) return null;
  ensureStageLoopState(state);
  const stringFields = [
    "change_request",
    "commit_message",
    "prod_url",
    "capture_script",
    "success_criteria",
    "assertions_json",
    "verification_mode",
    "resume_session",
    "target_image_url",
    "target_image_hash",
    "viewport_matrix_json",
    "deterministic_setup_json",
    "base_branch",
    "before_ref",
    "context",
    "reviewer",
    "build_command",
    "build_output",
    "server_image",
    "server_command",
    "server_path",
    "wait_for_selector",
    "discord_channel",
    "discord_thread_id",
    "discord_message_id",
    "discord_source_url",
    "auth_localStorage_json",
    "auth_cookies_json",
    "auth_headers_json",
    "proof_plan",
    "implementation_notes",
    "implementation_mode",
    "ship_mode"
  ];
  for (const field of stringFields) {
    if (params[field] !== void 0) {
      state[field] = normalizeOptionalString(params[field]);
    }
  }
  if (params.server_path !== void 0) {
    state.server_path_source = "tool_param";
  }
  if (params.implementation_notes !== void 0) {
    const issues = knownEnvironmentIssuesFromNotes(state.implementation_notes || "");
    if (issues.length) state.implementation_environment_issues = issues;
  }
  if (params.reference !== void 0) state.reference = params.reference;
  if (params.mode !== void 0) {
    const previewMode = previewModeFromWorkflowMode(params.mode);
    if (previewMode) {
      state.mode = previewMode;
    } else {
      state.workflow_mode = normalizeOptionalString(params.mode);
    }
  }
  if (params.implementation_mode !== void 0) state.implementation_mode = normalizeOptionalString(params.implementation_mode);
  if (params.require_diff !== void 0) state.require_diff = params.require_diff;
  if (params.allow_code_changes !== void 0) state.allow_code_changes = params.allow_code_changes;
  if (params.allow_static_preview_fallback !== void 0) {
    state.allow_static_preview_fallback = params.allow_static_preview_fallback;
  }
  if (params.server_port !== void 0) state.server_port = String(params.server_port);
  if (params.color_scheme !== void 0) state.color_scheme = params.color_scheme || "";
  if (params.use_auth !== void 0) state.use_auth = params.use_auth ? "true" : "";
  if (params.leave_draft !== void 0) state.leave_draft = params.leave_draft ? "true" : "";
  if (params.advance_stage !== void 0) state.last_requested_advance_stage = params.advance_stage;
  applyRequestedInteractionRouteContract(state);
  if (params.recon_assessment_json !== void 0) {
    const raw = normalizeOptionalString(params.recon_assessment_json) || "";
    if (!raw) {
      state.recon_assessment = {};
      state.recon_assessment_source = null;
    } else {
      const parsed = JSON.parse(raw);
      state.recon_assessment = {
        ...parsed,
        source: (parsed?.source || "supervising_agent").toString()
      };
      state.recon_assessment_source = state.recon_assessment.source;
      if (parsed?.baseline_understanding && typeof parsed.baseline_understanding === "object") {
        state.recon_baseline_understanding = parsed.baseline_understanding;
      }
      const refined = parsed?.refined_inputs || {};
      if (typeof refined?.server_path === "string") {
        state.server_path = normalizeOptionalString(refined.server_path) || "";
        state.server_path_source = "supervising_agent";
      }
      if (typeof refined?.wait_for_selector === "string") state.wait_for_selector = normalizeOptionalString(refined.wait_for_selector) || "";
      if (typeof refined?.reference === "string" && refined.reference.trim()) state.reference = refined.reference.trim();
    }
  }
  if (params.author_packet_json !== void 0) {
    const raw = normalizeOptionalString(params.author_packet_json) || "";
    if (!raw) {
      state.supervisor_author_packet = null;
    } else {
      const parsed = JSON.parse(raw);
      if (typeof parsed?.capture_script === "string") {
        parsed.capture_script = normalizeCaptureScript(parsed.capture_script);
      }
      state.supervisor_author_packet = parsed;
      if (typeof parsed?.proof_plan === "string") state.proof_plan = normalizeOptionalString(parsed.proof_plan) || "";
      if (typeof parsed?.capture_script === "string") state.capture_script = normalizeCaptureScript(parsed.capture_script);
      if (isInteractionVerificationMode(state.verification_mode) && interactionCaptureScriptLooksPassive(state.capture_script || "")) {
        const warning = "Interaction proof capture script appears passive: it does not perform a browser interaction or return structured proof evidence.";
        appendStateWarning(state, "author_warnings", warning);
        setStructuredInteractionCaptureFailure(state, warning);
      }
      if (parsed?.baseline_understanding_used && typeof parsed.baseline_understanding_used === "object") {
        state.author_baseline_understanding_used = parsed.baseline_understanding_used;
      }
      if (parsed?.interaction_contract && typeof parsed.interaction_contract === "object") {
        state.interaction_contract = parsed.interaction_contract;
      }
      if (parsed?.proof_contract && typeof parsed.proof_contract === "object") {
        state.proof_contract = parsed.proof_contract;
      }
      const refined = parsed?.refined_inputs || {};
      const expectedTerminalPath = normalizeOptionalString(
        typeof refined?.expected_terminal_path === "string" ? refined.expected_terminal_path : typeof parsed?.expected_terminal_path === "string" ? parsed.expected_terminal_path : ""
      ) || "";
      if (typeof refined?.server_path === "string") {
        const refinedServerPath = normalizeOptionalString(refined.server_path) || "";
        state.server_path = authorPacketServerPath(
          state,
          parsed,
          refined,
          refinedServerPath,
          expectedTerminalPath
        );
        state.server_path_source = "supervising_agent";
      }
      if (typeof refined?.wait_for_selector === "string") state.wait_for_selector = normalizeOptionalString(refined.wait_for_selector) || "";
      if (typeof refined?.reference === "string" && refined.reference.trim()) state.reference = refined.reference.trim();
      if (expectedTerminalPath) {
        state.expected_terminal_path = expectedTerminalPath;
      }
      if (typeof parsed?.confidence === "string") state.supervisor_author_confidence = normalizeOptionalString(parsed.confidence) || null;
      if (parsed?.rationale !== void 0) state.supervisor_author_rationale = parsed.rationale;
      if (typeof parsed?.summary === "string") state.supervisor_author_summary = normalizeOptionalString(parsed.summary) || null;
      invalidateVerifyEvidence(state);
    }
  }
  if (params.proof_assessment_json !== void 0) {
    const raw = normalizeOptionalString(params.proof_assessment_json) || "";
    if (!raw) {
      state.proof_assessment = {};
      state.proof_assessment_source = null;
    } else {
      const parsed = JSON.parse(raw);
      const assessment = normalizeProofAssessmentStageFields({
        ...parsed,
        source: (parsed?.source || "supervising_agent").toString()
      });
      const readyBlocker = assessment?.decision === "ready_to_ship" ? visualDeltaShipGateReason({ ...state, proof_assessment: assessment, proof_assessment_source: assessment.source }) : null;
      if (readyBlocker) {
        assessment.blocked_decision = assessment.decision;
        assessment.decision = "revise_capture";
        assessment.recommended_stage = "verify";
        assessment.continue_with_stage = "verify";
        assessment.evidence_collection_incomplete = true;
        assessment.recovery_stage = "verify";
        assessment.recovery_reason = readyBlocker;
        assessment.evidence_issue_code = visualDeltaEvidenceIssueCode(state, readyBlocker);
        assessment.visual_delta = visualDeltaForState(state);
        assessment.suggested_repair = "Keep the same Riddle Proof run in evidence/comparison recovery: repair or retry the visual comparator/fetch path, wait for artifact readiness if applicable, or produce a measured visual_delta artifact before proof review can mark ready_to_ship.";
        const blockers = Array.isArray(assessment.blockers) ? assessment.blockers : [];
        assessment.blockers = [...blockers, readyBlocker];
      }
      state.proof_assessment = assessment;
      state.proof_assessment_source = state.proof_assessment.source;
      if (typeof state.proof_assessment?.decision === "string") {
        state.proof_decision = state.proof_assessment.decision;
      }
      if (typeof parsed?.summary === "string") {
        state.proof_assessment_summary = normalizeOptionalString(parsed.summary) || null;
      }
      if (state.proof_assessment?.decision === "ready_to_ship") {
        state.merge_recommendation = "ready-to-ship";
      } else if (typeof state.proof_assessment?.decision === "string" && state.proof_assessment.decision.trim()) {
        state.merge_recommendation = "do-not-merge";
      }
      appendProofSummaryLine(state, `Supervising proof assessment: ${state.proof_assessment.decision || "unknown"}`);
      if (readyBlocker) {
        appendProofSummaryLine(state, `Ready-to-ship assessment routed to evidence recovery: ${readyBlocker}`);
      }
      if (state.proof_assessment_summary) {
        appendProofSummaryLine(state, `Assessment summary: ${state.proof_assessment_summary}`);
      }
      const reasons = Array.isArray(parsed?.reasons) ? parsed.reasons.filter((item) => typeof item === "string" && item.trim()).slice(0, 4) : [];
      if (reasons.length) {
        appendProofSummaryLine(state, `Assessment reasons: ${reasons.join("; ")}`);
      }
    }
  }
  if (params.assertions_json !== void 0) {
    const raw = normalizeOptionalString(params.assertions_json) || "";
    if (!raw) {
      state.parsed_assertions = null;
    } else {
      state.parsed_assertions = JSON.parse(raw);
    }
  }
  if ((params.proof_plan !== void 0 || params.capture_script !== void 0) && hasAuthoredProofPlan(state)) {
    state.author_summary = state.author_summary || "Proof authoring inputs were updated from tool params.";
  }
  syncAuthoringState(state);
  writeState(statePath, state);
  return state;
}
function summarizeState(state) {
  if (!state) {
    return {
      stage: "missing",
      summary: "No riddle-proof state file exists yet.",
      state: null
    };
  }
  ensureStageLoopState(state);
  const attemptCounts = Object.fromEntries(
    WORKFLOW_STAGE_ORDER.map((stage) => [stage, Number(state.stage_attempts?.[stage]?.count || 0)])
  );
  const selected = {
    workspace_ready: Boolean(state.workspace_ready),
    repo: state.repo || null,
    branch: state.branch || null,
    mode: state.mode || null,
    workflow_mode: state.workflow_mode || null,
    implementation_mode: state.implementation_mode || null,
    require_diff: state.require_diff ?? null,
    allow_code_changes: state.allow_code_changes ?? null,
    no_implementation_mode: noImplementationModeFor(state),
    reference: state.reference || null,
    before_ref: state.before_ref || null,
    allow_static_preview_fallback: Boolean(state.allow_static_preview_fallback),
    commit_message: state.commit_message || null,
    author_status: state.author_status || null,
    author_summary: state.author_summary || null,
    author_request: state.author_request || null,
    proof_plan_status: state.proof_plan_status || null,
    proof_plan: state.proof_plan || null,
    proof_plan_request: state.proof_plan_request || null,
    proof_profile_applied: Boolean(state.proof_profile),
    proof_profile: state.proof_profile || null,
    recon_status: state.recon_status || null,
    recon_assessment: state.recon_assessment || null,
    recon_assessment_request: state.recon_assessment_request || null,
    recon_assessment_source: state.recon_assessment_source || null,
    recon_decision_request: state.recon_decision_request || null,
    recon_attempts_used: Array.isArray(state.recon_results?.attempt_history) ? state.recon_results.attempt_history.length : 0,
    recon_attempts_max: state.recon_results?.max_attempts || null,
    recon_hypothesis: state.recon_hypothesis || null,
    implementation_status: state.implementation_status || null,
    implementation_summary: state.implementation_summary || null,
    implementation_detection_summary: state.implementation_detection_summary || null,
    implementation_detection: state.implementation_detection || null,
    implementation_environment_issues: state.implementation_environment_issues || [],
    verify_status: state.verify_status || null,
    verify_summary: state.verify_summary || null,
    verify_decision_request: state.verify_decision_request || null,
    proof_assessment: state.proof_assessment || null,
    proof_assessment_request: state.proof_assessment_request || null,
    proof_assessment_source: state.proof_assessment_source || null,
    merge_recommendation: state.merge_recommendation || null,
    before_cdn: state.before_cdn || null,
    after_cdn: state.after_cdn || null,
    prod_cdn: state.prod_cdn || null,
    pr_url: state.pr_url || null,
    pr_branch: state.target_branch || state.branch || null,
    pr_state: state.pr_state || null,
    ship_commit: state.ship_commit || null,
    ship_remote_head: state.ship_remote_head || null,
    merge_commit: state.merge_commit || null,
    merged_at: state.merged_at || null,
    ship_push: state.ship_push || null,
    ship_report: state.ship_report || null,
    cleanup_report: state.cleanup_report || null,
    proof_comment_url: state.proof_comment_url || null,
    proof_assessment_comment_url: state.proof_assessment_comment_url || null,
    marked_ready: typeof state.marked_ready === "boolean" ? state.marked_ready : null,
    left_draft: typeof state.left_draft === "boolean" ? state.left_draft : null,
    ci_status: state.ci_status || null,
    reviewer: state.reviewer || null,
    active_checkpoint: state.active_checkpoint || null,
    active_checkpoint_stage: state.active_checkpoint_stage || null,
    continue_with_stage: checkpointContinueStage(state),
    stage_decision_request: state.stage_decision_request || {},
    stage_attempt_counts: attemptCounts,
    explicit_stage_gate: Boolean(state.explicit_stage_gate),
    last_requested_advance_stage: state.last_requested_advance_stage || null,
    recon_results: state.recon_results || null,
    verify_results: state.verify_results || null,
    proof_session: state.proof_session || null,
    parent_proof_session: state.parent_proof_session || null,
    proof_session_artifact_url: state.proof_session_artifact_url || null
  };
  const parts = [
    state.workspace_ready ? "workspace ready" : "workspace not ready",
    state.mode ? `mode=${state.mode}` : null,
    state.reference ? `reference=${state.reference}` : null,
    state.author_status ? `author=${state.author_status}` : null,
    state.proof_plan_status ? `proof=${state.proof_plan_status}` : null,
    state.recon_status ? `recon=${state.recon_status}` : null,
    state.implementation_status ? `implement=${state.implementation_status}` : null,
    state.verify_status ? `verify=${state.verify_status}` : null,
    state.active_checkpoint ? `checkpoint=${state.active_checkpoint}` : null,
    state.after_cdn ? "after evidence captured" : null,
    state.pr_url ? "PR linked" : null
  ].filter(Boolean);
  return {
    stage: state.stage || (state.after_cdn ? "verified" : state.workspace_ready ? "setup" : "unknown"),
    summary: parts.length ? parts.join(", ") : "State file present.",
    state: selected
  };
}

export {
  WORKFLOW_STAGE_ORDER,
  canonicalProofAssessmentStageForDecision,
  normalizeProofAssessmentStageFields,
  previewModeFromWorkflowMode,
  noImplementationModeFor,
  CHECKPOINT_CONTRACT_VERSION,
  BUNDLED_RIDDLE_PROOF_DIR,
  RIDDLE_PROOF_DIR_CANDIDATES,
  resolveRiddleProofDir,
  resolveConfig,
  ensureAction,
  workflowFile,
  buildSetupArgs,
  readState,
  writeState,
  ensureStageLoopState,
  clearStageDecisionRequest,
  recordStageAttempt,
  setStageDecisionRequest,
  checkpointContinueStage,
  invalidateVerifyEvidence,
  visualDeltaRequiredForState,
  visualDeltaForState,
  visualDeltaShipGateReason,
  proofAssessmentHardBlockersForState,
  requiredBaselineLabelsForState,
  validateShipGate,
  buildCheckpointContract,
  mergeStateFromParams,
  summarizeState,
  proof_run_core_exports
};
