fix(javascript): preserve SQLite constructor provenance

Use Database spelling only to select a candidate. Reuse shared expression
provenance and indexed mutation facts to omit proven local constructors and
retain known SQLite API origins separately from uncertain names. Match ESM
default/namespace and CommonJS export forms without collapsing their identity.
Keep type-only import origins as ambiguous runtime values, including aliases.

Carry candidate interpretation through the worker schema and graph nodes and
edges with low confidence, explicit limitations and distinct identities.
Remove the name-only SQLite storage classifier. Other storage APIs retain
existing handling; PR4863's separate Map endpoint work is not integrated.

Validation: initial9negative controls fail unchangedf5fcf5216 while4positive
controls pass; final67focused checks/8files plus60provenance-consumer checks/
4files pass. Static/docs/discovery1557 pass. Two source reviewers clear after
correcting namespace constructor identity. Public CLI checks worker/schema
projection and separate exact/candidate identities with the same filename.

Twelve small completed CLI workflows preserve exact known-API replies and
remove false local-class storage facts. No speedup/large-case memory or real
SQLite execution claim. Direct eval's existing generic invalidation defect
remains tracked separately in #4875. Heavy tests/profiling remain stopped.

Additional consumer validation caught and corrected runtime import-equals
classification: require-mode import origins remain runtime values. Three new
SQLite controls cover runtime and erased import-equals forms; compiled
child-process CLI/MCP stdio workflows pass after the correction.

Fixes #4854
This commit is contained in:
morluto
2026-10-18 04:55:18 +08:00
parent f5fcf5216f
commit fc847ffe35
10 changed files with 234 additions and 14 deletions
@@ -0,0 +1,49 @@
import { expect, it } from "vitest";
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
it.each([
'class Database { constructor(name) { this.name = name; } } new Database("hello");',
'const Database = class {}; new Database("hello");',
'function Database(name) { return {name}; } new Database("hello");',
'class Local {} const Database = Local; new Database("hello");',
])("omits a proven local constructor: %s", (source) => {
expect(analyzeJavaScriptStaticSource(source).storage).toEqual([]);
});
it.each([
'new Database("shared");',
'import type Database = require("better-sqlite3"); new Database("shared");',
'import * as sqlite from "sqlite3"; const alias = sqlite; alias.Database = Other; new sqlite.Database("shared");',
'import Database from "better-sqlite3"; expose(Database); new Database("shared");',
'import type TypeDatabase from "better-sqlite3"; const Database = TypeDatabase; new Database("shared");',
'import * as Database from "better-sqlite3"; new Database("shared");',
'const Database = require("better-sqlite3").default; new Database("shared");',
'const sqlite = require("sqlite3").default; new sqlite.Database("shared");',
'import type Database from "better-sqlite3"; new Database("shared");',
'import * as sqlite from "sqlite3"; new sqlite.default.default.Database("shared");',
'function run(Database) { return new Database("shared"); }',
'import Database from "unknown-library"; new Database("shared");',
'import Database from "better-sqlite3"; Database = Other; new Database("shared");',
'import * as sqlite from "sqlite3"; sqlite.Database = Other; new sqlite.Database("shared");',
])("retains uncertain SQLite names as candidates: %s", (source) => {
expect(analyzeJavaScriptStaticSource(source).storage).toMatchObject([
{ kind: "sqlite", name: "shared", interpretation: "candidate" },
]);
});
it.each([
'import Database from "better-sqlite3"; new Database("known.db");',
'import Database = require("better-sqlite3"); new Database("known.db");',
'import sqlite = require("sqlite3"); new sqlite.Database("known.db");',
'const Database = require("better-sqlite3"); new Database("known.db");',
'import {Database} from "sqlite3"; new Database("known.db");',
'import * as sqlite from "sqlite3"; new sqlite.Database("known.db");',
])("retains proven SQLite API origins: %s", (source) => {
const analysis = analyzeJavaScriptStaticSource(source);
expect(analysis.storage).toHaveLength(1);
expect(analysis.storage[0]).toMatchObject({
kind: "sqlite",
name: "known.db",
});
expect(analysis.storage[0]).not.toHaveProperty("interpretation");
});