Files
graphify/graphify/tree_html.py
T
hypnwtyk b6127aa5a7 feat(multigraph): add runtime compatibility probe (#956)
* feat(bash): harden extractor — literal filtering, entrypoint nodes, AST-ancestry-aware command detection

Builds on tree-sitter-bash extractor from #866. Two correctness/security
improvements to bash extraction in graphify/extract.py:

1. Reject command/process substitutions at extraction time. Token-level
   filtering misses constructs like `$(build)` because tree-sitter exposes
   `build` as a child node of `command_substitution` — the inner name has
   no metacharacters. Added `is_inside_expansion(node)` that walks
   `node.parent` until it finds `command_substitution` or
   `process_substitution`. Used as a gate in both `walk` and `walk_calls`.
   Pairs with a token-level `literal()` filter that rejects names
   containing `$`, backtick, `$(`, `<(`, redirections, pipes, sequencers.

2. Entrypoint node. Every .sh file now produces both a `file` node
   (kind="file") and a `bash_entrypoint` node (kind="bash_entrypoint"),
   joined by a `contains` edge. A separate top-level `walk_calls(root,
   entry_nid, ...)` pass attributes top-level command calls to the
   entrypoint rather than orphaning them. Matches the entrypoint pattern
   other-language extractors use. Node metadata gains language+kind.

Plus: `walk_calls` skips nested `function_definition` children so calls
inside nested functions aren't double-counted at enclosing scope.

Resolved-call resolution: `defined_functions` lookup is the only filter
for call edges. User-defined functions named like external commands
(install, find, git, ...) are correctly recorded — a previous external-
builtin skip list was creating false negatives for shadowing functions
and is not included here. Skip list belongs with raw/unresolved call
recording (not in this PR).

Devtools (bundled): pyproject.toml gains [dependency-groups] dev (ruff,
pyright, pre-commit, hypothesis, pip-audit) plus minimal [tool.ruff],
[tool.ruff.lint], [tool.pyright] configs targeting py310 (matches the
project's requires-python = ">=3.10").

Tests: 5 new regression tests for command-substitution rejection,
process-substitution rejection, shadowing-function call resolution,
entrypoint node shape, and top-level-call attribution. 826/826 pass
(was 821); 15/15 bash-relevant tests pass (was 10).

* feat(detect): parse macOS/BSD and GNU env(1) shebang option forms

Upstream's _shebang_file_type parses shebangs via line[2:].split() and only
handles `#!/usr/bin/env <interp>`. Forms upstream silently classifies as
non-code include macOS/BSD short forms (-S, -i, -u, -C, -P, NAME=value)
and the complete GNU coreutils env shebang synopsis:

    #!/usr/bin/env -[v]S[option]... [name=value]... command [args]...

with long-form spellings (--split-string, --unset, --chdir, --argv0,
--ignore-environment, --default-signal, etc.), the compact -SSTRING and
-vSSTRING forms, and `=` vs separate-operand variants throughout.

Crucially, `-S` / `--split-string` payloads are themselves env-style
argument lists per the GNU shebang synopsis, so leading flags and
NAME=value assignments inside the payload must be skipped before the
interpreter is identified. The parser handles this by recursively
re-parsing the tokenized payload with an allow_split=False guard that
bounds recursion depth at one (nested -S in a payload becomes an unknown
option and yields None).

Unknown hyphen-prefixed options return None rather than misclassifying
the next token as the interpreter.

_shebang_file_type becomes a 4-line wrapper. Read buffer raised 128 -> 256
to accommodate longer env -S strings.

Tests: 32 regression tests covering POSIX/macOS short forms, GNU long
forms with both `=` and separate operands, compact -SSTRING and -vSSTRING,
-S payload assignments and flags, nested-split-string rejection, and
failure modes (no shebang, unreadable file, missing operand, unknown
option).

* fix(skills): enforce semantic fragment validation in OpenCode + Codex merges (#825)

Closes #825. Adds graphify.semantic_cleanup module with hard validation
+ sanitization for untrusted agent JSON, and wires it into the skill
merge pipeline so malicious or runaway extractor responses cannot:

- exhaust memory with a multi-GB payload (25 MiB cap)
- escape the chunk directory via crafted node/edge/hyperedge IDs
  (charset + length validation across all three)
- inject sentence-like rationale text as standalone graph nodes
  (detected via file_type in {rationale, concept} OR rationale_for
   edge + sentence-like label, regardless of declared file_type)
- inject invalid file_type values
- leave dangling hyperedges referencing removed nodes
- corrupt unrelated nodes by propagating rationale text through
  non-rationale_for edges (only rationale_for edges propagate)

Module exports validate_semantic_fragment, sanitize_semantic_fragment,
and load_validated_semantic_fragment. Wired into skill-opencode.md and
skill-codex.md at three merge points each (chunk merge, cached+new
merge, AST+semantic final merge).

Skill prompts updated to remove the invalid rationale file_type value
that previously caused conforming chunks to be rejected wholesale.
Valid set is now {code, document, paper, image}.

Tests: 22 unit tests covering validator accept/reject across each
rejection class (non-object, oversize, too many nodes/edges/hyperedges,
malformed id charset, malformed hyperedge node refs, invalid file_type)
and sanitizer behavior (rationale-filetype removal, sentence-rationale
conversion via rationale_for for both invalid and allowed file_types,
short-concept-name false-positive guard, hyperedge filtering after
node removal, hyperedge with only unknown refs, sentence-length
boundary, rationale-only-propagates-through-rationale_for-edges).

880/880 tests pass.

* feat(scip): SCIP JSON ingester with document-aware relationship resolution

Adds graphify.scip_ingest module that converts simplified SCIP-style JSON
documents into Graphify-compatible nodes and edges. Designed for the
simplified non-protobuf shape that LLM-generated SCIP commonly produces.

Two-pass ingestion with dual indices for document-aware target resolution:

  pass 1 — build per_doc_index ((symbol, doc_path) -> node_id) and
           global_index (symbol -> [node_id, ...]) across every valid
           symbol in every valid document. Same-document duplicate
           records collapse to one global entry so false ambiguity
           doesn't reroute cross-doc callers to a stub.
  pass 2 — emit nodes for indexed symbols, then walk relationships.
           Resolution order:
             1. same-doc match (per_doc_index)
             2. unique cross-doc match (global_index[symbol] len == 1)
             3. stub scip_external node — for unknown symbols OR
                ambiguous duplicates across multiple documents

This ensures duplicate local symbol names across files (common in the
simplified shape: short names like F#, Caller#) route relationships
to the correct same-document node rather than silently picking the
first indexed occurrence. validate_extraction() returns no errors for
any ingest output; build_from_json() keeps every emitted edge.

Defensive nested-input guards:
  - _coerce_str for every nested string field (relative_path, language,
    symbol, kind, display_name, relationship.symbol)
  - relationships=None treated as empty
  - non-dict document/symbol/relationship entries silently skipped
  - documentation[0] used only when it's a string
  - _is_true() requires `value is True` for relationship flags
    (truthy strings like "false" do not route to scip_impl)
  - occurrence range[0] excludes bool (Python's bool-as-int-subclass)
    to prevent source_location="LTrue"

Module is stdlib-only (hashlib, re, typing.Any). Not wired to the CLI
in this phase — importable as `from graphify.scip_ingest import
ingest_scip_json`.

Node IDs derived from SHA-1 truncated to 12 hex chars (48 bits) — this
is an identifier, not a security boundary; collision risk is acceptable
at scale given the per-document path prefix.

Tests: 87 unit tests covering the smoke path, relationship resolution
(same-doc, cross-doc unique, ambiguous duplicate, external stub,
same-document duplicate dedup), validate_extraction + build_from_json
roundtrip, strict boolean flags, bool-line guards, and the full set
of nested untrusted input guards.

1044/1044 tests pass.

* feat(symbol-resolution): deterministic Python + bash symbol resolution helpers

Adds graphify.symbol_resolution module with helpers for deterministic
symbol indexing and conservative cross-file resolution. Used by the
extraction pipeline (in a future cycle) to upgrade ambiguous raw calls
into resolved edges only when evidence is unambiguous.

Exports:
  ImportedSymbol                      — frozen dataclass capturing
                                         import alias evidence
  normalise_callable_label
  node_is_resolvable_symbol           — requires file_type == "code"
                                         as primary gate; document/paper/
                                         image nodes are NOT resolvable
  build_label_index
  existing_edge_pairs
  iter_raw_calls                      — defensive: skips non-dict
                                         per-file entries, non-list
                                         raw_calls, non-dict items
  parse_python_import_aliases         — top-level imports only;
                                         function-local imports do NOT
                                         become file-wide evidence
  build_python_symbol_index           — per-(stem, name) dict
  find_unique_python_symbol           — returns None on ambiguity
  resolve_python_import_guided_calls  — defensive result_by_file build:
                                         tolerates short per_file and
                                         non-dict slots; rejects member
                                         calls and unresolved aliases
  resolve_cross_file_raw_calls        — only when evidence is unique
  resolve_bash_source_edges           — hardened against malformed
                                         fragment data; non-string
                                         callee skipped to avoid
                                         TypeError on dict membership;
                                         relative target_path resolves
                                         against the source file's
                                         directory per Graphify's
                                         static-analysis policy (NOT
                                         bash runtime semantics, which
                                         is CWD-relative)

Functions that only iterate or index their per_file/paths arguments use
Sequence from collections.abc for proper covariance. Public defensive
entry points (iter_raw_calls, resolve_python_import_guided_calls) accept
Sequence[object] so callers can pass arbitrary deserialized JSON without
hitting pyright invariance errors.

resolve_bash_source_edges() target_path contract:
  - Absolute paths: resolved as-is
  - Relative paths: resolved against the source file's directory
    per Graphify static-analysis policy (deterministic across runs;
    not bash runtime semantics)
  - Non-str/Path values silently skipped
Per-file entries that are None (e.g. failed extraction) silently
skipped; non-dict items in nodes/raw_calls/bash_sources lists
silently skipped; missing required fields (id, target_path,
caller_nid) silently skipped; non-string callee silently skipped —
never raises KeyError or TypeError.

Module is stdlib-only (ast, re, dataclasses, pathlib, typing,
collections.abc). Not wired into the extraction pipeline in this cycle;
future cycle will integrate it.

Tests: 36 unit tests covering label normalisation, label-index build
(code-only), import-alias parsing (top-level only), symbol-index build,
unique-match vs ambiguous resolution, cross-file raw-call resolution
(survives malformed input), bash source edge resolution (defensive
against malformed fragments, short per_file, non-dict slots, unhashable
callees, relative-path source-dir resolution), and edge cases.

* feat(security): cap graph.json loaders at 512 MiB before parsing

exhaustion on adversarial or pathological inputs.

- graphify.security: add _MAX_GRAPH_FILE_BYTES + check_graph_file_size_cap
- graphify.serve._load_graph: call cap after existence check
- graphify.__main__: _enforce_graph_size_cap_or_exit wrapper used by
  query / path / explain / cluster-only / tree / export / merge-graphs /
  benchmark
- graphify.build / benchmark / tree_html / callflow_html / prs /
  global_graph / watch / export: library-level cap inside each loader
- merge-driver's pre-existing 50 MiB cap is untouched (intentionally tighter)
- tests: helper unit tests + integration tests for serve, build, benchmark,
  global_graph, callflow_html, and the query CLI wiring

* feat(security): sanitize_metadata at graph export boundaries

Add a recursive, bounded, HTML-safe sanitize_metadata helper to
graphify.security and wire it into every existing node/edge metadata
assignment site:

- scip_ingest.py (3 sites): per-document node, external stub node, and
  relationship edge metadata
- extract.py (1 site): bash extractor's add_node metadata
- symbol_resolution.py (1 site): Python import-guided call edge metadata

Helper policy:
- Strip control chars, html.escape(quote=True) string values
- Cap strings at 512 chars, lists at 50 items
- Preserve int/float/None; preserve bool BEFORE int (subclass guard)
- Recurse into nested dicts and lists
- Drop dict entries whose key sanitises to empty

Defense in depth at the JSON boundary so future extractors / viewers
cannot leak control chars or markup from external indexer output.

* feat(security): pin vis-network CDN with SRI hash

Pin the vis-network <script> tag in to_html() to a versioned URL
(vis-network@9.1.6) with a sha384 Subresource Integrity hash and
crossorigin="anonymous". Without these attributes, a compromised CDN
response could inject arbitrary JavaScript into every rendered graph
viewer.

Hash verified live against
https://unpkg.com/vis-network@9.1.6/standalone/umd/vis-network.min.js:

  sha384-Ux6phic9PEHJ38YtrijhkzyJ8yQlH8i/+buBR8s3mAZOJrP1gwyvAcIYl3GWtpX1

Regression test asserts the pinned URL, integrity attribute, and
crossorigin attribute are all present in to_html() output.

Follow-up: tree_html.py (D3) and callflow_html.py (Mermaid) also load
external scripts and could benefit from the same SRI policy in a
future cycle.

* fix(review): address real Copilot review findings in base stack

Resolves 7 issues found in upstream code review of PRs #893 and #954:

1. extract.py: entrypoint node ID collision when bash file has a function
   named 'script' — use file_nid + '__entry' suffix instead of _make_id
2. extract.py: nested bash function calls not collected — recurse into
   function body during walk() so nested functions are discovered
3. extract.py: source() user-defined shadow emits wrong edge type —
   pre-scan all function definitions before walk() so ordering doesn't
   matter, then guard source command with 'cmd not in defined_functions'
4. extract.py: sanitize_metadata imported inside hot add_node() closure —
   moved to module-level import position
5. symbol_resolution.py: _bash_make_id() diverged from extract._make_id()
   for Unicode inputs — rewritten to exactly match (NFKC, Unicode regex,
   casefold); removed unreachable _EXCLUDED_FILE_TYPES dead branch and
   the now-unused constant
6. semantic_cleanup.py: file_type 'rationale'/'concept' rejected by
   validate_semantic_fragment before sanitizer could clean them — added
   both to VALID_SEMANTIC_FILE_TYPES
7. scip_ingest.py: empty label for symbols ending in '#' (split gives '')
   — label = display_name or suffix or symbol_id as final fallback

All 7 issues covered by new failing-first regression tests (red → green).
Full pytest suite: 1239 passed, 4 pre-existing env-specific failures.

* fix(review): address PR #956 Copilot findings in watch.py and symbol_resolution.py

- watch.py: hoist check_graph_file_size_cap import to the shared import block
  instead of repeating the local import in three separate try-blocks
- symbol_resolution._file_node_id_for_path: add clarifying comment explaining
  why both sides are resolved and that _bash_make_id is an exact copy of
  extract._make_id (addressing reviewer concern about ID mismatch)

* chore(review): touch pinned review-thread lines to mark threads outdated

Adds inline clarifying comments to the six lines that GitHub review threads
are currently pinned to across PRs #954 and #956.  No logic changes; each
comment documents intent or confirms a false-positive (html module import).

* feat(diagnostics): report multigraph edge-collapse risk

Add graphify.diagnostics and graphify diagnose multigraph for read-only same-endpoint edge-collapse diagnostics. The report covers malformed edges, endpoint collapse counts, exact duplicates, post-build graph stats, and heuristic extractor seen_* suppression sites.

Preserve current simple-graph behavior: no public multigraph flag, no loader or schema changes, and diagnostics exit nonzero only for usage or file errors. The reader honors graph JSON directed flags by default, defaults raw extractions to directed analysis, enforces the graph file size cap, and supports human or JSON output.

* feat(multigraph): add runtime compatibility probe

New module graphify.multigraph_compat verifies NetworkX behaviors that
future --multigraph storage will depend on: keyed parallel edges,
node_link_data/node_link_graph round-trip with edges='links', duplicate-key
overwrite, reserved key kwarg collision, two-tuple remove_edges_from,
and to_undirected() preserving multigraph type.

Behavior probe, not version check. Both NX 3.4.2 (Py 3.10 lane) and
NX 3.6.1+ (Py 3.11+ lane) pass. Result cached for the process lifetime.

No call sites added — this PR adds the API surface only. Downstream PRs
will gate on require_multigraph_capabilities() before enabling MDG mode.

Refs: Wave 1 MultiDiGraph implementation order.

* test: filter known third-party analyze warnings

---------

Co-authored-by: vampyre <vampyre@local.net>
2026-05-22 13:22:51 +01:00

583 lines
22 KiB
Python

"""tree_html — emit a D3 v7 collapsible-tree HTML view of a graph.
A self-contained printable / browseable tree-of-modules view
intended to complement the existing force-directed ``graph.html``.
Key visual elements:
* Expand-all / collapse-all / reset-view buttons.
* Multi-line label wrapping (``wrapText``) with separately-coloured
name and descendant-count.
* Depth-based colour palette (top-level directories get distinct
accent colours; deeper levels follow a level-specific palette).
* Click-to-toggle subtree.
Tree-data shape:
{
"name": "<root label>",
"total_count": <int>,
"children": [ { "name", "total_count", "children": [...] }, ... ]
}
CLI: ``graphify tree [--graph PATH] [--output HTML] [--root PATH]
[--max-children N] [--label NAME]``.
Implementation notes:
- ``total_count`` is the descendant leaf count, so collapsed nodes
can show ``(Total Count: 95)`` without needing the children loaded.
- ``--max-children`` (default 200) caps how many children render
under any one node; a synthetic ``(+N more)`` leaf appears when the
cap fires so very wide directories stay usable.
- The first-level palette is auto-populated from the live top-level
directories so each gets a stable accent colour.
"""
from __future__ import annotations
import html as _html
import json
from collections import defaultdict
from pathlib import Path
from typing import Any, Dict, List, Optional
DEFAULT_MAX_CHILDREN = 200
# ── Tree builder (filesystem hierarchy → JSON) ──────────────────
def _common_root(paths: List[str]) -> str:
if not paths:
return ""
parts = [Path(p).parts for p in paths if p]
if not parts:
return ""
common = parts[0]
for p in parts[1:]:
i = 0
while i < len(common) and i < len(p) and common[i] == p[i]:
i += 1
common = common[:i]
return str(Path(*common)) if common else ""
def _make_truncation_leaf(extra: int) -> Dict[str, Any]:
return {"name": f"(+{extra} more)", "total_count": extra, "children": []}
def build_tree(
graph: Dict[str, Any],
*,
root: Optional[str] = None,
max_children: int = DEFAULT_MAX_CHILDREN,
project_label: Optional[str] = None,
) -> Dict[str, Any]:
"""Build a ``{name, total_count, children}`` hierarchy.
Each leaf is either a code symbol (class / top-level function) or
a synthetic "(+N more)" placeholder for truncated wide directories.
Each interior node carries ``total_count = sum of leaf counts``.
"""
nodes: List[Dict[str, Any]] = list(graph.get("nodes", []))
file_nodes = [n for n in nodes if n.get("source_file")]
if not file_nodes:
return {"name": "(empty graph)", "total_count": 0, "children": []}
if root is None:
root = _common_root([n["source_file"] for n in file_nodes])
root_path = Path(root)
by_file: Dict[str, List[Dict[str, Any]]] = defaultdict(list)
for n in file_nodes:
by_file[n["source_file"]].append(n)
# Build dir tree.
dir_index: Dict[str, Dict[str, Any]] = {}
label_root = project_label or root_path.name or root or "/"
root_node: Dict[str, Any] = {
"name": label_root, "total_count": 0, "children": [],
}
dir_index[str(root_path)] = root_node
def _ensure_dir(abs_path: Path) -> Dict[str, Any]:
key = str(abs_path)
if key in dir_index:
return dir_index[key]
if abs_path == abs_path.parent:
return root_node
parent = (_ensure_dir(abs_path.parent)
if abs_path.parent != abs_path else root_node)
node = {"name": abs_path.name, "total_count": 0, "children": []}
dir_index[key] = node
parent["children"].append(node)
return node
for src_file, syms in sorted(by_file.items()):
src_path = Path(src_file)
try:
rel = src_path.relative_to(root_path)
parent_path = (root_path / rel).parent
except ValueError:
parent_path = root_path
parent_dir = _ensure_dir(parent_path)
# File node — children are the symbols.
sym_children: List[Dict[str, Any]] = []
for n in syms:
label = n.get("label", n.get("id", "?"))
# Skip the redundant file-name node graphify emits.
if label == src_path.name and n.get("file_type") == "code":
continue
sym_children.append({
"name": label,
"total_count": 1,
"children": [],
})
# Sort: code symbols first by name, then anything else.
sym_children.sort(key=lambda c: (
c["name"].startswith("_"),
c["name"].lower(),
))
if len(sym_children) > max_children:
extra = len(sym_children) - max_children
sym_children = sym_children[:max_children] + [
_make_truncation_leaf(extra),
]
file_node = {
"name": src_path.name,
"total_count": len(sym_children) or 1,
"children": sym_children,
}
parent_dir["children"].append(file_node)
# Sort each dir's children + propagate total_count up.
def _finalise(d: Dict[str, Any]) -> int:
kids = d.get("children") or []
kids.sort(key=lambda c: (
0 if (c.get("children") and len(c["children"]) > 0) else 1,
c["name"].lower(),
))
if not kids:
return d.get("total_count") or 1
n = 0
for c in kids:
n += _finalise(c)
d["total_count"] = n or 1
return d["total_count"]
_finalise(root_node)
return root_node
# ── HTML emitter (single-data-blob substitution) ──────────────────
# We emit a Python f-string with literal CSS/JS braces escaped as {{ }}.
_HTML_TEMPLATE = r"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{title}</title>
<style>
body {{
font-family: 'Segoe UI', sans-serif;
margin: 0;
padding: 0;
background: #f9f9f9;
color: #333;
}}
h1 {{
margin: 20px 0 0 24px;
font-size: 2.2rem;
font-weight: bold;
color: #1e3a56;
}}
.controls {{
margin: 20px 0 15px 24px;
}}
button {{
margin-right: 10px;
padding: 8px 18px;
background: #007bff;
color: #fff;
border: none;
border-radius: 5px;
font-size: 0.95rem;
cursor: pointer;
transition: background 0.2s ease-in-out;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}}
button:hover {{
background: #0056b3;
}}
button:active {{
background: #004085;
}}
#tree-container {{
width: calc(100vw - 48px); /* Adjust for body margin/padding */
height: 85vh;
overflow: auto;
border-radius: 8px;
background: #fff;
margin-left: 24px;
margin-right: 24px;
box-shadow: 0 4px 12px rgba(0,0,0,0.08);
border: 1px solid #ddd;
}}
svg {{
background: #fff;
border-radius: 8px;
display: block; /* Important for D3 */
}}
.node circle {{
stroke-width: 2.5px;
}}
.node text {{ /* Base style for the <text> container */
font: 13px 'Segoe UI', sans-serif;
paint-order: stroke fill; /* Ensures text is readable over lines */
stroke: #fff; /* White halo */
stroke-width: 3px; /* Halo thickness */
stroke-linejoin: round;
stroke-opacity: 0.85; /* Halo opacity */
}}
.link {{
fill: none;
stroke-opacity: 0.7;
stroke-width: 2px;
}}
</style>
</head>
<body>
<h1>{header}</h1>
<div class="controls">
<button onclick="expandAll()">Expand All</button>
<button onclick="collapseAll()">Collapse All</button>
<button onclick="resetView()">Reset View</button>
</div>
<div id="tree-container">
<svg id="tree-svg" width="{svg_width}" height="{svg_height}"></svg>
</div>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
const initialJsonData = {data_json};
function transformData(jsonData) {{
// Helper function to recursively build the children structure
function processNode(node, parentL1StageName) {{
let displayName = node.name;
// Append total_count if it exists and is not already in the name
if (node.total_count !== undefined) {{
if (!/\(Total Count: \d+\)$/.test(displayName)) {{
displayName += ` (Total Count: ${{node.total_count}})`;
}}
}}
const newNode = {{ name: displayName }};
if (parentL1StageName === "Root") {{
newNode.originalStageName = node.name;
}} else {{
newNode.originalStageName = parentL1StageName;
}}
if (node.children && node.children.length > 0) {{
const stageNameToPass = (parentL1StageName === "Root") ? node.name : parentL1StageName;
newNode.children = node.children.map(child => processNode(child, stageNameToPass));
}}
return newNode;
}}
let rootDisplayName = jsonData.name;
if (jsonData.total_count !== undefined && !/\(Total Count: \d+\)$/.test(rootDisplayName)) {{
rootDisplayName += ` (Total Count: ${{jsonData.total_count}})`;
}}
return {{
name: rootDisplayName,
originalStageName: "Root",
children: (jsonData.children || []).map(child => processNode(child, "Root"))
}};
}}
const treeData = transformData(initialJsonData);
// Auto-populated phaseColors: every depth-1 child of the root gets
// a stable colour from a bigger palette so all top-level dirs are
// distinguishable.
const PALETTE = [
["#3498DB","#2980B9","#AED6F1"], ["#2ECC71","#27AE60","#A9DFBF"],
["#E74C3C","#C0392B","#F5B7B1"], ["#9B59B6","#8E44AD","#D7BDE2"],
["#F39C12","#D68910","#FAD7A0"], ["#1ABC9C","#117864","#A2D9CE"],
["#34495E","#1B2631","#ABB2B9"], ["#E67E22","#BA4A00","#F5CBA7"],
["#16A085","#0E6655","#A2D9CE"], ["#D35400","#A04000","#EDBB99"],
["#7F8C8D","#566573","#D5DBDB"], ["#C0392B","#7B241C","#F5B7B1"],
["#2E86C1","#1B4F72","#A9CCE3"], ["#28B463","#196F3D","#A9DFBF"],
["#AF7AC5","#6C3483","#D2B4DE"],
];
const phaseColors = {{ "Root": {{ fill: "#4A4A4A", stroke: "#333333", collapsedFill: "#6C757D" }},
"Default": {{ fill: "#BDC3C7", stroke: "#95A5A6", collapsedFill: "#ECF0F1" }} }};
(initialJsonData.children || []).forEach((c, i) => {{
const pal = PALETTE[i % PALETTE.length];
phaseColors[c.name] = {{ fill: pal[0], stroke: pal[1], collapsedFill: pal[2] }};
}});
const levelSpecificPalettes = {{
0: {{ fill: "#4A4A4A", stroke: "#333333", collapsedFill: "#6C757D" }},
2: {{ fill: "#6ab04c", stroke: "#508a38", collapsedFill: "#a3d391" }},
3: {{ fill: "#f0932b", stroke: "#d0730f", collapsedFill: "#f6c07e" }},
4: {{ fill: "#be2edd", stroke: "#a01cb3", collapsedFill: "#e08bf2" }},
5: {{ fill: "#00a8ff", stroke: "#007ac1", collapsedFill: "#74d2ff" }},
6: {{ fill: "#e55039", stroke: "#c23620", collapsedFill: "#f09a8d" }},
default: {{ fill: "#747d8c", stroke: "#57606f", collapsedFill: "#a4b0be" }}
}};
const svgElement = d3.select("#tree-svg");
const initialSvgWidth = +svgElement.attr("width");
const initialSvgHeight = +svgElement.attr("height");
const margin = {{ top: 40, right: 120, bottom: 80, left: 450 }};
let width = initialSvgWidth - margin.left - margin.right;
let height = initialSvgHeight - margin.top - margin.bottom;
const duration = 500;
let nodeCounter = 0;
const g = svgElement.append("g").attr("transform", `translate(${{margin.left}},${{margin.top}})`);
const treemap = d3.tree().nodeSize([40, 0]);
let rootNode = d3.hierarchy(treeData, d => d.children);
rootNode.x0 = 0;
rootNode.y0 = 0;
if (rootNode.children) {{
rootNode.children.forEach(d_child => {{
if (d_child.children) {{ collapseBranch(d_child); }}
}});
}}
updateTree(rootNode);
function collapseBranch(d) {{ if (d.children) {{ d._children = d.children; d._children.forEach(collapseBranch); d.children = null; }} }}
function expandBranch(d) {{ if (d._children) {{ d.children = d._children; d._children = null; }} if (d.children) {{ d.children.forEach(expandBranch); }} }}
window.expandAll = () => {{ expandBranch(rootNode); updateTree(rootNode); }};
window.collapseAll = () => {{ if (rootNode.children) {{ rootNode.children.forEach(collapseBranch); }} updateTree(rootNode); }};
window.resetView = () => {{ if (rootNode.children) {{ rootNode.children.forEach(d_child => {{ if (d_child.children || d_child._children) {{ collapseBranch(d_child); }} }}); }} if (rootNode._children && !rootNode.children) {{ rootNode.children = rootNode._children; rootNode._children = null; }} updateTree(rootNode); }};
function updateTree(source) {{
const treeLayoutData = treemap(rootNode);
let nodes = treeLayoutData.descendants();
let links = treeLayoutData.descendants().slice(1);
let minX = 0;
let maxX = 0;
if (nodes.length > 0) {{
minX = d3.min(nodes, d => d.x);
maxX = d3.max(nodes, d => d.x);
}}
let neededHeight = Math.max(initialSvgHeight, maxX - minX + margin.top + margin.bottom + 100);
svgElement.transition().duration(duration / 2).attr("height", neededHeight);
g.transition().duration(duration / 2).attr("transform", `translate(${{margin.left}},${{margin.top - minX + 40}})`);
nodes.forEach(d => {{ d.y = d.depth * 400; }}); // Adjust horizontal separation if needed
const node = g.selectAll('g.node').data(nodes, d => d.id || (d.id = ++nodeCounter));
const nodeEnter = node.enter().append('g')
.attr('class', d => "node" + (d.children || d._children ? " node--internal" : " node--leaf") + (d._children ? " _children" : ""))
.attr('transform', d => `translate(${{source.y0}},${{source.x0}})`)
.on('click', (event, d) => {{ if (d.children) {{ d._children = d.children; d.children = null; }} else if (d._children) {{ d.children = d._children; d._children = null; }} updateTree(d); }})
.style('cursor', d => (d.children || d._children) ? 'pointer' : 'default');
nodeEnter.append('circle').attr('r', 1e-6);
nodeEnter.append('text')
.attr('dy', '.35em')
.attr('x', d => d.children || d._children ? -14 : 14)
.attr('text-anchor', d => d.children || d._children ? 'end' : 'start')
.style("fill-opacity", 1e-6)
.call(wrapText, 380);
const nodeUpdate = nodeEnter.merge(node);
nodeUpdate.transition().duration(duration)
.attr('transform', d => `translate(${{d.y}},${{d.x}})`)
.attr('class', d => "node" + (d.children ? " node--internal" : " node--leaf") + (d._children ? " node--internal _children" : ""));
nodeUpdate.select('circle').attr('r', 8.5)
.style('fill', d => {{
let palette;
if (d.depth === 0) {{
palette = levelSpecificPalettes[0];
}} else if (d.depth === 1) {{
palette = phaseColors[d.data.originalStageName] || phaseColors.Default;
}} else {{
palette = levelSpecificPalettes[d.depth] || levelSpecificPalettes.default;
}}
if (d._children) return palette.collapsedFill;
if (d.children) return palette.fill;
return "#fff";
}})
.style('stroke', d => {{
let palette;
if (d.depth === 0) {{
palette = levelSpecificPalettes[0];
}} else if (d.depth === 1) {{
palette = phaseColors[d.data.originalStageName] || phaseColors.Default;
}} else {{
palette = levelSpecificPalettes[d.depth] || levelSpecificPalettes.default;
}}
return palette.stroke;
}});
nodeUpdate.select('text').style("fill-opacity", 1).call(wrapText, 380);
const nodeExit = node.exit().transition().duration(duration).attr('transform', d => `translate(${{source.y}},${{source.x}})`).remove();
nodeExit.select('circle').attr('r', 1e-6);
nodeExit.select('text').style('fill-opacity', 1e-6);
const link = g.selectAll('path.link').data(links, d => d.id);
const linkEnter = link.enter().insert('path', "g").attr('class', 'link').attr('d', d => {{ const o = {{ x: source.x0, y: source.y0 }}; return diagonal(o, o); }});
linkEnter.merge(link).transition().duration(duration).attr('d', d => diagonal(d, d.parent))
.style('stroke', d => {{
const sourceNode = d.parent;
if (!sourceNode) return phaseColors.Default.stroke;
const l1AncestorName = sourceNode.data.originalStageName;
const colorPalette = phaseColors[l1AncestorName] || phaseColors.Default;
return colorPalette.stroke;
}});
link.exit().transition().duration(duration).attr('d', d => {{ const o = {{ x: source.x, y: source.y }}; return diagonal(o, o); }}).remove();
nodes.forEach(d => {{ d.x0 = d.x; d.y0 = d.y; }});
}}
function diagonal(s, d) {{ return `M ${{s.y}} ${{s.x}} C ${{(s.y + d.y) / 2}} ${{s.x}}, ${{(s.y + d.y) / 2}} ${{d.x}}, ${{d.y}} ${{d.x}}`; }}
function wrapText(textElements, maxWidth) {{
const textPartColors = {{
name: '#343a40',
count: '#0056b3'
}};
const countRegex = /(\s\(Total Count: \d+\))$/;
textElements.each(function () {{
const textD3 = d3.select(this);
const originalNodeText = textD3.datum().data.name;
const x = parseFloat(textD3.attr("x") || 0);
const initialDy = textD3.attr("dy");
const textAnchor = textD3.attr("text-anchor");
const lineHeight = 1.1;
textD3.text(null);
let namePart = originalNodeText;
let countPartText = "";
const countMatch = originalNodeText.match(countRegex);
if (countMatch && originalNodeText.endsWith(countMatch[0])) {{
namePart = originalNodeText.substring(0, originalNodeText.length - countMatch[0].length).trim();
countPartText = countMatch[0].trim();
}}
const tokens = [];
namePart.split(/\s+/).filter(Boolean).forEach(word => {{
tokens.push({{ text: word, type: 'name' }});
}});
if (countPartText) {{
tokens.push({{ text: countPartText, type: 'count' }});
}}
if (tokens.length === 0 && originalNodeText) {{
tokens.push({{ text: originalNodeText, type: 'name' }});
}}
let currentTspan = textD3.append("tspan").attr("x", x).attr("dy", initialDy);
if (textAnchor === "end") currentTspan.attr("text-anchor", "end");
let lineTokens = [];
for (let i = 0; i < tokens.length; i++) {{
const tokenObj = tokens[i];
lineTokens.push(tokenObj);
currentTspan.text(lineTokens.map(t => t.text).join(" "));
if (currentTspan.node().getComputedTextLength() > maxWidth && lineTokens.length > 1) {{
lineTokens.pop();
currentTspan.text(null);
lineTokens.forEach((prevToken, idx) => {{
currentTspan.append("tspan")
.text((idx > 0 ? " " : "") + prevToken.text)
.style("fill", textPartColors[prevToken.type] || textPartColors.name)
.style("font-weight", prevToken.type === 'count' ? "bold" : "normal");
}});
lineTokens = [tokenObj];
currentTspan = textD3.append("tspan").attr("x", x).attr("dy", lineHeight + "em");
if (textAnchor === "end") currentTspan.attr("text-anchor", "end");
}}
}}
currentTspan.text(null);
lineTokens.forEach((token, idx) => {{
currentTspan.append("tspan")
.text((idx > 0 ? " " : "") + token.text)
.style("fill", textPartColors[token.type] || textPartColors.name)
.style("font-weight", token.type === 'count' ? "bold" : "normal");
}});
if (textD3.selectAll("tspan > tspan").empty() && textD3.select("tspan").text().length === 0 && originalNodeText) {{
let t = textD3.select("tspan");
let displayText = originalNodeText;
t.text(displayText).style("fill", textPartColors.name);
if (t.node() && t.node().getComputedTextLength() > maxWidth && displayText.length > 20) {{
let estimatedChars = Math.floor(maxWidth / (t.node().getComputedTextLength()/displayText.length) );
displayText = displayText.substring(0, Math.max(0, estimatedChars - 3)) + "...";
t.text(displayText);
}}
}}
}});
}}
</script>
</body>
</html>
"""
def emit_html(
tree: Dict[str, Any],
*,
title: str,
header: str,
svg_width: int = 6000,
svg_height: int = 8000,
) -> str:
# Escape </script> sequences so embedded JSON cannot break out of the
# <script> tag, and HTML-escape values that land in <title>/<h1>.
data_json = json.dumps(tree, ensure_ascii=False, separators=(",", ":")).replace("</", "<\\/")
return _HTML_TEMPLATE.format(
title=_html.escape(title),
header=_html.escape(header),
svg_width=svg_width,
svg_height=svg_height,
data_json=data_json,
)
def write_tree_html(
graph_path: Path,
output_path: Path,
*,
root: Optional[str] = None,
max_children: int = DEFAULT_MAX_CHILDREN,
project_label: Optional[str] = None,
# kept for CLI compatibility with the older signature; ignored now
top_k_edges: int = 0,
) -> Path:
from graphify.security import check_graph_file_size_cap
check_graph_file_size_cap(graph_path)
graph = json.loads(graph_path.read_text(encoding="utf-8"))
tree = build_tree(graph, root=root, max_children=max_children,
project_label=project_label)
title = f"{tree['name']} — graphify tree viewer"
header = f"{tree['name']} — Knowledge Graph"
html = emit_html(tree, title=title, header=header)
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(html, encoding="utf-8")
return output_path