Skip to content

fix(docs): deflake memories.py doc example (async-consolidation race) - #2152

Merged
nicoloboschi merged 1 commit into
mainfrom
fix/doc-example-memories-flake
Jun 16, 2026
Merged

fix(docs): deflake memories.py doc example (async-consolidation race)#2152
nicoloboschi merged 1 commit into
mainfrom
fix/doc-example-memories-flake

Conversation

@benfrank241

Copy link
Copy Markdown
Member

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_compile passes.
  • Confirmed update_memorysubmit_async_consolidation creates a tracked operation visible to list_operations (so the poll actually drains the racing work).
  • Doc-block purity checked: no wait_for_idle inside any [docs:...] marker.
  • Final validation is the test-doc-examples (python) job on this PR.

🤖 Generated with Claude Code

…curation steps

The memories.py API doc example ran update_memory edit → edit-fields →
invalidate → restore back-to-back on the same unit. Each edit re-embeds and
re-consolidates in the background (a tracked consolidation op), so a later
step could race that work and 404 on a unit mid-rewrite — the restore
intermittently failed with "Memory unit not found". The fixed sleep(3) after
the seed retains was also unreliable under CI load with a live LLM.

Replace the sleep with a wait_for_idle() helper that polls list_operations
until the bank has no pending/processing operations, and drain between each
curation step. All waits sit outside the [docs:...] blocks, so the rendered
documentation snippets are unchanged.
@nicoloboschi
nicoloboschi merged commit 5e71ceb into main Jun 16, 2026
76 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