// Decisiones simuladas, sin modelo ni efectos externos. function run(decide, maxSteps = 3) { const history = []; for (let step = 0; step < maxSteps; step++) { const action = decide(history); if (action.name === "finish") return action.result; if (action.name !== "search") throw new Error("Herramienta no permitida"); history.push({ result: "encontrada" }); } throw new Error("Límite de pasos"); } const result = run(history => history.length ? { name: "finish", result: history[0].result } : { name: "search" }); console.log("Resultado: " + result); try { run(() => ({ name: "search" })); throw new Error("El bucle no se detuvo"); } catch (error) { if (error.message !== "Límite de pasos") throw error; console.log("Límite de pasos respetado"); }