{"type": "session", "harness": "opencode", "id": "aiagent-0045", "name": "planning"} {"type": "message", "message": {"role": "system", "content": "You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies."}} {"type": "message", "message": {"role": "user", "content": "What are ReAct, Plan-and-Execute, Tree-of-Thoughts and self-reflection planning strategies? Show plan-then-execute with re-planning. Include code examples."}} {"type": "message", "message": {"role": "assistant", "content": "**Planning patterns:**\n\n- **ReAct (Yao et al., 2022):** Thought -> Action -> Observation loop\n- **Plan-and-Execute:** planner writes steps, executor runs them, re-plan on failure\n- **Tree-of-Thoughts:** branch multiple reasonings, score and keep best\n- **Self-reflection:** generate -> critique -> revise\n\nPlanning is an LLM call that returns steps; execution grounds them in tools.\n\n```python\ndef parse_steps(text: str) -> list[str]:\n return [ln.strip(\"- \").strip() for ln in text.splitlines() if ln.strip()][:10]\n\ndef execute_step(step: str, tools: dict) -> str:\n # route step to a tool; stub for example\n return f\"done: {step}\"\n\ndef synthesize(results: list[str]) -> str:\n return \"\\n\".join(results)\n\ndef plan_and_execute(goal: str, llm, tools: dict) -> str:\n plan_text = llm.chat.completions.create(\n model=\"model-identifier-from-lm-studio\",\n messages=[{\"role\": \"user\",\n \"content\": f\"Create a numbered step-by-step plan to: {goal}\"}],\n ).choices[0].message.content or \"\"\n steps = parse_steps(plan_text)\n results: list[str] = []\n for step in steps:\n try:\n results.append(execute_step(step, tools))\n except Exception as e:\n # re-plan once with failure context\n fix = llm.chat.completions.create(\n model=\"model-identifier-from-lm-studio\",\n messages=[{\"role\": \"user\",\n \"content\": f\"Step failed '{step}': {e}. Propose one alternative step.\"}],\n ).choices[0].message.content or step\n results.append(execute_step(fix.strip(), tools))\n return synthesize(results)\n```\n\nNote: do not hardcode model versions as facts (e.g. Python is 3.15 as of Oct 2026, not 3.12) - query the environment instead."}}