Fix node:sqlite V8 crash on invalid UTF-8 in text columns (#272)

node:sqlite calls v8::String::NewFromUtf8 with kAbort on TEXT columns.
Cursor chat blobs often contain truncated multi-byte chars from streaming
boundaries, which triggers a V8 CHECK abort (not a JS exception).

Select all text-content columns as CAST(col AS BLOB) so node:sqlite
returns Uint8Array instead. Decode in JS with TextDecoder fatal:false
which replaces bad bytes with U+FFFD. Covers all three SQLite providers
(Cursor, Goose, OpenCode).

Removes the version blocklist (MIN_NODE_22_PATCH) and lowers engines
requirement from >=22.20 to >=22 since the BLOB cast approach works
on all Node 22.x versions.

Closes #264
Closes #250
This commit is contained in:
Resham Joshi 2026-05-10 17:05:08 -07:00 • committed by GitHub
parent d142bd97ef
commit 02f4635cec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 101 additions and 76 deletions

View file

@ -23,29 +23,18 @@ let DatabaseSync: DatabaseSyncCtor | null = null
let loadAttempted = false
let loadError: string | null = null
/// Minimum Node 22.x patch version that contains the node:sqlite UTF-8 fix.
/// Older 22.x lines crash with `Check failed: (location_) != nullptr` when a
/// SQLite TEXT column returns bytes that V8's String::NewFromUtf8 rejects —
/// commonly the case for Cursor's text blobs (truncated multi-byte chars at
/// streaming boundaries) and OpenCode message text (rich tooling output).
/// Track of issue: https://github.com/getagentseal/codeburn/issues/264
/// Track of upstream: https://github.com/nodejs/node — fix landed in 22.x via
/// later patches; stable on Node 24+.
const MIN_NODE_22_PATCH = 20
const textDecoder = new TextDecoder('utf-8', { fatal: false })
function checkBuggyNodeVersion(): string | null {
const match = /^v(\d+)\.(\d+)\.(\d+)/.exec(process.version)
if (!match) return null
const major = parseInt(match[1]!, 10)
const minor = parseInt(match[2]!, 10)
if (major === 22 && minor < MIN_NODE_22_PATCH) {
return (
`codeburn: Node ${process.version} ships an older node:sqlite that crashes on ` +
`non-UTF-8 bytes in Cursor/OpenCode session text. Upgrade to Node 22.${MIN_NODE_22_PATCH}+ ` +
`or 24+ to avoid the V8 fatal error. (https://nodejs.org)`
)
}
return null
/// Safely decode a BLOB column (Uint8Array) to a UTF-8 string. Node's
/// node:sqlite crashes with a V8 CHECK abort when a TEXT column contains
/// invalid UTF-8 (common in Cursor chat blobs with truncated multi-byte
/// chars). By selecting those columns as `CAST(... AS BLOB)` in SQL, we
/// get a Uint8Array here and decode it in JS where bad bytes become the
/// U+FFFD replacement character instead of aborting the process.
export function blobToText(value: Uint8Array | string | null | undefined): string {
if (value == null) return ''
if (typeof value === 'string') return value
return textDecoder.decode(value)
}
/// Lazily imports `node:sqlite`. On Node 22/23 it emits an ExperimentalWarning the first
@ -56,15 +45,6 @@ function loadDriver(): boolean {
if (loadAttempted) return DatabaseSync !== null
loadAttempted = true
// Refuse to load on a Node version known to crash mid-query. Treating the
// SQLite providers as unavailable is much friendlier than letting the user
// hit a V8 CHECK abort that takes down the whole CLI.
const versionWarning = checkBuggyNodeVersion()
if (versionWarning !== null) {
loadError = versionWarning
return false
}
const origEmit = process.emit.bind(process)
let restored = false
const restore = () => {