feat(extraction): capture function-as-value — callback registration sites in callers/impact (#756) (#807)

A function name used as a VALUE — passed as an argument
(signal(SIGINT, handler), qsort(..., compare)), assigned to a function
pointer or field (ops->recv_cb = my_cb, OnClick := Handler), or placed in
a struct initializer / handler table ({ .recv_cb = my_cb },
{ "get", getCommand }) — produced no edge in ANY of the 19 tree-sitter
languages, so registered callbacks looked dead and their registration
sites were invisible to callers/impact.

This adds table-driven function-as-value capture across all 19 languages
(plus the wrapper forms: &fn, &Cls::method, Java Class::m, Kotlin ::f,
Swift #selector, ObjC @selector, Ruby method(:sym), Scala eta, Pascal
@Handler), gated at extraction (same-file definitions + imported
bindings; C-family file-scope initializers are constant-expression
contexts and skip the gate, which is how redis-style cross-file command
tables resolve), and resolved by a dedicated strategy: function/method
targets only, same-file first, unique-or-drop cross-file, no fuzzy
fallback ever. Edges persist as kind 'references' with metadata.fnRef,
so getCallers/getImpactRadius surface them with zero graph-layer
changes; MCP callers/callees label them "via callback registration".

Precision rules bought by real-repo false positives (full A/B record in
docs/design/function-ref-capture.md): C++ is &-explicit outside
file-scope tables (fmt's begin/out/size collisions; out-of-line member
defs are function-kind); TS/JS/Python bare ids resolve to functions only
(TS class fields extract as method-kind — pre-existing quirk); Swift
refuses same-file method overload-families; param-forward shapes
(this.x = x, value: value) and destructuring are skipped; minified
bundles (*.min.js) produce no candidates.

Validated on 17 public OSS repos (redis, excalidraw, gin, bytes, okhttp,
okio, Alamofire, flask, sinatra, Newtonsoft.Json, scopt, provider,
busted, Fusion, AFNetworking, PascalCoin, fmt): node counts identical,
zero calls edges lost or gained, references strictly additive
(+3,200 registration edges total), precision spot-checked by reading
sampled source lines (redis 30/30, flask 8/8). Deliberately NOT covered:
indirect-dispatch resolution (o->cb(x) → impl) — that needs data-flow
through struct fields, and a wrong edge is worse than none.

EXTRACTION_VERSION 18 → 19 (re-index to benefit).

Closes #756

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Colby Mchenry
2026-06-11 14:20:27 -05:00
committed by GitHub
co-authored by Claude Opus 4.8
parent 0df9246752
commit 8a114ba53c
13 changed files with 1706 additions and 16 deletions
+115 -1
View File
@@ -158,7 +158,7 @@ export function crossesKnownFamily(a: string, b: string): boolean {
* both-known filter so `.vue`/`.svelte` (own tag) importing `.ts` survives.
*/
function applyLanguageGate(candidates: Node[], ref: UnresolvedRef): Node[] {
if (ref.referenceKind === 'references') {
if (ref.referenceKind === 'references' || ref.referenceKind === 'function_ref') {
return candidates.filter((c) => sameLanguageFamily(c.language, ref.language));
}
if (ref.referenceKind === 'imports') {
@@ -167,6 +167,113 @@ function applyLanguageGate(candidates: Node[], ref: UnresolvedRef): Node[] {
return candidates;
}
/**
* Resolve a function-as-value reference (#756) — a function name used as a
* callback/function-pointer value (`register(handler)`, `o->cb = handler`,
* `{ .cb = handler }`, `signal(SIGINT, handler)`). The ONLY strategy allowed
* for `function_ref` refs: exact name, function/method targets only, same
* language family, same-file first, and cross-file only when the match is
* UNIQUE. No fuzzy fallback, no qualified-name walking — a wrong callback
* edge is worse than none.
*/
export function matchFunctionRef(
ref: UnresolvedRef,
context: ResolutionContext
): ResolvedRef | null {
// In JS/TS/Python a bare identifier can never be a method value (methods
// are only reachable through a receiver — `this.m` / `self.m` /
// `Cls.m`), so bare fn-refs match FUNCTIONS only. This also sidesteps the
// pre-existing TS quirk of class fields extracting as method-kind nodes,
// which otherwise soaked up local names passed as arguments (excalidraw
// A/B finding; same pattern in vendored docopt.py). Python's `self.m`
// form keeps method targets via its own capture shape. C++ likewise: a
// bare identifier can only be a FREE function (member values need
// `&Cls::method`). Other languages keep method targets: C# method groups,
// Swift/Dart implicit-self, Java/Kotlin method references.
const bareFnOnly =
ref.language === 'typescript' || ref.language === 'tsx' ||
ref.language === 'javascript' || ref.language === 'jsx' ||
ref.language === 'cpp' || ref.language === 'python';
// Qualified member-pointer (`&Widget::on_click` → "Widget::on_click"):
// resolve the member ON THAT SCOPE — exempt from bareFnOnly (the `&Cls::m`
// shape is an explicit member reference). Unique-or-drop like everything else.
if (ref.referenceName.includes('::')) {
const memberName = ref.referenceName.slice(ref.referenceName.lastIndexOf('::') + 2);
const scoped = context
.getNodesByName(memberName)
.filter(
(n) =>
(n.kind === 'function' || n.kind === 'method') &&
sameLanguageFamily(n.language, ref.language) &&
n.id !== ref.fromNodeId &&
(n.qualifiedName === ref.referenceName ||
n.qualifiedName.endsWith(`::${ref.referenceName}`))
);
if (scoped.length === 0) return null;
const sameFileScoped = scoped.filter((n) => n.filePath === ref.filePath);
const pool = sameFileScoped.length > 0 ? sameFileScoped : scoped;
if (sameFileScoped.length === 0 && scoped.length > 1) return null;
const target = pool.reduce((a, b) => (a.startLine <= b.startLine ? a : b));
return {
original: ref,
targetNodeId: target.id,
confidence: 0.9,
resolvedBy: 'function-ref',
};
}
const candidates = context
.getNodesByName(ref.referenceName)
.filter(
(n) =>
(n.kind === 'function' || (!bareFnOnly && n.kind === 'method')) &&
sameLanguageFamily(n.language, ref.language) &&
n.id !== ref.fromNodeId // a function registering itself is not a dependency edge
);
if (candidates.length === 0) return null;
// Same-file definition wins — the extraction gate guarantees most survivors
// have one, and it's the dominant C pattern (static callback registered in
// a same-file ops struct).
const sameFile = candidates.filter((n) => n.filePath === ref.filePath);
if (sameFile.length > 0) {
// Swift: several same-named METHODS in one file is an API overload family
// (`Session.request(...)` × N), and a bare identifier hitting it is almost
// always a same-named parameter, not a method value (Alamofire A/B
// finding) — refuse rather than guess. A single method (SwiftUI's
// `action: handleTap`) still resolves.
if (
ref.language === 'swift' &&
sameFile.length > 1 &&
sameFile.every((n) => n.kind === 'method')
) {
return null;
}
// Same-name overloads in one file are the same conceptual symbol; pick
// the first by position for determinism.
const target = sameFile.reduce((a, b) => (a.startLine <= b.startLine ? a : b));
return {
original: ref,
targetNodeId: target.id,
confidence: sameFile.length === 1 ? 0.95 : 0.9,
resolvedBy: 'function-ref',
};
}
// Cross-file (imported names the import resolver didn't already claim):
// only an unambiguous match resolves.
if (candidates.length === 1) {
return {
original: ref,
targetNodeId: candidates[0]!.id,
confidence: 0.8,
resolvedBy: 'function-ref',
};
}
return null;
}
/**
* Try to resolve a reference by exact name match
*/
@@ -1124,6 +1231,13 @@ export function matchReference(
ref: UnresolvedRef,
context: ResolutionContext
): ResolvedRef | null {
// Function-as-value refs (#756) resolve ONLY through the dedicated matcher —
// never the fuzzy/qualified fallthrough below (a wrong callback edge is
// worse than none).
if (ref.referenceKind === 'function_ref') {
return matchFunctionRef(ref, context);
}
// Try strategies in order of confidence
let result: ResolvedRef | null;