import { RpaError } from "@/modules/rpa/errors";

/** Kernel step 子阶段 fail-fast 包装（定位非法结构来源） */
export async function runKernelSubStep<T>(
  step: string,
  subStep: string,
  fn: () => Promise<T>,
): Promise<T> {
  try {
    return await fn();
  } catch (error) {
    const message = error instanceof Error ? error.message : String(error);
    const stack = error instanceof Error ? error.stack : undefined;
    throw new RpaError(
      "STRUCT_CHANGE",
      `[${step}/${subStep}] ${message}`,
      { retryable: true },
    );
  }
}
