Start and shutdown stalls: FTS rebuild on every open (~300 ms) and an awaited child `pi` process at session end (2–5 s)
Symptom
Opening a session and quitting pi both pause visibly while pi-memory is installed. /new, /resume and /fork pause the same way as quitting.
Measured environment: pi 1.3.0, Node v26.12.0, @samfp/pi-memory 1.6.1, store 442 KB (39 facts, 21 lessons), memory.embedding = openai-compatible on localhost, memory.consolidationModel = a fast flash model.
One property of this machine matters for the absolute numbers: a single fsync costs ~77 ms (measured over 50 × write+fsync). On a normal NVMe the same work is several times cheaper, but the shape below does not change — the costs are per-commit/per-process, not per-byte.
1. session_start: full FTS rebuild on every store open (~300 ms)
MemoryStore.migrate() ends with an unconditional
this.db.exec(`INSERT INTO semantic_fts(semantic_fts) VALUES('rebuild')`);
this.db.exec(`INSERT INTO lessons_fts(lessons_fts) VALUES('rebuild')`);
although the triggers created two statements earlier keep both indexes current. Driving the real session_start handler from the built bundle:
| current | 354 ms |
| rebuilding only when the index is actually stale | 1.4 ms |
Fixed in #40. The guard compares each content table against its _docsize shadow table, because COUNT(*) on an external-content FTS table is answered by scanning the content table and therefore cannot detect a stale index.
2. session_shutdown / session_before_switch: an awaited child pi process (2–5 s)
Both handlers await consolidateSession(), which spawns a whole new agent just to make one LLM call:
pi.exec("pi", ["-p", prompt, "--print", "--no-extensions", "--no-tools", "--no-session", "--model", ...])
Measured:
- bare child boot with a trivial prompt: 1.75 s
- end-to-end with a realistic 2.4 KB conversation prompt: 3.9 s
- worst case is
EXEC_TIMEOUT_MS = 45 s/HARD_TIMEOUT_MS = 60 s
pi stops the TUI and then awaits session_shutdown handlers (interactive-mode.ts: this.stop() before runtimeHost.dispose()), so the terminal sits on a frozen frame for that whole window, and the resume hint only prints afterwards. session_before_switch runs the same path, which is why switching away from a long conversation stalls too.
Possible directions, none of which I want to presume:
- setting to control it — e.g.
memory.consolidation: "session-end" | "manual" | "off", so/memory-consolidate(or something else) owns the timing; - get it off the shutdown path — a debounced background run during the session, so quitting only closes the store. The tricky part is that the parent applies the extracted facts, so a fire-and-forget child at exit has nobody left to write them;
- cap the wait — race the consolidation against ~1.5 s. Simple, but the extraction of that session is then lost whenever the provider is slower than the cap.
3. Minor: one commit per write at synchronous=FULL
The store sets journal_mode=WAL but leaves synchronous at FULL, and every mutation is its own BEGIN IMMEDIATE … COMMIT:
| 5 single-fact writes | |
|---|---|
synchronous=FULL (current) |
85 ms each |
synchronous=NORMAL (recommended for WAL) |
13 ms each |
backfillEmbeddings also persists one entry per transaction on the main thread after session_start, so a session that adds N facts makes the next start stall synchronously for ~85·N ms. NORMAL + a single transaction per backfill batch would make that path disappear; durability differs only after an OS/power crash, which a memory store can tolerate. Not in #40, since it is a durability-policy decision rather than a bug.
What is not the cause (already measured, so nobody needs to re-check)
session_startseeding fromctx.sessionManager.getBranch(): 0.6 ms for a 4.8 MB / 501-entry session.store.stats(): threeCOUNT(*)s, sub-millisecond.- SQLite open itself: 11 ms, independent of store size.
- Per-turn injection:
buildSelectiveBlockis pure FTS — no embedding request on the prompt path, so sending a message is unaffected. - WAL checkpoint on
store.close(): 0.1 ms.