feat(graph): graph view with network, local, synapse, cluster and namespace modes

- synapse-ffi: graph_data() returns the whole page-level graph (links, tags,
  property refs, block refs/embeds per page, unlinked mentions, decayed
  synapses) as parallel arrays with degree, backlinks, Louvain clusters and
  primary tags; cached by op seq, synapse state and hour.
- synapse-ffi: ForceLayout, a Barnes-Hut force layout stepped from Dart on a
  Rust worker thread (pin/unpin for dragging, reheat).
- app: Graph view with a mode selector (Network, Local, Synapses, Clusters,
  Namespaces, Timeline), filters (title, tag, date range, min links,
  orphans, journals, mentions), pan/zoom, node drag, hover highlighting,
  activation pulse, cluster hulls, radial namespace/tag tree, and a local
  graph panel on every page. Edges render as one vertex mesh, nodes in
  colour/size batches, culled to the viewport.
- Tests: Rust extraction/mentions/cache/clusters/layout; widget tests for
  mode switching, filters and layout restarts; a real-app integration
  smoke test with frame timings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Opus 5.5 committed 2026-10-21 15:39:07 +02:00
1 parent 6a4a76670b
commit e43452f5af
29 files changed
+6177 -76

No files matched your search

+281
View File
@@ -0,0 +1,281 @@
import 'dart:collection';
import 'dart:typed_data';
import '../backend/graph_backend.dart';
/// Edge kinds, matching `synapse_ffi::api::dto::edge_kind`.
enum EdgeKind {
link('Link'),
tag('Tag'),
property('Property'),
blockRef('Block ref'),
embed('Embed'),
mention('Mention'),
synapse('Synapse');
const EdgeKind(this.label);
final String label;
bool get structural => this != EdgeKind.mention && this != EdgeKind.synapse;
}
/// The page-level graph with adjacency, built once per [GraphData].
class GraphModel {
GraphModel(this.data)
: _kinds = [for (final k in data.kinds) EdgeKind.values[k]] {
final n = data.nodes.length;
adjacency = List.generate(n, (_) => <int>[]);
for (var e = 0; e < edgeCount; e++) {
adjacency[data.sources[e]].add(e);
adjacency[data.targets[e]].add(e);
}
for (var i = 0; i < n; i++) {
_byId[data.nodes[i].id] = i;
}
}
final GraphData data;
final List<EdgeKind> _kinds;
late final List<List<int>> adjacency;
final _byId = <String, int>{};
int get nodeCount => data.nodes.length;
int get edgeCount => data.kinds.length;
GraphNode node(int i) => data.nodes[i];
EdgeKind kind(int e) => _kinds[e];
int source(int e) => data.sources[e];
int target(int e) => data.targets[e];
double weight(int e) => data.weights[e];
int? indexOf(String id) => _byId[id];
int other(int e, int i) => source(e) == i ? target(e) : source(e);
bool isJournal(int i) => node(i).journalDate != null;
/// Tag pages actually used as tags, most used first.
late final List<int> tags = () {
final count = <int, int>{};
for (var e = 0; e < edgeCount; e++) {
if (kind(e) == EdgeKind.tag) {
count[target(e)] = (count[target(e)] ?? 0) + 1;
}
}
final l = count.keys.toList()
..sort((a, b) => count[b]!.compareTo(count[a]!));
return l;
}();
/// Hop distance from `seed` over edges of `kinds`, up to `depth`.
Map<int, int> neighbourhood(int seed, int depth, Set<EdgeKind> kinds) {
final dist = <int, int>{seed: 0};
final q = Queue<int>()..add(seed);
while (q.isNotEmpty) {
final i = q.removeFirst();
final d = dist[i]!;
if (d >= depth) continue;
for (final e in adjacency[i]) {
if (!kinds.contains(kind(e))) continue;
final j = other(e, i);
if (!dist.containsKey(j)) {
dist[j] = d + 1;
q.add(j);
}
}
}
return dist;
}
/// The induced subgraph on `nodes` (global indices) with edges of `kinds`. Parallel
/// edges between the same pair are merged for the layout but kept for drawing.
///
/// `springs` adds layout-only attractions (global pairs and strengths) that are not drawn.
SubGraph subgraph(
List<int> nodes,
Set<EdgeKind> kinds, {
List<(int, int, double)> springs = const [],
}) {
final local = <int, int>{};
for (var k = 0; k < nodes.length; k++) {
local[nodes[k]] = k;
}
final edges = <int>[];
for (final i in nodes) {
for (final e in adjacency[i]) {
if (source(e) != i) continue; // visit each edge once, from its source
if (!kinds.contains(kind(e))) continue;
if (local.containsKey(target(e))) edges.add(e);
}
}
return SubGraph(this, nodes, local, edges, springs: springs);
}
}
/// Filters of the network modes.
class GraphFilter {
const GraphFilter({
this.journals = false,
this.orphans = true,
this.minDegree = 0,
this.tag,
this.query = '',
this.from,
this.to,
this.kinds = const {
EdgeKind.link,
EdgeKind.tag,
EdgeKind.property,
EdgeKind.blockRef,
EdgeKind.embed,
EdgeKind.mention,
},
});
final bool journals;
final bool orphans;
final int minDegree;
/// Only pages tagged with this tag page (and the tag itself).
final int? tag;
/// Title substring.
final String query;
/// Created-at range (inclusive days, `YYYY-MM-DD`).
final String? from;
final String? to;
final Set<EdgeKind> kinds;
GraphFilter copyWith({
bool? journals,
bool? orphans,
int? minDegree,
int? Function()? tag,
String? query,
String? Function()? from,
String? Function()? to,
Set<EdgeKind>? kinds,
}) => GraphFilter(
journals: journals ?? this.journals,
orphans: orphans ?? this.orphans,
minDegree: minDegree ?? this.minDegree,
tag: tag == null ? this.tag : tag(),
query: query ?? this.query,
from: from == null ? this.from : from(),
to: to == null ? this.to : to(),
kinds: kinds ?? this.kinds,
);
List<int> apply(GraphModel g) {
final q = query.trim().toLowerCase();
final tagged = tag == null
? null
: {
tag!,
for (final e in g.adjacency[tag!])
if (g.kind(e) == EdgeKind.tag && g.target(e) == tag) g.source(e),
};
String day(int i) {
final ms = g.node(i).createdAt.toInt();
final d = DateTime.fromMillisecondsSinceEpoch(ms);
return '${d.year.toString().padLeft(4, '0')}-${d.month.toString().padLeft(2, '0')}'
'-${d.day.toString().padLeft(2, '0')}';
}
final base = <int>[
for (var i = 0; i < g.nodeCount; i++)
if ((journals || !g.isJournal(i)) &&
(tagged == null || tagged.contains(i)) &&
(q.isEmpty || g.node(i).title.toLowerCase().contains(q)) &&
(from == null || day(i).compareTo(from!) >= 0) &&
(to == null || day(i).compareTo(to!) <= 0))
i,
];
if (orphans && minDegree == 0) return base;
// Degree counts only neighbours that are themselves shown, over the visible kinds.
final shown = base.toSet();
int degree(int i) {
final seen = <int>{};
for (final e in g.adjacency[i]) {
if (!kinds.contains(g.kind(e))) continue;
final j = g.other(e, i);
if (shown.contains(j)) seen.add(j);
}
return seen.length;
}
return [
for (final i in base)
if (() {
final d = degree(i);
return d >= minDegree && (orphans || d > 0);
}())
i,
];
}
}
/// A view-sized piece of the graph: global node indices, a global→local map and the global
/// ids of the edges between them.
class SubGraph {
SubGraph(
this.model,
this.nodes,
this.local,
this.edges, {
this.springs = const [],
});
final GraphModel model;
final List<int> nodes;
final Map<int, int> local;
final List<int> edges;
/// Layout-only attractions between global node pairs.
final List<(int, int, double)> springs;
int get length => nodes.length;
/// Edge endpoints in local indices, deduplicated per pair, with summed weights (for the
/// layout). Synapse weights are scaled so strong synapses pull harder.
(Uint32List, Uint32List, Float32List) layoutEdges() {
final pairs = <int, double>{};
for (final e in edges) {
final a = local[model.source(e)]!;
final b = local[model.target(e)]!;
final key = a < b ? a * length + b : b * length + a;
final w = model.kind(e) == EdgeKind.synapse
? model.weight(e)
: model.kind(e) == EdgeKind.mention
? 0.3
: 1.0;
pairs[key] = (pairs[key] ?? 0) + w;
}
for (final (ga, gb, w) in springs) {
final a = local[ga], b = local[gb];
if (a == null || b == null || a == b) continue;
final key = a < b ? a * length + b : b * length + a;
pairs[key] = (pairs[key] ?? 0) + w;
}
final s = Uint32List(pairs.length);
final t = Uint32List(pairs.length);
final w = Float32List(pairs.length);
var k = 0;
pairs.forEach((key, weight) {
s[k] = key ~/ length;
t[k] = key % length;
w[k] = weight;
k++;
});
return (s, t, w);
}
}
/// Runs a force layout; implemented over the Rust `ForceLayout` (and by a fake in tests).
abstract class LayoutEngine {
/// Runs up to `iterations` ticks: positions `x0, y0, ...` and the remaining alpha (0 when
/// settled).
Future<(Float32List, double)> step(int iterations);
Future<void> pin(int index, double x, double y);
Future<void> unpin(int index);
Future<void> reheat(double alpha);
void dispose();
}