iai-personal-memory-engine icon indicating copy to clipboard operation
iai-personal-memory-engine copied to clipboard

[bug] capture-transcript reports inserted records it never persists — write buffer is not flushed before exit

Open chrismartinsoares opened this issue 3 months ago • 1 comments

Summary

iai-mcp capture-transcript prints {"inserted": N} for records it never persists. Measured on a real batch of 71 transcripts: 1323 insertions reported, 403 rows in records.

Why

cmd_capture_transcript (src/iai_mcp/cli/_capture.py) creates a bare MemoryStore(), writes, and returns without closing it:

store = MemoryStore()
counts = capture_transcript(store, args.transcript_path, ...)
_stamp_capture_batch_liveness(counts)
print(json.dumps(counts, ensure_ascii=False))
return 0

Records go through the write buffer (_record_buffer / should_flush_record_buffer), which drains on threshold or in MemoryStore.close() — and close() is what calls flush_record_buffer. The process exits with a partially filled buffer, and those records never reach disk.

The event log is written synchronously, so events grows while records lags behind. That is what makes it invisible: the command reports success, the store looks alive, and only a row count reveals the gap.

Reproduce

Isolated store, one transcript:

IAI_MCP_STORE=/tmp/probe iai-mcp crypto init
IAI_MCP_STORE=/tmp/probe iai-mcp capture-transcript --session-id p <any>.jsonl
# -> {"inserted": 22, "reinforced": 0, "skipped": 0, "errors": 0}

sqlite3 /tmp/probe/hippo/brain.sqlite3 "SELECT COUNT(*) FROM records;"   # 0
sqlite3 /tmp/probe/hippo/brain.sqlite3 "SELECT COUNT(*) FROM events;"    # 22

Suggested fix

Use the context manager so the buffer flushes:

with MemoryStore() as store:
    counts = capture_transcript(store, args.transcript_path, ...)
    _stamp_capture_batch_liveness(counts)
print(json.dumps(counts, ensure_ascii=False))

Verified: 22 reported becomes 22 rows, 22 edges, 126 tags. On the full batch, 1323 reported = 1323 rows.

store = MemoryStore() without closing appears at roughly 20 more CLI call sites, but those are read paths with no buffer to lose, so only the write path seems to need changing.

Environment

Windows 11, Python 3.11.15, iai-pme 3.0.2. Nothing platform-specific about this one — the buffer behaves the same everywhere, so any host running capture-transcript on a batch smaller than the flush threshold should lose the whole batch.

Happy to open a PR.

chrismartinsoares avatar Aug 18 '26 08:08 chrismartinsoares

Fixed in v3.0.3 — capture-transcript now closes the store on every path, so the reported inserts match what reaches disk. Thanks for catching the events-vs-records asymmetry.

CodeAbra avatar Aug 19 '26 05:08 CodeAbra