@@ -1638,6 +1638,12 @@ def dispatch_command(cmd: str) -> None:
16381638 co_exclude_hubs : float | None = None
16391639 label_max_concurrency : int = 4
16401640 label_batch_size : int = 100
1641+ # #2534: defaults make presence undetectable for these, so track whether
1642+ # the user actually passed them — the label-reuse branch below warns when
1643+ # labeling flags are silently ignored. (--backend/--model default to None,
1644+ # so `is not None` already means "explicitly passed" for those.)
1645+ label_max_concurrency_explicit = False
1646+ label_batch_size_explicit = False
16411647 i_arg = 0
16421648 while i_arg < len (args ):
16431649 a = args [i_arg ]
@@ -1660,13 +1666,13 @@ def dispatch_command(cmd: str) -> None:
16601666 elif a .startswith ("--exclude-hubs=" ):
16611667 co_exclude_hubs = float (a .split ("=" , 1 )[1 ]); i_arg += 1
16621668 elif a == "--max-concurrency" and i_arg + 1 < len (args ):
1663- label_max_concurrency = int (args [i_arg + 1 ]); i_arg += 2
1669+ label_max_concurrency = int (args [i_arg + 1 ]); label_max_concurrency_explicit = True ; i_arg += 2
16641670 elif a .startswith ("--max-concurrency=" ):
1665- label_max_concurrency = int (a .split ("=" , 1 )[1 ]); i_arg += 1
1671+ label_max_concurrency = int (a .split ("=" , 1 )[1 ]); label_max_concurrency_explicit = True ; i_arg += 1
16661672 elif a == "--batch-size" and i_arg + 1 < len (args ):
1667- label_batch_size = int (args [i_arg + 1 ]); i_arg += 2
1673+ label_batch_size = int (args [i_arg + 1 ]); label_batch_size_explicit = True ; i_arg += 2
16681674 elif a .startswith ("--batch-size=" ):
1669- label_batch_size = int (a .split ("=" , 1 )[1 ]); i_arg += 1
1675+ label_batch_size = int (a .split ("=" , 1 )[1 ]); label_batch_size_explicit = True ; i_arg += 1
16701676 elif a in ("--no-viz" , "--missing-only" ) or a .startswith ("--min-community-size=" ):
16711677 i_arg += 1
16721678 elif a .startswith ("--" ):
@@ -1774,6 +1780,22 @@ def dispatch_command(cmd: str) -> None:
17741780 # as fresh forever, permanently blocking real labeling on later runs.
17751781 placeholder_only = False
17761782 if labels_path .exists () and not force_relabel :
1783+ # #2534: this branch never calls the LLM, so labeling flags would be
1784+ # silently ignored. Reuse is still the correct (exit 0) outcome — but
1785+ # say so, instead of letting `--backend openai` look like it relabeled.
1786+ _ignored_label_flags = [flag for flag , given in (
1787+ ("--backend" , label_backend is not None ),
1788+ ("--model" , label_model is not None ),
1789+ ("--batch-size" , label_batch_size_explicit ),
1790+ ("--max-concurrency" , label_max_concurrency_explicit ),
1791+ ) if given ]
1792+ if _ignored_label_flags :
1793+ print (
1794+ f"[graphify] warning: { '/' .join (_ignored_label_flags )} ignored: "
1795+ f"reusing saved labels at { labels_path } . "
1796+ f"Run `graphify label` to relabel, or delete the labels file." ,
1797+ file = sys .stderr ,
1798+ )
17771799 # Reuse saved labels, but don't blindly trust them: the graph may have
17781800 # been re-scoped/re-clustered since labeling, in which case a cid now
17791801 # covers a DIFFERENT community and its old (LLM) name is wrong (#label-stale).
@@ -1868,16 +1890,41 @@ def dispatch_command(cmd: str) -> None:
18681890 )
18691891 # Only let the LLM OVERRIDE where it produced a real name — its no-backend
18701892 # fallback returns "Community {cid}" placeholders, which must not clobber
1871- # the deterministic hub labels.
1893+ # the deterministic hub labels. Also reject a model echoing the prompt
1894+ # key back ("5" for community 5) — that is an id, not a name (#2534).
18721895 labels .update ({
18731896 cid : v for cid , v in generated_labels .items ()
1874- if v and v != f"Community { cid } "
1897+ if v and v != f"Community { cid } " and v != str ( cid )
18751898 })
18761899 stages .mark ("label" )
18771900 questions = suggest_questions (G , communities , labels )
1901+ # cluster-only re-clusters an EXISTING graph: the code content is exactly
1902+ # what extract saw, so keep the extract-time commit stamp instead of
1903+ # re-deriving it from the shell's cwd — running from another repo used to
1904+ # re-stamp graph.json with THAT repo's HEAD (#2534). When the loaded
1905+ # graph predates the stamp, fall back to the analysed repo's HEAD via
1906+ # the cwd-aware helper (#2316).
1907+ _commit = _raw .get ("built_at_commit" )
1908+ if not _commit :
1909+ from graphify .watch import _git_head as _gh
1910+ _commit = _gh (cwd = watch_path )
1911+ # Snapshot BEFORE any artifact is replaced: GRAPH_REPORT.md was written
1912+ # first, so the dated folder held the NEW report, not the previous (#2402).
1913+ from graphify .export import backup_if_protected as _backup
1914+ _backup (out )
1915+ # The #479 guard can refuse this write, so it goes before the sidecars —
1916+ # a report and labels describing a clustering graph.json does not contain
1917+ # are worse than no run at all (#2436).
1918+ if not to_json (G , communities , str (out / "graph.json" ),
1919+ community_labels = labels , built_at_commit = _commit ):
1920+ print (
1921+ "graph.json NOT written: refusing to overwrite (see warning above). "
1922+ "GRAPH_REPORT.md, .graphify_labels.json and .graphify_analysis.json "
1923+ "left untouched." ,
1924+ file = sys .stderr ,
1925+ )
1926+ sys .exit (1 )
18781927 tokens = label_token_usage
1879- from graphify .export import _git_head as _gh
1880- _commit = _gh ()
18811928 from graphify .report import load_learning_for_report as _llfr
18821929 report = generate (G , communities , cohesion , labels , gods , surprises ,
18831930 {"warning" : "cluster-only mode — file stats not available" },
@@ -1886,8 +1933,6 @@ def dispatch_command(cmd: str) -> None:
18861933 learning = _llfr (out / "graph.json" ))
18871934 (out / "GRAPH_REPORT.md" ).write_text (report , encoding = "utf-8" )
18881935 stages .mark ("report" )
1889- from graphify .export import backup_if_protected as _backup
1890- _backup (out )
18911936 analysis = {
18921937 "communities" : {str (k ): v for k , v in communities .items ()},
18931938 "cohesion" : {str (k ): v for k , v in cohesion .items ()},
@@ -1899,7 +1944,6 @@ def dispatch_command(cmd: str) -> None:
18991944 json .dumps (analysis , indent = 2 , ensure_ascii = False ),
19001945 encoding = "utf-8" ,
19011946 )
1902- to_json (G , communities , str (out / "graph.json" ), community_labels = labels )
19031947 # Don't persist placeholder-only labels (or their .sig): leaving the
19041948 # sidecar absent lets a later run generate real labels instead of reading
19051949 # back "Community N" as authoritative (#2073).
@@ -2067,11 +2111,17 @@ def dispatch_command(cmd: str) -> None:
20672111 _enforce_graph_size_cap_or_exit (graph_path )
20682112 if output_path is None :
20692113 output_path = graph_path .parent / "GRAPH_TREE.html"
2070- out = write_tree_html (
2071- graph_path = graph_path , output_path = output_path ,
2072- root = root , max_children = max_children ,
2073- top_k_edges = top_k_edges , project_label = project_label ,
2074- )
2114+ try :
2115+ out = write_tree_html (
2116+ graph_path = graph_path , output_path = output_path ,
2117+ root = root , max_children = max_children ,
2118+ top_k_edges = top_k_edges , project_label = project_label ,
2119+ )
2120+ except ValueError as exc :
2121+ # e.g. an explicit --root that matches no source_file — the tree
2122+ # would silently flatten, so fail loudly instead (#2534).
2123+ print (f"error: { exc } " , file = sys .stderr )
2124+ sys .exit (1 )
20752125 size_kb = out .stat ().st_size / 1024
20762126 print (f"wrote { out } ({ size_kb :.1f} KB)" )
20772127 print (f"open with: xdg-open { out } (or file://{ out .resolve ()} )" )
0 commit comments