zettelkasten.spine¶
zettelkasten.spine ¶
Shared spine materializer — the single source of truth for spine scaffolds.
A spine is a materialized synthesis structure living in a dedicated graph: an apex node for the structure itself, one dimension node per column/facet rolling up into the apex, optional hub nodes per row entity, and member attach edges wiring the corpus notes into that structure.
Three independent code paths used to stamp (or read) this scaffold with hard-coded tag/relation conventions and would silently drift:
coordinator.extraction.prep_spine— builds an empty spine skeleton from a schema'ssynthesisblock for a grounded-extraction run.zettelkasten.organizations.promote_organization— materializes an existing matrix lens (cols + rows) and bulk-attaches its current members.zettelkasten.server.discover_spines— reads spines back by those exact tag conventions.
This module factors the conventions (APEX_TAGS / DIMENSION_TAG /
HUB_TAGS / SPEC_TAG) and the node/edge stamping (build_spine_skeleton) into
one place so the three can never drift. The graph mutations themselves are
abstracted behind a tiny ops protocol with two backends:
- :class:
ServerSpineGraphOps— writes through the zettelkasten MCP server primitives (used byprep_spineso it keeps its warm-cache behavior). - :class:
SpineGraphOps— writes directly through :class:ZettelGraphbound to agraphs_dir(used by promotion + the dashboard + tests), so the whole flow is exercisable against a synthetic tree with no server process.
SpineError ¶
ServerSpineGraphOps ¶
Materializer primitives backed by the zettelkasten MCP server functions.
Used by prep_spine so it keeps writing through the same server
primitives (and the warm graph cache) it always has. The server functions are
imported lazily on each call so test monkeypatches on zettelkasten.server
are honored.
Source code in zettelkasten/spine.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
ensure_structure_node ¶
ensure_structure_node(graph: str, node_id: str, title: str, ntype: str, body: str, tags, *, synthesis_status: str = '', spine_title: str = '') -> str
Create/find a structure (tree) node — server backend.
The server add_note primitive cannot stamp an arbitrary source, so
a structure node materialized through this backend keeps the legacy
find-by-title identity (node_id is accepted for a uniform
:func:build_spine_skeleton contract but not persisted). That is correct for the
extraction path: a grounded-extraction spine carries no overlay corrections,
so the durable-id payoff (relabel survival of corrections) is unneeded there.
spine_title is likewise accepted for signature parity with
:meth:SpineGraphOps.ensure_structure_node (the legacy-composite tier-2
fallback) but unused here, as this backend does not persist durable ids.
Source code in zettelkasten/spine.py
node_links ¶
The outgoing links of node_id in graph (empty if absent).
Reads through the server's warm graph cache so it observes edges this backend just wrote (used by the sub-spine seed's stability check).
Source code in zettelkasten/spine.py
remove_link ¶
remove_link(graph: str, source_id: str, target_id: str, relation: str, *, target_graph: str = '') -> bool
Drop a specific outgoing source --relation--> target edge — server backend.
Mirrors :meth:SpineGraphOps.remove_link (the direct-IO backend) so nested
connect/disconnect is SYMMETRIC across both backends: the sub-spine
disconnect / delete-reparent path works no matter which ops backend it runs
through. Matches on the SAME (target, relation, target_graph) tuple that
:meth:ensure_link dedupes on, so it removes exactly the edge a prior ensure
created and never over-removes a sibling edge sharing the source+relation but
pointing at a different (target, graph). Returns True iff an edge was
removed. The write goes through the server's warm graph cache (the same
handle :meth:node_links / :meth:ensure_apex_node read/write through) so a
removal this backend makes is immediately observed by subsequent reads.
Source code in zettelkasten/spine.py
apex_node_ids ¶
Ids of every APEX_TAGS node in graph (the single-apex guard).
Reads through the server's warm graph cache so it observes an apex this backend just created. A well-formed spine has exactly one.
Source code in zettelkasten/spine.py
find_node_by_stable_id ¶
Id of the node carrying node_id in source (server backend), or "".
Reads through the server's warm graph cache so it observes a node this
backend just created / stamped. Mirrors
:meth:SpineGraphOps.find_node_by_stable_id — used to resolve the durable
apex sentinel on the server-backed build path.
Source code in zettelkasten/spine.py
ensure_apex_node ¶
ensure_apex_node(graph: str, node_id: str, title: str, ntype: str, body: str, tags, *, synthesis_status: str = '') -> str
Create/find the spine APEX keyed by the durable node_id — server backend.
The server add_note primitive cannot stamp an arbitrary source, so
the durable apex sentinel (APEX_NODE_ID) is applied by a follow-up write
through the warm graph cache. This mirrors
:meth:SpineGraphOps.ensure_apex_node's three-tier identity so a build-path
(:func:coordinator.extraction.prep_spine) title drift re-titles the SAME
apex node instead of minting a SECOND APEX_TAGS node — which would both
trip the exactly-one-apex guard AND orphan the primary sub-spine edge stored
on the original apex. Tiers:
- Find by the durable
source['node_id'](the apex sentinel); if the title drifted, re-title the SAME node (its id + every edge preserved). - Else find the existing
APEX_TAGSapex and STAMP the sentinel onto it in place (the one-time forward migration for a legacy title-keyed apex); never clobber a foreign durable id. - Else create a fresh apex (via
add_note) and stamp the sentinel on it.
Passing an empty node_id degrades to plain find-by-title creation
(:meth:ensure_node), matching the fallback the shared builder used before.
Source code in zettelkasten/spine.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
SpineGraphOps ¶
Materializer primitives backed by direct :class:ZettelGraph IO.
Graphs are loaded once via get_graph and reused within one operation, so
node/edge writes stay coherent in memory and on disk. Used by promotion (the
dashboard route + tests), where the whole flow operates against a single
graphs_dir and needs no server process.
Source code in zettelkasten/spine.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 | |
find_node_by_stable_id ¶
The id of the structure node carrying node_id in source, or "".
Structure (tree) nodes are keyed by their DURABLE source['node_id'] (§11.3
of the spine-promotion design) so a relabel re-titles the SAME node instead
of orphaning it — the structural analogue of :meth:find_hub_by_row for
row hubs.
Source code in zettelkasten/spine.py
ensure_structure_node ¶
ensure_structure_node(graph: str, node_id: str, title: str, ntype: str, body: str, tags, *, synthesis_status: str = '', spine_title: str = '') -> str
Idempotently create/find a structure (tree) node keyed by stable node_id.
Identity is resolved in three tiers so the durable id survives a relabel AND a legacy (node-id-less) spine migrates forward on its next promote:
- Find by the stable
source['node_id']. If the title drifted (a column relabel), re-title the SAME node — its graph id (and thus everyspine-memberedge +column['apex']reference) is preserved. - Else find by title (the legacy find-by-title dedup
build_spine_skeletonalways used) and STAMPnode_idonto it, so a spine materialized before V2b acquires durable ids the first time it is re-promoted. Two exact title formats are tried, in order: the current BAREtitle(e.g."Dataset"), then — whenspine_titleis supplied — the OLD COMPOSITE format a pre-bare-label spine minted the node under,f"{spine_title} — {title}"(space, em-dash, space; the exact stringorganizations.pyused before dimension titles were changed to the bare column label). A legacy composite-title node is thus still matched on its first re-promote after that change; it is then re-titled IN PLACE to the baretitleand stamped with the durablenode_id, so itsspine-memberedges + overlay corrections are preserved rather than orphaned by a fresh mint. Only these two EXACT titles are tried — no positional or fuzzy guessing. HARDENED: tier 2 migrates ONLY a GENUINE legacy DIMENSION node — one taggedDIMENSION_TAG, with no durable id yet and no conflicting identity. It REFUSES to stampnode_idonto ANY non-dimension same-title collision: a row hub (source['row_id']or aHUB_TAGStag), the apex or a spec (APEX_TAGS/SPEC_TAG), a plain note (no dimension tag), or a node already carrying a DIFFERENTnode_id. A title collision (e.g. a dimension whose label equals the apex title, a row-hub label, or a stray note) must never let a dimension hijack a foreign node's identity — doing so wired the apexcomponent-ofitself and silently dropped a member under the apex-exclusion rollup. Any such foreign collision falls through to tier 3. - Else create a fresh node carrying
source['node_id'].
Passing an empty node_id degrades to plain find-by-title creation
(no durable id), matching :meth:ensure_node. spine_title is used
ONLY for the tier-2 legacy-composite fallback and only when a durable
node_id is requested, so the degraded and greenfield paths are
byte-identical to before.
Source code in zettelkasten/spine.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 | |
apex_node_ids ¶
Ids of every APEX_TAGS node in graph (the single-apex guard).
A well-formed spine has exactly one; a second is the corruption
:func:build_spine_skeleton's guard fails loud on.
Source code in zettelkasten/spine.py
ensure_apex_node ¶
ensure_apex_node(graph: str, node_id: str, title: str, ntype: str, body: str, tags, *, synthesis_status: str = '') -> str
Idempotently create/find the spine APEX, keyed by the durable node_id.
The apex is the ROOT structure node, so — like the dimensions — it must
survive a relabel / org-rename / title_template drift by re-titling the
SAME node rather than minting a second APEX_TAGS node (which would
orphan the primary sub-spine edge stored on the original apex). Identity is
resolved in three tiers:
- Find by the durable
source['node_id'](the apex sentinel). If the title drifted, re-title the SAME node — its graph id (and every edge on it, including the sub-spinecomponent-of) is preserved. - Else find the existing apex by its
APEX_TAGS(a spine has exactly one apex, so this is unambiguous) and STAMP the sentinelnode_idonto it IN PLACE. This is the one-time, byte-safe forward migration for a legacy spine whose apex predates the durable id — it NEVER mints a duplicate. - Else create a fresh apex carrying the sentinel
source['node_id'].
Unlike :meth:ensure_structure_node (which REFUSES to migrate an
APEX_TAGS node so a dimension can never hijack the apex's identity),
this method is the apex's OWN builder: migrating the apex in place is
exactly the intent. Passing an empty node_id degrades to find-by-title
creation, matching :meth:ensure_node.
Source code in zettelkasten/spine.py
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 | |
find_hub_by_row ¶
The id of the hub node carrying row_id in note.source, or "".
Hubs are keyed by the row's STABLE id (not its label) so two rows sharing a label never collapse into one hub and a label change re-titles the same hub instead of orphaning it.
Source code in zettelkasten/spine.py
ensure_hub ¶
Idempotently create/update a row hub keyed by stable row_id.
Stores the original row_id on the hub's source so the link-form
rebuild recovers it (overlay corrections key on it). Updates the title to
label if it changed, so re-promoting after a row relabel re-titles the
existing hub rather than orphaning it.
Source code in zettelkasten/spine.py
set_hub_basis ¶
Persist a hub's MEMBER BASIS (the <home>::<note_id> uid set) on its
source['member_basis'].
The basis is the last-known membership the overlap remap falls back to when a hub has zero LIVE member edges (every member was overlay-removed): without it a regrown semantic group could not be matched to its existing hub and would mint a duplicate row. Stored spine-side on the hub node, so base notes stay pristine.
Source code in zettelkasten/spine.py
structural_node_ids ¶
Ids of every node in graph carrying ANY of tags (hub/dim scan).
Source code in zettelkasten/spine.py
node_row_id ¶
The stable source['row_id'] a node carries (hubs only), or "".
Read BEFORE :meth:delete_node so a caller can tell which overlay
corrections a soon-to-be-pruned hub was carrying (orphan detection).
Source code in zettelkasten/spine.py
delete_node ¶
Remove a single node (file + in-memory index) from graph.
Also scrubs any DANGLING SAME-GRAPH edge that pointed AT the deleted node
(e.g. the structural apex --related--> hub link a hub prune would
otherwise leave behind). A cross-graph edge whose target merely shares this
id but lives in ANOTHER graph (a spine-member edge into a base note's
home graph) is left intact — its target still exists in its own graph — so
a surviving node never loses an edge it genuinely still needs.
Source code in zettelkasten/spine.py
node_links ¶
The outgoing links of node_id in graph (empty if absent).
remove_link ¶
remove_link(graph: str, source_id: str, target_id: str, relation: str, *, target_graph: str = '') -> bool
Drop a specific outgoing source --relation--> target edge.
Matches on (target, relation, graph) (the same tuple :meth:ensure_link
dedupes on), so it removes exactly the edge a prior ensure created. Returns
True if an edge was removed. Used by sub-spine disconnect and by an explicit
connect that MOVES a child to a new primary parent.
Source code in zettelkasten/spine.py
ensure_link ¶
ensure_link(graph: str, source_id: str, target_id: str, relation: str, *, target_graph: str = '', confidence: 'float | None' = None, provenance: str = '', verified: bool = False, primary: bool = False) -> bool
Idempotently add an outgoing source --relation--> target edge.
confidence / provenance / verified (all default-empty) are
carried onto the edge so a spine-member edge can record the routing
provenance of the member it attaches (inferred-vs-deterministic). They are
REFRESHED in place on a re-run whose metadata drifted (e.g. a re-mine
resync with a changed confidence), but identical metadata is a no-op — so
the membership reconcile stays idempotent (no duplicate edges, no churn).
Structural edges (apex/dimension/hub wiring) pass none and stay byte-clean.
primary marks a durable sub-spine parent edge (component-of apex→apex)
and is UPGRADE-ONLY on a matching edge: a re-link may PROMOTE an existing
edge to primary=True when requested, but an ordinary re-link (primary
defaulting False — every structural/membership resync) NEVER demotes an
existing primary=True edge. Clearing primary is the job of an explicit
disconnect/unset (:func:disconnect_sub_spine_edge / :meth:remove_link),
not this idempotent add — so a routine re-extraction can't silently wipe a
hand-set / seeded primary parent. This mirrors the server link_notes
upgrade-only refresh so both backends obey the same stability rule.
Source code in zettelkasten/spine.py
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 | |
set_member_targets ¶
Reconcile node_id's outgoing relation edges to EXACTLY targets.
targets is the desired set of (target_note_id, home_graph) pairs the
node SHOULD carry. Any existing outgoing edge with relation whose
(target, graph) is not in targets is DELETED — a member that LEFT the
routing (re-typed, semantic re-cluster dropped it, or an overlay
member_remove). Edges of any OTHER relation (the structural
dimension→apex / apex→hub wiring) are left untouched.
This is the spine-side symmetric to :meth:remove_links_into_graph: it
scrubs the SPINE graph's OWN membership edges (the v2 source of truth) so an
add-only resync can't report stale membership to a direct reader. It NEVER
touches the legacy base-side compat edges — those live on base notes in
their home graph, which this never loads. Returns the number of edges
removed (it only ever deletes; adds are :meth:ensure_link's job).
Source code in zettelkasten/spine.py
remove_links_into_graph ¶
remove_links_into_graph(home_graph: str, target_graph: str, *, targets: 'set[str] | None' = None) -> int
Drop outgoing links from home_graph notes that target target_graph.
The teardown primitive: an attach edge belongs to spine X iff its
synthesis-graph endpoint is target_graph (the spine's graph), so
scrubbing by link.graph removes exactly that spine's membership edges
and leaves the base notes otherwise pristine. targets (optional)
narrows the scrub to edges into specific node ids — used to detach base
notes from an orphaned hub/dimension on re-promote without disturbing the
rest of the spine.
Raises on a graph-load failure so a caller (delete_spine) can prove the
scrub actually covered every source graph before tearing the spine down,
instead of silently treating an unreadable graph as "nothing to scrub".
Source code in zettelkasten/spine.py
delete_graph ¶
Remove a graph folder (and all its notes/edges). Returns True if it existed.
Source code in zettelkasten/spine.py
SubSpineCycleError ¶
build_spine_skeleton ¶
build_spine_skeleton(ops, *, graph_name: str, graph_description: str = '', apex_title: str, apex_type: str, apex_body: str = '', apex_relation: str = DEFAULT_APEX_RELATION, attach_relation: str = DEFAULT_ATTACH_RELATION, dimensions: list[dict[str, Any]], spec: 'dict[str, Any] | None' = None, scaffold_status: str = 'scaffold') -> dict[str, Any]
Stamp the apex + (optional spec) + the dimension TREE, idempotently.
dimensions is a list of {key, tag, title, type, body} facet specs.
Each becomes a node tagged [<tag>, DIMENSION_TAG]. A spine is a structure
TREE rooted at the apex (§11.3 — the V2b model): a facet may carry a
children list of the same shape, and each child is wired child -> parent
(apex_relation, default component-of) to ARBITRARY depth — so
dimension/hub stop being fixed levels and become "structure node at
depth N". A FLAT list (no children) yields the historical depth-1 shape
exactly: every facet is component-of the apex. spec (optional) is
{title, type, body, relation}, wired spec -> apex.
Every structure node is given a DURABLE id (§11.4): an explicit node_id
(or stable_id) on the facet, else derived from its schema PATH (the
slash-joined chain of key\ s from the root). The id is persisted spine-side
on source['node_id'] so readback + overlay corrections can bind to it and
survive a relabel/drift/re-promote. Node creation is dedup'd (by stable id,
then by title), so re-running on an unchanged graph is a no-op.
scaffold_status (default "scaffold") is stamped as synthesis_status
on every freshly-created apex/spec/dimension node, so an un-authored spine is
queryable as incomplete until a synthesizer flips it to "materialized".
Pass "" to skip stamping. (Re-running over existing nodes never changes
their status — the dedup returns early, preserving a materialized flip.)
Returns {synthesis_graph, apex_id, spec_id, dimension_nodes, attach_relation,
apex_relation} where dimension_nodes maps EACH facet's key (interior
AND leaf, full tree) to its node id (member attach edges + hub wiring are the
caller's job — they differ between the empty-scaffold and bulk-attach paths).
Source code in zettelkasten/spine.py
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 | |
resolve_apex ¶
resolve_apex(zg: ZettelGraph) -> tuple[str, str]
THE single deterministic apex resolver for a loaded spine graph.
Every consumer that needs "which node is this spine's apex?" — the id used for
UID keys, parent links, and matrix row ids — MUST resolve it through here so
spine.find_apex_id, tables._spine_apex and the org overlay route can
never disagree on a legacy or drifted graph. Resolution walks four tiers in
strict priority order, returning (id, title):
- Durable sentinel — the node whose
source['node_id']equalsAPEX_NODE_ID. This is the authoritative apex once stamped; it survives a re-title and carries the primary sub-spine edge, so it wins outright even if a legacy duplicate apex lingers. - Structural vote — the node the most
DIMENSION_TAGnodes point to via an intra-graphcomponent-ofedge (link.graph in ('', zg.name)). This recovers the tree root on a pre-sentinel graph where tag alone is ambiguous (e.g. a derivedmodelnode tagged alongside the real apex). A vote TIE is broken by the lexicographically-first (id-sorted) candidate — never by dict iteration / note insertion order — so the winner is stable across loads. - First FULL-
APEX_TAGSnode by ID-SORTED order — a deterministic tie-break when there are no dimensions/structural edges. Requires the node to carry the FULLAPEX_TAGSset (set(APEX_TAGS) <= node.tags), matchingapex_node_ids/_guard_single_apex— a mere tag intersection would let a spec node (tagged('spec', 'extraction-synthesis')) masquerade as apex. Sorting by id (rather than dict iteration order) keeps the choice stable. ("", "")— the graph carries no apex at all.
Source code in zettelkasten/spine.py
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 | |
find_apex_id ¶
find_apex_id(ops, graph_name: str, get_graph: Callable[[str], ZettelGraph]) -> str
The id of the spine apex node in graph_name (tagged APEX_TAGS), or "".
Thin wrapper over :func:resolve_apex: loads the graph and returns the
resolved apex id, preserving the historical empty-on-failure behavior when the
graph can't be loaded.
Source code in zettelkasten/spine.py
find_primary_parent_link ¶
The child apex's existing PRIMARY sub-spine parent edge, or None.
Identifies a sub-spine parent by its primary flag on a component-of
edge (the intra-spine dimension→apex tree uses component-of too, but never
primary, so the flag alone is an unambiguous discriminator).
Source code in zettelkasten/spine.py
connect_sub_spine_edge ¶
connect_sub_spine_edge(ops, *, child_graph: str, child_apex_id: str, parent_graph: str, parent_apex_id: str, respect_existing: bool = False) -> dict[str, Any]
Write the primary child-apex --component-of--> parent-apex edge.
A child spine has exactly ONE primary parent (design §2: the composition is a
true tree), so an EXPLICIT connect that points the child at a DIFFERENT parent
MOVES it — the stale primary edge is removed before the new one is written.
The write is idempotent (ensure_link dedupes), so re-connecting to the same
parent is a no-op.
respect_existing implements the STABILITY RULE (design §3): when True (the
build-time seed), a pre-existing primary parent edge WINS — if the child already
carries a primary parent pointing ELSEWHERE, the seed does NOT replace it (a
hand-set / hand-moved parent is never silently reverted). Left False for the
explicit connect front door, which is itself a hand-authoring action.
Rejects a connect that would close a parent CYCLE — connecting the child
beneath a parent that is already a DESCENDANT of the child (connect(A->B)
then connect(B->A)) raises :class:SubSpineCycleError. The immediate
self-parent case (child_graph == parent_graph) is guarded upstream by the
front doors; this covers the deeper multi-hop cycle.
Returns {connected: bool, added: bool, moved_from: {...}|None,
kept_existing: {...}|None}.
Source code in zettelkasten/spine.py
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 | |
disconnect_sub_spine_edge ¶
disconnect_sub_spine_edge(ops, *, child_graph: str, child_apex_id: str, parent_graph: str = '', parent_apex_id: str = '') -> dict[str, Any]
Remove the child apex's primary sub-spine parent edge.
With no parent_apex_id given, removes whatever primary parent edge the
child currently carries (the common "detach this sub-spine" case). When a
specific parent is named, only that edge is removed (and only if it is the
child's primary parent). Idempotent: removing an absent edge reports
removed=False.