hindsight icon indicating copy to clipboard operation
hindsight copied to clipboard

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

Open benfrank241 opened this issue 5 months ago • 0 comments

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_memory → submit_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

benfrank241 avatar Jun 11 '26 17:06 benfrank241