Skip to content

fix(pi): run memory learning in background - #50

Merged
OLDyade merged 1 commit into
mainfrom
fix/pi-background-learning
Aug 4, 2026
Merged

fix(pi): run memory learning in background#50
OLDyade merged 1 commit into
mainfrom
fix/pi-background-learning

Conversation

@OLDyade

@OLDyade OLDyade commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • enqueue Pi turn-end extraction, skill evolution, Dream, and learned-skill sync instead of awaiting them inside the foreground agent_end hook
  • preserve strict FIFO ordering and the originating session/model context for every background turn
  • report background failures through Pi native UI notifications and reject session shutdown after the final sweep
  • drain pending work before session_shutdown completes so normal exits do not lose memory writes

Why

Pi waits for extension agent_end promises before clearing its foreground working state. A real local run completed the assistant response at 16:49:07 but remained working until MemFlywheel finished at 16:50:15, adding about 68 seconds of visible latency.

Validation

  • pnpm run test — 261 tests passed
  • pnpm run format:check
  • git diff --check
  • real Pi 0.83.0 E2E using the configured deepseek/deepseek-v4-flash model:
    • assistant response completed at 17:00:00.230
    • Pi emitted agent_end and agent_settled without waiting for memory work
    • typed memory was written asynchronously at 17:00:03.979
    • Dream/index work continued in the background without holding the foreground lifecycle
    • a fresh session used Pi native read on the typed memory body and recalled the exact marker

Lifecycle contract

agent_end -> snapshot + enqueue -> return immediately
background queue -> extraction -> skill evolution -> Dream -> skill sync
session_shutdown -> drain queue -> final sweep -> cleanup

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.

Signed-off-by: MemScribe Maintainers <OLDyade@users.noreply.github.com>

@FenjuFu FenjuFu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 backgroundFailures so one bad turn doesn't block the rest, which is what you want for fire-and-forget background work.
  • No lost work on exitsession_shutdown drains 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.

@OLDyade
OLDyade merged commit 02c2cd2 into main Aug 4, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants