Skip to content

fix(audit): keep audit writes referenced and drain them on shutdown - #5092

Merged
nicoloboschi merged 7 commits into
vectorize-io:mainfrom
G26karthik:fix/audit-drain-in-flight-writes
Oct 6, 2026
Merged

nicoloboschi merged 7 commits into
vectorize-io:mainfrom
G26karthik:fix/audit-drain-in-flight-writes

Conversation

@G26karthik

@G26karthik G26karthik commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Problem

I found two ways the audit log can silently lose a row.

  • log_fire_and_forget throws 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 is lambda: self._backend and close() clears _backend, so a row scheduled just before shutdown finds no pool and is skipped with only a debug log. The comments in audit.py already 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._pending already uses. I added AuditLogger.drain(), bounded to 5 s so a stuck database cannot hang shutdown, and call it from MemoryEngine.close() after the task backend shuts down and before the pool closes.

The bookkeeping is now shared instead of duplicated: engine/background_writes.py holds a
PendingWrites helper (strong refs, per-key buckets, bounded drain) and both the audit log and
llm_trace use it β€” llm_trace had the same set-of-tasks code plus its own _discard_pending /
_flush_pending. That also fixes llm_trace._attach_memory_ids, whose create_task was
unreferenced 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 survives gc.collect(), drain() completes a write scheduled just before shutdown, and drain() 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), and ruff check, ruff format --check and ty check hindsight_api/ are clean. close() runs in every memory fixture teardown, so the database-backed suite in CI exercises the new drain() call.

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.
Copilot AI balanced review requested due to automatic review settings October 2, 2026 14:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added the core Core Hindsight service (API, engine, DB, clients, CLI, UI, docs) label Oct 2, 2026
G26karthik and others added 5 commits October 2, 2026 19:55
…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.
@nicoloboschi

Copy link
Copy Markdown
Collaborator

Refactored the in-flight write bookkeeping into a shared engine/background_writes.py (PendingWrites): strong task refs, per-key buckets, bounded drain. llm_trace had the same code (its _pending set, _discard_pending, _flush_pending) and now uses the helper, which also covers _attach_memory_ids' unreferenced create_task you flagged. Added tests/test_background_writes.py for the per-key drain isolation. Running full CI on an upstream branch before merge (fork PRs skip the API/LLM jobs).

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.
@nicoloboschi
nicoloboschi merged commit 4eb24a2 into vectorize-io:main Oct 6, 2026
220 checks passed
@G26karthik
G26karthik deleted the fix/audit-drain-in-flight-writes branch October 6, 2026 19:40
@nicoloboschi nicoloboschi mentioned this pull request Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core Hindsight service (API, engine, DB, clients, CLI, UI, docs)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants