fix(docs): deflake memories.py doc example (async-consolidation race)
Problem
The test-doc-examples (python) CI job has been intermittently red on the memories.py API example, failing with:
hindsight-docs/examples/api/memories.py, line ~111, in main
raise NotFoundException ... (404)
"Memory unit '<uuid>' not found"
This is a pre-existing main-side flake (the example was introduced in #1976) that surfaces on any PR touching .github/**, since the example is main-owned and re-runs there.
Root cause
The example curates one unit with four back-to-back calls: edit text → edit fields → invalidate → restore. Each update_memory edit re-embeds and re-consolidates in the background (a tracked consolidation operation, plus graph maintenance / mental-model refreshes). The next step fired immediately, racing that background work — so a later call (most often the restore) could hit the unit while it was mid-rewrite and 404. The fixed await asyncio.sleep(3) after the seed retains was also unreliable under CI load with a live LLM.
Fix
Add a small wait_for_idle() helper that polls list_operations until the bank has no pending/processing operations, and drain between each curation step (and after the seed retains, replacing the sleep(3)).
- Deterministic: waits for the actual async work to finish instead of guessing with a sleep.
- Best-effort & bounded: caps at ~60s per wait, then falls through (never hangs the example).
- Documentation snippets are unchanged — every
wait_for_idle()call sits outside the# [docs:...]blocks that get rendered into the docs site.
Verification
python -m py_compilepasses.- Confirmed
update_memory→submit_async_consolidationcreates a tracked operation visible tolist_operations(so the poll actually drains the racing work). - Doc-block purity checked: no
wait_for_idleinside any[docs:...]marker. - Final validation is the
test-doc-examples (python)job on this PR.
🤖 Generated with Claude Code