GitNexus/gitnexus/test/integration/impact-epistemic-lower-bound.test.ts
Abhigyan Patwari 169aa5d960
fix(scope): label library-rooted receiver drops as external from provider evidence (#3658)
* refactor(scope): expose the position a receiver-chain fold reaches

`foldReceiverChain` answers "which class", so a chain whose last step is
declared as a type the program does not contain (`Dictionary<string,
User>`, `String`) reads only as a miss. That miss is informative to the
receiver-origin classifier: every hop was typed by a program declaration
and the last one names a library type.

Extract the fold loop into `foldReceiverChainState` and export
`foldReceiverChainPosition`, which returns the final position including
one whose declared type named no class, and reports nothing to
`recordReceiverType`. `foldReceiverChain` keeps its behaviour: it now
wraps the same loop and makes the receiver-type report from the final
state, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(scope): classify library-rooted receiver drops from provider evidence

`classifyReceiverOrigin` had one source of positive external evidence,
`LanguageProvider.isBuiltInName`. That set also filters callee names out
of the call graph, so it could not grow to cover host globals, stdlib
imports or library-typed values without dropping real edges, and every
such drop hedged `impact()` to `lower-bound`. It also read a base name as
evidence even where the program bound that name itself: a parameter
named `console` with an unresolved type, or an unresolved import of a
built-in spelling from the program's own module, came back `external`.

Add two provider data hooks read only by the classifier:
`externalReceiverNames` (globals, platform roots, library types) and
`isExternalModuleSpecifier` (import specifiers reserved for the runtime
or stdlib). The classifier now answers `external` when:
- the base is unbound at the site and is a built-in or listed name;
- the base is bound only by imports that resolved to no program file and
  all name a library module;
- the receiver chain folds, through declarations each owner makes, to a
  position declared as a library type (`this.data.Keys` with
  `Dictionary<string, User> data`), and the program declares nothing by
  the dropped member's name (an extension method or trait impl could be
  the real target). The fold skips ancestor-hoisted bindings, which can
  lend another class's member type to the step.
A base the program binds (a parameter, a local, a resolved import, an
import from a non-library module) never takes a name-based verdict. The
in-program arms are unchanged and still win first. No drop is added or
removed and no edge changes.

Java supplies its reserved `java` package root for both hooks: the JVM
refuses to define classes under `java.*`, so `java.util.Optional.of(...)`
and `import java.util.Map` are rooted in the platform. Without the
specifier hook, the stricter import rule would have demoted the existing
`import java.util.List; List.of(...)` verdict to unknown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(typescript): name JavaScript host globals as the program boundary

`document.body.appendChild(...)`, `window.history.pushState(...)` and
`Buffer.from(raw).toString()` were recorded `unknown`: none of
`document`, `window`, `Buffer`, `process` or `globalThis` is in
`BUILT_INS`, and they cannot be added there because that set also
filters callee names, so `process` would drop every call to a program
function named `process`. Each drop hedged `impact()` to `lower-bound`
although no program node could have been the target.

List the host objects in `EXTERNAL_RECEIVER_NAMES` and supply it to the
TypeScript, JavaScript and Vue providers as `externalReceiverNames`. A
name counts only where the program binds nothing by it and declares
nothing by it, so a `document: Doc` parameter, an untyped `window`
parameter, a local `navigator` and a program `class Buffer` keep their
verdicts. `location`, `history`, `self` and `crypto` stay out: they are
ordinary local names.

Corpus: 5 call drops move from unknown to external. `process.env.*`
(3 reads) stays in-program there because three fixtures declare a
top-level `function process()`, one of them in a global script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(python): name standard-library imports as the program boundary

`os.environ.setdefault(...)` after `import os`, and
`Path(__file__).resolve()` after `from pathlib import Path`, were
recorded `unknown`. The imports resolve to no file in the program, but an
unresolved import alone cannot be told apart from an in-program module
the resolver missed, and nothing said that `os` and `pathlib` belong to
the interpreter.

