graphify icon indicating copy to clipboard operation
graphify copied to clipboard

json_config: `$schema`/`$ref` in _CONFIG_JSON_KEYS makes every JSON-Schema file explode into keyword nodes (#1224 regression path)

Open harshslr opened this issue 4 months ago • 5 comments

_is_config_json() treats $schema / $ref as config signals, but they are the defining markers of a JSON Schema — so schema/contract files get the full #1224 keyword-tree explosion.

Version: graphify 0.9.27 (Python 3.12, Windows). Code path is byte-identical on main today.

Symptom

A single JSON-Schema contract file (contracts/events-v1.json, 160 KB) contributed 362 nodes whose labels are bare schema keywords — description ×113, type ×83, $ref ×53, required ×20, properties ×20, additionalProperties ×20, enum ×18, oneOf, $comment, format, pattern, items, title, $id, minimum — plus 726 incident edges, every one of them _origin: "ast".

Because the same keyword recurs once per definition, Louvain then produces ~12 communities whose entire membership is schema structure (description ~ $ref ~ type), which pollutes clustering, the god-node analysis and GRAPH_REPORT.md. On a 2,725-node graph that was 13% of all nodes.

This is the failure mode #1224 fixed for data JSON, leaking back through the top-level key probe.

Root cause

graphify/extractors/json_config.py:

_CONFIG_JSON_KEYS = frozenset({
    ..., "extends", "$ref", "$schema", "compilerOptions",
})

_is_config_json() returns True if any top-level key is in that set, so any JSON Schema — which by definition starts with "$schema": "https://json-schema.org/..." — is classified as a config manifest and AST-walked in full.

The signal is inverted: $schema as a key appears both in configs that point at a schema (biome.json, renovate.json) and in files that are a schema. A root-level $ref is a schema construct only.

Repro

repro.json (12 lines):

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/events-v1.json",
  "title": "Event catalogue",
  "$defs": {
    "dataClassification": {
      "description": "Handling class for the event payload.",
      "type": "string",
      "enum": ["Public", "Internal", "CUI"]
    }
  }
}
from graphify.extractors.json_config import extract_json
from pathlib import Path
r = extract_json(Path('repro.json'))
print(r.get('skipped'))
print([n['label'] for n in r['nodes']])

Actual:

skipped: None
nodes: ['repro.json', '$schema', '$id', 'title', '$defs', 'dataClassification',
        'description', 'type', 'enum', 'Public', 'Internal', 'CUI']
edges: repro_json contains repro_schema | repro_json contains repro_id | ...
       repro_defs_dataclassification contains repro_dataclassification_description
       repro_dataclassification_enum extends ref_public | ... ref_internal | ... ref_cui

Expected: either skipped: "json schema (not a config/manifest)", or — better — dataClassification --contains--> Public/Internal/CUI with no keyword nodes at all.

Suggested fix

Distinguish "config that references a schema" from "file that is a schema", before the key probe:

  • Drop $ref from _CONFIG_JSON_KEYS — a root-level $ref is never a config signal.
  • Keep $schema as a config signal only when the root does not also look like a schema: root has $defs / definitions, or the $schema value points at json-schema.org. Configs such as biome.json and renovate.json carry a vendor schema URL and no $defs, so they keep matching.

Worth considering beyond the classification gate: JSON Schema is genuinely interesting to graph, just not keyword-first. Walking it semantically — definition contains its fields, enum name contains its values, skipping the keyword level entirely — turns the same file into real domain structure (envelope contains event_id, dataClassification contains CUI).

Workaround for anyone hitting this

Contract the keyword nodes rather than deleting them, or the fields underneath go with them. On the graph above, a plain delete orphaned 273 substantive nodes (event_id, tenant_id, payload_material_issued, CUI, …) and split it from 99 to 374 components. Rebuilding a DiGraph from graph.json links (which preserve parent→child order even though the graph is undirected), reconnecting parent→child across each keyword node and then removing it, gave 2,725 → 2,363 nodes, 4,019 → 3,861 edges, 276 → 247 communities, zero newly-orphaned nodes.

.graphifyignore is not a workaround here: it would also drop the file's semantic extraction, which is the valuable part.

harshslr avatar Jul 28 '26 08:07 harshslr

Excellent reproduction — and the inversion insight ($schema as signal matches both "config pointing at a schema" and "file that IS a schema") is exactly the bug.

We hit this in a codebase with 23 JSON Schema contracts (API request/response definitions), and the keyword explosion produced the same pollution pattern: ~18% of all nodes were bare schema keywords (description, type, properties, $ref), and Louvain gave us 8 communities whose only membership was schema structure. That noise ratio made the god-node analysis and clustering unusable until we contracted the keyword layer out.

Two observations from our fix:

1. The semantic vs syntactic distinction is the line

