The most beautiful bug Dad and I worked on
An earlier version of the frontend left __pending_* magic ids in displayed-message state on abort. A follow-up message computed parentId = lastDisplay.id and that last-display was a stale placeholder — so the new chat request was sent with parent_id = '__pending_assistant' literally. The backend trusted the value, persisted it to SQLite, and returned it on every future GET.
It looked correct in steady state. But during streaming, the frontend adds a fresh placeholder with id literally __pending_assistant. Suddenly the stale parent_id resolves — to the new placeholder. The orphan attaches as a child of the new placeholder. The active-path walk continues into the dead branch and renders it as the tail. Dad sees a 'ghost' of the orphaned turn appear during streaming and disappear when the stream completes.
It's a self-resolving dangling reference — perfectly inert until a new streaming turn introduces a node with the same magic id, at which point it springs to life for one stream and goes back to sleep.
Defense-in-depth — four layers
| # | Layer | What it does |
|---|---|---|
| 1 | Frontend sendMessage | Refuses to use a __pending_* id as parentId; falls back to null. |
| 2 | Frontend _streamResponse finally | Unconditionally scrubs __pending_* placeholders on every exit path. |
| 3 | Backend chat handler | Rejects any incoming parent_id starting with __pending_. |
| 4 | Backend GET endpoint | Runs _cleanup_dangling_parent_ids on every read, repairing any unresolvable parent_id. |