issue-59: db_query returns undefined content text for multi-statement SQL, breaking the session #60
No reviewers
Labels
No labels
bug
chore
documentation
enhancement
feature
ready
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
david/pi-extensions-and-skills!60
Loading…
Reference in a new issue
No description provided.
Delete branch "bug/issue-59/db-query-returns-undefined-content-text-for-multi-statement-sql-breaking-the-session"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Root cause
node-postgres resolves multi-statement SQL (simple query protocol) with an array of Result objects instead of a single result. In that shape
result.rowsisundefined, so:db_query:JSON.stringify(result.rows)→ JSundefined→ tool returns{ content: [{ type: "text", text: undefined }] }. That malformed block is persisted into session history, and every subsequent LLM call throwsCannot read properties of undefined (reading 'length')in pi-ai's token estimation (estimateMessageTokensreadsblock.text.lengthunguarded) — the session appears hung until restarted.db_query_one:result.rows.length === 0threw the same error directly (caught by the tool runner, so recoverable, but still broken on legitimate input).Fix
collectRows(result)/collectRowCount(result)helpers that normalize both single-result and array-of-results shapes; rows are flattened across statements and text is always a JSON string.db_query,db_query_one, plus defensive normalization indb_list_tables/db_table_schema).registerDbTools(pi, pool)so tests can exercise real tool execution with a fake pool (no live DB, no module mocking).Tests
New
extensions/postgres/multi-statement.test.ts: helper unit tests + tool-execution regression tests covering the array-of-results shape, single-result behavior, and the no-rows placeholder. All 42 tests pass (bun testinextensions/postgres). Also verified live against a local postgres: multi-statement DDL+INSERT and two-SELECT queries now return proper string payloads with flattened rows.Closes #59