From 265a411f4d0432997d27e0bd61448436bb612d2b Mon Sep 17 00:00:00 2001 From: Dasemu Date: Tue, 27 Oct 2026 05:38:25 +0100 Subject: [PATCH] fix(ffi): invalidate the graph cache on same-millisecond synapse fires A visit that re-fires a synapse in the same millisecond as its last fire kept the synapse count and max(last_fired) unchanged, so graph_data() served a stale graph (and cache_is_invalidated_by_edits_and_visits failed intermittently). The cache key now includes the total synapse weight. Co-Authored-By: Claude Opus 5.5 --- crates/synapse-ffi/src/api/graph_view.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/crates/synapse-ffi/src/api/graph_view.rs b/crates/synapse-ffi/src/api/graph_view.rs index 5abae7b..ab4ac94 100644 --- a/crates/synapse-ffi/src/api/graph_view.rs +++ b/crates/synapse-ffi/src/api/graph_view.rs @@ -19,6 +19,9 @@ pub struct CacheKey { seq: i64, synapses: i64, synapse_last: i64, + /// Sum of raw weights (micro-units): a re-fire within the same millisecond keeps count + /// and `last_fired` but changes the weight. + synapse_weight: i64, /// Synapse weights decay with time: rebuild at least hourly. hour: i64, } @@ -47,15 +50,22 @@ impl SynapseGraph { } fn cache_key(g: &Graph) -> Result { - let (synapses, synapse_last) = g.conn().query_row( - "SELECT count(*), coalesce(max(last_fired), 0) FROM synapses", + let (synapses, synapse_last, weight) = g.conn().query_row( + "SELECT count(*), coalesce(max(last_fired), 0), total(weight) FROM synapses", [], - |r| Ok((r.get::<_, i64>(0)?, r.get::<_, i64>(1)?)), + |r| { + Ok(( + r.get::<_, i64>(0)?, + r.get::<_, i64>(1)?, + r.get::<_, f64>(2)?, + )) + }, )?; Ok(CacheKey { seq: g.last_seq()?, synapses, synapse_last, + synapse_weight: (weight * 1e6).round() as i64, hour: g.now_ms() / 3_600_000, }) }