fix(pi): run memory learning in background - #50
Merged
Conversation
Signed-off-by: MemScribe Maintainers <OLDyade@users.noreply.github.com>
FenjuFu
approved these changes
Aug 3, 2026
FenjuFu
left a comment
Contributor
There was a problem hiding this comment.
LGTM. Moving Pi's turn-end work (extraction / skill evolution / dream / learned-skill sync) off the foreground agent_end hook into a background queue is the right call — the user no longer blocks on memory processing after each turn.
The queue implementation is the idiomatic and correct shape:
- Strict FIFO via the
backgroundTail = backgroundTail.then(...)promise chain — tasks run one after another, never concurrently, so ordering and the per-turn session/model context are preserved. - Failures don't stall the chain — each task's error is caught and pushed to
backgroundFailuresso one bad turn doesn't block the rest, which is what you want for fire-and-forget background work. - No lost work on exit —
session_shutdowndrains the tail before completing, and a drain error is recorded and rethrown rather than swallowed, so a failed final sweep still surfaces.
CI is fully green — lint/typecheck, all four test matrices, and e2e-k8s. Nice, tightly-scoped change with test coverage in pi-port.test.ts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
agent_endhooksession_shutdowncompletes so normal exits do not lose memory writesWhy
Pi waits for extension
agent_endpromises before clearing its foreground working state. A real local run completed the assistant response at16:49:07but remained working until MemFlywheel finished at16:50:15, adding about 68 seconds of visible latency.Validation
pnpm run test— 261 tests passedpnpm run format:checkgit diff --checkdeepseek/deepseek-v4-flashmodel:17:00:00.230agent_endandagent_settledwithout waiting for memory work17:00:03.979readon the typed memory body and recalled the exact markerLifecycle contract
The foreground is eventually consistent with the memory store: a new session started immediately after an answer may begin before that turn has been extracted, while same-session conversation context remains available.