Skip to content

update: semantic nodes attributed to code files are never reconciled against the AST (doc-described API surfaces as implemented methods, tagged EXTRACTED) #2270

Open

Description

@chas523

Summary

Semantic (non-AST) nodes attributed to a code source_file are never reconciled against that file's AST symbol table, and are never reclaimed by graphify update. They are serialized with confidence: EXTRACTED, so consumers cannot distinguish them from real AST-derived symbols.

Concretely: a design doc describing a planned API produced graph nodes that look exactly like implemented methods of a real class, and they survived every subsequent update.

Environment

  • graphify (PyPI graphifyy) 0.9.29, installed via pipx
  • Python 3.14, macOS 15 (arm64)
  • Repo: TypeScript/NestJS backend, mixed corpus (code + docs/*.md)

What happens

I ran the full /graphify . pipeline (code + docs, semantic pass included) in May, then graphify update . (AST-only) today.

graphify explain "SendbirdService" lists 6 methods that do not exist as code, interleaved with the real ones:

node origin
.createConversationChannel() docs/sendbird-chat-integration.md, line SendbirdService.createConversationChannel(type='direct', is_distinct=true)
.archiveConversation() same design doc
.deactivateUser() same design doc
.getOrCreateStaffUserId() no occurrence anywhere in the repo
.createSendbirdGroupChannel() no occurrence anywhere in the repo
.addUserToChannelIfMissing() no occurrence anywhere in the repo

Three are doc-described API that was never implemented under those names: the semantic pass read the doc, correctly picked up the symbols, and attributed them to src/sendbird/sendbird.service.ts — the class the doc names — rather than to the doc it read them from. The other three have no occurrence in the corpus at all.

All 6 carry _origin: null and source_file: src/sendbird/sendbird.service.ts. All 19 edges incident to them are tagged confidence: EXTRACTED.

The doc-derived case is not an extraction-quality problem. It is a provenance problem: an intended API and an implemented API end up represented identically.

Reproduction

This isolates the survival mechanism. It hand-injects the node, so it does not reproduce the EXTRACTED tagging — that comes from the real graph above.

mkdir repro && cd repro
cat > svc.ts <<'EOF'
export class Svc {
  alpha(): number { return 1; }
  beta(): number { return 2; }
  gamma(): number { return this.alpha() + this.beta(); }
}
EOF

graphify update .
# → 5 nodes, all _origin: "ast", line numbers correct

Inject one semantic-origin node for a method absent from svc.ts — what a doc-derived or superseded semantic node looks like:

import json
g = json.load(open('graphify-out/graph.json'))
svc = [n['id'] for n in g['nodes'] if n.get('label') == 'Svc'][0]
g['nodes'].append({
    "label": ".ghostMethod()", "file_type": "code", "source_file": "svc.ts",
    "source_location": "L99", "_origin": None, "norm_label": "ghostmethod",
    "id": "svc_svc_ghostmethod", "community": 0, "community_name": "Svc",
})
g['links'].append({
    "source": svc, "target": "svc_svc_ghostmethod", "relation": "method",
    "source_file": "svc.ts", "source_location": "L99",
})
json.dump(g, open('graphify-out/graph.json', 'w'))

Scenario A — no source change

$ graphify update .
[graphify watch] No code-graph topology changes detected; outputs left untouched.
→ .ghostMethod() SURVIVED

graph.json is not rewritten, so no reconciliation is attempted. --force behaves identically — it lifts the shrink guard (cli.py:3528), not this early return (watch.py:1271-1284).

Scenario B — real topology change

Add a delta() method to svc.ts, so the file is genuinely re-extracted:

$ graphify update .
[graphify watch] Rebuilt: 7 nodes, 8 edges, 2 communities
→ .ghostMethod() SURVIVED
svc.ts          L1   ast
Svc             L1   ast
.alpha()        L2   ast
.beta()         L3   ast
.gamma()        L4   ast
.delta()        L5   ast
.ghostMethod()  L99  None   ← no such symbol in svc.ts

svc.ts was re-extracted and is in new_sources, yet the orphan keyed to that same source_file is retained.

Why

Three mechanisms compound:

  1. watch.py:580-598 — the preserved_nodes filter drops a node only when _origin == "ast". A non-AST node is never dropped by an AST rebuild. The comment states this is deliberate ("Semantic nodes lack the AST origin marker and remain preserved"), and for a doc's own nodes it clearly is right. The gap is a semantic node whose source_file is a code file: the AST pass has authoritative knowledge of that file's symbols and could reconcile, but provenance scoping prevents it.
  2. build.py:748-757 (#1145 ghost merge) — reclaims a non-AST node only when an AST twin shares (source_file, label). A symbol that was never implemented has no twin, so it is not classified as a ghost. This is a merge path, not a prune path.
  3. watch.py:1271-1284 — when AST topology is unchanged, graph.json is left untouched, so 1 and 2 never run at all.

Because a code file's semantic layer is only superseded by another semantic pass, and graphify update never runs one, these nodes are unreclaimable in an AST-only workflow.

Impact

  • graphify explain lists doc-only, never-implemented methods alongside real ones
  • edges to them carry confidence: EXTRACTED, documented as "AST-derived, deterministic"
  • filtering by confidence cannot remove them (this graph: 45237 EXTRACTED vs 160 INFERRED)

For an agent consuming the graph, a planned-but-unbuilt API is indistinguishable from shipped code — the failure mode is confidently calling a method that does not exist.

Suggested fixes

In rough order of value:

  1. Attribute doc-derived symbols to the doc. A node extracted from docs/x.md should keep source_file: docs/x.md and express the relationship to the class as an edge, rather than adopting the code file as its own source_file. This alone removes the whole class of problem.
  2. Reconcile semantic nodes against the AST symbol table of their own source_file. When rebuilding file F, a non-AST node claiming source_file == F with no AST counterpart is stale by construction — prune it or downgrade it. Distinct from the #1145 twin case.
  3. Never emit confidence: EXTRACTED on an edge incident to a node with _origin != "ast". INFERRED (or a new UNVERIFIED) would at least make it filterable.
  4. Let the early return still reconcile provenance, or add update --reconcile to force the merge path without full re-extraction.

Fix 3 alone would make the situation detectable rather than silent.

Side note — docstring mismatch

build_merge (build.py:1268-1274) documents unconditional per-file replacement:

Re-extracted files REPLACE their prior contribution: any source_file present in new_chunks is dropped from the loaded graph before merging, so a changed file's stale nodes/edges don't accumulate.

graphify update routes through watch._rebuild_code (cli.py:1902-1908), which applies the provenance-scoped rule instead. The two are easy to conflate when reasoning about update — worth a note in that docstring.

Workaround

Deleting graphify-out/ and rebuilding AST-only produces a clean graph. On this repo that moved method-location accuracy from 42 correct / 59 wrong / 11 orphaned to 224/224 correct across an 80-file sample, with all 13660 nodes _origin: "ast". Plain update on that clean graph is then idempotent and stays accurate — the problem is confined to graphs that already contain a semantic layer over code files.

Activity

  1. changed the title [-]update: orphaned semantic nodes for deleted code symbols are never reclaimed and are tagged confidence=EXTRACTED[/-] [+]update: semantic nodes attributed to code files are never reconciled against the AST (doc-described API surfaces as implemented methods, tagged EXTRACTED)[/+] on Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions