Skip to content

Use blocks_pkey for failed parent answer lookups in the chat feed - #1354

Merged
selfcontained merged 1 commit into
mainfrom
fix/feed-open-input-uuid-index
Oct 30, 2026
Merged

selfcontained merged 1 commit into
mainfrom
fix/feed-open-input-uuid-index

Conversation

@selfcontained

Copy link
Copy Markdown
Owner

Problem

On a host with ~28k blocks, chat streams took 2–7 s to load. The feed's open-input lookup (FAILED_PARENT_INPUT_SQL in apps/server/src/chat/feed.ts, used by listOpenInputs and listBlockEntries) compared answer.id::text to the stored blockId. Postgres can't use blocks_pkey for that comparison, so it scanned every block once per parent-handled ask. One measured stream did 1,046 inner seq scans and took 6.94 s; that query accounted for 4.1 s of a 4.17 s composeStreamFeed call.

Fix

Compare answer.id as a uuid. Only canonical lowercase ids are cast, which is the same set the text comparison could match, so a malformed blockId still matches nothing instead of failing the query.

Validation

  • New test: the failed-parent answer still shows as an open input, a malformed blockId doesn't break the feed, and with enable_seqscan = off the plan uses blocks_pkey on blocks answer. The test fails when the old ::text comparison is restored.
  • Read-only EXPLAIN ANALYZE on a local production DB (20k blocks): 55.8 ms → 0.18 ms. On the slow host, a diagnostic version of this query ran in about 2 ms, against 6.94 s for the original.
  • pnpm run check, plus vitest on stream-service.test.ts and test/chat: 299 passed.

🤖 Generated with Claude Code

Comparing answer.id::text to the stored blockId forced a sequential scan
of blocks once per parent-handled ask, which took 3-7s per feed load on a
host with ~28k blocks. Compare as uuid instead, casting only canonical
lowercase ids so malformed values still match nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@selfcontained
selfcontained merged commit 9930fc7 into main Oct 30, 2026
1 check passed
@selfcontained
selfcontained deleted the fix/feed-open-input-uuid-index branch October 30, 2026 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant