diff --git a/src/agent_comms/messaging/historical_views.py b/src/agent_comms/messaging/historical_views.py index a8326b64ed..6f32192fc0 100644 --- a/src/agent_comms/messaging/historical_views.py +++ b/src/agent_comms/messaging/historical_views.py @@ -720,7 +720,7 @@ def page(self, view: HistoryView, presentation: BusPresentation, bounds: PageBou current = replace(bounds, traversal=traversal.for_source(index == start)) request = view.page_request(view.capture(snapshot), current) current_snapshot = presentation.registry.snapshot() - document = presentation.bus.reads.read() + document = presentation.bus.reads.read_shared() opened = Bus(Bus.path_in(Path(source.root))) raw = request.read_opened(opened) projected = replace(raw, messages=tuple( diff --git a/src/agent_comms/messaging/human_read_cut.py b/src/agent_comms/messaging/human_read_cut.py index 00056fd9ed..ccfdb2dea8 100644 --- a/src/agent_comms/messaging/human_read_cut.py +++ b/src/agent_comms/messaging/human_read_cut.py @@ -19,4 +19,4 @@ def ledger(self) -> ReadLedger: @cached_property def document(self) -> ReadDocument: - return self.ledger.read() + return self.ledger.read_shared() diff --git a/src/agent_comms/messaging/message_bus.py b/src/agent_comms/messaging/message_bus.py index 5e22ac8e1b..f9685d8a34 100644 --- a/src/agent_comms/messaging/message_bus.py +++ b/src/agent_comms/messaging/message_bus.py @@ -159,7 +159,7 @@ def _unread_cut( self, name: str, snapshot: RegistrySnapshot, target: NamedTarget, after: InboxStart, bounds: PageBounds, ) -> Iterator[UnreadCut]: - catalog = self._channels.read() + catalog = self._channels.read_shared() require_channel = partial(self.channel_in, catalog) delivery = DeliveryScope.capture(name, snapshot, catalog) selection = delivery.selection(target, snapshot, require_channel) diff --git a/src/agent_comms/messaging/published_bus.py b/src/agent_comms/messaging/published_bus.py index a9144468ca..76859efa41 100644 --- a/src/agent_comms/messaging/published_bus.py +++ b/src/agent_comms/messaging/published_bus.py @@ -40,7 +40,7 @@ def __init__[Registry](self, root: Path, registry: Registry, publisher: type[Wri def require_channel(self, target: str) -> Channel: - return self.channel_in(self._channels.read(), target) + return self.channel_in(self._channels.read_shared(), target) def channel_in(self, catalog: CatalogAccess, target: str) -> Channel: """A lookup needs its own channel's clocks only. Every inbox read makes several, and the @@ -50,4 +50,4 @@ def channel_in(self, catalog: CatalogAccess, target: str) -> Channel: def channel_declarations(self): with self.opened_activity() as activity: - return self._channels.read().views(activity) + return self._channels.read_shared().views(activity) diff --git a/src/agent_comms/messaging/read_ledger.py b/src/agent_comms/messaging/read_ledger.py index fd8ddb8c65..2bf81fd282 100644 --- a/src/agent_comms/messaging/read_ledger.py +++ b/src/agent_comms/messaging/read_ledger.py @@ -191,7 +191,7 @@ def advance(document: ReadDocument) -> DocumentChange[ReadDocument, ReadDocument def cursor( self, viewer: str, snapshot: RegistryProvenance, *, document: ReadDocument | None = None ) -> ReadCursor: - document = self.read() if document is None else document + document = self.read_shared() if document is None else document return document.cursor(viewer, snapshot, ObservedPath.at(Bus.path_in(self.path.parent))) def displayed_recipient( @@ -263,7 +263,7 @@ def _transcript_key(viewer: str, source: str, inode: int) -> tuple[str, str, int def transcript_seen( self, viewer: str, source: str, inode: int, *, document: ReadDocument | None = None ) -> int: - document = self.read() if document is None else document + document = self.read_shared() if document is None else document return document.transcripts.get(self._transcript_key(viewer, source, inode), 0) def mark_transcript( diff --git a/src/agent_comms/storage/locked_store.py b/src/agent_comms/storage/locked_store.py index ab216314d3..b406663ea8 100644 --- a/src/agent_comms/storage/locked_store.py +++ b/src/agent_comms/storage/locked_store.py @@ -1524,8 +1524,7 @@ def _decode_retained(self, source: DocumentBytes) -> DecodedDocument[T]: def _read_current(self) -> DecodedDocument[T]: return self._decode_retained(self._copy_sources(self)[0]) - def _read_shared(self) -> T: - """Read the cached document without copying; callers must treat the value as read-only.""" + def _read_with(self, extract: Callable[[DecodedDocument[T]], T]) -> T: try: revision = FileRevision.from_stat(self.path.stat()) except FileNotFoundError: @@ -1533,19 +1532,15 @@ def _read_shared(self) -> T: return self.empty() return self._read_captured( lambda: self._cached_value(revision, self._read_current), - lambda captured: captured.value, + extract, ) + def read_shared(self) -> T: + """Read the cached document without copying; callers must treat the value as read-only.""" + return self._read_with(lambda captured: captured.value) + def read(self) -> T: - try: - revision = FileRevision.from_stat(self.path.stat()) - except FileNotFoundError: - _read_cache.pop(self.path, None) - return self.empty() - return self._read_captured( - lambda: self._cached_value(revision, self._read_current), - lambda captured: captured.detached(), - ) + return self._read_with(lambda captured: captured.detached()) def _publish_encoded_unlocked(self, encoded: JsonValue) -> None: _read_cache.pop(self.path, None) diff --git a/src/agent_comms/transport/connector_store.py b/src/agent_comms/transport/connector_store.py index 8d7ad298e8..deeabcb856 100644 --- a/src/agent_comms/transport/connector_store.py +++ b/src/agent_comms/transport/connector_store.py @@ -603,7 +603,7 @@ def empty(self) -> ConnectorGrants: def caller_for(self, token: str) -> ConnectorCaller: """Resolve one bearer without deep-copying the grants document on every HTTP request.""" - return self._read_shared().caller_for(token) + return self.read_shared().caller_for(token) def thread_granted(self, thread: str) -> str: token = secrets.token_urlsafe(32)