Skip to content

Graphs that teach > graphs that impress. Turn any code into an interactive knowledge graph you can explore, search, and ask questions about. Works with Claude Code, Codex, Cursor, Copilot, Gemini CLI, and more.

fix(core): distinguish unbound class expression scopes

Lum1104acc795050db9
diff --git a/docs/incremental/symbol-loss-validation.md b/docs/incremental/symbol-loss-validation.md
index 03bab426..48a380b8 100644
--- a/docs/incremental/symbol-loss-validation.md
+++ b/docs/incremental/symbol-loss-validation.md
@@ -15,7 +15,7 @@ The source-evidence layer mixed structural declarations, runtime-installed names
3. A unique current declaration plus a unique current graph binding preserves the old symbol. A declaration without a matching graph node reports `still-present`.
4. Missing identities become `unknown` if parsing, baseline identity, evidence version, or current ID ownership cannot be verified. Empty extraction never proves deletion.
5. Supplemental evidence describes possible declarations using `kind`, `owner`, `name`, optional name suffix, location and reason. A null field is explicitly unknown, never an empty/free scope. Only evidence compatible with the missing identity prevents deletion. Ordinary reads, strings and parameter names are not declarations. Literal symbol names are not normalized as graph-ID delimiters (for example, #run differs from .run).
-6. Verified standard installers preserve exact names and owners (including Ruby readers versus writers). Dynamic keys have unknown names; unresolved, shadowed or reassigned receivers have unknown owners; arbitrary evaluation/installer aliases or shadowed installer APIs may require file-wide uncertainty. The gate does not execute code or restore old semantic data.
+6. Verified standard installers preserve exact names and owners (including Ruby readers versus writers). Dynamic keys have unknown names; unresolved, shadowed or reassigned receivers have unknown owners; arbitrary evaluation/installer aliases or shadowed installer APIs may require file-wide uncertainty. Unbound class expressions retain a distinct internal lexical scope; expressions exposed through unresolved assignments retain uncertainty. The gate does not execute code or restore old semantic data.
7. If the uniquely mapped old identity is absent and no compatible uncertainty remains, report `deleted`. A different class's same-name declaration is not that identity. Reusing the old graph ID for a different owner remains an identity conflict until the analyzer supplies distinct descriptors.
8. Any unresolved entry blocks publication. One targeted repair may regenerate complete affected files. Failed repair leaves all three durable baseline files unchanged.
diff --git a/tests/skill/understand/test_symbol_evidence_matrix.test.mjs b/tests/skill/understand/test_symbol_evidence_matrix.test.mjs
index c59db9aa..fd81cc41 100644
--- a/tests/skill/understand/test_symbol_evidence_matrix.test.mjs
+++ b/tests/skill/understand/test_symbol_evidence_matrix.test.mjs
@@ -261,6 +261,24 @@ describe('scoped symbol evidence contract', () => {
}
});
+ it.each(['ts', 'tsx', 'js', 'jsx'])('keeps unbound %s class expressions separate from external class bindings', extension => {
+ for (const expression of ['class A', 'class B', 'class']) {
+ for (const code of [`consume(${expression} { run() {} }); function keep() {}`,
+ `(${expression} { run() {} }); function keep() {}`]) {
+ expect(classify(extension, code).result.missing[0].status).toBe('deleted');
+ expect(classify(extension, code, { beforeSource: 'class A {}', name: 'A', oldExtra: {type: 'class'} })
+ .result.missing[0].status).toBe('deleted');
+ }
+ }
+ // An external binding through a wrapper/property is still unresolved.
+ for (const code of ['const A = consume(class B { run() {} }); function keep() {}',
+ 'target.A = class B { run() {} }; function keep() {}']) {
+ expect(classify(extension, code).result.missing[0].status).toBe('unknown');
+ expect(classify(extension, code, { beforeSource: 'class A {}', name: 'A', oldExtra: {type: 'class'} })
+ .result.missing[0].status).toBe('unknown');
+ }
+ });
+
it('distinguishes Ruby alias writer names, source names and global-variable aliases', () => {
for (const declaration of ['keep run', ':"run=" :"keep="', '$run $keep']) {
expect(classify('rb', `${shells.rb}class A\n alias ${declaration}\nend`).result.missing[0].status).toBe('deleted');
diff --git a/understand-anything-plugin/packages/core/src/plugins/symbol-evidence.ts b/understand-anything-plugin/packages/core/src/plugins/symbol-evidence.ts
index 297057df..be10bcd2 100644
--- a/understand-anything-plugin/packages/core/src/plugins/symbol-evidence.ts
+++ b/understand-anything-plugin/packages/core/src/plugins/symbol-evidence.ts
@@ -63,6 +63,9 @@ function unwrap(node: Node | null): Node | null {
function classExpression(node: Node): boolean {
return node.type === "class" && node.childForFieldName("body")?.type === "class_body";
}
+// A lexical expression scope is distinct from an unresolved external binding.
+// This internal label cannot be a JavaScript declaration name.
+const UNBOUND_CLASS_EXPRESSION = "<unbound-class-expression>";
function expressionOwner(node: Node): string | null {
let value = node;
while (value.parent?.type === "parenthesized_expression") value = value.parent;
@@ -71,7 +74,13 @@ function expressionOwner(node: Node): string | null {
if (parent?.type === "assignment_expression" && parent.childForFieldName("right")?.id === value.id) {
return identifier(parent.childForFieldName("left"));
}
- return null;
+ // A surrounding assignment may expose the expression through a wrapper or
+ // property target. Without one, its local name is not a file-level binding.
+ for (let outer = parent; outer; outer = outer.parent) {
+ if (["variable_declarator", "assignment_expression"].includes(outer.type)) return null;
+ if (FUNCTION_NODES.has(outer.type) || ["expression_statement", "return_statement"].includes(outer.type)) break;
+ }
+ return UNBOUND_CLASS_EXPRESSION;
}
function lexicalOwner(node: Node): string | null {
for (let parent = node.parent; parent; parent = parent.parent) {
@@ -300,7 +309,8 @@ export function collectSymbolEvidence(root: Node, structure: StructuralAnalysis,
}
// Escaped/opaque declaration spellings cannot establish absence. Ordinary
// identifiers, references and string literals do not imply declarations.
- if (declared && !objectMethod && !localDeclaration && (CLASS_NODES.has(node.type) || FUNCTION_NODES.has(node.type))
+ if (declared && !objectMethod && !localDeclaration && !classExpression(node)
+ && (CLASS_NODES.has(node.type) || FUNCTION_NODES.has(node.type))
&& declaredName === null) {
add(declared, CLASS_NODES.has(node.type) ? "" : owner, null, "Unresolved declaration name",
CLASS_NODES.has(node.type) ? "class" : "callable");