GitNexus/gitnexus/test/unit/pdg-callee-id-capture.test.ts
Gergő Magyar ed8ab1c246
Some checks are pending
CodeQL / Analyze (python) (push) Waiting to run
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
Gitleaks / gitleaks (push) Waiting to run
Publish / Classify release event (push) Waiting to run
Publish / RC guard (marker + release-PR skip) (push) Blocked by required conditions
Publish / ci (push) Blocked by required conditions
Publish / Publish to npm (push) Blocked by required conditions
Publish / Build & Push RC Docker images (push) Blocked by required conditions
Scorecard / Scorecard analysis (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-cli) (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-web) (push) Waiting to run
fix(scope-resolution): resolve callable reference flows (#2437) (#2522)
* docs(plans): add provider-hook value-refs plan (#2437)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs(plans): deepen #2437 plan to USES + property-dispatch design

Design revised after prior-art research (Kythe ref vs ref/call, Joern
METHOD_REF, Feldthaus field-based call graphs, CodeQL impliedReceiverStep):
registration sites emit reference-class USES, invocation is recovered by a
field-based property-dispatch pass synthesizing CALLS at member-call sites.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(scope-resolution): model provider-hook value references (#2437)

Functions referenced as object-literal property values (provider hooks like
emitScopeCaptures: emitCppScopeCaptures) previously produced no edge at all,
so impact/context reported a false-safe 0 upstream dependents.

Two coordinated halves, per prior art (Kythe ref vs ref/call, Joern
METHOD_REF, Feldthaus ICSE'13 field-based call graphs, CodeQL
impliedReceiverStep):

- Registration -> USES: new ReferenceKind 'value-ref'; TS/JS queries capture
  pair values and shorthand properties (with @reference.property-key);
  emitted as a reference-class USES edge, reason 'scope-resolution:
  value-ref'. Resolution is callable-gated so plain values emit nothing.
- Dispatch -> CALLS: new shared pass emitPropertyDispatchCalls synthesizes
  CALLS (reason 'property-dispatch', confidence 0.7, per-key fan-out cap 32
  calibrated on this repo's 16-provider hook tables) from member-call sites
  to every function registered under the same property key.

Deviation from plan: the pass owns value-ref resolution entirely via the
post-finalize findCallableBindingInScope walker — the shared registries only
see pre-finalize local bindings, so imported hooks (the c-cpp.ts case) were
unresolvable through lookupForSite; Reference.propertyKey passthrough
dropped as unnecessary.

SCHEMA_BUMP 13 -> 14: ParsedFile gains value-ref sites + propertyKey.

Verified end-to-end: impact(emitCppScopeCaptures, upstream) now reports 8
impacted / HIGH with extractParsedFile (true dispatch caller) at d=1 via
property-dispatch and the c-cpp.ts registration via USES.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(scope-resolution): cover value-ref registration and property dispatch (#2437)

Integration: same-file/cross-file/aliased/shorthand registrations emit USES;
non-callable and destructuring values emit nothing; dispatch sites gain
property-dispatch CALLS (incl. JS twins and per-language partitioning);
fan-out-capped keys are dropped entirely; factory-call values unchanged.
Unit: capture-shape pins for @reference.value-ref + @reference.property-key.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(scope-resolution): surface dropped property-dispatch keys in stats (#2437)

Review finding: skippedKeys was returned but discarded — a hook table
larger than the fan-out cap silently reopened the #2437 gap for those
keys. Log dropped keys and fold value-ref USES + dispatch CALLS into
referenceEdgesEmitted.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs(plans): add callable reference-flow implementation plan

* fix(scope-resolution): close property-dispatch review gaps

* feat(scope-resolution): add callable flow facts

* feat(scope-resolution): resolve callable value flow

* feat(scope-resolution): resolve callable references across providers

* fix: harden callable reference flow resolution

* fix(scope-resolution): preserve callable binding semantics

* docs(plans): add pr-2522-review-fixes plan

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(storage): bump INCREMENTAL_SCHEMA_VERSION for callable-value-flow edges

Callable-value-flow CALLS/USES edges (#2437) can connect two files whose
content did not change, but the incremental write set only covers changed
files — a top-up against a pre-v7 index would silently omit the new edges
for every unchanged file pair, indefinitely. Force the one-time full
re-analyze (review finding 1, #2522).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(storage): sanitize callable-flow sites per-site at load, log drops

The load-time validator rejected the WHOLE ParsedFile when one site was
malformed or over-bound, with no logging — and C++ legitimately emits
empty-string parameterTypes entries ('' = unknown, the
ReferenceSite.argumentTypes convention) for cv-only/ERROR-recovered types,
so real repos fell into a permanent, silent warm-cache-miss reparse loop
through the #1983-sensitive main-thread path (review finding 7, #2522).

Now: '' entries are valid in type arrays; a malformed/over-bound site drops
only itself (counted, warned once per load); only non-array garbage —
evidence the serialization itself is untrustworthy — rejects the file.
Deviation from plan §6 wording: validator-side tolerance replaces emit-side
clamps — smaller diff, same asymmetry closed at the single chokepoint.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(scope-resolution): keep declarations in the union for reassigned callable cells

The binding-lookup suppression for fact-constrained cells was wholesale:
reassigning a declared function through its own name (greet = other;
greet()) deferred the call to the solver, which then refused the lexical
lookup that resolves the declaration — an unresolvable RHS yielded zero
CALLS for a call that resolved pre-flow (review finding 8, #2522).

Suppression now applies only to cells bound by FORMAL facts — its actual
purpose (a parameter whose grammar emits no declaration binding must not
adopt a same-named outer function). Copy/alias/store/load destinations keep
their declaration as an inclusion seed (Andersen-style union).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(scope-resolution): count forfeited deferred sites in the budget-bailout warning

On work-budget exhaustion the deferred invoke sites end the run with zero
CALLS — free-call fallback and reference emission already skipped them —
but the warning said 'ordinary graph emission remains untouched', which is
false for exactly those sites. The warning context now carries the
unresolved deferred-site count and the comment states the real cost
(review finding: budget-bailout honesty, #2522).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(scope-resolution): surface dropped property-dispatch keys in stats and warn payload

The over-cap warning carried only a count; the dropped key NAMES were
discarded and RunScopeResolutionStats had no field, so the PR-body claim
'includes them in resolver statistics' was unimplemented (review finding,
#2522; reviewer ask on the fan-out cap). The warn payload now names up to
20 dropped keys and the stats carry propertyDispatchSkippedKeys.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* refactor(scope-resolution): drop producer-less ownerQualifiedName from formal sites

No capture emitter anywhere produces @callable-flow.owner-qualified-name —
the solver branch consuming it was unreachable in production, yet the field
was typed, parsed, validated, and unit-tested with hand-built input (review
finding 16, #2522; YAGNI). Re-add with a real producer if C++ qualified
member declarators ever need it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* refactor(scope-resolution): drop dead callable-flow knobs

CallableFlowPassingMode 'callable-object' had no producer and no consumer
distinguishing it, and CallableFlowCaptureOptions.extractCallArguments had
no language providing it (unlike its live sibling extractCallCallee) —
review finding 17, #2522 (YAGNI). The invocation-kind 'callable-object'
is a different, live concept and stays.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(ingestion): bind subscripted callable cells to the container, not the index

terminalIdentifier iterates children in reverse, so tbl[i] = handler seeded
the INDEX variable's cell (polluting a same-named formal) and tbl[i](7)
looked up the callee under i in a different scope — no join, no CALLS edge
for the classic function-pointer-array dispatch (review finding 12, #2522).
Subscript nodes now recurse into their container field only, in both
bindingIdentifier and terminalIdentifier, across the fielded grammars
(C/C++/JS/TS/Python/Go/Java).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(ingestion): make cross-function file-scope callable bindings resolvable

Two stacked gaps killed the canonical C callback-registration pattern
(fp assigned in init(), called in run()) — the exact #2437 false-safe this
PR exists to fix (review finding H1, #2522):

1. isVisibleValueBinding only consulted assignment regions and formals, so
   a call in a function OTHER than the assigning one emitted no invoke
   fact. A declared callable-typed binding is now a value binding wherever
   its declaration is visible (visibleCallableSignature).
2. The C scope query had no @declaration.variable pattern for function-
   pointer declarators — void (*fp)(int); created no scope-tree binding,
   so the seed (init) and invoke (run) cells canonicalized to different
   keys and never joined. Both bare and initialized forms now bind.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(c): detect variadic parameters via the named variadic_parameter node

tree-sitter-c materializes '...' as a named variadic_parameter node; the
anonymous-token checks never matched, so variadic function-pointer
signatures were emitted with a wrong fixed arity and no '...' sentinel
(review finding, #2522). C++ is unaffected ('...' stays an anonymous token
there); the token checks remain for such grammars.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(ingestion): emit invoke facts for field-stored callable member calls

The C ops-vtable pattern (o->run = handler; o->run(1)) captured the store
but never the call — the member path in emitCallFacts bailed for languages
without protocol methods, and the value-binding index recorded the member
store under the OBJECT's name ('o'), not the member's ('run') (review
finding 11/M3, #2522). Member destinations now also record their terminal
member name, and a member call whose name-cell has a visible store emits an
indirect invoke — gated on the store so plain accessor calls (map.get)
stay inert.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(cpp): disambiguate (obj->*ptr)() ERROR recovery by token order

tree-sitter-cpp groups the recovered '->*' two ways depending on
error-recovery cost (identifier lengths): [identifier, ERROR '->*m'] or
[ERROR 'obj->*', identifier]. The recovery assumed the first shape, so the
second silently swapped receiver/member and dropped the call site — the
committed test passed only by name luck (review finding H2, #2522). The
identifier's position relative to '->*' inside the ERROR now decides roles.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(cpp): class members are never file-local in hasFileLocalCallableLinkage

The name-keyed file-local set is populated from every static declaration,
so an in-class 'static void make();' (external linkage — in-class static
means no-instance) and any member sharing a name with a static free
function were over-marked, refusing legitimate cross-file
declaration/definition joins (review finding 13/M2, #2522). Method and
Constructor defs now bypass the name-set, per the hook's own linkage-only
contract.

Deviation from plan step 13: the regression is a unit-level contract pin
rather than an end-to-end join test — C++ merges out-of-line member
definitions onto the member node by qualified identity, so the graph shape
cannot discriminate the join refusal for members.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(cpp): classify parameter passing mode from the declarator chain only

A whole-subtree scan for reference_declarator inverted copy vs alias:
void reg(void (*cb)(int& out)) marked the by-value pointer cb as
'reference' because of the NESTED parameter's int&, making the solver
back-propagate formal targets into every caller's argument cell — alias
semantics for a copy (review finding 14/M5, #2522). The chain walk never
descends into nested parameter lists; a reference anywhere ON the chain
(int& x, void (*&cb)(int)) still aliases.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(ruby): bare identifiers are calls, not callable references

Ruby parses a receiver-less zero-arg method call identically to a variable
read, so 'action = process' — which CALLS process and stores its return —
seeded action with the callable and minted a wrong CALLS edge from any
dispatch through it, confirmed end-to-end (review finding 15/HIGH, #2522).
New provider knob bareNamesAreCalls: a bare name that is not a provably
local value binding and not an explicit reference form (method(:x),
lambda/proc) emits no flow fact, on both the assignment and argument paths.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(go): pair multi-value := positionally instead of cross-wiring

The shared field fallback took the FIRST LHS identifier and the LAST RHS
identifier of Go's expression_list pair, cross-wiring 'a, b := f, g' and
synthesizing a garbage comma-joined qualified name — the real relationships
were silently dropped (review finding 16, #2522). extractAssignment may now
return multiple pairs; Go pairs list entries positionally and emits nothing
for a length mismatch (multi-return call RHS).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(java): drop get/test from callableProtocolMethods

'get' and 'test' collide with ubiquitous non-functional-interface APIs
(Map/List/Optional/Future.get), so every ordinary container access emitted
a spurious callable-object invoke fact — high-volume misleading graph facts
with a cross-wiring risk on receiver-name reuse (review finding 17, #2522).
Supplier.get/Predicate.test dispatch is deliberately traded away until the
check can gate on the receiver's declared type.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(rust): pin the qualified-name no-degrade guard as a hard invariant

Rust's scoped_identifier callable-reference capture over-includes unit enum
variants and associated constants (Shape::Square seeds as if callable);
they stay edge-free only because resolveSeedCandidates refuses to degrade
an unresolved qualified name to a simple-name lookup (review finding 18,
#2522). Capture-side type filtering would false-negative on tuple-variant
constructors, so the guard IS the contract: documented as a hard invariant
(Go's mis-shaped multi-value forms also rely on it) and pinned end-to-end.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(php): remove nonexistent optional_parameter node type

tree-sitter-php has no 'optional_parameter' — defaults ride on
simple_parameter — so the entry was dead weight the #1920 literal gate
does not cover for capture-option Sets (review finding 19, #2522).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(cobol): detect procedure pointers on fixed-format sources

Two stacked defects made the feature a no-op on classic sequence-numbered
fixed format (review finding 20/H3, #2522):
1. parseDataItemClauses' USAGE alternation knew POINTER but not
   PROCEDURE-POINTER/FUNCTION-POINTER, so the dataItems filter was dead.
2. The raw-line fallback scanned UNCLEANED text, where the sequence number
   satisfied the leading digits and the LEVEL NUMBER got captured as the
   pointer name. It now scans preprocessed lines and requires a letter-
   initial name (COBOL data names must contain a letter).
161 COBOL preprocessor/copy-expander tests stay green; free-format matrix
case unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(cobol): skip comment lines in SET seed/copy scans

A commented-out SET (indicator-column '*'/'/' or free-format '*>')
produced a live seed and a false CALLS edge from dead code (review
finding 21/M1, #2522). The scan now skips indicator-column comment lines
and strips inline '*>' tails before matching.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs(architecture): document callable-flow-only mode and skipped-key reporting

The Callable-value flow section omitted scopeResolutionEdgeMode:
'callable-flow-only' — a real emit-pipeline branch that suppresses all
ordinary emission for standalone providers (review finding 22, #2522) —
and predated the skipped-key names/stats surfacing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs(scope-resolution): correct value-ref resolution attribution and stale pdg-gating comments

The value-ref contract comment claimed MethodRegistry resolution — the
mechanism is the post-finalize findCallableBindingInScope walker owned by
emitPropertyDispatchCalls (resolveReferenceSites skips these sites). Three
'only under --pdg' calleeIdSink comments were falsified by the #2437 gating
change (callee-id-sink.ts's header was updated; these copies were missed).
Review finding 23, #2522.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(ingestion): direct unit coverage for synthesizeCallableFlowCaptures

The 1,100-line shared synthesizer had no test naming it — only downstream
consumers were covered (review finding 24, #2522). Pins seed/invoke/
formal/argument emission, subscript container binding, store-gated member
invokes, produced-value guards, and the bareNamesAreCalls knob over a
minimal options object so assertions target the synthesizer's own
semantics.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(resolvers): deepen shallow-language coverage; fix Kotlin/Swift reassignment gaps it exposed

Adds the COBOL SET x TO y copy-branch scenario and conditional-assignment
scenarios for Kotlin, C#, Swift, and Dart (10 languages previously had one
generic case each — review finding 25, #2522). The new scenarios exposed
two real capture gaps, fixed here:
- tree-sitter-kotlin's 'assignment' node is fieldless, so nested
  reassignments (chosen = ::target inside a block) produced no flow facts;
  Kotlin's extractAssignment now decomposes it positionally.
- tree-sitter-swift fields its assignment as target:/result:, neither in
  the shared fallback's field lists; both added.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(infra): literal-validation gate for callable-capture option Sets

The #1920 gate validates query literals and exported configs but not the
module-private *_CALLABLE_CAPTURE_OPTIONS Sets consumed by the shared
synthesizer — a typo'd node type silently captures nothing (PHP shipped a
dead 'optional_parameter'; review finding 26, #2522). Every <key>NodeTypes
Set literal is now validated against its language's grammar; name-carrying
sets (callableProtocolMethods, memberPointerOperators) are deliberately
outside the contract.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(storage): centralize corrupt-fixture casts into makeStoreEntry

The callable-flow store tests scattered 'as unknown as' double-casts per
fixture (review finding 27, #2522; standing no-as-any rule). One typed
helper now owns the single controlled escape hatch for building malformed
serialization-boundary payloads.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore(bench): refresh capture fingerprints after review fixes

python-scope: the committed baseline (8d5c3699) never matched this
branch's code — CI's benchmarks arm was red on the PR head (review
finding 2/HIGH, #2522); regenerated (a99e69ab), scaling 1.04 in budget.
scope-capture: ruby/cpp/swift/java/kotlin drifted from the review-fix
commits (bare-name suppression, passing modes + ->* recovery, assignment
fields, protocol narrowing, positional assignment); all 14 languages
re-verified PASS with ratios <= 1.18 against the 1.5 budget.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore(docs): untrack docs/plans working documents

docs/ is gitignored (local working docs); the plan files were force-added
past the ignore. Untracked from the index only — they stay on disk.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(golden): regenerate captures goldens after callable-flow review fixes

The per-language digest guards (csharp/go/php/python/ruby/rust/swift)
locked the pre-fix capture output; the review-fix series intentionally
changed it — store-gated member invokes, subscript container binding,
Ruby bare-name suppression, Swift assignment fields, positional pairing.
Regenerated with UPDATE_GOLDEN=1; clean verification run 59/59; all other
parity/golden guards (pipeline-graph, spring-route, python parity) pass
untouched at 33/33.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(ingestion): prototypes are callees, not callable value cells

The cross-function visibility fix indexed EVERY signature-bearing
declaration as a value binding — including plain function/method
prototypes (void f(int);). Every call to a declared function then became
an indirect invoke, and with emitCanonicalInvokeReference (C/C++) minted a
free-call reference that resolved through the registry, bypassing the
precise passes' two-phase/ambiguity/subobject suppression — eight phantom
CALLS edges in the cpp resolver suite on CI.

Only declarations whose binding identifier sits under a pointer/
parenthesized declarator (callable-typed variables like void (*fp)(int);)
create value cells now. cpp resolver suite 331/331; callable-value-flow +
C/C++ suites 181/181 (the cross-function fp regression still passes); cpp
fingerprint rebaselined, both bench gates PASS.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-17 17:20:02 +01:00

638 lines
20 KiB
TypeScript

/**
* Unit tests for the resolved-callee-id capture sink (#2227 follow-up plan U2).
*
* During Phase-4 scope-resolution CALLS-edge emission, each resolved call site's
* `(line, col) → calleeId` is accumulated across ALL THREE CALLS emit paths,
* each BEFORE its dedup (KTD6/R8), gated on `--pdg`. A later unit (U3) joins
* this to CFG BasicBlocks by exact call-site position.
*
* Coordinate base (KTD7 — load-bearing): the sink keys on
* `atRange.startLine` / `atRange.startCol`, which are 1-based line / 0-based col
* (`nodeToCapture` builds them as `row + 1` / `column`; the `Range` doc confirms
* "1-based startLine; 0-based startCol"). This is byte-equal to U1's
* `SiteRecord.at` (`[startPosition.row + 1, startPosition.column]`), so the U3
* position join lands.
*
* Strategy:
* - `tryEmitEdge` / `tryEmitEdgeWithExplicitTargetId` and `emitReferencesViaLookup`
* are driven directly with a real `ScopeResolutionIndexes` + `GraphNodeLookup`
* (the emit-references.test.ts fixture pattern), so the capture runs on the
* real emit path.
* - `emitFreeCallFallback` is driven with a hand-built but fully-typed real
* `ParsedFile` whose Module-scope bindings resolve a free call — exercising
* the inline `addRelationship` capture line (the regression guard for the
* "only tryEmitEdge" bug).
*/
import { describe, it, expect } from 'vitest';
import {
buildDefIndex,
buildMethodDispatchIndex,
buildModuleScopeIndex,
buildQualifiedNameIndex,
buildScopeTree,
type BindingRef,
type NodeLabel,
type ParsedFile,
type Range,
type Reference,
type ReferenceSite,
type Scope,
type ScopeId,
type SymbolDefinition,
} from 'gitnexus-shared';
import { createKnowledgeGraph } from '../../src/core/graph/graph.js';
import type { KnowledgeGraph } from '../../src/core/graph/types.js';
import type { ScopeResolutionIndexes } from '../../src/core/ingestion/model/scope-resolution-indexes.js';
import {
buildGraphNodeLookup,
type GraphNodeLookup,
} from '../../src/core/ingestion/scope-resolution/graph-bridge/node-lookup.js';
import {
tryEmitEdge,
tryEmitEdgeWithExplicitTargetId,
} from '../../src/core/ingestion/scope-resolution/graph-bridge/edges.js';
import { emitReferencesViaLookup } from '../../src/core/ingestion/scope-resolution/graph-bridge/references-to-edges.js';
import { emitFreeCallFallback } from '../../src/core/ingestion/scope-resolution/passes/free-call-fallback.js';
import { buildWorkspaceResolutionIndex } from '../../src/core/ingestion/scope-resolution/workspace-index.js';
import { createSemanticModel } from '../../src/core/ingestion/model/semantic-model.js';
import {
createCalleeIdAccumulator,
calleeIdPosKey,
type CalleeIdAccumulator,
} from '../../src/core/ingestion/scope-resolution/graph-bridge/callee-id-sink.js';
// ─── Fixture builders ─────────────────────────────────────────────────────
const FILE = 'x.ts';
const range = (sl = 1, sc = 0, el = 100, ec = 0): Range => ({
startLine: sl,
startCol: sc,
endLine: el,
endCol: ec,
});
const def = (
nodeId: string,
type: SymbolDefinition['type'] = 'Function',
qname?: string,
filePath = FILE,
): SymbolDefinition => ({
nodeId,
filePath,
type,
...(qname !== undefined ? { qualifiedName: qname } : {}),
});
const scope = (
id: ScopeId,
parent: ScopeId | null,
kind: Scope['kind'],
ownedDefs: readonly SymbolDefinition[] = [],
r: Range = range(),
filePath = FILE,
bindings: Record<string, readonly BindingRef[]> = {},
): Scope => ({
id,
parent,
kind,
range: r,
filePath,
bindings: new Map(Object.entries(bindings)),
ownedDefs,
imports: [],
typeBindings: new Map(),
});
function makeIndexes(
scopes: readonly Scope[],
allDefs: readonly SymbolDefinition[],
): ScopeResolutionIndexes {
return {
scopeTree: buildScopeTree([...scopes]),
defs: buildDefIndex([...allDefs]),
qualifiedNames: buildQualifiedNameIndex([...allDefs]),
moduleScopes: buildModuleScopeIndex(
scopes
.filter((s) => s.kind === 'Module')
.map((s) => ({ filePath: s.filePath, moduleScopeId: s.id })),
),
methodDispatch: buildMethodDispatchIndex({
owners: [],
computeMro: () => [],
implementsOf: () => [],
}),
imports: new Map(),
bindings: new Map(),
bindingAugmentations: new Map(),
workspaceFqnBindings: new Map(),
workspaceTypeBindings: new Map(),
namespaceFqnBindings: new Map(),
namespaceTypeBindings: new Map(),
accessibleNamespacesByScope: new Map(),
referenceSites: [],
sccs: [],
stats: {
totalFiles: 0,
totalEdges: 0,
linkedEdges: 0,
unresolvedEdges: 0,
sccCount: 0,
largestSccSize: 0,
},
};
}
/** A graph node for a Function so `buildGraphNodeLookup` registers it. */
function fnNode(graph: KnowledgeGraph, id: string, name: string, filePath = FILE): void {
graph.addNode({
id,
label: 'Function' as NodeLabel,
properties: { name, filePath, qualifiedName: name },
});
}
/** A call-kind reference site at a given position, for the receiver-bound /
* direct `tryEmitEdge` driver. The bridge reads `inScope`, `atRange`, `kind`. */
function callSite(inScope: ScopeId, line: number, col: number): ReferenceSite {
return {
name: 'callee',
atRange: range(line, col, line, col + 4),
inScope,
kind: 'call',
};
}
/** Collapse `accumulator.get(file)` into a plain `{ posKey: sortedIds[] }`
* object for unconditional `toEqual` / `toMatchObject` assertions. */
function snapshot(acc: CalleeIdAccumulator, filePath: string): Record<string, string[]> {
const byPos = acc.get(filePath);
const out: Record<string, string[]> = {};
for (const [key, ids] of byPos ?? new Map<string, ReadonlySet<string>>()) {
out[key] = [...ids].sort();
}
return out;
}
// ─── Path 1: tryEmitEdge ──────────────────────────────────────────────────
describe('callee-id capture — tryEmitEdge (receiver-bound path)', () => {
it('captures two receiver-bound CALLS at distinct positions (pos → {id})', () => {
const callerFn = def('def:caller', 'Function', 'caller');
const targetA = def('def:targetA', 'Function', 'targetA');
const targetB = def('def:targetB', 'Function', 'targetB');
const mod = scope('scope:m', null, 'Module', [callerFn, targetA, targetB]);
const indexes = makeIndexes([mod], [callerFn, targetA, targetB]);
const graph = createKnowledgeGraph();
fnNode(graph, 'fn:caller', 'caller');
fnNode(graph, 'fn:targetA', 'targetA');
fnNode(graph, 'fn:targetB', 'targetB');
const lookup: GraphNodeLookup = buildGraphNodeLookup(graph);
const acc = createCalleeIdAccumulator();
const seen = new Set<string>();
const okA = tryEmitEdge(
graph,
indexes,
lookup,
callSite('scope:m', 10, 4),
targetA,
'call',
seen,
0.85,
false,
{ sink: acc, filePath: FILE },
);
const okB = tryEmitEdge(
graph,
indexes,
lookup,
callSite('scope:m', 20, 8),
targetB,
'call',
seen,
0.85,
false,
{ sink: acc, filePath: FILE },
);
expect(okA).toBe(true);
expect(okB).toBe(true);
expect(snapshot(acc, FILE)).toEqual({
[calleeIdPosKey(10, 4)]: ['fn:targetA'],
[calleeIdPosKey(20, 8)]: ['fn:targetB'],
});
});
it('R2 dispatch — one site, two resolved targets → pos → {idA, idB}', () => {
const callerFn = def('def:caller', 'Function', 'caller');
const targetA = def('def:dispA', 'Function', 'dispA');
const targetB = def('def:dispB', 'Function', 'dispB');
const mod = scope('scope:m', null, 'Module', [callerFn, targetA, targetB]);
const indexes = makeIndexes([mod], [callerFn, targetA, targetB]);
const graph = createKnowledgeGraph();
fnNode(graph, 'fn:caller', 'caller');
fnNode(graph, 'fn:dispA', 'dispA');
fnNode(graph, 'fn:dispB', 'dispB');
const lookup = buildGraphNodeLookup(graph);
const acc = createCalleeIdAccumulator();
const seen = new Set<string>();
// Same site (same position) dispatched to two distinct targets — mirrors
// interface-dispatch emitting a secondary CALLS edge for one call site.
const site = callSite('scope:m', 30, 2);
tryEmitEdge(graph, indexes, lookup, site, targetA, 'call', seen, 0.85, false, {
sink: acc,
filePath: FILE,
});
tryEmitEdge(graph, indexes, lookup, site, targetB, 'interface-dispatch', seen, 0.85, false, {
sink: acc,
filePath: FILE,
});
expect(snapshot(acc, FILE)).toEqual({
[calleeIdPosKey(30, 2)]: ['fn:dispA', 'fn:dispB'],
});
});
it('dedup-independence — same target, two lines, collapse on → both positions captured', () => {
const callerFn = def('def:caller', 'Function', 'caller');
const target = def('def:target', 'Function', 'target');
const mod = scope('scope:m', null, 'Module', [callerFn, target]);
const indexes = makeIndexes([mod], [callerFn, target]);
const graph = createKnowledgeGraph();
fnNode(graph, 'fn:caller', 'caller');
fnNode(graph, 'fn:target', 'target');
const lookup = buildGraphNodeLookup(graph);
const acc = createCalleeIdAccumulator();
const seen = new Set<string>();
// collapse = true ⇒ the dedup key drops the line, so the SECOND edge is
// deduped away (returns false). The capture is BEFORE the dedup, so BOTH
// positions are recorded regardless.
const okFirst = tryEmitEdge(
graph,
indexes,
lookup,
callSite('scope:m', 11, 0),
target,
'call',
seen,
0.85,
true,
{ sink: acc, filePath: FILE },
);
const okSecond = tryEmitEdge(
graph,
indexes,
lookup,
callSite('scope:m', 12, 0),
target,
'call',
seen,
0.85,
true,
{ sink: acc, filePath: FILE },
);
expect(okFirst).toBe(true);
// Collapsed dedup drops the second EDGE...
expect(okSecond).toBe(false);
expect(graph.relationships).toHaveLength(1);
// ...but BOTH call-site positions are captured.
expect(snapshot(acc, FILE)).toEqual({
[calleeIdPosKey(11, 0)]: ['fn:target'],
[calleeIdPosKey(12, 0)]: ['fn:target'],
});
});
it('R4 gating — sink undefined (pdg off): no capture and identical edge output', () => {
const callerFn = def('def:caller', 'Function', 'caller');
const target = def('def:target', 'Function', 'target');
const mod = scope('scope:m', null, 'Module', [callerFn, target]);
const indexes = makeIndexes([mod], [callerFn, target]);
const withGraph = createKnowledgeGraph();
fnNode(withGraph, 'fn:caller', 'caller');
fnNode(withGraph, 'fn:target', 'target');
const withLookup = buildGraphNodeLookup(withGraph);
const acc = createCalleeIdAccumulator();
tryEmitEdge(
withGraph,
indexes,
withLookup,
callSite('scope:m', 7, 3),
target,
'call',
new Set<string>(),
0.85,
false,
{ sink: acc, filePath: FILE },
);
const offGraph = createKnowledgeGraph();
fnNode(offGraph, 'fn:caller', 'caller');
fnNode(offGraph, 'fn:target', 'target');
const offLookup = buildGraphNodeLookup(offGraph);
tryEmitEdge(
offGraph,
indexes,
offLookup,
callSite('scope:m', 7, 3),
target,
'call',
new Set<string>(),
0.85,
false,
undefined,
);
// pdg-off: nothing captured.
expect(offGraph.relationships).toHaveLength(1);
// The EDGE rows are byte-identical between on and off (only capture differs).
expect(offGraph.relationships).toEqual(withGraph.relationships);
// The on-run DID capture (so the comparison is meaningful, not vacuous).
expect(snapshot(acc, FILE)).toEqual({ [calleeIdPosKey(7, 3)]: ['fn:target'] });
});
});
// ─── Path 1b: tryEmitEdgeWithExplicitTargetId ─────────────────────────────
describe('callee-id capture — tryEmitEdgeWithExplicitTargetId', () => {
it('captures the explicit target id at the call-site position', () => {
const callerFn = def('def:caller', 'Function', 'caller');
const mod = scope('scope:m', null, 'Module', [callerFn]);
const indexes = makeIndexes([mod], [callerFn]);
const graph = createKnowledgeGraph();
fnNode(graph, 'fn:caller', 'caller');
const lookup = buildGraphNodeLookup(graph);
const acc = createCalleeIdAccumulator();
const ok = tryEmitEdgeWithExplicitTargetId(
graph,
indexes,
lookup,
callSite('scope:m', 42, 6),
'fn:explicitTarget',
'global',
new Set<string>(),
0.85,
false,
{ sink: acc, filePath: FILE },
);
expect(ok).toBe(true);
expect(snapshot(acc, FILE)).toEqual({
[calleeIdPosKey(42, 6)]: ['fn:explicitTarget'],
});
});
});
// ─── Path 3: emitReferencesViaLookup ──────────────────────────────────────
describe('callee-id capture — emitReferencesViaLookup', () => {
it('captures a CALLS emitted via the inline addRelationship', () => {
const callerFn = def('def:saveUser', 'Function', 'saveUser');
const targetFn = def('def:User.save', 'Method', 'User.save');
const mod = scope('scope:m', null, 'Module', [callerFn, targetFn]);
const indexes = makeIndexes([mod], [callerFn, targetFn]);
const graph = createKnowledgeGraph();
fnNode(graph, 'fn:saveUser', 'saveUser');
graph.addNode({
id: 'm:User.save',
label: 'Method' as NodeLabel,
properties: { name: 'save', filePath: FILE, qualifiedName: 'User.save' },
});
const lookup = buildGraphNodeLookup(graph);
const ref: Reference = {
fromScope: 'scope:m',
toDef: 'def:User.save',
atRange: range(10, 4, 10, 8),
kind: 'call',
confidence: 0.75,
evidence: [],
};
const referenceIndex = {
bySourceScope: new Map<ScopeId, readonly Reference[]>([['scope:m', [ref]]]),
};
const acc = createCalleeIdAccumulator();
const result = emitReferencesViaLookup(graph, indexes, referenceIndex, lookup, undefined, acc);
expect(result.emitted).toBe(1);
const targetId = graph.relationships[0]!.targetId;
expect(snapshot(acc, FILE)).toEqual({
[calleeIdPosKey(10, 4)]: [targetId],
});
});
it('R4 gating — sink undefined: identical edges, nothing captured', () => {
const callerFn = def('def:caller', 'Function', 'caller');
const targetFn = def('def:helper', 'Function', 'helper');
const mod = scope('scope:m', null, 'Module', [callerFn, targetFn]);
const indexes = makeIndexes([mod], [callerFn, targetFn]);
const ref: Reference = {
fromScope: 'scope:m',
toDef: 'def:helper',
atRange: range(5, 2, 5, 8),
kind: 'call',
confidence: 0.8,
evidence: [],
};
const referenceIndex = {
bySourceScope: new Map<ScopeId, readonly Reference[]>([['scope:m', [ref]]]),
};
const mkGraph = (): KnowledgeGraph => {
const g = createKnowledgeGraph();
fnNode(g, 'fn:caller', 'caller');
fnNode(g, 'fn:helper', 'helper');
return g;
};
const onGraph = mkGraph();
const acc = createCalleeIdAccumulator();
emitReferencesViaLookup(
onGraph,
indexes,
referenceIndex,
buildGraphNodeLookup(onGraph),
undefined,
acc,
);
const offGraph = mkGraph();
emitReferencesViaLookup(
offGraph,
indexes,
referenceIndex,
buildGraphNodeLookup(offGraph),
undefined,
undefined,
);
expect(offGraph.relationships).toEqual(onGraph.relationships);
expect(offGraph.relationships).toHaveLength(1);
expect(snapshot(acc, FILE)).toEqual({
[calleeIdPosKey(5, 2)]: [onGraph.relationships[0]!.targetId],
});
});
});
// ─── Path 2: emitFreeCallFallback (regression guard for the "only tryEmitEdge" bug) ─
describe('callee-id capture — emitFreeCallFallback (inline addRelationship)', () => {
// Build a real (hand-constructed, fully-typed) ParsedFile whose Module-scope
// bindings resolve a free call `helper()` to a local Function — so
// emitFreeCallFallback emits a CALLS via its own inline addRelationship and
// the capture line runs.
const FREE_FILE = 'free.ts';
const targetDef = def('def:helper', 'Function', 'helper', FREE_FILE);
const callerDef = def('def:main', 'Function', 'main', FREE_FILE);
const freeCallSite: ReferenceSite = {
name: 'helper',
atRange: range(3, 2, 3, 8),
inScope: 'scope:free-mod',
kind: 'call',
callForm: 'free',
arity: 0,
};
const moduleScope = scope(
'scope:free-mod',
null,
'Module',
[callerDef, targetDef],
range(1, 0, 100, 0),
FREE_FILE,
{ helper: [{ def: targetDef, origin: 'local' }] },
);
const parsed: ParsedFile = {
filePath: FREE_FILE,
moduleScope: 'scope:free-mod',
scopes: [moduleScope],
parsedImports: [],
localDefs: [callerDef, targetDef],
referenceSites: [freeCallSite],
};
const buildDriver = (): {
graph: KnowledgeGraph;
indexes: ScopeResolutionIndexes;
lookup: GraphNodeLookup;
} => {
const indexes = makeIndexes([moduleScope], [callerDef, targetDef]);
const graph = createKnowledgeGraph();
fnNode(graph, 'fn:main', 'main', FREE_FILE);
fnNode(graph, 'fn:helper', 'helper', FREE_FILE);
return { graph, indexes, lookup: buildGraphNodeLookup(graph) };
};
it('captures the resolved callee id at the free-call site', () => {
const { graph, indexes, lookup } = buildDriver();
const model = createSemanticModel();
const workspaceIndex = buildWorkspaceResolutionIndex([parsed]);
const acc = createCalleeIdAccumulator();
const emitted = emitFreeCallFallback(
graph,
indexes,
[parsed],
lookup,
{ bySourceScope: new Map() },
new Set<string>(),
model,
workspaceIndex,
{ calleeIdSink: acc },
);
expect(emitted).toBe(1);
const callsEdge = graph.relationships.find((r) => r.type === 'CALLS')!;
expect(callsEdge.targetId).toBe('fn:helper');
expect(snapshot(acc, FREE_FILE)).toEqual({
[calleeIdPosKey(3, 2)]: ['fn:helper'],
});
});
it('R4 gating — sink undefined: same CALLS edge, nothing captured', () => {
const on = buildDriver();
const off = buildDriver();
const model = createSemanticModel();
const workspaceIndex = buildWorkspaceResolutionIndex([parsed]);
const acc = createCalleeIdAccumulator();
emitFreeCallFallback(
on.graph,
on.indexes,
[parsed],
on.lookup,
{ bySourceScope: new Map() },
new Set<string>(),
model,
workspaceIndex,
{ calleeIdSink: acc },
);
emitFreeCallFallback(
off.graph,
off.indexes,
[parsed],
off.lookup,
{ bySourceScope: new Map() },
new Set<string>(),
createSemanticModel(),
buildWorkspaceResolutionIndex([parsed]),
{},
);
expect(off.graph.relationships).toEqual(on.graph.relationships);
expect(off.graph.relationships.filter((r) => r.type === 'CALLS')).toHaveLength(1);
expect(snapshot(acc, FREE_FILE)).toEqual({
[calleeIdPosKey(3, 2)]: ['fn:helper'],
});
});
});
describe('callee-id accumulator — delete (R6 per-file release)', () => {
it('delete(file) frees that file map and leaves other files intact', () => {
const acc = createCalleeIdAccumulator();
acc.add('a.ts', 2, 4, 'fn:a');
acc.add('b.ts', 5, 0, 'fn:b');
expect(snapshot(acc, 'a.ts')).toEqual({ [calleeIdPosKey(2, 4)]: ['fn:a'] });
acc.delete('a.ts');
expect(acc.get('a.ts')).toBeUndefined();
expect(snapshot(acc, 'b.ts')).toEqual({ [calleeIdPosKey(5, 0)]: ['fn:b'] });
});
it('delete of an absent file is a no-op', () => {
const acc = createCalleeIdAccumulator();
acc.add('b.ts', 5, 0, 'fn:b');
acc.delete('missing.ts');
expect(snapshot(acc, 'b.ts')).toEqual({ [calleeIdPosKey(5, 0)]: ['fn:b'] });
});
});
describe('callee-id accumulator — selective callable-flow capture', () => {
it('retains only positions accepted by the capture filter', () => {
const acc = createCalleeIdAccumulator(
(filePath, line, col) => filePath === 'wanted.ts' && line === 7 && col === 3,
);
acc.add('wanted.ts', 7, 3, 'Function:wanted');
acc.add('wanted.ts', 8, 3, 'Function:other-line');
acc.add('other.ts', 7, 3, 'Function:other-file');
expect(snapshot(acc, 'wanted.ts')).toEqual({ '7:3': ['Function:wanted'] });
expect(acc.get('other.ts')).toBeUndefined();
});
});