Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (copilot): stream-matched vision 'ended' guard + clear-before-stop; stale ui:read resolves null; bound checksumCache

Adam Eivy1bdf9525978d
diff --git a/client/src/services/voiceClient.js b/client/src/services/voiceClient.js
index d04856bc6f..ecb86671c1 100644
--- a/client/src/services/voiceClient.js
+++ b/client/src/services/voiceClient.js
@@ -478,8 +478,13 @@ let visionStream = null;
let onVisionEndedCb = null;
const clearVisionStream = ({ notify = true } = {}) => {
- if (visionStream) visionStream.getTracks().forEach((t) => t.stop());
+ const stream = visionStream;
+ // Null the global BEFORE stopping tracks: t.stop() synchronously fires the
+ // track 'ended' handler, and that handler no-ops unless `visionStream === its
+ // own stream` — so clearing first keeps a caller-initiated stop (notify:false)
+ // from re-entering via the 'ended' path and firing onVisionCaptureEnded.
visionStream = null;
+ if (stream) stream.getTracks().forEach((t) => t.stop());
if (notify && typeof onVisionEndedCb === 'function') onVisionEndedCb();
};
@@ -510,7 +515,11 @@ export const enableVisionCapture = async () => {
if (!stream) return false;
visionStream = stream;
// User-stopped (browser chrome) → clear + notify the UI so the toggle flips.
- stream.getVideoTracks().forEach((t) => t.addEventListener('ended', () => clearVisionStream(), { once: true }));
+ // Guard on `visionStream === stream` so a late 'ended' from a SUPERSEDED
+ // stream (quick disable→re-enable) can't tear down the newer capture session.
+ stream.getVideoTracks().forEach((t) => t.addEventListener('ended', () => {
+ if (visionStream === stream) clearVisionStream();
+ }, { once: true }));
return true;
};
diff --git a/server/services/dataSync.js b/server/services/dataSync.js
index aa487b30d1..82c3b107b2 100644
--- a/server/services/dataSync.js
+++ b/server/services/dataSync.js
@@ -714,11 +714,25 @@ const CATEGORIES = {
videoHistory: { getSnapshot: getVideoHistorySnapshot, applyRemote: applyVideoHistoryRemote }
};
-// Per-category `{ fingerprints, checksum }` cache. The orchestrator hits
-// getChecksum every cycle — by far the hottest sync-side I/O — so caching
-// keyed on underlying-file `(mtime, size)` lets it stat-and-return when
-// nothing has changed, instead of re-materializing the full payload.
+// Per-`(category, forPeerId)` `{ fingerprints, checksum }` cache. The
+// orchestrator hits getChecksum every cycle — by far the hottest sync-side I/O —
+// so caching keyed on underlying-file `(mtime, size)` lets it stat-and-return
+// when nothing has changed, instead of re-materializing the full payload.
const checksumCache = new Map();
+// `forPeerId` segments the key and ultimately comes from an inbound `?forPeer=`
+// query param, so left unbounded the Map could grow once per distinct peer id
+// ever seen (peers re-register with fresh instanceIds over time). Bound it with
+// simple insertion-order (oldest-first) eviction — a home federation has only a
+// handful of peers × ~7 categories, so this cap is never hit in practice; it
+// just caps the worst case. Re-inserting an existing key refreshes its order.
+const CHECKSUM_CACHE_MAX = 256;
+const setChecksumCache = (key, value) => {
+ if (checksumCache.has(key)) checksumCache.delete(key);
+ checksumCache.set(key, value);
+ while (checksumCache.size > CHECKSUM_CACHE_MAX) {
+ checksumCache.delete(checksumCache.keys().next().value);
+ }
+};
// Combine mtime/size/inode of one regular file into a single fingerprint
// string. Inode is included so an atomic-write replace (which mints a new
@@ -833,7 +847,7 @@ export async function getChecksum(category, { forPeerId } = {}) {
// that I/O on every poll that hits the cache.
const exclude = await resolveExcludeSet(category, forPeerId);
const snapshot = await cat.getSnapshot({ exclude });
- checksumCache.set(cacheKey, { fingerprints, checksum: snapshot.checksum });
+ setChecksumCache(cacheKey, { fingerprints, checksum: snapshot.checksum });
return { checksum: snapshot.checksum };
}
const exclude = await resolveExcludeSet(category, forPeerId);
diff --git a/server/services/voice/pipeline.js b/server/services/voice/pipeline.js
index 174a457173..24ec5a8b90 100644
--- a/server/services/voice/pipeline.js
+++ b/server/services/voice/pipeline.js
@@ -165,10 +165,15 @@ const requestUiText = (state, emit, signal, timeoutMs = UI_TEXT_READ_TIMEOUT_MS)
// Drop our own waiter so a late response (after timeout/abort) is a no-op
// rather than resolving a stale promise.
state.uiTextWaiters.delete(requestId);
- // Cache onto the snapshot only when it's STILL the one we requested for, so
- // a follow-up same-turn ui_read on the same page skips another round-trip
- // without risking a stale-page overwrite.
- if (value !== null && state.ui && state.ui === snapshotAtRequest) state.ui.text = value;
+ // If the snapshot changed (navigation / new index) between request and
+ // response, the fetched text is for a page that's no longer current —
+ // resolve null (stale) rather than handing ui_read the wrong page's text.
+ // Reference equality treats "no snapshot at request AND none now" as
+ // unchanged (e.g. tests with a bare state). When still current, cache the
+ // text for a same-turn re-read (only when there's a snapshot object to hold it).
+ const stillCurrent = state.ui === snapshotAtRequest;
+ if (!stillCurrent) { resolve(null); return; }
+ if (value !== null && state.ui) state.ui.text = value;
resolve(value);
};
const onAbort = () => finish(null);
diff --git a/server/services/voice/pipeline.test.js b/server/services/voice/pipeline.test.js
index 5278e76f47..2fbd86c177 100644
--- a/server/services/voice/pipeline.test.js
+++ b/server/services/voice/pipeline.test.js
@@ -166,6 +166,31 @@ describe('requestUiText (lazy visible-text fetch)', () => {
expect(state.uiTextWaiters.has(requestId)).toBe(false);
});
+ it('caches the text onto the current snapshot for a same-turn re-read', async () => {
+ const snap = { path: '/tasks', text: null };
+ const state = { ui: snap };
+ const emitted = [];
+ const p = requestUiText(state, (e, d) => emitted.push(d), undefined);
+ state.uiTextWaiters.get(emitted[0].requestId)('tasks page body');
+ await expect(p).resolves.toBe('tasks page body');
+ expect(state.ui.text).toBe('tasks page body');
+ });
+
+ it('resolves null (stale) and does not cache when the snapshot changed mid-flight', async () => {
+ const oldSnap = { path: '/tasks', text: null };
+ const state = { ui: oldSnap };
+ const emitted = [];
+ const p = requestUiText(state, (e, d) => emitted.push(d), undefined);
+ // Navigation: a new voice:ui:index replaced state.ui before the response.
+ const newSnap = { path: '/calendar', text: null };
+ state.ui = newSnap;
+ state.uiTextWaiters.get(emitted[0].requestId)('tasks page body');
+ await expect(p).resolves.toBeNull();
+ // Neither snapshot is polluted with the other page's text.
+ expect(oldSnap.text).toBeNull();
+ expect(newSnap.text).toBeNull();
+ });
+
it('resolves null on timeout (legacy client never replies)', async () => {
const state = {};
const emit = () => {};