Supply `isExternalModuleSpecifier` from a curated set of standard-library
modules, matched on the specifier's first segment (`os.path` -> `os`).
A relative specifier never matches. The classifier still requires the
import to have resolved to no program file, so a local `os.py` that the
resolver finds wins, and a parameter named `os` is the program's own.
Generic nouns an application plausibly uses for its own modules
(`types`, `queue`, `signal`, `secrets`, `string`, `email`, `http`, ...)
are left out. Third-party packages are not listed: an unresolved
`fastapi` import stays `unknown`.

Corpus: 4 call drops move from unknown to external
(`os.environ.setdefault` x3, `Path(__file__).resolve()`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(zig): name @import("std") and @import("builtin") as the program boundary

`std.sort.pdq(...)` and `const Allocator = std.mem.Allocator;` were
recorded `unknown`: `std` is bound by `const std = @import("std")`, an
import the resolver deliberately leaves external, and nothing told the
classifier that the module is the standard library rather than an
in-program file it failed to find.

Supply `isExternalModuleSpecifier` for the two module names Zig
reserves: `std` and the compiler-generated `builtin`. Exact names only,
so `@import("std.zig")` (a file path) stays `unknown`, as does `root`
(the program's own root module) and any module `build.zig` names.

Corpus: 9 drops move from unknown to external: `std.sort.pdq` (2 calls,
2 reads), `std.mem.Allocator` (1 read), and the
`zig-qualified-literal` literals `std.Thread.Mutex{}` and
`std.mem.Allocator{...}` (2 calls, 2 reads). The last four are that
fixture's edge negative control (`useExternal` must emit no edge, which
still holds); their receiver is std, so `external` is the right origin.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(csharp): name the System namespace and Dictionary as the program boundary

`System.Console.WriteLine(...)` was recorded `unknown`: `Console` is in
`BUILT_INS` but the chain base is `System`, which is not, and `this.data.Keys`
with `Dictionary<string, User> data` was recorded `in-program` because
the base `this` resolves to a program class even though the member lives
on a BCL type.

Supply `externalReceiverNames` with `System` (the BCL root namespace),
`Dictionary` (the one collection capture does not reduce to its element,
because it takes two type arguments) and `string` (a keyword alias that
no program can declare). The base check covers `System.Console`; the
chain fold reaches `Dictionary<string, User>` through the program's own
field declaration. A program `class System`, a field of a program type,
and an extension method the program declares on `Dictionary` all keep
their verdicts.

Corpus: 3 call drops move from unknown to external
(`System.Console.WriteLine`). The two `this.data.Keys/Values` reads in
`csharp-nested-member-foreach` stay in-program there: the fixture's
global-namespace `App` is one of several in the merged corpus, so `this`
binds another fixture's `App` and the fold declines. Analysed alone,
both read `external`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(kotlin): name java.lang.System and the platform roots as the program boundary

`System.out.println(...)` in Kotlin was recorded `unknown`: Kotlin's
`BUILT_INS` lists stdlib function names, not `System`, which Kotlin/JVM
imports by default from `java.lang`.

Supply `externalReceiverNames` with `System` and the package roots no
program can declare into (`java`, refused by the JVM; `kotlin`, reserved
by the compiler for the standard library), and `isExternalModuleSpecifier`
for imports under those roots. A program `object System` keeps the
receiver `in-program`.

Corpus: 1 call drop moves from unknown to external
(`kotlin-object-literal-scope` `System.out.println`, whose decoy
`println` on the enclosing object still gets no edge).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(bench): rebaseline receiver-origin buckets for library receivers

The preceding commits label library- and runtime-rooted drops `external`
instead of `unknown`. Call drops by origin move from 55 in-program, 44
external and 29 unknown to 55, 62 and 11. Total drops (192), call drops
(128), every per-extension and per-shape count, and every shape-arm
state are unchanged; a same-corpus A/B against main keeps all 2,838
CALLS/ACCESSES edges byte-identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(scope): preserve library member collision hedge

* fix(scope): cover more library receiver evidence

* fix(vue): classify imported runtime receivers

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 12:14:28 +01:00

560 lines
24 KiB
TypeScript

/**
* Integration test: epistemic lower-bound flag (#1858)
*
* When a symbol sits behind an interface / indirection boundary, callers that
* bind via a DI container or dynamic dispatch are not traced to the concrete
* symbol — so impact()/context() report a *lower bound*, not an exact figure.
* Instead of a silent confident zero, the result is annotated
* `epistemic: 'lower-bound'` with a human-readable boundary note. A fully
* resolved leaf with no indirection stays `epistemic: 'exact'`.
*
* Graph shape (the canonical Symfony/DI case from #1858 / the #1589 comment):
* SignupController --CALLS--> Logger (interface)
* EmailLogger --IMPLEMENTS--> Logger
* FileLogger --IMPLEMENTS--> Logger
* The controller binds to the *interface*; the concrete impl is wired by the
* container, so impact("EmailLogger", upstream) finds no direct caller — but
* must flag that the true blast radius is higher.
*/
import { describe, it, expect, beforeAll, beforeEach, vi } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { LocalBackend } from '../../src/mcp/local/local-backend.js';
import { runPipelineFromRepo } from '../../src/core/ingestion/pipeline.js';
import { listRegisteredRepos, loadMeta } from '../../src/storage/repo-manager.js';
import {
MAX_UNRESOLVED_RECEIVER_MEMBERS,
summarizeUnresolvedReceivers,
} from '../../src/core/ingestion/scope-resolution/unresolved-receivers.js';
import type { ResolutionOutcome } from '../../src/core/ingestion/scope-resolution/resolution-outcome.js';
import { withTestLbugDB } from '../helpers/test-indexed-db.js';
vi.mock('../../src/storage/repo-manager.js', () => ({
listRegisteredRepos: vi.fn().mockResolvedValue([]),
cleanupOldKuzuFiles: vi.fn().mockResolvedValue({ found: false, needsReindex: false }),
findSiblingClones: vi.fn().mockResolvedValue([]),
loadMeta: vi.fn().mockResolvedValue({ scopeExtractionReceipt: 1 }),
}));
const SEED = [
// Interface + two implementations + an interface-level consumer.
`CREATE (iface:Interface {id: 'Interface:src/Logger.ts:Logger', name: 'Logger', filePath: 'src/Logger.ts', startLine: 1, endLine: 5, isExported: true, content: '', description: ''})`,
`CREATE (email:Class {id: 'Class:src/EmailLogger.ts:EmailLogger', name: 'EmailLogger', filePath: 'src/EmailLogger.ts', startLine: 1, endLine: 20, isExported: true, content: '', description: ''})`,
`CREATE (file:Class {id: 'Class:src/FileLogger.ts:FileLogger', name: 'FileLogger', filePath: 'src/FileLogger.ts', startLine: 1, endLine: 20, isExported: true, content: '', description: ''})`,
`CREATE (ctrl:Class {id: 'Class:src/SignupController.ts:SignupController', name: 'SignupController', filePath: 'src/SignupController.ts', startLine: 1, endLine: 30, isExported: true, content: '', description: ''})`,
`MATCH (a:Class {id:'Class:src/EmailLogger.ts:EmailLogger'}), (b:Interface {id:'Interface:src/Logger.ts:Logger'}) CREATE (a)-[:CodeRelation {type:'IMPLEMENTS', confidence:0.85, reason:'implements', step:0}]->(b)`,
`MATCH (a:Class {id:'Class:src/FileLogger.ts:FileLogger'}), (b:Interface {id:'Interface:src/Logger.ts:Logger'}) CREATE (a)-[:CodeRelation {type:'IMPLEMENTS', confidence:0.85, reason:'implements', step:0}]->(b)`,
`MATCH (a:Class {id:'Class:src/SignupController.ts:SignupController'}), (b:Interface {id:'Interface:src/Logger.ts:Logger'}) CREATE (a)-[:CodeRelation {type:'CALLS', confidence:0.85, reason:'interface-call', step:0}]->(b)`,
// A fully-resolved leaf with no indirection — must stay `exact`.
`CREATE (leaf:Function {id: 'Function:src/util.ts:formatDate', name: 'formatDate', filePath: 'src/util.ts', startLine: 1, endLine: 3, isExported: true, content: '', description: ''})`,
`CREATE (caller:Function {id: 'Function:src/page.ts:renderHeader', name: 'renderHeader', filePath: 'src/page.ts', startLine: 1, endLine: 10, isExported: true, content: '', description: ''})`,
`MATCH (a:Function {id:'Function:src/page.ts:renderHeader'}), (b:Function {id:'Function:src/util.ts:formatDate'}) CREATE (a)-[:CodeRelation {type:'CALLS', confidence:0.9, reason:'direct', step:0}]->(b)`,
...['noKnownCallers', 'constructor', 'toString', '__proto__'].map(
(name) =>
`CREATE (:Function {id: 'Function:src/util.ts:${name}', name: '${name}', filePath: 'src/util.ts', startLine: 5, endLine: 7, isExported: true, content: '', description: ''})`,
),
...[
['listOrders', 'query'],
['createOrder', 'mutation'],
['syncOrders', 'action'],
['readInternal', 'internalQuery'],
['writeInternal', 'internalMutation'],
['runInternal', 'internalAction'],
].map(
([name, factory]) =>
`CREATE (:Const {id: 'Const:src/convex.ts:${name}', name: '${name}', filePath: 'src/convex.ts', startLine: 1, endLine: 3, content: '', description: '', convexEndpointFactory: '${factory}'})`,
),
`CREATE (:Const {id: 'Const:src/negative.ts:localQuery', name: 'localQuery', filePath: 'src/negative.ts', startLine: 1, endLine: 1, content: '', description: '', convexEndpointFactory: ''})`,
`CREATE (:Const {id: 'Const:src/negative.ts:nestedQuery', name: 'nestedQuery', filePath: 'src/negative.ts', startLine: 2, endLine: 2, content: '', description: '', convexEndpointFactory: ''})`,
`CREATE (:Const {id: 'Const:src/negative.ts:memberQuery', name: 'memberQuery', filePath: 'src/negative.ts', startLine: 3, endLine: 3, content: '', description: '', convexEndpointFactory: ''})`,
];
withTestLbugDB(
'impact-epistemic-lower-bound',
(handle) => {
let backend: LocalBackend;
beforeAll(() => {
backend = (handle as any)._backend;
});
beforeEach(() => {
// One query can read metadata for both freshness and epistemic boundaries.
// Keep each fixture snapshot stable for every read in that test.
vi.mocked(loadMeta).mockResolvedValue({
scopeExtractionReceipt: 1,
} as Awaited<ReturnType<typeof loadMeta>>);
});
it('flags impact() on a concrete impl behind an interface as lower-bound', async () => {
const result = await backend.callTool('impact', {
target: 'EmailLogger',
direction: 'upstream',
});
expect(result).not.toHaveProperty('error');
expect(result.epistemic).toBe('lower-bound');
expect(Array.isArray(result.boundaries)).toBe(true);
expect(result.boundaries.join(' ')).toContain('Logger');
});
it('publishes causes.dispatchBoundary as untraced SYMBOLS, not the note count', async () => {
const result = await backend.callTool('impact', {
target: 'EmailLogger',
direction: 'upstream',
});
// One boundary node (Logger) produces exactly one note, so publishing
// `boundaries.length` here would print `1` — the number of SENTENCES —
// next to a `receiverTyping` that counts call sites, and a consumer
// branching on the two would read the smaller cause as the dominant one.
// The magnitude behind this boundary is 2 implementations (EmailLogger,
// FileLogger) + 1 interface-level consumer (SignupController) = 3.
expect(result.boundaries).toHaveLength(1);
expect(result.causes).toMatchObject({
receiverTyping: 0,
dispatchBoundary: 3,
externalBoundary: 0,
});
});
it('flags impact() on the interface itself as lower-bound', async () => {
const result = await backend.callTool('impact', {
target: 'Logger',
direction: 'upstream',
});
expect(result.epistemic).toBe('lower-bound');
});
it('keeps a fully-resolved leaf exact (no false boundary)', async () => {
const result = await backend.callTool('impact', {
target: 'formatDate',
direction: 'upstream',
});
expect(result.epistemic).toBe('exact');
expect(result.boundaries).toBeUndefined();
// The real caller is still reported — the flag is additive, not lossy.
expect(result.impactedCount).toBeGreaterThanOrEqual(1);
});
describe.each(['impact', 'context'] as const)('%s() receiver summary', (tool) => {
const query = (name: string) =>
backend.callTool(
tool,
tool === 'impact'
? { target: name, file_path: 'src/util.ts', direction: 'upstream' }
: { name, file_path: 'src/util.ts' },
);
it.each([
['noKnownCallers', 0],
['formatDate', 1],
] as const)('hedges an omitted %s with %i known callers', async (name, callers) => {
vi.mocked(loadMeta).mockResolvedValue({
repoPath: '',
lastCommit: '',
indexedAt: '',
scopeExtractionReceipt: 1,
unresolvedReceiverMembers: {
counts: { anotherMember: 3 },
totalSites: 10,
omittedNames: 2,
},
} as Awaited<ReturnType<typeof loadMeta>>);
const result = await query(name);
expect(result).not.toHaveProperty('error');
expect(result.epistemic).toBe('lower-bound');
expect(result.boundaries.join(' ')).toContain('truncated');
expect(result.boundaries.join(' ')).toContain(`\`${name}\``);
expect(result.boundaries.join(' ')).toContain('dropped-call count');
expect(result.boundaries.join(' ')).toContain('cannot be determined');
expect(result.causes).toMatchObject({ receiverTyping: 0, externalBoundary: 0 });
if (tool === 'impact') {
expect(result.impactedCount).toBe(callers);
} else {
expect(result.status).toBe('found');
expect(result.incoming.calls?.length ?? 0).toBe(callers);
}
});
it('keeps actual retained call-site counts when other names were omitted', async () => {
vi.mocked(loadMeta).mockResolvedValue({
repoPath: '',
lastCommit: '',
indexedAt: '',
scopeExtractionReceipt: 1,
unresolvedReceiverMembers: {
counts: { formatDate: 3 },
totalSites: 10,
omittedNames: 2,
externalCounts: { formatDate: 4 },
externalSites: 4,
},
} as Awaited<ReturnType<typeof loadMeta>>);
const result = await query('formatDate');
expect(result.epistemic).toBe('lower-bound');
expect(result.causes).toMatchObject({ receiverTyping: 3, externalBoundary: 4 });
expect(result.boundaries).toHaveLength(1);
expect(result.boundaries[0]).toContain('3 call sites invoking `formatDate` were dropped');
expect(result.boundaries[0]).not.toContain('truncated');
});
it.each([
['uncapped', { counts: { anotherMember: 1 }, totalSites: 1 }],
[
'external-only capped',
{
counts: {},
totalSites: 0,
externalCounts: { formatDate: 4 },
externalSites: 10,
externalOmittedNames: 2,
},
],
])('leaves an absent name exact with an %s summary', async (_label, summary) => {
vi.mocked(loadMeta).mockResolvedValue({
repoPath: '',
lastCommit: '',
indexedAt: '',
scopeExtractionReceipt: 1,
unresolvedReceiverMembers: summary,
} as Awaited<ReturnType<typeof loadMeta>>);
const result = await query('formatDate');
expect(result.epistemic).toBe('exact');
expect(result.boundaries).toBeUndefined();
expect(result.causes?.receiverTyping ?? 0).toBe(0);
expect(result.causes?.externalBoundary ?? 0).toBe(
'externalCounts' in summary ? summary.externalCounts.formatDate : 0,
);
});
it.each(['in-program', 'external'] as const)(
'consumes a persisted summary actually capped after 501 %s names',
async (receiverOrigin) => {
const outcomes: ResolutionOutcome[] = [
...Array.from({ length: MAX_UNRESOLVED_RECEIVER_MEMBERS }, (_, i) => `aaa_member${i}`),
'noKnownCallers',
].map((name, i) => ({
kind: 'suppressed',
reason: 'receiver-unresolved',
candidateIds: [],
phase: 'receiver-bound-calls',
filePath: 'src/caller.py',
name,
range: { startLine: i + 1, endLine: i + 1, startCol: 0, endCol: 30 },
siteKind: 'call',
receiverOrigin,
}));
const summary = JSON.parse(JSON.stringify(summarizeUnresolvedReceivers(outcomes)));
const external = receiverOrigin === 'external';
expect(Object.keys(external ? summary.externalCounts : summary.counts)).toHaveLength(
MAX_UNRESOLVED_RECEIVER_MEMBERS,
);
expect(external ? summary.externalSites : summary.totalSites).toBe(501);
expect(external ? summary.externalOmittedNames : summary.omittedNames).toBe(1);
expect(external ? summary.externalCounts : summary.counts).not.toHaveProperty(
'noKnownCallers',
);
vi.mocked(loadMeta).mockResolvedValue({
scopeExtractionReceipt: 1,
unresolvedReceiverMembers: summary,
} as Awaited<ReturnType<typeof loadMeta>>);
const result = await query('noKnownCallers');
expect(result.epistemic).toBe(external ? 'exact' : 'lower-bound');
expect(result.causes?.receiverTyping ?? 0).toBe(0);
expect(result.causes?.externalBoundary ?? 0).toBe(0);
if (external) expect(result.boundaries).toBeUndefined();
else expect(result.boundaries.join(' ')).toContain('truncated');
},
);
it.each([undefined, null, 0, -1, NaN, Infinity, 1.5, '2', '<invalid>', {}, true])(
'does not infer truncation from malformed or nonpositive omittedNames=%s',
async (omittedNames) => {
vi.mocked(loadMeta).mockResolvedValue({
scopeExtractionReceipt: 1,
unresolvedReceiverMembers: { counts: {}, totalSites: 0, omittedNames },
} as Awaited<ReturnType<typeof loadMeta>>);
const result = await query('formatDate');
expect(result.epistemic).toBe('exact');
expect(result.boundaries).toBeUndefined();
expect(result.causes).toBeUndefined();
},
);
it.each(['constructor', 'toString', '__proto__'])(
'never invents a count for omitted prototype-like member %s',
async (name) => {
vi.mocked(loadMeta).mockResolvedValue({
scopeExtractionReceipt: 1,
unresolvedReceiverMembers: { counts: {}, totalSites: 3, omittedNames: 2 },
} as Awaited<ReturnType<typeof loadMeta>>);
const result = await query(name);
expect(result.epistemic).toBe('lower-bound');
expect(result.causes).toMatchObject({ receiverTyping: 0, externalBoundary: 0 });
expect(result.boundaries.join(' ')).toContain('truncated');
expect(result.boundaries.join(' ')).not.toMatch(/native code|NaN|undefined|\[object/);
},
);
it.each(['constructor', 'toString', '__proto__'])(
'preserves a genuinely recorded prototype-like member %s',
async (name) => {
vi.mocked(loadMeta).mockResolvedValue({
scopeExtractionReceipt: 1,
unresolvedReceiverMembers: JSON.parse(
JSON.stringify({ counts: { [name]: 3 }, totalSites: 5, omittedNames: 2 }),
),
} as Awaited<ReturnType<typeof loadMeta>>);
const result = await query(name);
expect(result.epistemic).toBe('lower-bound');
expect(result.causes.receiverTyping).toBe(3);
expect(result.boundaries.join(' ')).toContain(`3 call sites invoking \`${name}\``);
expect(result.boundaries.join(' ')).not.toContain('truncated');
},
);
});
// The summaries above are hand-built. This one comes from a real analysis,
// so the verdict is the classifier's, end to end: a drop rooted at a host
// global must leave `impact()` exact, while the same member dropped on an
// untyped value — or on a parameter that merely shares the global's name —
// must still make it a lower bound (#3498 triage).
it.each([
[
'a drop rooted at a library global',
'exact',
'export function render(): void {\n document.body.formatDate();\n}\n',
{ receiverTyping: 0, externalBoundary: 1 },
],
[
'a drop on an untyped parameter',
'lower-bound',
'export function render(svc): void {\n svc.load().formatDate();\n}\n',
{ receiverTyping: 1, externalBoundary: 0 },
],
[
'a drop on a parameter named like the library global',
'lower-bound',
'export function render(document: Doc): void {\n document.body.formatDate();\n}\n',
{ receiverTyping: 1, externalBoundary: 0 },
],
] as const)(
'impact() reads %s from an analysed summary as %s',
async (_label, epistemic, source, causes) => {
const repoDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gn-epistemic-origin-'));
try {
fs.mkdirSync(path.join(repoDir, 'src'), { recursive: true });
fs.writeFileSync(path.join(repoDir, 'src', 'caller.ts'), source, 'utf8');
const analysed = await runPipelineFromRepo(repoDir, () => {}, {});
const summary = JSON.parse(
JSON.stringify(summarizeUnresolvedReceivers(analysed.resolutionOutcomes)),
);
vi.mocked(loadMeta).mockResolvedValue({
scopeExtractionReceipt: 1,
unresolvedReceiverMembers: summary,
} as Awaited<ReturnType<typeof loadMeta>>);
const result = await backend.callTool('impact', {
target: 'formatDate',
file_path: 'src/util.ts',
direction: 'upstream',
});
expect(result.epistemic).toBe(epistemic);
expect(result.causes).toMatchObject(causes);
} finally {
fs.rmSync(repoDir, { recursive: true, force: true });
}
},
60000,
);
it('marks impact as a lower bound when scope extraction omitted files', async () => {
vi.mocked(loadMeta).mockResolvedValue({
repoPath: '/test/repo',
lastCommit: 'abc123',
indexedAt: new Date().toISOString(),
scopeExtractionReceipt: 1,
scopeExtractionFailures: {
total: 2,
paths: ['src/broken-a.ts', 'src/broken-b.ts'],
},
});
const result = await backend.callTool('impact', {
target: 'formatDate',
direction: 'upstream',
});
expect(result.epistemic).toBe('lower-bound');
expect(result.boundaries.join(' ')).toContain('Scope extraction failed for 2 files');
expect(result.causes).toMatchObject({ scopeExtractionFiles: 2 });
});
it('never renders repository-controlled failure paths in boundary prose', async () => {
vi.mocked(loadMeta).mockResolvedValue({
repoPath: '/test/repo',
lastCommit: 'abc123',
indexedAt: new Date().toISOString(),
scopeExtractionReceipt: 1,
scopeExtractionFailures: {
total: 1,
paths: ['src/`break`\n\u001b[31m\u202e\u200binject.ts'],
},
});
const result = await backend.callTool('impact', {
target: 'formatDate',
direction: 'upstream',
});
const prose = result.boundaries.join(' ');
expect(prose).toContain('Scope extraction failed for 1 file');
expect(prose).not.toMatch(/[\n\u001b\u202e\u200b`]/u);
expect(result.causes).toMatchObject({ scopeExtractionFiles: 1 });
});
it.each([
['missing receipt', {}],
['missing metadata', null],
['malformed summary', { scopeExtractionReceipt: 1, scopeExtractionFailures: 'invalid' }],
])('treats %s as an unknown lower bound', async (_label, metadata) => {
vi.mocked(loadMeta).mockResolvedValue(metadata as Awaited<ReturnType<typeof loadMeta>>);
const result = await backend.callTool('impact', {
target: 'formatDate',
direction: 'upstream',
});
expect(result.epistemic).toBe('lower-bound');
expect(result.boundaries.join(' ')).toContain(
'Scope-extraction completeness was not recorded',
);
expect(result.causes).toMatchObject({ scopeExtractionFiles: 0 });
});
it('treats a metadata read failure as an unknown lower bound', async () => {
vi.mocked(loadMeta).mockRejectedValue(new Error('metadata unavailable'));
const result = await backend.callTool('impact', {
target: 'formatDate',
direction: 'upstream',
});
expect(result.epistemic).toBe('lower-bound');
expect(result.boundaries.join(' ')).toContain(
'Scope-extraction completeness was not recorded',
);
});
it.each([
['listOrders', 'query'],
['createOrder', 'mutation'],
['syncOrders', 'action'],
['readInternal', 'internalQuery'],
['writeInternal', 'internalMutation'],
['runInternal', 'internalAction'],
])('marks Convex %s/%s runtime dispatch as a lower bound', async (target, factory) => {
const result = await backend.callTool('impact', {
target,
file_path: 'src/convex.ts',
direction: 'upstream',
});
expect(result.epistemic).toBe('lower-bound');
expect(result.boundaries.join(' ')).toContain(`Convex ${factory}`);
expect(result.boundaries.join(' ')).toContain('anyApi');
expect(result.causes.dispatchBoundary).toBe(0);
});
it.each(['localQuery', 'nestedQuery', 'memberQuery'])(
'keeps non-wrapper control %s exact',
async (target) => {
const result = await backend.callTool('impact', {
target,
file_path: 'src/negative.ts',
direction: 'upstream',
});
expect(result.epistemic).toBe('exact');
expect(result.boundaries).toBeUndefined();
},
);
it('context() carries the same epistemic signal', async () => {
const result = await backend.callTool('context', {
name: 'EmailLogger',
file_path: 'src/EmailLogger.ts',
});
expect(result.status).toBe('found');
expect(result.epistemic).toBe('lower-bound');
});
it('context() reports persisted scope extraction omissions as a lower bound', async () => {
vi.mocked(loadMeta).mockResolvedValue({
repoPath: '/test/repo',
lastCommit: 'abc123',
indexedAt: new Date().toISOString(),
scopeExtractionReceipt: 1,
scopeExtractionFailures: {
total: 2,
paths: ['src/broken-a.ts', 'src/broken-b.ts'],
},
});
const result = await backend.callTool('context', {
name: 'formatDate',
file_path: 'src/util.ts',
});
expect(result.status).toBe('found');
expect(result.epistemic).toBe('lower-bound');
expect(result.boundaries.join(' ')).toContain('Scope extraction failed for 2 files');
expect(result.causes).toMatchObject({ scopeExtractionFiles: 2 });
});
it('context() on a leaf interface itself is lower-bound (#1858 review F3)', async () => {
// Logger is a leaf interface — it implements/extends nothing, so the only
// boundary signal is computeEpistemicBoundary's symType==='Interface'
// self-branch. Before the F3 fix, context() collapsed symKind to 'Class'
// and this returned 'exact'.
const result = await backend.callTool('context', { name: 'Logger' });
expect(result.status).toBe('found');
expect(result.epistemic).toBe('lower-bound');
});
},
{
seed: SEED,
poolAdapter: true,
afterSetup: async (handle) => {
vi.mocked(listRegisteredRepos).mockResolvedValue([
{
name: 'test-repo',
path: '/test/repo',
storagePath: handle.tmpHandle.dbPath,
indexedAt: new Date().toISOString(),
lastCommit: 'abc123',
stats: { files: 6, nodes: 6, communities: 0, processes: 0 },
},
]);
const backend = new LocalBackend();
await backend.init();
(handle as any)._backend = backend;
},
},
);