Repository navigation
perf(gateway): run multi-memory prime, recall, and contradiction reads concurrently - #785
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
STiFLeR7
left a comment
There was a problem hiding this comment.
prime() now waits for later reads after its result is already an error (gateway/memory_plane.py:1047 at 13bda6c).
I ran the actual memory_plane.prime() with an in-memory provider and two memories. The first read raises MemoryError(503, "memory_unavailable", ...) immediately. The second waits on an asyncio.Event. On main (4f8b14d), prime() returns memory_unavailable before the second read starts. On this head, after 500 ms the second read is still waiting and prime() cannot return. Releasing the event immediately reveals the first error. No gateway or vendor service is needed to reproduce this.
The PR preserves which error wins, but gather(..., return_exceptions=True) delays that error until every read settles. An unrelated slow provider can now hold a known failure until its timeout. Could we keep concurrent starts and process outcomes in input order, cancelling and draining unfinished siblings when the first error in that order is known? That would preserve the current error precedence and prompt failure. The same wait-for-later-reads pattern is present in recall().
|
@STiFLeR7 Thanks for the repro. Pushed follow-up commit 74148a4 to keep concurrent starts while processing outcomes in input order and cancelling/draining remaining siblings as soon as the first error in that order (or an outer/AnyIO cancellation) occurs:
Added |
prime now reads a set's memories in overlap instead of one by one. A set can hold up to RECALL_MAX_MEMORIES (200), and an engine (a vendor's API, a small self-hosted server) should not get every read of one turn at once. A semaphore caps the overlapping reads at PRIME_CONCURRENCY = 8; the order, the budget and the first-error-in-order behaviour are unchanged. The test reads 24 memories and sees the peak at exactly 8. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
richard-epsilla
left a comment
There was a problem hiding this comment.
Reviewed from first principles and verified on a test instance as part of 0.43.1 (release branch rel/0.43.1, built and run live).
Fixes #784
The bug
On
main(e120bb7b),prime,recall, and_other_sidesingateway/memory_plane.pyrun their engine calls one after another.Provider.recall_manyin the same file already states the intended pattern: "The default asks every memory at once (a person's task searches the person's memory and the agent's, and one after the other was the sum of two searches)" (gateway/memory_plane.py:262).To see it, call
GET /v1/memories/{mid}/prime?depth=1on a memory with three children through a fixture provider that waits 150 ms perprimecall. The request takes 0.615 s (median of 3 runs), the sum of four sequential calls.recallqueries each provider group sequentially, and_other_sidesat the end of everyrecall(turn-start recall included) awaitsprovider_ofand.get()sequentially for each contradicting record. Repeated binds also repeat credential lookups, andReMe.bind()health checks when its cache is cold. This is a latency problem only. It has no security impact.Expected behavior
prime, multi-grouprecall, and_other_sidesbind each unique(provider, org, workspace)once, all at the same time, and then run their engine reads at the same time. Latency follows the slowest call, not the sum. Outputs, tie ordering, budget truncation,degradedorder,abstain, and which error is raised stay the same as onmain.The fix
I added
_provider_keyand_bind_alltogateway/memory_plane.py._bind_allbinds each unique(provider, org, workspace)once withasyncio.gather. Addingorgtorecall's old(provider, workspace)key matchesprovider_ofbinding by org; callers pass memories from one org today, so groups do not change. Only read paths use_bind_all, because the sharedAnnouncedwrapper keeps the first memory's_above, and only write methods read it.prime,recall, and_other_sidesgather engine calls withreturn_exceptions=True, walk results in input order to raise the same first error asmain(see the tests), andrecallpasses itsboundmap directly to_other_sides._other_sidesdeduplicates(where, other)pairs in insertion order and checksother in havebefore raising, so an engine error on a pair the sequential loop never reached does not fail the recall.The raised error matches
main, though a failing batch makes more engine calls than before:primebinds every key and primes memories before the first bind failure even if an earlier call fails,recallbinds and queries every group even when an earlier group fails, errors surface after the slowest call finishes, and_other_sidesdiscards extra active copies read concurrently. No new concurrency cap is added:primeis bounded byRECALL_MAX_MEMORIES = 200viareach_below, and_other_sidesby the recalllimit(100 max) times each result'scontradictsreferences, matchingProvider.recall_many. The wire API,Provider.recall_many,gateway/memory_routes.py, andgateway/memory_local.pydo not change.gateway/tests/test_memory_fanout_perf.pyadds 8 tests covering concurrent barriers forprime,recall, and_other_sides,TestClientlatency onGET /v1/memories/{mid}/prime?depth=1, single-memory parity, bind deduplication and reuse, stable order and budget cuts,degradedandabstain, contradiction deduplication and skipped-pair engine-error isolation, input-order error precedence over 20 seeded runs, and zero leftover tasks after outer cancellation.Verification
Python 3.12.3.
e120bb7b)gateway/tests/test_memory_fanout_perf.pyGET /v1/memories/{mid}/prime?depth=1fixture route (4 x 150 ms)0.615,0.617,0.615s)0.158,0.164,0.169s; 3.75x faster)gateway/testsscripts/support-matrix/memoriesThe 2 that pass on
maintest error precedence/cancellation and contradiction dedup/skipped-pair isolation, which sequential code already satisfies. Ingateway/tests, the 27 skipped tests require services or keys absent on this machine: 14 need PostgreSQL onlocalhost:55432, 1 needs MySQL onlocalhost:33306, 10 needMEM0_API_KEY, and 2 needHR_TEST_TOKENROUTER_KEY. I did not runrunner/tests, theui/checks, or a container build, because the change touches onlygateway/memory_plane.pyand its new test file.