{"record":{"id":"af3fed48dd9f19f0","repo":"rohitg00/ai-engineering-from-scratch","slug":"step-requires-executing-reflecting-got-self-sta","errorCode":null,"errorMessage":"_step requires EXECUTING/REFLECTING, got {self.state.value}","messagePattern":"_step requires EXECUTING/REFLECTING, got (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/20-agent-harness-loop-contract/code/main.py","lineNumber":281,"sourceCode":"            return self._complete(\"no_plan\")\n        self._transition(State.EXECUTING)\n        return self._step()\n\n    def _step(self) -> PullRequest | SessionResult:\n        if self.state == State.IDLE:\n            return self._begin_plan()\n        budget_hit = self._check_budget()\n        if budget_hit is not None:\n            return budget_hit\n        if self.state == State.REFLECTING:\n            self._cursor += 1\n            self.budget.turns += 1\n            if self._cursor >= len(self._plan):\n                return self._complete(\"goal_met\")\n            self._transition(State.EXECUTING)\n            return self._step()\n        if self.state != State.EXECUTING:\n            raise RuntimeError(f\"_step requires EXECUTING/REFLECTING, got {self.state.value}\")\n        step = self._plan[self._cursor]\n        self.hooks.fire(\"before_step\", {\"step\": step})\n        self._emit(\"step.start\", {\"step_id\": step.id, \"desc\": step.description})\n        if step.requires_tool:\n            try:\n                self.hooks.fire(\"before_tool_call\", {\"step\": step})\n            except HookAbort as exc:\n                step.error = f\"hook_abort:{exc}\"\n                self._emit(\"tool.error\", {\"step\": step.id, \"error\": step.error})\n                self.hooks.fire(\"on_error\", {\"step\": step, \"error\": step.error})\n                self._transition(State.REFLECTING)\n                return self._step()\n            self.budget.tool_calls += 1\n            self._emit(\"tool.call\", {\"step\": step.id, \"tool\": step.tool_name, \"args\": step.tool_args})\n            self._transition(State.AWAITING_TOOL)\n            self._emit(\"step.end\", {\"step_id\": step.id, \"outcome\": \"awaiting_tool\"})\n            self.hooks.fire(\"after_step\", {\"step\": step, \"outcome\": \"awaiting_tool\"})\n            return PullRequest(","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/20-agent-harness-loop-contract/code/main.py#L263-L299","documentation":"Internal invariant at the top of Harness._step: the step executor is only meaningful while the session is EXECUTING or REFLECTING (IDLE is redirected to _begin_plan earlier). Reaching _step in any other state means the state machine and the step loop have desynchronized — an internal bug, not a caller input error.","triggerScenarios":"Thrown at phases/19-capstone-projects/20-agent-harness-loop-contract/code/main.py:281 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Audit _transition calls in _begin_plan, _step, and _pause for a missing or out-of-order transition","Reproduce with a trace of emitted events to find where the state diverged","Add state assertions at each loop entry point as a tripwire","Ensure resume paths set the state via _transition, not by direct assignment to self.state"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}