10 KiB
Architecture
app/ (Flutter, Dart) ──FFI──▶ synapse-ffi ──▶ synapse-core ◀── synapse-search
│ ▲ ▲ synapse-import
│ │ └─────── synapse-history
├──▶ synapse-modules (WASM sandbox, host crypto)
├──▶ synapse-ai ──┘ (Embedder) synapse-caldav
└──▶ synapse-sync ──HTTP──▶ synapse-server (axum)
AI assistant ──MCP stdio──▶ synapse-mcp ──▶ synapse-core │
AI assistant ──MCP HTTP───────────────────▶ /v1/graphs/{id}/mcp ┘ (synapse-mcp tools)
Graph on disk
<graph>/synapse.db (SQLite, WAL) + <graph>/blobs/ (content-addressed, blake3). Sibling
crates may add their own tables to the same database (prefix them: search_*, history_*,
caldav_*, sync_*) or their own files under <graph>/ (e.g. <graph>/index/ for tantivy).
Data model (synapse-core)
- Page: title (unique, case-insensitive), optional
journal_date,origin_journal(the day it was born), properties. Journal ids are UUIDv5 of the date, so the same day has the same id on every replica; other ids are UUIDv7. - Block: outliner node (
parent_id, fractionalposition), Markdowncontent, properties, collapsed flag. - Refs: extracted on every write from
[[Page]],#tag,#[[tag]],key:: [[Page]],((block)),{{embed ((block))}}. Page refs are stored by normalised title, so they never dangle and survive sync/renames (rename rewrites the text). - Aliases: alternative titles resolving to a page.
- page_activity: (page, day, edits) — links pages to the journals of days they were edited.
- Synapses: local weighted associations between pages (Hebbian, 30-day half-life).
- Assets: metadata rows; bytes in the blob store.
Tags and types (synapse-core::{tags, types})
- Tags are plain names, never pages. A page carries
Page.tags: Vec<String>(pages.tags, synced with the page, LWW per page), stored sorted case-insensitively bytitle_key. The derivedpage_tags(page_id, tag_key)index covers the page's tags plus its type and is rewritten on every page write. Journals and templates cannot be tagged. API:set_page_tags,add_page_tag,remove_page_tag,pages_with_tag,tag_catalog,rename_tag(tags and types, merging duplicates) andremove_tag(explicit tags only). - A page has at most one type (
Page.types, 0 or 1 entries;Page::page_type()), a name saying what the page is. It implies a tag of the same name. API:set_page_type(page, Option<&str>),type_catalog(types in use by count, then the seededSUGGESTED_TYPES). - Type definitions (schema v8): graph-wide metadata of a type name lives in
type_defs(type_key, name, icon, removed, hlc)(TypeDef), synced with its own opOp::PutTypeDef { def }, last writer wins per name key (likeSetAlias). It carries the type's emoji (icon, a short non-ASCII string,normalize_icon) and keeps a type in the vocabulary while no page uses it;removedis a tombstone (it also hides a seeded suggestion of that name). API:create_type,set_type_icon(name, Option<&str>),rename_type(rename_tagplus keeping an unused type),remove_type(clears the type from its pages, tombstones the definition),type_defs,all_page_types.rename_tagmoves the definition to the new name (a target with its own icon keeps it).type_cataloglists used types, then defined unused ones, then remaining suggestions, each with its icon;tag_catalogcounts per tag the pages listing it (explicit), typed with it (typed) and carrying it through their hub (from_hub). - A page's direct hub is an implicit tag, like its type: members of hub "Anime" are filed
under "Anime" (
page_tags,pages_with_tag,tag_catalog, the graph's tag node). It is not stored inPage.tagsand merges with a same-name type or tag. Only the direct hub counts (not ancestors); hubs, templates and journals get none. The index is derived, so renaming or deleting a hub, or moving a member, just re-indexespage_tags(no ops).rename_tagnever renames a hub: it only changes explicit tags and types. - Schema v6 turned former tag pages (kind
tag) into plain notes and trimmed multiple types to the first;PageKindhas noTagany more and "tag" parses as a note. - In the graph (
synapse-ffi) every tag in use is a virtual node (GraphNode.is_tag, idtag:<title_key>, no page), with aTAGGEDedge from each tagged page (type included). A tag named like a hub in the graph uses the hub's node instead; its members, already linked byCONTAINS, get noTAGGEDedge. - Known limitations: replicas still on the old schema may overwrite tags with stale values (LWW per page), and
rename_tagis a bulk page edit, so concurrent edits on another replica can undo part of it. - Exports use
synapse-tags::andsynapse-type::(seehistory.md); type definitions go totypes.json.
Modules (synapse-core::module_data, synapse-modules)
Sandboxed WebAssembly extensions (docs/modules.md). The core only knows their data:
module_data(module, key, value, hlc) (schema v9), one synced op PutModuleData per entry
(LWW, NULL value = tombstone), never interpreted by the core. synapse-modules runs modules
(wasmi, fuel and memory limits), checks permissions on every host call, keeps keys for the
crypto host functions, and manages installation (<graph>/modules/, per device). The FFI
exposes install/list/enable/uninstall and JSON requests; the app renders their UI trees.
Hubs and namespaces (synapse-core::structure)
Two orthogonal ways to organise pages.
- Namespace (kinship by title, derived):
A/Bis a child ofA(namespace_parent,Graph::namespace_children). Parents may not exist as pages (virtual levels). - Hub (folder, explicit): a page with
kind = hub(PageKind, schema v3 columnpages.kind). A hub has adescriptionand no blocks (insert_block/put_block/move_blockinto a hub fail withError::Invalid). Created withcreate_hub, or byconvert_to_hub(manual, from an existing note) and undone withconvert_to_note.- Membership lives on the member:
Page.hubs: Vec<PageId>(pages.hubs, JSON), synced with the page (UpsertPage, LWW per page), so no new op exists. A derived local indexpage_hubs(page_id, hub_id)is rewritten on every page write (local or remote) and rebuilt by the v2 → v3 migration. - One hub per page: the list holds at most one id (local writes with more are refused;
add_to_hubmoves the page). The wire format stays a list, and readers (index,structure) use only its first entry, so data from older replicas converges. - Template: every hub owns exactly one page with
kind = template(member of its hub, possibly empty, an ordinary outline).create_hub/convert_to_hubcreate it,hub_templatereturns it (creating one for older hubs). Templates are hidden fromhub_members,list_pagesandstructure, cannot be deleted alone and go with their hub (convert_to_notedeletes it).create_page_from_template(hub, title)makes a note in the hub with a copy of the template's blocks. - API:
set_page_hubs,add_to_hub,remove_from_hub,hub_members(sub-hubs first, then by title),hubs_of. Rules: the target must be a hub, no self-membership, no journals, no cycles (checked locally); duplicates dropped. Readers ignore ids that dangle or point at a page that is no longer a hub. Deleting a hub or converting it to a note removes its id from every member (oneUpsertPageper member). hub_conversion_previewsuggests a title (last namespace segment), a description (first prose block, plain text, <= 280 chars) and members (outline refs, then property and tag refs).convert_to_hubapplies aHubConversionin one transaction: rename (link rewrite), optionally move the outline to a new note (keep_outline_as), delete the remaining blocks, set kind/description and add the members.Graph::structure()returns every hub with members and nesting depth, and each page's home hub (deepest containing hub; ties: fewer members, then title; a hub is its own home). Cycles created by concurrent edits on two replicas are broken deterministically (edges ordered by the member page's HLC). The graph view addsCONTAINSedges hub → member.- Sync limits:
apply_remotedoes not enforce "no blocks" (rejecting would break convergence), so a block added on one replica while another converts the page can land in a hub;convert_to_notekeeps such blocks. Clients older than schema v3 dropkind,descriptionandhubswhen they upsert a page, so every client must be on v3.
- Membership lives on the member:
- Logseq built-ins (
contents,TODO,hls__*, ...) are recognised byis_legacy_page; the app offers to purge them, together with empty pages (legacy_candidates/purge_pagesinsynapse-ffi). The importer only skips a narrower set (seeimport.md).
Temporal model
journal_neighbors(date)gives the previous/next existing journal (episodic chain).- Pages created from a journal block are born there; other new pages are born in today's
journal.
timeline(from, to)returns, per day: journal, pages born, pages edited.
Change feed and sync
Every mutation appends an Op to the ops table with a hybrid logical clock timestamp.
ops_since(seq) is the single feed for indexers, backups and sync. apply_remote(hlc, op)
merges another replica's ops with last-writer-wins per entity and tombstones for deletes;
applied ops are re-logged locally so they propagate further.
Threading
Graph owns a rusqlite::Connection and is Send but not Sync. The FFI layer keeps it
behind a mutex (or one graph per worker thread opening the same directory; WAL allows it).
Brain-like queries
related(page) runs spreading activation (3 hops, fan-out normalised) over refs in both
directions, synapses, the journal chain and origin/activity anchors.