A JSON Schema file is interesting to graph — the domain definitions (envelope, dataClassification, CUI) are exactly the kind of cross-file references a knowledge graph should capture — but the keyword layer (description, type, enum as nodes) is AST pollution. The fix is to walk JSON Schema semantically: definition contains its fields, enum contains its values, skip the keyword scaffolding entirely.

We implemented this as a separate extraction path (detect $schema + $defs/definitions at root → route to extract_json_schema() instead of extract_json()), which walks the schema tree and emits only the substantive nodes. That kept the existing _is_config_json() logic untouched (Biome/Renovate/tsconfig still route to the AST extractor) and avoided the classification ambiguity.

2. The $ref and $schema split matters

  • $ref at root is never a config signal — it's a schema-internal reference. Safe to drop from _CONFIG_JSON_KEYS unconditionally.
  • $schema at root is ambiguous: it appears in both configs (Renovate, Biome) and schema files. The disambiguator is the value: if the URL contains json-schema.org, it's a schema definition; otherwise it's a config pointing at a vendor schema.

Your suggested heuristic ($schema + $defs/definitions = schema file) is correct and easier to implement than value-inspection. The only edge case is old-style schemas that use definitions instead of $defs (pre-draft-2019), which your check already covers.

On the contraction workaround:

Your observation that plain deletion orphans substantive nodes (273 orphaned, 99→374 component split) is the key failure mode of post-hoc cleanup. We tried the same contraction approach (rebuild DiGraph from graph.json, reconnect parent→child across each keyword node, remove keyword) and hit the same outcome: no newly orphaned nodes, cleaner clustering. But it's fragile — it assumes the keyword nodes form a strict parent→child chain, which breaks when a schema uses $ref to reuse definitions (now multiple parents point at the same keyword node).

The robust fix is to not emit the keyword nodes in the first place: route JSON Schema files to a semantic extractor that walks $defs/definitions directly and emits only the definition names, field names, and enum values. That eliminates the contraction step entirely and handles $ref reuse correctly.

If the schema extractor returns a subgraph with the right contains edges (dataClassification --contains--> Public/Internal/CUI), you can merge it into the main graph as-is, and the cross-file references (e.g., a TypeScript file imports CUI) just work.


This came out of knowledge-graph construction for a polyglot codebase in SwarmAI. Discussion: T-CUL (Cultivation > Configuration)

xg-gh-25 avatar Jul 28 '26 08:07 xg-gh-25

Correction to the version line above: I verified the code against v8 (the default branch), not main. The contents API without a ref returns the default branch, which is where my json_config.py comparison came from — the finding stands, it is current on v8.

main is a stale skeleton (~119 files, no graphify/extractors/), so the path named in this report does not exist on that branch at all.

Also opened #2256 for the other half of this: the extraction subagent prompt has no rule against keyword nodes either, so they can arrive via the semantic pass on a schema file the AST extractor correctly skipped. That one is prompt-only; this issue remains the code fix.

harshslr avatar Jul 28 '26 09:07 harshslr

Confirming this is still live in 0.9.53 — the _CONFIG_JSON_KEYS frozenset and the _is_config_json() top-level key probe are unchanged from what is quoted above.

Same shape here on a Python repo: 29 JSON Schema contract files produced 3,285 nodes, 23% of a 14,287-node graph, labels being the usual $defs / type / properties / description / required keyword layer.

One measurement I have not seen raised, which may bear on the fix design being discussed: that subgraph was completely disconnected from the rest of the graph. Between those 3,285 nodes and the other ~11,000 there were 0 edges, and 0 of the 18 hyperedges touched them. Median degree 1; 2,025 of the 3,285 sat at degree ≤ 1. The only edges reaching the rest of the graph from that directory came from sibling .yaml files, never from a *.schema.json node.

That is a small caveat on the expectation that routing schema files to a semantic extractor would let cross-file references "just work". On this corpus there were none to recover — nothing outside the schema layer could reach it by traversal at all, so the win from the fix is removing noise rather than surfacing latent links. Worth knowing before that path is scoped, since it changes what success looks like.

Cutting the other way, in favour of a semantic path: the content that would make these files worth graphing is not indexed either. Across those 29 files there are 364 description string values totalling ~110 KB, plus 29 titles — none of which become nodes, because the walker makes nodes from keys and discards string values. So today the extractor emits the scaffolding and drops the prose.

Worked around locally by excluding *.schema.json from extraction, which cost no connectivity for the reason above.

Emperiusm avatar Sep 02 '26 07:09 Emperiusm

