zettelkasten.synapse.substrate¶
zettelkasten.synapse.substrate ¶
In-memory, n-type substrate assembler for the synapse reasoning layer.
The substrate is the heterogeneous graph a reasoning agent reasons off instead
of fabricating: on demand it assembles typed, provenance-tethered
:class:Node and :class:Edge objects that a deterministic engine can later
verify for grounding. This is the second synapse workstream — it consumes the
MemDSL contract (:mod:zettelkasten.synapse.memdsl) and is consumed by the
epistemics / router / navigation / render workstreams (not yet built, so nothing
here imports them).
Design invariants pinned here:
- N-type, topology-general. :class:
Nodeand :class:Edgecarry arbitrary type/relation pairs — code→data, claim→requirement, code→code, memory→zk — so the substrate is not the bipartitememory × zkgraph that :mod:zettelkasten.synapse.memory_sourceassumes. The nodetypevocabulary follows :data:zettelkasten.graph.VALID_TYPESand edgerelationfollows :data:zettelkasten.graph.VALID_RELATIONS, but an unknown type (e.g. thecodeextension type) degrades gracefully exactly as the MemDSL node-type registry does — the substrate never rejects a node for a novel type. - Reuse, don't reinvent. Every store-backed adapter fuses its keyword +
semantic channels through :func:
zettelkasten.framing.project_query(the same RRF fusion / ANN / rerank the rest of synapse uses) and reuses the provenance extractors in :mod:zettelkasten.synapse.candidates. No adapter re-implements embedding, scoring, or staleness. - A read-only-agnostic protocol. The :class:
SourceAdapterprotocol is a minimal READ surface (retrieve/get/expand); it deliberately does NOT assume an immutable store. v1 ships only read-only adapters (they set :attr:SourceAdapter.mutabletoFalse), but a future session-scoped MUTABLE source (the v2 artifact-under-edit) satisfies the SAME protocol and simply adds its own write methods and flipsmutable— no change here. - Pure and in-memory. Assembly persists nothing. The shipped persisted link
overlay (:mod:
zettelkasten.synapse.overlay) andsynapse/sharing/are only ever read, never written, by this module.
Node identity uses the MemDSL addressing scheme store:source:id — every
:class:Node builds its canonical id via :class:zettelkasten.synapse.memdsl.
schema.Address rather than reinventing a key format.
Budget
dataclass
¶
A ceiling on how much substrate a single assembly may materialize.
Assembly is on-demand and bounded so a reasoning turn never drowns in nodes:
max_nodes— the hard cap on the total assembled neighborhood;Noneremoves this count cap (hop and downstream token bounds remain).per_source— how many candidates each adapter may contribute (the per-adapter retrievaltop_k).hops— expansion depth beyond the retrieved seeds (0= seeds only).
Source code in zettelkasten/synapse/substrate.py
Node
dataclass
¶
One typed, provenance-tethered substrate node.
A node is generic over store and type: store/source/id compose its
MemDSL :class:~zettelkasten.synapse.memdsl.schema.Address, type follows
the :data:zettelkasten.graph.VALID_TYPES vocabulary (an unknown type is
tolerated), and title/text are the human-readable body a renderer
shows.
Two provenance channels are kept distinct on purpose:
provenance— human/source-level origin handles (where the node came from: file-path tokens, citation ids, the source graph). For orientation and display.tethers— MACHINE-CHECKABLE grounding anchors the deterministic engine later verifies for drift/staleness (e.g. a git-pinnedpath@sha, a datasetcontent_hash, a citation handle). This is the seam the epistemics workstream reads to decide grounded-vs-stale.
epistemic_status is a PLACEHOLDER the epistemics module populates later;
the assembler leaves it None (assembly makes no epistemic claim).
outdated is its INSEPARABLE committed-drift companion — a purely-outdated
node wires to grounded (byte-indistinguishable from fresh at the scalar), so
the disclosure must ride alongside the scalar to survive the node boundary.
Source code in zettelkasten/synapse/substrate.py
Edge
dataclass
¶
A typed, directed relation between two substrate nodes.
src and dst are node :attr:Node.token address strings, so an edge is
self-contained and serializable. relation follows the
:data:zettelkasten.graph.VALID_RELATIONS vocabulary and may connect ANY
type pair, including within-store edges (e.g. a zk claim --depends-on-->
zk requirement) and cross-store edges (a mem decision
--applies-to--> a zk claim). confidence is a [0, 1] strength,
rationale an optional human note, and signals an open bag of the
evidence that produced the edge (e.g. semantic similarity, shared provenance).
Source code in zettelkasten/synapse/substrate.py
known_relation
property
¶
Whether :attr:relation is in the canonical vocabulary (advisory).
Neighborhood
dataclass
¶
The assembled, in-memory result: a set of nodes plus the edges among them.
nodes is keyed by :attr:Node.token (dedup identity); edges only ever
references tokens present in nodes (the assembler drops dangling edges).
Nothing here is persisted.
Source code in zettelkasten/synapse/substrate.py
SourceAdapter ¶
Bases: Protocol
A store-agnostic seam that yields substrate nodes + edges on demand.
An adapter maps ONE backing store into :class:Node/:class:Edge objects. It
is deliberately a minimal READ surface so it makes no assumption about
immutability — a future session-scoped MUTABLE source implements the same
three methods (plus its own write methods) and flips :attr:mutable.
Contract:
- :attr:
store— the MemDSL address store prefix this adapter emits (zk/mem/code/ …). Advisory; each :class:Nodealso carries its own store. - :attr:
mutable—Falsefor every v1 adapter (all read-only). The seam for the v2 artifact-under-edit: a mutable adapter sets thisTrueso the assembler (and downstream cache layers) can refuse to memoize its nodes. - :meth:
retrieve— the primary on-demand entry point: return up tobudget.per_sourcecandidate nodes most relevant toquery, using the store's own reused fusion/embedding machinery. - :meth:
get— ground a single node by :class:Address(used to re-fetch an elided body, or resolve an edge endpoint).Nonewhen it does not resolve. - :meth:
expand— best-effort one-hop neighborhood aroundnodeWITHIN this store: the neighbor nodes and the within-store edges to them. An adapter with no cheap link structure may return([], []).
Source code in zettelkasten/synapse/substrate.py
NotesSourceAdapter ¶
A read-only adapter over any object exposing the ZK source-graph surface.
This is the shared implementation behind the memory, zettelkasten, and dataset
adapters. Its backing graph need only expose .notes (a mapping of id →
note-like with .title/.type/.tags/.aliases/.body/.links)
and an optional .embeddings search index — exactly the surface
:func:zettelkasten.framing.project_query consumes. Retrieval therefore
REUSES that hybrid RRF fusion unchanged (one synthetic single-source scope),
rather than reinventing keyword/semantic scoring.
Subclasses customize node typing, provenance, and tethers via
:meth:_provenance / :meth:_tethers; the base keeps them empty.
Source code in zettelkasten/synapse/substrate.py
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 421 422 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 | |
retrieve ¶
Retrieve candidate nodes for query via reused hybrid fusion.
Delegates to :func:zettelkasten.framing.project_query over a single
synthetic source, so keyword + semantic channels are fused with exactly
the same RRF/rerank the rest of synapse uses. A type filter (if any) is
applied to the fused hits. Any retrieval failure yields [] — a bad
source loses the ranking, it never crashes assembly.
Source code in zettelkasten/synapse/substrate.py
get ¶
get(address: Address) -> 'Node | None'
Resolve a single node by address (matched on store + source + id).
The source segment is part of node identity — an adapter bound to one
source must not resolve an id living in a sibling source (e.g. another ZK
box), so a store OR source mismatch yields None.
Source code in zettelkasten/synapse/substrate.py
expand ¶
Return within-store neighbors + edges from a note's own links.
Best-effort and structural only: it reads the note's declared links
(.target/.relation) and resolves each target within THIS source.
A store whose notes carry no such link structure (e.g. the memory tree
adapter) simply returns ([], []).
Source code in zettelkasten/synapse/substrate.py
MemoryAdapter ¶
Bases: NotesSourceAdapter
Read-only adapter over the .memory/ research tree (mem:tree:<id>).
Wraps :func:zettelkasten.synapse.memory_source.get_memory_source (the cached
:class:~zettelkasten.synapse.memory_source.MemorySourceGraph), so it reuses
memory's existing model2vec vectors for the semantic channel with no corpus
re-embed. Provenance is the entry's normalized git-pinned file tokens; the
tethers are the RAW path@sha pins (the drift anchors the engine checks).
Source code in zettelkasten/synapse/substrate.py
ZettelAdapter ¶
Bases: NotesSourceAdapter
Read-only adapter over one zettelkasten box (zk:<box>:<note_id>).
Retrieval reuses the box graph's own embedding index + keyword channel through
:func:~zettelkasten.framing.project_query. Provenance is the note's cited-work
ids and file-path tokens (via :mod:~zettelkasten.synapse.candidates); tethers
are its citation handles (the grounding a verified quote/finding rests on).
Source code in zettelkasten/synapse/substrate.py
DatasetAdapter ¶
Bases: ZettelAdapter
Read-only adapter over dataset-typed zettelkasten notes.
A thin specialization of :class:ZettelAdapter that restricts retrieval to
dataset notes and, on top of the citation tethers, adds the dataset's
content_hash — the reproducible-bytes anchor from the note's data
block (see :mod:zettelkasten.datasets). That hash is the tether the engine
uses to prove a measured claim still rests on the same numbers.
Source code in zettelkasten/synapse/substrate.py
CodeAdapter ¶
Best-effort, read-only adapter over angelo's own source code (code:…).
Thin by design (v1): source code is not a first-class synapse store, so this
adapter resolves a symbol/file anchor for a query against the memory code
graph (:mod:memory.code_context) when one is available and emits a single
code node for it. When no code graph is provided — or resolution fails —
it degrades to yielding nothing rather than raising, so it is always safe to
include in an assembly. It exists to prove the protocol is genuinely
store-agnostic (a code node can sit beside zk/mem nodes and take
part in edges such as code→data); a richer code adapter can replace it
without touching the protocol.
Source code in zettelkasten/synapse/substrate.py
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 | |
retrieve ¶
Resolve a single best-effort code anchor for query (or []).
Source code in zettelkasten/synapse/substrate.py
expand ¶
is_valid_type ¶
Whether node_type is in the canonical note-type vocabulary.
Advisory only: the substrate tolerates unknown types (they degrade gracefully like the MemDSL node-type registry). Consumers use this to decide whether a type needs special-casing, never to reject a node.
Source code in zettelkasten/synapse/substrate.py
is_valid_relation ¶
assemble_neighborhood ¶
assemble_neighborhood(query: str, adapters: 'list[SourceAdapter]', budget: 'Budget | None' = None, *, expand: bool = False, linkers: 'tuple[Linker, ...]' = ()) -> Neighborhood
Assemble an on-demand, in-memory substrate neighborhood for query.
The pipeline is deliberately simple and topology-general:
- Retrieve. Each adapter contributes up to
budget.per_sourcenodes via its own reused fusion; nodes are merged and de-duplicated on their :attr:Node.tokenup tobudget.max_nodeswhen that cap is set. - Expand (optional). When
expandis set andbudget.hops > 0, the frontier is expandedbudget.hopslevels: each frontier node's producing adapter yields its within-store one-hop neighborhood, newly-discovered neighbor nodes are admitted up to the cap, and each becomes part of the next hop's frontier (so a chain A→B→C is fully reached athops=2). - Link. Each injected
linkermay add cross-cutting edges (e.g. typed cross-store links from the persisted overlay) among the assembled nodes. - Prune. Edges whose endpoints are not both present are dropped, so the returned graph is always internally consistent.
Nothing is persisted. Returns a :class:Neighborhood.
Source code in zettelkasten/synapse/substrate.py
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 | |
overlay_linker ¶
Build a :data:Linker that reads typed cross-store edges from the overlay.
Returns a callable that, given the assembled node map, reads the SHIPPED,
persisted synapse link overlay (:func:zettelkasten.synapse.overlay.
load_overlay, read-only — never written) and emits an :class:Edge for every
stored memory ↔ zk link whose confidence clears min_conf AND whose
BOTH endpoints are already present in the neighborhood. Endpoints are matched
by the full (store, source, id) triple against the assembled nodes — the
zk endpoint uses the edge's own zk_source (box) segment — so an overlay
edge attaches only to the correct node and never conflates two boxes that
happen to share an id. The linker stays topology-agnostic: it only connects
nodes the adapters already surfaced.