graphitect 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (336) hide show
  1. graphify/__init__.py +30 -0
  2. graphify/__main__.py +757 -0
  3. graphify/_minhash.py +107 -0
  4. graphify/affected.py +318 -0
  5. graphify/always_on/agents-md.md +12 -0
  6. graphify/always_on/antigravity-rules.md +14 -0
  7. graphify/always_on/claude-md.md +9 -0
  8. graphify/always_on/gemini-md.md +9 -0
  9. graphify/always_on/kiro-steering.md +5 -0
  10. graphify/always_on/vscode-instructions.md +17 -0
  11. graphify/analyze.py +769 -0
  12. graphify/benchmark.py +152 -0
  13. graphify/build.py +2300 -0
  14. graphify/cache.py +1746 -0
  15. graphify/callflow_html.py +2051 -0
  16. graphify/cargo_introspect.py +109 -0
  17. graphify/cli.py +4745 -0
  18. graphify/cluster.py +409 -0
  19. graphify/command-kilo.md +15 -0
  20. graphify/cross_repo_calls.py +216 -0
  21. graphify/cross_repo_types.py +75 -0
  22. graphify/csharp_dispatch.py +154 -0
  23. graphify/dedup.py +1213 -0
  24. graphify/detect.py +2566 -0
  25. graphify/diagnostics.py +406 -0
  26. graphify/export.py +1349 -0
  27. graphify/exporters/__init__.py +1 -0
  28. graphify/exporters/base.py +14 -0
  29. graphify/exporters/graphdb.py +173 -0
  30. graphify/exporters/html.py +637 -0
  31. graphify/extract.py +7856 -0
  32. graphify/extractors/MIGRATION.md +107 -0
  33. graphify/extractors/__init__.py +66 -0
  34. graphify/extractors/apex.py +215 -0
  35. graphify/extractors/base.py +85 -0
  36. graphify/extractors/bash.py +579 -0
  37. graphify/extractors/blade.py +53 -0
  38. graphify/extractors/commonlisp.py +540 -0
  39. graphify/extractors/csharp.py +448 -0
  40. graphify/extractors/dart.py +564 -0
  41. graphify/extractors/dm.py +494 -0
  42. graphify/extractors/elixir.py +241 -0
  43. graphify/extractors/engine.py +6509 -0
  44. graphify/extractors/fortran.py +311 -0
  45. graphify/extractors/go.py +527 -0
  46. graphify/extractors/json_config.py +240 -0
  47. graphify/extractors/julia.py +289 -0
  48. graphify/extractors/markdown.py +408 -0
  49. graphify/extractors/models.py +131 -0
  50. graphify/extractors/objc.py +566 -0
  51. graphify/extractors/ocaml.py +289 -0
  52. graphify/extractors/pascal.py +688 -0
  53. graphify/extractors/pascal_forms.py +196 -0
  54. graphify/extractors/powershell.py +522 -0
  55. graphify/extractors/razor.py +192 -0
  56. graphify/extractors/resolution.py +3584 -0
  57. graphify/extractors/robot.py +296 -0
  58. graphify/extractors/rust.py +470 -0
  59. graphify/extractors/sln.py +92 -0
  60. graphify/extractors/sql.py +720 -0
  61. graphify/extractors/terraform.py +181 -0
  62. graphify/extractors/verilog.py +329 -0
  63. graphify/extractors/zig.py +181 -0
  64. graphify/file_slice.py +246 -0
  65. graphify/global_graph.py +194 -0
  66. graphify/google_workspace.py +237 -0
  67. graphify/hooks.py +933 -0
  68. graphify/ids.py +93 -0
  69. graphify/ingest.py +358 -0
  70. graphify/install.py +2366 -0
  71. graphify/llm.py +3544 -0
  72. graphify/manifest.py +4 -0
  73. graphify/manifest_ingest.py +311 -0
  74. graphify/mcp_ingest.py +386 -0
  75. graphify/multigraph_compat.py +212 -0
  76. graphify/pascal_resolution.py +129 -0
  77. graphify/paths.py +436 -0
  78. graphify/pg_introspect.py +165 -0
  79. graphify/prs.py +770 -0
  80. graphify/querylog.py +80 -0
  81. graphify/reflect.py +882 -0
  82. graphify/report.py +346 -0
  83. graphify/resolver_registry.py +85 -0
  84. graphify/ruby_resolution.py +242 -0
  85. graphify/scip_ingest.py +363 -0
  86. graphify/security.py +460 -0
  87. graphify/semantic_cleanup.py +336 -0
  88. graphify/serve.py +2608 -0
  89. graphify/skill-agents.md +710 -0
  90. graphify/skill-aider.md +1283 -0
  91. graphify/skill-amp.md +710 -0
  92. graphify/skill-claw.md +713 -0
  93. graphify/skill-codex.md +710 -0
  94. graphify/skill-copilot.md +713 -0
  95. graphify/skill-devin.md +1410 -0
  96. graphify/skill-droid.md +710 -0
  97. graphify/skill-kilo.md +722 -0
  98. graphify/skill-kiro.md +713 -0
  99. graphify/skill-opencode.md +705 -0
  100. graphify/skill-pi.md +713 -0
  101. graphify/skill-trae.md +711 -0
  102. graphify/skill-vscode.md +709 -0
  103. graphify/skill-windows.md +755 -0
  104. graphify/skill.md +713 -0
  105. graphify/skills/agents/references/add-watch.md +56 -0
  106. graphify/skills/agents/references/exports.md +87 -0
  107. graphify/skills/agents/references/extraction-spec.md +70 -0
  108. graphify/skills/agents/references/github-and-merge.md +46 -0
  109. graphify/skills/agents/references/hooks.md +33 -0
  110. graphify/skills/agents/references/query.md +311 -0
  111. graphify/skills/agents/references/transcribe.md +52 -0
  112. graphify/skills/agents/references/update.md +210 -0
  113. graphify/skills/amp/references/add-watch.md +56 -0
  114. graphify/skills/amp/references/exports.md +87 -0
  115. graphify/skills/amp/references/extraction-spec.md +70 -0
  116. graphify/skills/amp/references/github-and-merge.md +46 -0
  117. graphify/skills/amp/references/hooks.md +33 -0
  118. graphify/skills/amp/references/query.md +311 -0
  119. graphify/skills/amp/references/transcribe.md +52 -0
  120. graphify/skills/amp/references/update.md +210 -0
  121. graphify/skills/claude/references/add-watch.md +56 -0
  122. graphify/skills/claude/references/exports.md +87 -0
  123. graphify/skills/claude/references/extraction-spec.md +70 -0
  124. graphify/skills/claude/references/github-and-merge.md +46 -0
  125. graphify/skills/claude/references/hooks.md +33 -0
  126. graphify/skills/claude/references/query.md +311 -0
  127. graphify/skills/claude/references/transcribe.md +52 -0
  128. graphify/skills/claude/references/update.md +210 -0
  129. graphify/skills/claw/references/add-watch.md +56 -0
  130. graphify/skills/claw/references/exports.md +87 -0
  131. graphify/skills/claw/references/extraction-spec.md +31 -0
  132. graphify/skills/claw/references/github-and-merge.md +46 -0
  133. graphify/skills/claw/references/hooks.md +33 -0
  134. graphify/skills/claw/references/query.md +311 -0
  135. graphify/skills/claw/references/transcribe.md +52 -0
  136. graphify/skills/claw/references/update.md +210 -0
  137. graphify/skills/codex/references/add-watch.md +56 -0
  138. graphify/skills/codex/references/exports.md +87 -0
  139. graphify/skills/codex/references/extraction-spec.md +31 -0
  140. graphify/skills/codex/references/github-and-merge.md +46 -0
  141. graphify/skills/codex/references/hooks.md +33 -0
  142. graphify/skills/codex/references/query.md +311 -0
  143. graphify/skills/codex/references/transcribe.md +52 -0
  144. graphify/skills/codex/references/update.md +210 -0
  145. graphify/skills/copilot/references/add-watch.md +56 -0
  146. graphify/skills/copilot/references/exports.md +87 -0
  147. graphify/skills/copilot/references/extraction-spec.md +70 -0
  148. graphify/skills/copilot/references/github-and-merge.md +46 -0
  149. graphify/skills/copilot/references/hooks.md +33 -0
  150. graphify/skills/copilot/references/query.md +311 -0
  151. graphify/skills/copilot/references/transcribe.md +52 -0
  152. graphify/skills/copilot/references/update.md +210 -0
  153. graphify/skills/droid/references/add-watch.md +56 -0
  154. graphify/skills/droid/references/exports.md +87 -0
  155. graphify/skills/droid/references/extraction-spec.md +70 -0
  156. graphify/skills/droid/references/github-and-merge.md +46 -0
  157. graphify/skills/droid/references/hooks.md +33 -0
  158. graphify/skills/droid/references/query.md +311 -0
  159. graphify/skills/droid/references/transcribe.md +52 -0
  160. graphify/skills/droid/references/update.md +210 -0
  161. graphify/skills/kilo/references/add-watch.md +56 -0
  162. graphify/skills/kilo/references/exports.md +87 -0
  163. graphify/skills/kilo/references/extraction-spec.md +70 -0
  164. graphify/skills/kilo/references/github-and-merge.md +46 -0
  165. graphify/skills/kilo/references/hooks.md +33 -0
  166. graphify/skills/kilo/references/query.md +311 -0
  167. graphify/skills/kilo/references/transcribe.md +52 -0
  168. graphify/skills/kilo/references/update.md +210 -0
  169. graphify/skills/kiro/references/add-watch.md +56 -0
  170. graphify/skills/kiro/references/exports.md +87 -0
  171. graphify/skills/kiro/references/extraction-spec.md +31 -0
  172. graphify/skills/kiro/references/github-and-merge.md +46 -0
  173. graphify/skills/kiro/references/hooks.md +33 -0
  174. graphify/skills/kiro/references/query.md +311 -0
  175. graphify/skills/kiro/references/transcribe.md +52 -0
  176. graphify/skills/kiro/references/update.md +210 -0
  177. graphify/skills/opencode/references/add-watch.md +56 -0
  178. graphify/skills/opencode/references/exports.md +87 -0
  179. graphify/skills/opencode/references/extraction-spec.md +70 -0
  180. graphify/skills/opencode/references/github-and-merge.md +46 -0
  181. graphify/skills/opencode/references/hooks.md +33 -0
  182. graphify/skills/opencode/references/query.md +311 -0
  183. graphify/skills/opencode/references/transcribe.md +52 -0
  184. graphify/skills/opencode/references/update.md +210 -0
  185. graphify/skills/pi/references/add-watch.md +56 -0
  186. graphify/skills/pi/references/exports.md +87 -0
  187. graphify/skills/pi/references/extraction-spec.md +31 -0
  188. graphify/skills/pi/references/github-and-merge.md +46 -0
  189. graphify/skills/pi/references/hooks.md +33 -0
  190. graphify/skills/pi/references/query.md +311 -0
  191. graphify/skills/pi/references/transcribe.md +52 -0
  192. graphify/skills/pi/references/update.md +210 -0
  193. graphify/skills/trae/references/add-watch.md +56 -0
  194. graphify/skills/trae/references/exports.md +87 -0
  195. graphify/skills/trae/references/extraction-spec.md +70 -0
  196. graphify/skills/trae/references/github-and-merge.md +46 -0
  197. graphify/skills/trae/references/hooks.md +35 -0
  198. graphify/skills/trae/references/query.md +311 -0
  199. graphify/skills/trae/references/transcribe.md +52 -0
  200. graphify/skills/trae/references/update.md +210 -0
  201. graphify/skills/vscode/references/add-watch.md +56 -0
  202. graphify/skills/vscode/references/exports.md +87 -0
  203. graphify/skills/vscode/references/extraction-spec.md +70 -0
  204. graphify/skills/vscode/references/github-and-merge.md +46 -0
  205. graphify/skills/vscode/references/hooks.md +33 -0
  206. graphify/skills/vscode/references/query.md +311 -0
  207. graphify/skills/vscode/references/transcribe.md +52 -0
  208. graphify/skills/vscode/references/update.md +210 -0
  209. graphify/skills/windows/references/add-watch.md +56 -0
  210. graphify/skills/windows/references/exports.md +87 -0
  211. graphify/skills/windows/references/extraction-spec.md +70 -0
  212. graphify/skills/windows/references/github-and-merge.md +46 -0
  213. graphify/skills/windows/references/hooks.md +33 -0
  214. graphify/skills/windows/references/query.md +311 -0
  215. graphify/skills/windows/references/transcribe.md +52 -0
  216. graphify/skills/windows/references/update.md +210 -0
  217. graphify/symbol_resolution.py +556 -0
  218. graphify/transcribe.py +186 -0
  219. graphify/tree_html.py +603 -0
  220. graphify/validate.py +95 -0
  221. graphify/watch.py +2280 -0
  222. graphify/wiki.py +405 -0
  223. graphitect/__init__.py +28 -0
  224. graphitect/__main__.py +4 -0
  225. graphitect/_vendor/__init__.py +2 -0
  226. graphitect/_vendor/archify/LICENSE +22 -0
  227. graphitect/_vendor/archify/SKILL.md +137 -0
  228. graphitect/_vendor/archify/THIRD_PARTY_NOTICES.md +69 -0
  229. graphitect/_vendor/archify/assets/JetBrainsMono-OFL.txt +93 -0
  230. graphitect/_vendor/archify/assets/template.html +14935 -0
  231. graphitect/_vendor/archify/bin/archify.mjs +2091 -0
  232. graphitect/_vendor/archify/bin/open-artifact.mjs +86 -0
  233. graphitect/_vendor/archify/bin/preview.mjs +653 -0
  234. graphitect/_vendor/archify/bin/visual-check.mjs +829 -0
  235. graphitect/_vendor/archify/brand-marks/README.md +31 -0
  236. graphitect/_vendor/archify/brand-marks/catalog.json +131 -0
  237. graphitect/_vendor/archify/delta/architecture-delta.mjs +1221 -0
  238. graphitect/_vendor/archify/examples/agent-run.lifecycle.json +60 -0
  239. graphitect/_vendor/archify/examples/agent-tool-call.workflow.json +94 -0
  240. graphitect/_vendor/archify/examples/async-job-roundtrip.sequence.json +61 -0
  241. graphitect/_vendor/archify/examples/brand-aware-delivery.architecture.json +47 -0
  242. graphitect/_vendor/archify/examples/cache-miss-request.sequence.json +82 -0
  243. graphitect/_vendor/archify/examples/checkout-platform.base.architecture.json +31 -0
  244. graphitect/_vendor/archify/examples/checkout-platform.head.architecture.json +31 -0
  245. graphitect/_vendor/archify/examples/dataflow-product-analytics.html +15045 -0
  246. graphitect/_vendor/archify/examples/deployment-release.lifecycle.json +49 -0
  247. graphitect/_vendor/archify/examples/event-stream.dataflow.json +57 -0
  248. graphitect/_vendor/archify/examples/incident-response.workflow.json +64 -0
  249. graphitect/_vendor/archify/examples/lifecycle-agent-run.html +14980 -0
  250. graphitect/_vendor/archify/examples/product-analytics.dataflow.json +76 -0
  251. graphitect/_vendor/archify/examples/production-deployment.architecture.json +71 -0
  252. graphitect/_vendor/archify/examples/release-delivery.workflow.json +62 -0
  253. graphitect/_vendor/archify/examples/sequence-cache-miss-request.html +15060 -0
  254. graphitect/_vendor/archify/examples/web-app-rendered.html +15009 -0
  255. graphitect/_vendor/archify/examples/web-app.architecture.json +46 -0
  256. graphitect/_vendor/archify/examples/workflow-agent-tool-call-rendered.html +15051 -0
  257. graphitect/_vendor/archify/migrations/workflow-v2.mjs +279 -0
  258. graphitect/_vendor/archify/package-lock.json +149 -0
  259. graphitect/_vendor/archify/package.json +39 -0
  260. graphitect/_vendor/archify/recipes/scenarios.mjs +391 -0
  261. graphitect/_vendor/archify/references/authoring-contract.md +243 -0
  262. graphitect/_vendor/archify/references/brand-marks.md +65 -0
  263. graphitect/_vendor/archify/references/delivery-contract.md +120 -0
  264. graphitect/_vendor/archify/references/viewer-runtime.md +45 -0
  265. graphitect/_vendor/archify/renderers/architecture/grid.mjs +62 -0
  266. graphitect/_vendor/archify/renderers/architecture/render-architecture.mjs +1078 -0
  267. graphitect/_vendor/archify/renderers/dataflow/README.md +104 -0
  268. graphitect/_vendor/archify/renderers/dataflow/render-dataflow.mjs +483 -0
  269. graphitect/_vendor/archify/renderers/lifecycle/README.md +115 -0
  270. graphitect/_vendor/archify/renderers/lifecycle/render-lifecycle.mjs +561 -0
  271. graphitect/_vendor/archify/renderers/sequence/README.md +114 -0
  272. graphitect/_vendor/archify/renderers/sequence/render-sequence.mjs +464 -0
  273. graphitect/_vendor/archify/renderers/shared/brand-marks.mjs +563 -0
  274. graphitect/_vendor/archify/renderers/shared/cli.mjs +218 -0
  275. graphitect/_vendor/archify/renderers/shared/desktop-readability.mjs +26 -0
  276. graphitect/_vendor/archify/renderers/shared/diagnostics.mjs +127 -0
  277. graphitect/_vendor/archify/renderers/shared/engineering-profiles.mjs +157 -0
  278. graphitect/_vendor/archify/renderers/shared/generated-brand-marks.mjs +2003 -0
  279. graphitect/_vendor/archify/renderers/shared/generated-validators.mjs +13 -0
  280. graphitect/_vendor/archify/renderers/shared/geometry.mjs +1423 -0
  281. graphitect/_vendor/archify/renderers/shared/i18n.mjs +595 -0
  282. graphitect/_vendor/archify/renderers/shared/layout-report.mjs +40 -0
  283. graphitect/_vendor/archify/renderers/shared/legend.mjs +217 -0
  284. graphitect/_vendor/archify/renderers/shared/output-path.mjs +340 -0
  285. graphitect/_vendor/archify/renderers/shared/repository-evidence.mjs +238 -0
  286. graphitect/_vendor/archify/renderers/shared/repository-location.mjs +58 -0
  287. graphitect/_vendor/archify/renderers/shared/text-fit.mjs +49 -0
  288. graphitect/_vendor/archify/renderers/shared/utils.mjs +232 -0
  289. graphitect/_vendor/archify/renderers/shared/validator.mjs +86 -0
  290. graphitect/_vendor/archify/renderers/workflow/README.md +223 -0
  291. graphitect/_vendor/archify/renderers/workflow/render-workflow.mjs +35 -0
  292. graphitect/_vendor/archify/renderers/workflow/workflow-compiler.mjs +4400 -0
  293. graphitect/_vendor/archify/renderers/workflow/workflow-migration-geometry.mjs +144 -0
  294. graphitect/_vendor/archify/schemas/README.md +211 -0
  295. graphitect/_vendor/archify/schemas/architecture.schema.json +178 -0
  296. graphitect/_vendor/archify/schemas/common.schema.json +115 -0
  297. graphitect/_vendor/archify/schemas/dataflow.schema.json +243 -0
  298. graphitect/_vendor/archify/schemas/lifecycle.schema.json +266 -0
  299. graphitect/_vendor/archify/schemas/sequence.schema.json +223 -0
  300. graphitect/_vendor/archify/schemas/workflow.schema.json +428 -0
  301. graphitect/_vendor/archify/scripts/check-render-output.mjs +836 -0
  302. graphitect/_vendor/archify/scripts/check-update.mjs +1667 -0
  303. graphitect/_vendor/archify/scripts/generate-brand-marks.mjs +141 -0
  304. graphitect/_vendor/archify/scripts/generate-validators.mjs +66 -0
  305. graphitect/_vendor/archify/scripts/render-examples.mjs +26 -0
  306. graphitect/_vendor/archify/scripts/update-contract.mjs +182 -0
  307. graphitect/_vendor/archify/skill-release.json +10 -0
  308. graphitect/cli.py +981 -0
  309. graphitect/deliver/__init__.py +5 -0
  310. graphitect/deliver/archify_adapter.py +1877 -0
  311. graphitect/deliver/archify_ir.py +160 -0
  312. graphitect/deliver/archify_repair.py +135 -0
  313. graphitect/deliver/doc_compiler.py +916 -0
  314. graphitect/ground/__init__.py +5 -0
  315. graphitect/ground/describe_source.py +27 -0
  316. graphitect/ground/fullread_source.py +56 -0
  317. graphitect/ground/graphify_source.py +107 -0
  318. graphitect/models.py +118 -0
  319. graphitect/skill/SKILL.md +80 -0
  320. graphitect/skill/agents/openai.yaml +4 -0
  321. graphitect/synthesize/__init__.py +5 -0
  322. graphitect/synthesize/engine.py +281 -0
  323. graphitect/synthesize/llm_backend.py +331 -0
  324. graphitect/synthesize/questions.py +139 -0
  325. graphitect/synthesize/rubric.py +104 -0
  326. graphitect-0.2.0.dist-info/METADATA +284 -0
  327. graphitect-0.2.0.dist-info/RECORD +336 -0
  328. graphitect-0.2.0.dist-info/WHEEL +5 -0
  329. graphitect-0.2.0.dist-info/entry_points.txt +2 -0
  330. graphitect-0.2.0.dist-info/licenses/LICENSE +21 -0
  331. graphitect-0.2.0.dist-info/licenses/LICENSE-ARCHIFY-MIT +22 -0
  332. graphitect-0.2.0.dist-info/licenses/LICENSE-GRAPHIFY-APACHE-2.0 +202 -0
  333. graphitect-0.2.0.dist-info/licenses/LICENSE-GRAPHIFY-MIT +21 -0
  334. graphitect-0.2.0.dist-info/licenses/NOTICE-ARCHIFY-THIRD-PARTY.md +69 -0
  335. graphitect-0.2.0.dist-info/licenses/NOTICE-GRAPHIFY +8 -0
  336. graphitect-0.2.0.dist-info/top_level.txt +2 -0
