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: pure setState in toggleStar + retry annotation manifest

[._.]/ Adam Eivy58699ef347f2
diff --git a/client/src/hooks/useMediaAnnotations.js b/client/src/hooks/useMediaAnnotations.js
index 20c77dc7ca..6f0e167159 100644
--- a/client/src/hooks/useMediaAnnotations.js
+++ b/client/src/hooks/useMediaAnnotations.js
@@ -1,4 +1,4 @@
-import { useCallback, useEffect, useState } from 'react';
+import { useCallback, useEffect, useRef, useState } from 'react';
import { listMediaAnnotations, setMediaAnnotation } from '../services/api';
import socket from '../services/socket';
import toast from '../components/ui/Toast';
@@ -32,6 +32,12 @@ function enrichAll(raw) {
export function useMediaAnnotations() {
const [annotations, setAnnotations] = useState({});
+ // Mirror the latest annotations in a ref so toggleStar can read prior state
+ // without using setAnnotations as a side-channel. Calling setState with a
+ // side effect inside the updater function is unsafe under StrictMode /
+ // concurrent rendering (state updaters must be pure).
+ const annotationsRef = useRef(annotations);
+ useEffect(() => { annotationsRef.current = annotations; }, [annotations]);
useEffect(() => {
let cancelled = false;
@@ -96,15 +102,16 @@ export function useMediaAnnotations() {
return res?.entry ?? null;
}, []);
- // Reads prior starred state from the functional setter so the callback's
- // identity doesn't change on every annotations update (otherwise every
- // MediaCard receives a new `onToggleStar` prop after each star flip).
+ // Reads prior starred state from the ref so the callback's identity stays
+ // stable across annotations updates (otherwise every MediaCard receives a
+ // new `onToggleStar` prop after each star flip). The ref pattern replaces
+ // the previous setAnnotations-as-side-channel, which violated React's
+ // "state updaters must be pure" contract under StrictMode / concurrent
+ // rendering.
const toggleStar = useCallback((item) => {
if (!item?.key) return;
- setAnnotations((prev) => {
- updateAnnotation(item.key, { starred: !prev[item.key]?.own?.starred });
- return prev;
- });
+ const priorStarred = !!annotationsRef.current[item.key]?.own?.starred;
+ updateAnnotation(item.key, { starred: !priorStarred });
}, [updateAnnotation]);
return { annotations, toggleStar, updateAnnotation };
diff --git a/server/services/sharing/importer.js b/server/services/sharing/importer.js
index 65d2670240..4547298fd9 100644
--- a/server/services/sharing/importer.js
+++ b/server/services/sharing/importer.js
@@ -450,6 +450,17 @@ export async function processManifest(bucketId, manifestFilename) {
// that merges author-by-author into data/media-annotations.json.
if (manifest.kind === 'media-annotations') {
const outcome = await processAnnotationManifest(bucket, manifest);
+ // If the per-instance record JSON hasn't synced yet (cloud sync can deliver
+ // the manifest before the record), leave the manifest un-cursored so the
+ // watcher retries when records/ changes. Mirrors the asset/record pending
+ // path below — without this, a manifest delivered before its record would
+ // get markProcessed'd permanently and the annotation update would be lost.
+ if (outcome.missing) {
+ outcome.pendingRecords = [manifest.recordIds?.[0] || manifest.senderInstanceId];
+ sharingEvents.emit('manifest-processed', { bucketId, manifestId: manifest.id, manifestFilename, outcome });
+ console.log(`⏳ sharing: bucket=${bucket.name} manifest=${manifest.id} kind=media-annotations waitingForRecord=${outcome.reason}`);
+ return { processed: true, pending: true, manifest, outcome };
+ }
await markProcessed(bucketId, manifestFilename, manifest.id);
sharingEvents.emit('manifest-processed', { bucketId, manifestId: manifest.id, manifestFilename, outcome });
console.log(`📥 sharing: bucket=${bucket.name} manifest=${manifest.id} kind=media-annotations applied=${outcome.applied}`);