Repository navigation
fix(audit): keep audit writes referenced and drain them on shutdown - #5092
Merged
nicoloboschi merged 7 commits intoOct 6, 2026
Merged
Conversation
log_fire_and_forget dropped the only reference to each write task. The event loop holds tasks weakly, so a write nothing else references can be garbage collected mid-flight and its audit row is lost. close() also shut the database pool down without waiting for audit rows still in flight. A row scheduled just before shutdown found the pool gone (the pool getter returns None) and was skipped with only a debug log. Track in-flight writes, add AuditLogger.drain() (bounded to 5s so a stuck database cannot hang shutdown) and call it from MemoryEngine.close() before the pool is closed.
β¦d-forget writes The audit log and the LLM trace both schedule best-effort DB writes with create_task and both need the tasks kept somewhere. Move the bookkeeping (strong refs, per-key buckets, bounded drain) into engine/background_writes.py and use it from both, which also stops llm_trace's memory_id attach task from being collected mid-flight.
Collaborator
|
Refactored the in-flight write bookkeeping into a shared |
test_llm_trace called the removed _flush_pending (test-api 2/3 failure) and test_gemini_implicit_cache_ratio reached into the removed _pending dict; both now go through the recorder's PendingWrites, which grew an in_flight() check for the gemini test's retry loop.
This was referenced Oct 8, 2026
Merged
This was referenced Oct 16, 2026
Merged
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.
Problem
I found two ways the audit log can silently lose a row.
log_fire_and_forgetthrows away the only reference to each write task. The event loop keeps tasks weakly (asyncio docs), so a write nothing else references can be garbage collected mid-flight. I reproduced this with a standalone task that awaits a future only it references: the task was collected in 3 out of 3 runs.MemoryEngine.close()closes the pool without waiting for in-flight audit writes. The logger's pool getter islambda: self._backendandclose()clears_backend, so a row scheduled just before shutdown finds no pool and is skipped with only a debug log. The comments inaudit.pyalready treat an audit gap as a compliance risk, so I think a row lost at shutdown is worth closing.Fix
I keep in-flight write tasks in a set and discard them on completion, the same pattern
llm_trace._pendingalready uses. I addedAuditLogger.drain(), bounded to 5 s so a stuck database cannot hang shutdown, and call it fromMemoryEngine.close()after the task backend shuts down and before the pool closes.The bookkeeping is now shared instead of duplicated:
engine/background_writes.pyholds aPendingWriteshelper (strong refs, per-key buckets, bounded drain) and both the audit log andllm_traceuse it βllm_tracehad the same set-of-tasks code plus its own_discard_pending/_flush_pending. That also fixesllm_trace._attach_memory_ids, whosecreate_taskwasunreferenced for the same reason; it is scheduled through the helper too.
Test
I added three tests to
tests/test_audit_per_bank.py, none of which need a database: an in-flight write survivesgc.collect(),drain()completes a write scheduled just before shutdown, anddrain()with nothing in flight returns. The first two fail without my change.I ran
uv run pytest tests/test_audit_per_bank.py -k "garbage or drain or oracle or postgres or allowlist or resolution or unwired or no_bank"(9 passed), andruff check,ruff format --checkandty check hindsight_api/are clean.close()runs in everymemoryfixture teardown, so the database-backed suite in CI exercises the newdrain()call.