{"record":{"id":"40b33f693a4bc883","repo":"can1357/oh-my-pi","slug":"event-history-limit-was-exceeded-while-waiting-for","errorCode":null,"errorMessage":"Event history limit was exceeded while waiting for agent_end. Increase max_event_history to retain more streamed events.","messagePattern":"Event history limit was exceeded while waiting for agent_end\\. Increase max_event_history to retain more streamed events\\.","errorType":"exception","errorClass":"RpcError","httpStatus":null,"severity":"error","filePath":"python/omp-rpc/src/omp_rpc/client.py","lineNumber":1339,"sourceCode":"                f\"{streamed_prefix_count} streamed messages, but only \"\n                f\"{len(streamed_messages)} were retained\"\n            )\n        return streamed_messages[:streamed_prefix_count] + terminal.messages\n\n    def _wait_for_agent_end(\n        self,\n        start_index: int,\n        start_async_error_index: int,\n        timeout: float | None = None,\n    ) -> tuple[RpcAgentEvent, ...]:\n        deadline = time.monotonic() + (timeout if timeout is not None else 60.0)\n        with self._event_condition:\n            while True:\n                if self._closed_error is not None:\n                    raise RpcProcessExitError(str(self._closed_error))\n\n                if start_index < self._events.offset:\n                    raise RpcError(\n                        \"Event history limit was exceeded while waiting for agent_end. \"\n                        \"Increase max_event_history to retain more streamed events.\"\n                    )\n\n                if start_async_error_index < self._async_errors.offset:\n                    raise RpcError(\n                        \"Async error history limit was exceeded while waiting for agent_end. \"\n                        \"Increase max_event_history if your host needs to retain more background failures.\"\n                    )\n\n                async_errors = self._async_errors.snapshot_from(start_async_error_index)\n                if len(async_errors) > 0:\n                    raise async_errors[0]\n\n                event_payloads = self._events.snapshot_from(start_index)\n                if any(\n                    payload.get(\"type\") == \"agent_end\"\n                    and payload.get(\"isTerminal\") is not False","sourceCodeStart":1321,"sourceCodeEnd":1357,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/python/omp-rpc/src/omp_rpc/client.py#L1321-L1357","documentation":"Raised while waiting for agent_end when the requested start_index falls before the beginning of the retained event ring buffer (self._events.offset), i.e. events the waiter needs were evicted because max_event_history was too small for the run's event volume.","triggerScenarios":"Calling wait_for_agent_end with a start index captured early in a run that emitted more events than max_event_history holds; also hit when the client is shared across multiple long runs on one history buffer.","commonSituations":"Long-running prompt sessions emitting thousands of MessageUpdate/ToolExecution events with a default (small) history limit; multiple sequential prompts reusing one client without draining events.","solutions":["Increase max_event_history at client construction to exceed the expected event count for your runs","Consume events incrementally (listeners/callbacks) instead of batch-waiting from a very old index","Start waits from a recent index rather than an index captured many events earlier","Split very long runs into shorter prompts so per-run event volume stays within the limit"],"exampleFix":"// before\nclient = RpcClient(..., max_event_history=100)\n// after\nclient = RpcClient(..., max_event_history=50_000)  # headroom for long streaming runs","handlingStrategy":"validation","validationCode":"start_index = client.current_event_index()\nif start_index < client.events_offset():\n    raise RuntimeError(\"event index already evicted; increase max_event_history\")","typeGuard":null,"tryCatchPattern":"try:\n    events = client.wait_for_agent_end()\nexcept RpcError as exc:\n    if \"Event history limit\" in str(exc):\n        client = make_client(max_event_history=larger_limit)  # replay lost work\n    else:\n        raise","preventionTips":["Set max_event_history generously for long streaming runs","Capture wait start indices close to the wait call","Use incremental listeners instead of batch-waiting from ancient indices"],"tags":["rpc","event-history","buffer-overflow","streaming"],"backgroundTag":"event-history-overflow","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}