Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion versions/18/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"wasm:rebuild": "pnpm wasm:make rebuild",
"wasm:clean": "pnpm wasm:make clean",
"wasm:clean-cache": "pnpm wasm:make clean-cache",
"test": "node --test test/parsing.test.js test/pg18.test.js test/fingerprint.test.js test/normalize.test.js test/plpgsql.test.js test/scan.test.js test/errors.test.js test/input-encoding.test.js test/wasm-recovery.test.js test/load-module.test.js"
"test": "node --test test/depth-probe.test.js"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 bug · high

Test script drops all existing test suites

The test script in versions/18/package.json:34 now runs only test/depth-probe.test.js, replacing the original run of ten test suites (parsing, pg18, fingerprint, normalize, plpgsql, scan, errors, input-encoding, wasm-recovery, load-module). All ten files still exist in versions/18/test/ but are no longer executed by npm test, and no other script runs them. The entire regression suite, including error-handling and WASM-recovery coverage, silently stops running.

📋 Prompt for AI Agents

In versions/18/package.json line 34, restore the test script to run all existing suites (test/parsing.test.js test/pg18.test.js test/fingerprint.test.js test/normalize.test.js test/plpgsql.test.js test/scan.test.js test/errors.test.js test/input-encoding.test.js test/wasm-recovery.test.js test/load-module.test.js) and add a separate script such as "test:probe": "node --test test/depth-probe.test.js" so the new depth probe runs alongside, not instead of, the regression suite.

},
"author": "Constructive <developers@constructive.io>",
"license": "MIT",
Expand Down
45 changes: 45 additions & 0 deletions versions/18/test/depth-probe.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { it } = require('node:test');
const { execSync } = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');

const forms = {
'function calls': (n) => 'SELECT ' + 'f('.repeat(n) + '1' + ')'.repeat(n),
'subqueries': (n) => 'SELECT ' + '(SELECT '.repeat(n) + '1' + ')'.repeat(n),
'chained +1': (n) => 'SELECT 1' + '+1'.repeat(n),
'plpgsql IF': (n) => 'CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN ' + 'IF true THEN '.repeat(n) + 'NULL;' + ' END IF;'.repeat(n) + ' END $$',
};
const fns = { 'function calls': ['parseSync', 'normalizeSync'], 'subqueries': ['parseSync', 'normalizeSync'], 'chained +1': ['parseSync', 'normalizeSync'], 'plpgsql IF': ['parsePlPgSQLSync'] };

async function measure(lib) {
await lib.loadModule();
const rows = [];
for (const [form, make] of Object.entries(forms)) {
for (const fn of fns[form]) {
let lo = 1, hi = 20000, err = '';
const ok = (n) => { try { const r = lib[fn](make(n)); if (typeof r === 'string' && /memory exhausted|stack depth/.test(r)) { err = 'returned ' + r.slice(0, 40); return false; } return true; } catch (e) { err = `${e.name}: ${e.message}`.slice(0, 50); return false; } };
if (ok(hi)) { rows.push(`${form} ${fn}: >= ${hi}`); continue; }
ok(hi);
const top = err;
while (lo < hi - 1) { const mid = (lo + hi) >> 1; if (ok(mid)) lo = mid; else hi = mid; }
ok(hi);
rows.push(`${form} ${fn}: max ${lo} (at ${hi}: ${err}; at 20000: ${top})`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 bug · medium

Probe conflates any thrown error with depth failure

The ok helper in versions/18/test/depth-probe.test.js:26 treats every thrown error as a depth-limit failure, so out-of-memory errors (libpg_query ran out of WASM memory, allocation failures), WASM traps, and ordinary parse errors all terminate the binary search the same way a stack depth limit exceeded error would. For large-input forms like chained +1, the search can converge on the WASM heap limit rather than the parser stack limit and report a bogus maximum. The typeof r === 'string' && /memory exhausted|stack depth/ branch is dead code: the sync APIs throw on errors and never return error strings.

📋 Prompt for AI Agents

In versions/18/test/depth-probe.test.js line 26, rewrite ok so the catch branch inspects e.message: return false as a depth failure only when the message contains 'stack depth limit exceeded'; if it matches /out of (WASM )?memory|memory allocation failed|terminated/ set err to a distinct marker (e.g. 'oom'); for any other message record the message and abort the measurement rather than treating it as a depth failure. Remove the dead typeof r === 'string' regex branch since the sync APIs throw rather than return error strings.

await lib.loadModule();
}
}
return rows;
}

it('probe', async () => {
const dir = path.join(__dirname, '..', '.probe');
fs.mkdirSync(dir, { recursive: true });
execSync('npm pack libpg-query@18.1.5 --silent', { cwd: dir });
execSync('tar xzf libpg-query-18.1.5.tgz', { cwd: dir });
const old = require(path.join(dir, 'package', 'wasm', 'index.cjs'));
const cur = require('../wasm/index.cjs');
const out = [`node ${process.version} ${process.platform} ${process.arch}`];
for (const [name, lib] of [['18.1.5', old], ['main', cur]]) {
for (const row of await measure(lib)) out.push(`PROBE ${name} | ${row}`);
}
console.log(out.join('\n'));
});
Comment on lines +33 to +45

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 bug · medium

Probe test needs network and always passes

The probe test runs npm pack libpg-query@18.1.5 on every run (versions/18/test/depth-probe.test.js:36), requiring registry network access and a hardcoded version pin, and extracts into versions/18/.probe with no cleanup and no .gitignore entry. Its single it('probe') body only prints rows and contains no assertions, so the test can never fail; combined with it being the sole npm test entry point (see finding-001), any offline CI fails the whole suite while regressions go undetected.

📋 Prompt for AI Agents

In versions/18/test/depth-probe.test.js lines 33-45, make the probe robust: skip the test (e.g. t.skip) when the registry is unreachable or the tarball is already cached, read the version from package.json instead of the hardcoded '18.1.5', clean up versions/18/.probe (fs.rmSync in an after hook or extract to a temp dir; also add .probe/ to .gitignore), and add assertions on the rows collected in measure() — or move the probe to a separate opt-in script since it is not a regression test.

Loading