Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

fix(memory): read the newest page of a person's conclusions, not the oldest

Rufusf98fe4796db6
diff --git a/packages/core/openexecutive/memory/honcho_client.py b/packages/core/openexecutive/memory/honcho_client.py
index 0daadff7..f3742a47 100644
--- a/packages/core/openexecutive/memory/honcho_client.py
+++ b/packages/core/openexecutive/memory/honcho_client.py
@@ -2291,11 +2291,20 @@ async def _person_memory(person: Any, peer: Any, *, recent: int) -> PersonMemory
total, and the card without the identity lines the roster already
supplies. Every line goes through ``_block_safe_line`` because the text
was derived from what people wrote, inbound email included."""
- page = await peer.conclusions.aio.list(size=recent, reverse=True)
- # `reverse=True` asks the server for its newest page; the order within
- # that page is sorted here by instant so the first item is the newest
- # whatever the server's default. When the page carries no total, the
- # count is at best the page length.
+ page = await peer.conclusions.aio.list(size=recent, reverse=False)
+ # The server's own default is newest-first ("ordered by recency unless
+ # reverse is true" — Honcho's server-side conclusions/list route
+ # docstring); `reverse=True` would flip that to oldest-first, which is
+ # not what this function wants. The symptom, if this regresses: a
+ # person's newest notes never surface past the `size`-bounded page
+ # boundary, however recently they were added — the re-sort below only
+ # reorders whatever the wrong page happened to contain, it cannot
+ # recover items the page never fetched. The order within the returned
+ # page is
+ # still re-sorted here by instant, since ties and near-boundary
+ # timestamps are not guaranteed to already match this function's
+ # definition of "newest". When the page carries no total, the count is
+ # at best the page length.
items = sorted(page.items, key=lambda c: _as_instant(c.created_at), reverse=True)
total = getattr(page, "total", None)
if total is None:
diff --git a/packages/core/tests/unit/test_memories_people_route.py b/packages/core/tests/unit/test_memories_people_route.py
index f3e2184f..e46a7873 100644
--- a/packages/core/tests/unit/test_memories_people_route.py
+++ b/packages/core/tests/unit/test_memories_people_route.py
@@ -110,7 +110,16 @@ class _ConclusionsAio:
async def list(self, page: int = 1, size: int = 50, session: Any = None, *, reverse: bool = False) -> _Page:
self._calls.append({"size": size, "reverse": reverse})
- return _Page(self._conclusions[:size], self._total, no_total=self._no_total)
+ # Mirrors the real server: the default (reverse=False) is newest-first
+ # ("ordered by recency unless reverse is true"), so the list passed to
+ # `_Peer(conclusions=...)` IS taken as that default order, whatever
+ # order a given test happens to author it in — several tests
+ # deliberately author it non-chronologically to exercise the client-
+ # side re-sort. `reverse=True` inverts whatever was supplied. This is
+ # what would have caught the production bug where the code asked for
+ # `reverse=True` and silently got the oldest page every time.
+ ordered = list(reversed(self._conclusions)) if reverse else self._conclusions
+ return _Page(ordered[:size], self._total, no_total=self._no_total)
class _Conclusions:
@@ -246,7 +255,7 @@ def test_ok_maps_rostered_peers_principal_first(
assert [c["content"] for c in top["recent"]] == ["3 prefers a short table", "3 runs 48 units"]
assert top["error"] is None
assert body["conclusion_total"] == 14
- assert principal.calls == [{"size": 2, "reverse": True}], "newest first, capped by ?recent="
+ assert principal.calls == [{"size": 2, "reverse": False}], "newest first, capped by ?recent="
assert client.aio.peer_calls == [], "a read-only page must never get-or-create a peer"
audit = [r for r in audit_rows if r["event_type"] == "peer_memory"]
@@ -419,6 +428,34 @@ def test_conclusions_are_newest_first_whatever_the_server_order(roster: dict[str
assert person["last_observed_at"] == _T2.isoformat()
+def test_the_newest_page_is_requested_not_the_oldest(roster: dict[str, int]) -> None:
+ """Regression for a bug where the code called `reverse=True`, which
+ Honcho's conclusions/list treats as the OPPOSITE of its documented default
+ ("ordered by recency unless reverse is true") — silently fetching the
+ OLDEST page instead of the newest. With more conclusions than fit on one
+ page, client-side re-sorting cannot recover from that: it only reorders
+ whichever items the wrong page happened to contain, so the newest item
+ (the one never fetched at all) is the one that stays missing, not merely
+ misplaced. Only a case where the total exceeds `recent` exercises this —
+ every earlier test's conclusions all fit on one page, which is why the
+ bug shipped without a test catching it."""
+ conclusions = [
+ _Conclusion("newest", _T2),
+ _Conclusion("middle", datetime(2026, 9, 20, 15, 0, tzinfo=UTC)),
+ _Conclusion("oldest", _T1),
+ ]
+ client = _Client([_Peer(str(roster["member"]), conclusions=conclusions, total=3)])
+ with _with_client(client):
+ body = _client().get("/memories/people?recent=2").json()
+ person = body["people"][0]
+ assert [c["content"] for c in person["recent"]] == ["newest", "middle"], (
+ "reverse=True would silently fetch the oldest page ('oldest','middle') and, "
+ "after the client-side re-sort, return them as ['middle','oldest']"
+ )
+ assert person["last_observed_at"] == _T2.isoformat()
+
+
+
def test_ordering_is_by_instant_not_by_string(roster: dict[str, int]) -> None:
"""`13:00+05:00` sorts after `12:00Z` as text yet is five hours older;
a `Z` suffix sorts after `+`; a naive stamp is taken as UTC."""