From 9d69b19976414a4f5c0cadee69c49e230609ca6f Mon Sep 17 00:00:00 2001 From: carlos-alm Date: Mon, 10 Aug 2026 14:28:20 -0600 Subject: [PATCH] docs(dynamic-kind): clarify Track A resolved calls never surface via roles --dynamic (#2270) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Investigated whether resolved Track A dynamic calls (computed-literal, value-ref, dispatch-table) should persist dynamic_kind on their edge row so codegraph roles --dynamic can surface them, per an assumption in issue #2042 that extractor-level dynamicKind tagging implies that visibility. Confirmed empirically (both engines agree) that the current behavior — Track A calls resolve into ordinary edges with dynamic_kind=NULL, only flag-only Track B sink edges ever persist dynamic_kind — is the correct, intended architecture per ADR-002's own text: roles --dynamic exists to surface calls that would otherwise be silently dropped, and a resolved Track A call was never at risk of that. It is also not safe to change by threading dynamicKind through resolved edges too: incremental.ts/native-orchestrator.ts already use dynamic_kind IS NULL (alongside technique IS NULL) to find edges due a technique backfill — repurposing the column for Track A would silently exclude those edges from it. Documents this explicitly (DynamicKind's doc comment in types.ts, dynamicCallsData's in roles.ts) and adds a dual-engine regression test locking in the contract, so a future well-intentioned "fix" can't silently reintroduce the technique-backfill regression this avoids. docs check acknowledged: doc-comment clarification only, no CLI, language support, or architecture-table changes. --- src/domain/analysis/roles.ts | 10 +- src/types.ts | 22 +++ ...ue-2270-track-a-dynamic-kind-scope.test.ts | 145 ++++++++++++++++++ 3 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 tests/integration/issue-2270-track-a-dynamic-kind-scope.test.ts diff --git a/src/domain/analysis/roles.ts b/src/domain/analysis/roles.ts index 602af92b3..02d220c55 100644 --- a/src/domain/analysis/roles.ts +++ b/src/domain/analysis/roles.ts @@ -11,7 +11,15 @@ export interface DynamicCallCount { count: number; } -/** Return a count of flagged dynamic call sink edges, grouped by kind. */ +/** + * Return a count of flagged dynamic call sink edges, grouped by kind. + * + * Only Track B (flag-only) edges ever have a persisted `dynamic_kind` — a + * Track A call that resolved successfully becomes an ordinary edge with + * `dynamic_kind=NULL` and is intentionally invisible here. See the + * `DynamicKind` doc comment in `types.ts` (issue #2270) for the full + * rationale. + */ export function dynamicCallsData(customDbPath: string): DynamicCallCount[] { const db = openReadonlyOrFail(customDbPath, resolveBusyTimeoutMs(customDbPath)); try { diff --git a/src/types.ts b/src/types.ts index 8ce21f640..6624f3862 100644 --- a/src/types.ts +++ b/src/types.ts @@ -572,6 +572,28 @@ export interface LOCMetrics { * Taxonomy for dynamic/computed call sites — distinguishes resolvable kinds * (computed-literal, reflection) from flag-only kinds (eval, computed-key, * unresolved-dynamic) that cannot be resolved statically. + * + * `dynamicKind` on a `Call` and `dynamic_kind` on a persisted `edges` row are + * NOT the same thing, and only the latter is what `codegraph roles --dynamic` + * queries (issue #2270). Per ADR-002 (`docs/architecture/decisions/ + * 002-dynamic-call-resolution.md`), only Track B — flag-only sink edges + * (`eval`, `computed-key` when unresolved, `unresolved-dynamic`, `reflection` + * when unresolved) — ever get a persisted `dynamic_kind`, at `confidence=0.0`. + * A Track A call that resolves successfully (`computed-literal`, `value-ref`, + * `dispatch-table`, resolved `reflection`/`computed-key`) becomes an ordinary + * `calls` edge with `dynamic_kind=NULL` — the extractor-level `dynamicKind` + * tag did its job (told the resolver how to find the target) and is then + * discarded, exactly like any other resolution-time detail that isn't part + * of the persisted edge. This is deliberate, not a bug: `codegraph roles + * --dynamic`'s whole purpose is surfacing calls that would otherwise be + * silently dropped (ADR-002's "never silently dropped" guarantee) — a + * resolved Track A call was never at risk of that, so it has nothing to + * surface there. It is also NOT safe to change by simply threading + * `dynamicKind` through resolved edges too: `incremental.ts`/ + * `native-orchestrator.ts` already use `dynamic_kind IS NULL` (alongside + * `technique IS NULL`) to identify plain, not-yet-backfilled edges for a + * `technique` migration — repurposing the column for Track A would silently + * exclude those edges from that backfill. */ export type DynamicKind = | 'computed-literal' // obj["foo"]() — resolvable; already emitted as normal edge diff --git a/tests/integration/issue-2270-track-a-dynamic-kind-scope.test.ts b/tests/integration/issue-2270-track-a-dynamic-kind-scope.test.ts new file mode 100644 index 000000000..919c8615c --- /dev/null +++ b/tests/integration/issue-2270-track-a-dynamic-kind-scope.test.ts @@ -0,0 +1,145 @@ +/** + * Regression/contract test for #2270. + * + * Found while implementing #2042: ADR-002 and issue #2042's own framing + * implied that an extractor-level `dynamicKind` tag (e.g. `computed-literal` + * on `obj["foo"]()`) makes a call site visible via `codegraph roles + * --dynamic`. Empirically, that's never been true for ANY language, + * including JavaScript's own reference implementation — the tag exists on + * the extractor's `Call`, but `emitDirectCallEdgesForCall` (and every other + * resolved-edge emission path, both engines) hardcodes the persisted edge + * row's `dynamic_kind` column to NULL. Only the flag-only sink-edge path + * (`FLAG_ONLY_DYNAMIC_KINDS`: unresolved `eval`/`computed-key`/ + * `unresolved-dynamic`/`reflection`) ever writes a non-NULL `dynamic_kind`. + * + * Resolution (not a code change): per ADR-002's own text, `codegraph roles + * --dynamic`'s whole purpose is surfacing calls that would otherwise be + * silently dropped ("never silently dropped" guarantee) — a Track A call + * that resolves successfully was never at risk of that, so nothing here was + * ever silently dropped for it to surface. Confirmed this is intentional, + * not an oversight, and NOT safe to "fix" by threading `dynamicKind` through + * resolved edges too: `incremental.ts`/`native-orchestrator.ts` already use + * `dynamic_kind IS NULL` (alongside `technique IS NULL`) to find edges due a + * `technique` backfill — repurposing the column for Track A would silently + * exclude those edges from it. See the `DynamicKind` doc comment in + * `src/types.ts` for the full rationale. + * + * This test locks in the current (correct, intentional) behavior as an + * explicit, tested contract on both engines, so it can't silently regress + * — or silently "fix itself" into the unsafe direction — without a test + * failure forcing a conscious decision. + */ + +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import Database from 'better-sqlite3'; +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; +import { buildGraph } from '../../src/domain/graph/builder.js'; +import { isNativeAvailable } from '../../src/infrastructure/native.js'; + +const FIXTURE = { + 'repro.js': ` +function handler() { return 1; } +const obj = { handler }; +function run() { + return obj["handler"](); +} +`, +}; + +function readCallEdge( + dbPath: string, + sourceName: string, + targetName: string, +): { dynamic: number; dynamic_kind: string | null; confidence: number } { + const db = new Database(dbPath, { readonly: true }); + try { + const row = db + .prepare( + `SELECT e.dynamic AS dynamic, e.dynamic_kind AS dynamic_kind, e.confidence AS confidence + FROM edges e + JOIN nodes s ON e.source_id = s.id + JOIN nodes t ON e.target_id = t.id + WHERE e.kind = 'calls' AND s.name = ? AND t.name = ?`, + ) + .get(sourceName, targetName) as + | { dynamic: number; dynamic_kind: string | null; confidence: number } + | undefined; + expect(row, `no calls edge found from ${sourceName} to ${targetName}`).toBeDefined(); + return row!; + } finally { + db.close(); + } +} + +function countDynamicKindRows(dbPath: string): number { + const db = new Database(dbPath, { readonly: true }); + try { + const row = db + .prepare(`SELECT COUNT(*) AS cnt FROM edges WHERE dynamic_kind IS NOT NULL`) + .get() as { cnt: number }; + return row.cnt; + } finally { + db.close(); + } +} + +function runShared(getDbPath: () => string) { + it('resolves the computed-literal call to a real, high-confidence edge', () => { + const edge = readCallEdge(getDbPath(), 'run', 'handler'); + expect(edge.dynamic).toBe(1); + expect(edge.confidence).toBeGreaterThan(0); + }); + + it('does not persist dynamic_kind on the resolved Track A edge (intentional, #2270)', () => { + const edge = readCallEdge(getDbPath(), 'run', 'handler'); + expect(edge.dynamic_kind).toBeNull(); + }); + + it('is not surfaced by the dynamic_kind IS NOT NULL query codegraph roles --dynamic uses', () => { + // dynamicCallsData (src/domain/analysis/roles.ts) — the query backing + // `codegraph roles --dynamic` — is exactly `WHERE dynamic_kind IS NOT + // NULL`. Nothing in this single-call fixture should ever match it. + expect(countDynamicKindRows(getDbPath())).toBe(0); + }); +} + +describe('Track A resolved dynamic calls do not persist dynamic_kind (#2270) — WASM', () => { + let tmpDir: string; + + beforeAll(async () => { + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-2270-')); + for (const [rel, content] of Object.entries(FIXTURE)) { + fs.writeFileSync(path.join(tmpDir, rel), content); + } + await buildGraph(tmpDir, { engine: 'wasm', incremental: false, skipRegistry: true }); + }); + + afterAll(() => { + fs.rmSync(tmpDir, { recursive: true, force: true }); + }); + + runShared(() => path.join(tmpDir, '.codegraph', 'graph.db')); +}); + +describe.skipIf(!isNativeAvailable())( + 'Track A resolved dynamic calls do not persist dynamic_kind (#2270) — native', + () => { + let nativeTmpDir: string; + + beforeAll(async () => { + nativeTmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-2270-native-')); + for (const [rel, content] of Object.entries(FIXTURE)) { + fs.writeFileSync(path.join(nativeTmpDir, rel), content); + } + await buildGraph(nativeTmpDir, { engine: 'native', incremental: false, skipRegistry: true }); + }, 60_000); + + afterAll(() => { + fs.rmSync(nativeTmpDir, { recursive: true, force: true }); + }); + + runShared(() => path.join(nativeTmpDir, '.codegraph', 'graph.db')); + }, +);