This looks fixed on the v8 branch by 85664927 (fix(extract): stop walking JSON Schema files as config manifests (#2255)). Could a maintainer confirm and close?

omsenjalia avatar Oct 18 '26 08:10 omsenjalia

Follow-up on the fix for this issue: JSON Schema files with definitions/$defs produce zero nodes since 0.9.76, and nothing in the result says so

Summary

Since 0.9.76, graphify.extract.extract([path]) on a JSON Schema document that has $schema plus definitions, $defs or $id at the root returns 0 nodes and 0 edges. Thanks for 0.9.94, whose new skipped_sources key now names these files. Two things remain: the file is still listed in extracted_sources too, and everything the schema defined is lost from the graph.

The skip is deliberate (#2255, #4048: "stop walking JSON Schema files as config manifests"), and the keyword-node explosion it fixes is real. This issue is about what the change also removed:

  1. Reported, but also listed as extracted. In 0.9.94 the file appears in both skipped_sources and extracted_sources, and skipped_sources carries no reason. A caller that only reads extracted_sources still sees a successful extraction.

  2. The useful structure goes too. Definition names, property names and $ref targets go along with the keyword scaffolding. Before 0.9.76, a property that $refs #/definitions/Gadget gave the caller a node it could follow. Now the caller gets nothing.

  3. Draft-04 schemas are split in two. Draft-04 names a schema with id, not $id, and id is not in _SCHEMA_DEFINITION_KEYS. So a draft-04 schema with definitions is skipped, while a plain draft-04 object schema ($schema + id + properties) is still keyword-walked into $schema/id/type/properties nodes. On 0.9.94 an invented example gives 10 nodes, all of them scaffolding, and an empty skipped_sources. Corpora that are mostly draft-04 get both behaviours at once.

Minimal reproducer

widget.schema.json (all names invented):

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://example.com/widget.schema.json",
  "title": "Widget",
  "type": "object",
  "definitions": {
    "Gadget":   {"type": "object", "properties": {"gadgetName": {"type": "string"}, "sprocketCount": {"type": "integer"}}, "required": ["gadgetName"]},
    "Sprocket": {"type": "object", "properties": {"toothSize": {"type": "number"}, "parentGadget": {"$ref": "#/definitions/Gadget"}}},
    "Flange":   {"type": "object", "properties": {"flangeCode": {"type": "string", "enum": ["FL_A", "FL_B"]}}}
  },
  "$defs": {
    "Doohickey": {"type": "object", "properties": {"doohickeyLabel": {"type": "string"}}}
  },
  "properties": {
    "primaryGadget":  {"$ref": "#/definitions/Gadget"},
    "spareSprocket":  {"$ref": "#/definitions/Sprocket"},
    "edgeFlange":     {"$ref": "#/definitions/Flange"},
    "extraDoohickey": {"$ref": "#/$defs/Doohickey"}
  },
  "required": ["primaryGadget", "spareSprocket"]
}
from pathlib import Path
import graphify.extract as ex
from graphify.extractors import json_config as jc

p = Path("widget.schema.json")
r = ex.extract([p])
print(len(r["nodes"]), len(r["edges"]), r["failed_sources"], r.get("extracted_sources"))
print(jc.extract_json(p).get("skipped"))

Versions (fresh venv per version, python -I)

graphifyy nodes edges failed_sources note
0.9.50 48 52 []
0.9.54 48 52 []
0.9.55 43 47 [] #3330: non-extends arrays (required, enum) no longer emit extends edges. Expected.
0.9.75 43 47 []
0.9.76 0 0 [] file still listed in extracted_sources
0.9.93 0 0 [] same
0.9.94 0 0 [] now listed in skipped_sources, and still in extracted_sources

On 0.9.94, jc.extract_json(p) returns {"nodes": [], "edges": [], "skipped": "data json (not a config/manifest)"}.

Cause

graphify/extractors/json_config.py (0.9.76):

31: _SCHEMA_DEFINITION_KEYS = frozenset({"$defs", "definitions", "$id"})
...
77:     # A file that declares the meta-schema AND defines something IS a schema.
78:     if "$schema" in root_keys and (root_keys & _SCHEMA_DEFINITION_KEYS):
79:         return False
269:         if not _is_config_json(path, doc, source):
270:             return {"nodes": [], "edges": [], "skipped": "data json (not a config/manifest)"}

graphify/extract.py (0.9.76) leaves skipped files out of failed_sources, and does not report them anywhere else:

7637:         if _res.get("skipped"):
7638:             # The extractor declined this file by design (data JSON, #1224), so
7639:             # zero nodes is the intended outcome rather than a failure. ...
7642:             continue

Expected

  • A file in skipped_sources is not also listed in extracted_sources, and the skip reason is available, for example skipped_sources: {path: reason}.
  • Ideally, JSON Schema documents get a semantic extraction path, as suggested in the #2255 discussion: one node per definition (Gadget, Sprocket, Flange, Doohickey), contains edges to their properties, and references edges for $ref, with no keyword nodes (type, properties, required). Failing that, an opt-in to keep the previous walk.

Actual

0 nodes and 0 edges, the file listed in both skipped_sources and extracted_sources, and no reason given.

duncancrawford avatar Oct 26 '26 09:10 duncancrawford