@@ -0,0 +1,406 @@
1
+ """Read-only diagnostics for MultiDiGraph readiness."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import re
7
+ from collections import Counter, defaultdict
8
+ from copy import deepcopy
9
+ from pathlib import Path
10
+ from typing import Any
11
+
12
+ import networkx as nx
13
+
14
+
15
+ _SUPPRESSION_DECL_RE = re.compile(r"^\s*(?P<name>seen_[A-Za-z0-9_]+)\s*[:=]")
16
+ _TYPE_TUPLE_RE = re.compile(r"set\[tuple\[(?P<inside>[^\]]+)\]\]")
17
+
18
+
19
+ def _safe_text(value: Any) -> str:
20
+ if value is None:
21
+ return ""
22
+ if isinstance(value, (str, int, float, bool)):
23
+ return str(value)
24
+ return json.dumps(value, sort_keys=True, default=str, ensure_ascii=False)
25
+
26
+
27
+ def _edge_list(extraction: dict[str, Any]) -> list[Any]:
28
+ edges = extraction.get("edges")
29
+ if edges is None:
30
+ edges = extraction.get("links")
31
+ return edges if isinstance(edges, list) else []
32
+
33
+
34
+ def _node_ids(extraction: dict[str, Any]) -> set[str]:
35
+ nodes = extraction.get("nodes", [])
36
+ if not isinstance(nodes, list):
37
+ return set()
38
+ return {
39
+ str(node["id"])
40
+ for node in nodes
41
+ if isinstance(node, dict) and "id" in node and node.get("id") is not None
42
+ }
43
+
44
+
45
+ def _canonical_edge(edge: Any) -> dict[str, str]:
46
+ if not isinstance(edge, dict):
47
+ return {
48
+ "source": "",
49
+ "target": "",
50
+ "relation": "",
51
+ "confidence": "",
52
+ "source_file": "",
53
+ "source_location": "",
54
+ "context": "",
55
+ "_invalid": "non_object_edge",
56
+ }
57
+ source = edge.get("source", edge.get("from"))
58
+ target = edge.get("target", edge.get("to"))
59
+ return {
60
+ "source": _safe_text(source),
61
+ "target": _safe_text(target),
62
+ "relation": _safe_text(edge.get("relation")),
63
+ "confidence": _safe_text(edge.get("confidence")),
64
+ "source_file": _safe_text(edge.get("source_file")),
65
+ "source_location": _safe_text(edge.get("source_location")),
66
+ "context": _safe_text(edge.get("context")),
67
+ "_invalid": "",
68
+ }
69
+
70
+
71
+ def _exact_signature(edge: Any) -> str:
72
+ if not isinstance(edge, dict):
73
+ return "<non-object>"
74
+ normalized = dict(edge)
75
+ if "source" not in normalized and "from" in normalized:
76
+ normalized["source"] = normalized["from"]
77
+ if "target" not in normalized and "to" in normalized:
78
+ normalized["target"] = normalized["to"]
79
+ normalized.pop("from", None)
80
+ normalized.pop("to", None)
81
+ return json.dumps(
82
+ normalized,
83
+ sort_keys=True,
84
+ default=str,
85
+ ensure_ascii=False,
86
+ separators=(",", ":"),
87
+ )
88
+
89
+
90
+ def _count_extra(counter: Counter[Any]) -> int:
91
+ return sum(count - 1 for count in counter.values() if count > 1)
92
+
93
+
94
+ def _variant_group_count(
95
+ grouped_edges: dict[tuple[str, str], list[dict[str, str]]],
96
+ field: str,
97
+ *,
98
+ relation_sensitive: bool = False,
99
+ ) -> int:
100
+ groups = 0
101
+ for edges in grouped_edges.values():
102
+ if relation_sensitive:
103
+ by_relation: dict[str, set[str]] = defaultdict(set)
104
+ for edge in edges:
105
+ by_relation[edge["relation"]].add(edge[field])
106
+ groups += sum(1 for values in by_relation.values() if len(values) > 1)
107
+ elif len({edge[field] for edge in edges}) > 1:
108
+ groups += 1
109
+ return groups
110
+
111
+
112
+ def _tuple_arity_from_annotation(line: str) -> int:
113
+ match = _TYPE_TUPLE_RE.search(line)
114
+ if not match:
115
+ return 0
116
+ inside = match.group("inside").strip()
117
+ if not inside:
118
+ return 0
119
+ return inside.count(",") + 1
120
+
121
+
122
+ def scan_producer_suppression_sites(path: str | Path) -> dict[str, Any]:
123
+ """Find likely `seen_*` producer-suppression sets in an extractor file."""
124
+ source_path = Path(path)
125
+ if not source_path.exists():
126
+ return {
127
+ "path": str(source_path),
128
+ "total_sites": 0,
129
+ "sites": [],
130
+ "error": "file not found",
131
+ }
132
+
133
+ sites: list[dict[str, Any]] = []
134
+ lines = source_path.read_text(encoding="utf-8").splitlines()
135
+ for lineno, line in enumerate(lines, start=1):
136
+ match = _SUPPRESSION_DECL_RE.match(line)
137
+ if not match:
138
+ continue
139
+ sites.append(
140
+ {
141
+ "line": lineno,
142
+ "name": match.group("name"),
143
+ "tuple_arity": _tuple_arity_from_annotation(line),
144
+ "sample": line.strip()[:120],
145
+ }
146
+ )
147
+
148
+ return {
149
+ "path": str(source_path),
150
+ "total_sites": len(sites),
151
+ "sites": sites,
152
+ "error": "",
153
+ }
154
+
155
+
156
+ def diagnose_extraction(
157
+ extraction: dict[str, Any],
158
+ *,
159
+ directed: bool = True,
160
+ root: str | Path | None = None,
161
+ max_examples: int = 5,
162
+ extract_path: str | Path | None = None,
163
+ ) -> dict[str, Any]:
164
+ """Summarize same-endpoint edge-collapse risk for one JSON graph/extraction dict."""
165
+ from graphify.build import build_from_json
166
+
167
+ node_ids = _node_ids(extraction)
168
+ raw_edges = _edge_list(extraction)
169
+ canonical_edges = [_canonical_edge(edge) for edge in raw_edges]
170
+
171
+ # Code-typed semantic nodes the extractor could not verify against the source
172
+ # it read (#1949): likely-inferred (or hallucinated) symbols surfaced from a
173
+ # document. Count them so the flag on graph.json nodes is actually surfaced.
174
+ unverified_node_count = sum(
175
+ 1 for n in extraction.get("nodes", [])
176
+ if isinstance(n, dict) and n.get("verification") == "unverified"
177
+ )
178
+
179
+ exact_counts: Counter[str] = Counter(_exact_signature(edge) for edge in raw_edges)
180
+ directed_pairs: Counter[tuple[str, str]] = Counter()
181
+ undirected_pairs: Counter[tuple[str, str]] = Counter()
182
+ grouped: dict[tuple[str, str], list[dict[str, str]]] = defaultdict(list)
183
+
184
+ non_object_edges = 0
185
+ missing_endpoint_edges = 0
186
+ dangling_endpoint_edges = 0
187
+ self_loop_edges = 0
188
+ valid_candidate_edges = 0
189
+
190
+ for edge in canonical_edges:
191
+ if edge["_invalid"]:
192
+ non_object_edges += 1
193
+ continue
194
+ source = edge["source"]
195
+ target = edge["target"]
196
+ if not source or not target:
197
+ missing_endpoint_edges += 1
198
+ continue
199
+ if source not in node_ids or target not in node_ids:
200
+ dangling_endpoint_edges += 1
201
+ continue
202
+ if source == target:
203
+ self_loop_edges += 1
204
+ valid_candidate_edges += 1
205
+ directed_pair = (source, target)
206
+ undirected_pair = (source, target) if source <= target else (target, source)
207
+ directed_pairs[directed_pair] += 1
208
+ undirected_pairs[undirected_pair] += 1
209
+ grouped[directed_pair].append(edge)
210
+
211
+ examples: list[dict[str, Any]] = []
212
+ if max_examples > 0:
213
+ for (source, target), count in directed_pairs.most_common():
214
+ if count < 2:
215
+ continue
216
+ edges = grouped[(source, target)]
217
+ examples.append(
218
+ {
219
+ "source": source,
220
+ "target": target,
221
+ "edge_count": count,
222
+ "relations": sorted({edge["relation"] for edge in edges}),
223
+ "source_files": sorted({edge["source_file"] for edge in edges}),
224
+ "source_locations": sorted({edge["source_location"] for edge in edges}),
225
+ "contexts": sorted({edge["context"] for edge in edges}),
226
+ }
227
+ )
228
+ if len(examples) >= max_examples:
229
+ break
230
+
231
+ build_error = ""
232
+ graph_type = ""
233
+ post_build_edge_count: int | None = None
234
+ post_build_node_count: int | None = None
235
+ try:
236
+ graph_input = deepcopy(extraction)
237
+ graph: nx.Graph = build_from_json(graph_input, directed=directed, root=root)
238
+ graph_type = type(graph).__name__
239
+ post_build_edge_count = graph.number_of_edges()
240
+ post_build_node_count = graph.number_of_nodes()
241
+ except Exception as exc:
242
+ build_error = f"{type(exc).__name__}: {exc}"
243
+
244
+ suppression_path = (
245
+ Path(extract_path) if extract_path else Path(__file__).with_name("extract.py")
246
+ )
247
+
248
+ return {
249
+ "node_count": len(node_ids),
250
+ "unverified_node_count": unverified_node_count,
251
+ "raw_edge_count": len(raw_edges),
252
+ "non_object_edges": non_object_edges,
253
+ "missing_endpoint_edges": missing_endpoint_edges,
254
+ "dangling_endpoint_edges": dangling_endpoint_edges,
255
+ "self_loop_edges": self_loop_edges,
256
+ "valid_candidate_edges": valid_candidate_edges,
257
+ "exact_duplicate_edges": _count_extra(exact_counts),
258
+ "directed_unique_endpoint_pairs": len(directed_pairs),
259
+ "directed_same_endpoint_collapsed_edges": _count_extra(directed_pairs),
260
+ "undirected_unique_endpoint_pairs": len(undirected_pairs),
261
+ "undirected_same_endpoint_collapsed_edges": _count_extra(undirected_pairs),
262
+ "same_endpoint_group_count": sum(1 for count in directed_pairs.values() if count > 1),
263
+ "relation_variant_groups": _variant_group_count(grouped, "relation"),
264
+ "source_file_variant_groups": _variant_group_count(
265
+ grouped, "source_file", relation_sensitive=True
266
+ ),
267
+ "source_location_variant_groups": _variant_group_count(
268
+ grouped, "source_location", relation_sensitive=True
269
+ ),
270
+ "context_variant_groups": _variant_group_count(grouped, "context", relation_sensitive=True),
271
+ "post_build_graph_type": graph_type,
272
+ "post_build_node_count": post_build_node_count,
273
+ "post_build_edge_count": post_build_edge_count,
274
+ "post_build_error": build_error,
275
+ "producer_suppression": scan_producer_suppression_sites(suppression_path),
276
+ "examples": examples,
277
+ }
278
+
279
+
280
+ def _read_json_file(path: str | Path) -> dict[str, Any]:
281
+ """Read a JSON graph after applying Graphify's graph-load size cap."""
282
+ from graphify.security import check_graph_file_size_cap
283
+
284
+ json_path = Path(path)
285
+ check_graph_file_size_cap(json_path)
286
+ try:
287
+ data = json.loads(json_path.read_text(encoding="utf-8"))
288
+ except (json.JSONDecodeError, OSError) as exc:
289
+ raise RuntimeError(
290
+ f"Cannot parse {json_path}: {exc}. "
291
+ "The file may be corrupted — re-run 'graphify extract'."
292
+ ) from exc
293
+ if not isinstance(data, dict):
294
+ raise ValueError("diagnostic input must be a JSON object")
295
+ return data
296
+
297
+
298
+ def diagnose_file(
299
+ path: str | Path,
300
+ *,
301
+ directed: bool | None = None,
302
+ root: str | Path | None = None,
303
+ max_examples: int = 5,
304
+ extract_path: str | Path | None = None,
305
+ ) -> dict[str, Any]:
306
+ """Diagnose a graph/extraction JSON file without mutating it.
307
+
308
+ When `directed` is None, the JSON's "directed" flag is honored. Raw
309
+ extraction JSON that has no "directed" flag defaults to directed analysis.
310
+ """
311
+ data = _read_json_file(path)
312
+ if directed is None:
313
+ raw_directed = data.get("directed")
314
+ effective_directed = raw_directed if isinstance(raw_directed, bool) else True
315
+ else:
316
+ effective_directed = directed
317
+
318
+ summary = diagnose_extraction(
319
+ data,
320
+ directed=effective_directed,
321
+ root=root,
322
+ max_examples=max_examples,
323
+ extract_path=extract_path,
324
+ )
325
+ summary["input_path"] = str(path)
326
+ summary["effective_directed"] = effective_directed
327
+ return summary
328
+
329
+
330
+ def format_diagnostic_json(summary: dict[str, Any]) -> dict[str, Any]:
331
+ return {
332
+ "schema_version": 1,
333
+ "summary": {
334
+ key: value
335
+ for key, value in summary.items()
336
+ if key not in {"examples", "producer_suppression"}
337
+ },
338
+ "examples": summary.get("examples", []),
339
+ "producer_suppression": summary.get("producer_suppression", {}),
340
+ "notes": [
341
+ "Diagnostics are read-only.",
342
+ "A normal graph.json is already post-build and cannot recover raw producer edges.",
343
+ "Producer suppression sites are heuristic source-code evidence.",
344
+ ],
345
+ }
346
+
347
+
348
+ def format_diagnostic_report(summary: dict[str, Any]) -> str:
349
+ suppression = summary.get("producer_suppression", {})
350
+ lines = [
351
+ "[graphify] MultiDiGraph edge-collapse diagnostic",
352
+ f"input: {summary.get('input_path', '<in-memory>')}",
353
+ "input_stage: provided JSON (normal graph.json is post-build)",
354
+ f"effective_directed: {summary.get('effective_directed', '<direct-call>')}",
355
+ f"nodes: {summary['node_count']}",
356
+ f"unverified_code_nodes: {summary.get('unverified_node_count', 0)}",
357
+ f"raw_edges: {summary['raw_edge_count']}",
358
+ f"valid_candidate_edges: {summary['valid_candidate_edges']}",
359
+ f"missing_endpoint_edges: {summary['missing_endpoint_edges']}",
360
+ f"dangling_endpoint_edges: {summary['dangling_endpoint_edges']}",
361
+ f"self_loop_edges: {summary['self_loop_edges']}",
362
+ f"exact_duplicate_edges: {summary['exact_duplicate_edges']}",
363
+ f"directed_unique_endpoint_pairs: {summary['directed_unique_endpoint_pairs']}",
364
+ (
365
+ "directed_same_endpoint_collapsed_edges: "
366
+ f"{summary['directed_same_endpoint_collapsed_edges']}"
367
+ ),
368
+ f"undirected_unique_endpoint_pairs: {summary['undirected_unique_endpoint_pairs']}",
369
+ (
370
+ "undirected_same_endpoint_collapsed_edges: "
371
+ f"{summary['undirected_same_endpoint_collapsed_edges']}"
372
+ ),
373
+ f"same_endpoint_group_count: {summary['same_endpoint_group_count']}",
374
+ f"relation_variant_groups: {summary['relation_variant_groups']}",
375
+ f"source_file_variant_groups: {summary['source_file_variant_groups']}",
376
+ f"source_location_variant_groups: {summary['source_location_variant_groups']}",
377
+ f"context_variant_groups: {summary['context_variant_groups']}",
378
+ f"post_build_graph_type: {summary['post_build_graph_type']}",
379
+ f"post_build_edges: {summary['post_build_edge_count']}",
380
+ f"producer_suppression_sites: {suppression.get('total_sites', 0)}",
381
+ ]
382
+ if summary.get("post_build_error"):
383
+ lines.append(f"post_build_error: {summary['post_build_error']}")
384
+ if suppression.get("error"):
385
+ lines.append(f"producer_suppression_error: {suppression['error']}")
386
+ if suppression.get("sites"):
387
+ lines.append("producer_suppression_examples:")
388
+ for site in suppression["sites"][:8]:
389
+ lines.append(
390
+ f" - L{site['line']} {site['name']} arity={site['tuple_arity'] or 'unknown'}"
391
+ )
392
+ if summary.get("examples"):
393
+ lines.append("examples:")
394
+ for example in summary["examples"]:
395
+ lines.append(
396
+ " - "
397
+ f"{example['source']} -> {example['target']} "
398
+ f"edges={example['edge_count']} "
399
+ f"relations={example['relations']} "
400
+ f"locations={example['source_locations']} "
401
+ f"contexts={example['contexts']}"
402
+ )
403
+ lines.append(
404
+ "note: normal graph.json is post-build; raw producer loss must be measured earlier."
405
+ )
406
+ return "\n".join(lines)