{"record":{"id":"d6f3037dc2aedfc0","repo":"can1357/oh-my-pi","slug":"compacted-agent-end-references-streamed-prefix-co","errorCode":null,"errorMessage":"Compacted agent_end references {streamed_prefix_count} streamed messages, but only {len(streamed_messages)} were retained","messagePattern":"Compacted agent_end references (.+?) streamed messages, but only (.+?) were retained","errorType":"exception","errorClass":"RpcError","httpStatus":null,"severity":"error","filePath":"python/omp-rpc/src/omp_rpc/client.py","lineNumber":1319,"sourceCode":"        if terminal.message_count is None or terminal.message_count <= len(\n            terminal.messages\n        ):\n            return terminal.messages\n\n        run_start = 0\n        for event_index in range(len(events) - 1, -1, -1):\n            if isinstance(events[event_index], AgentStartEvent):\n                run_start = event_index + 1\n                break\n\n        streamed_messages = tuple(\n            event.message\n            for event in events[run_start:]\n            if isinstance(event, MessageEndEvent)\n        )\n        streamed_prefix_count = terminal.message_count - len(terminal.messages)\n        if streamed_prefix_count > len(streamed_messages):\n            raise RpcError(\n                \"Compacted agent_end references \"\n                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","sourceCodeStart":1301,"sourceCodeEnd":1337,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/python/omp-rpc/src/omp_rpc/client.py#L1301-L1337","documentation":"When a run ends with an AgentEndEvent that reports compaction, the client reconstructs the full message list by prepending streamed messages it captured during the run. This RpcError is raised when the terminal event claims more streamed (dropped-by-compaction) messages than the client actually retained, meaning the reconstruction would be incomplete and possibly silently truncated.","triggerScenarios":"Calling wait for agent_end / collecting events after a run where terminal.message_count - len(terminal.messages) exceeds the number of MessageEndEvents the client buffered between run_start and the terminal event — typically when max_event_history evicted early streamed events or a listener consumed/dropped events.","commonSituations":"Long agent runs with many streamed messages exceeding max_event_history; hosts that attach custom event listeners that dequeue events; client started with a small event-history buffer then running a compacting session.","solutions":["Increase max_event_history when constructing the RpcClient so all streamed MessageEndEvents are retained","Remove or fix custom listeners that consume events from the shared history during the run","Upgrade the library — this is a client-side consistency check; newer versions may stream messages differently","If reproducible, file a bug with the run's event trace since server and client disagree on message accounting"],"exampleFix":"// before\nclient = make_client(max_event_history=64)\n// after\nclient = make_client(max_event_history=2048)  # retain all streamed MessageEndEvents for compaction reconstruction","handlingStrategy":"try-catch","validationCode":"if client_max_event_history < expected_streamed_messages:\n    raise ValueError(\"max_event_history too small for compaction reconstruction\")","typeGuard":null,"tryCatchPattern":"try:\n    messages = client.collect_messages()\nexcept RpcError as exc:\n    if \"streamed messages\" in str(exc):\n        raise RuntimeError(\"history too small; raise max_event_history\") from exc\n    raise","preventionTips":["Size max_event_history above your worst-case streamed event count","Avoid listeners that drain the shared event history during runs","Test long compacting runs against your client configuration"],"tags":["rpc","compaction","event-history","state-inconsistency"],"backgroundTag":"event-history-overflow","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}