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

feat(sync): sync character avatar image between instances

Adam Eivy907c226fdcd2
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 6daf3ce870..2dbb1f0fb5 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -6,6 +6,8 @@
## Fixed
+- Character avatar image now synced between instances alongside character metadata
+- Snapshot sync status no longer shows false "behind" when remote doesn't report checksums
- Agent worktree branches now cleaned up even when worktree directory is already gone (fixes lingering branches after server restarts)
- Instance names like "null", "void", "NaN", "undefined" are now accepted as valid hostnames
- Peer renames no longer revert when remote peer re-announces (handleAnnounce now preserves user-set names)
diff --git a/server/services/syncOrchestrator.js b/server/services/syncOrchestrator.js
index c72f1344df..0ae6436e4f 100644
--- a/server/services/syncOrchestrator.js
+++ b/server/services/syncOrchestrator.js
@@ -6,7 +6,8 @@
* Maintains per-peer cursors and triggers sync on peer connect + interval.
*/
-import { writeFile, rename } from 'fs/promises';
+import { writeFile, rename, access } from 'fs/promises';
+import { join } from 'path';
import { readJSONFile, ensureDir, PATHS, dataPath } from '../lib/fileUtils.js';
import { createMutex } from '../lib/asyncMutex.js';
import { instanceEvents } from './instanceEvents.js';
@@ -72,6 +73,36 @@ async function fetchPeer(peer, path) {
}
}
+/**
+ * Fetch an image from a peer if we don't have it locally.
+ * avatarPath is like "/data/images/uuid.png"
+ */
+async function syncImageFromPeer(peer, avatarPath) {
+ const filename = avatarPath.split('/').pop();
+ if (!filename) return;
+ const localPath = join(PATHS.images, filename);
+
+ // Skip if we already have it
+ const exists = await access(localPath).then(() => true).catch(() => false);
+ if (exists) return;
+
+ const url = `http://${peer.address}:${peer.port}${avatarPath}`;
+ const controller = new AbortController();
+ const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
+ try {
+ const res = await fetch(url, { signal: controller.signal });
+ if (!res.ok) return;
+ const buffer = Buffer.from(await res.arrayBuffer());
+ await ensureDir(PATHS.images);
+ await writeFile(localPath, buffer);
+ console.log(`🔄 Synced avatar image: ${filename}`);
+ } catch {
+ // Non-critical — avatar will sync on next cycle
+ } finally {
+ clearTimeout(timeout);
+ }
+}
+
// --- Status ---
/**
@@ -238,6 +269,12 @@ async function syncDataCategoryFromPeer(peer, peerId, category, cachedChecksums)
if (!snapshot?.data) return { totalApplied: 0, checksum: null };
const result = await dataSync.applyRemote(category, snapshot.data);
+
+ // After character sync, fetch avatar image if we don't have it locally
+ if (category === 'character' && snapshot.data?.avatarPath) {
+ await syncImageFromPeer(peer, snapshot.data.avatarPath);
+ }
+
return { totalApplied: result.applied ? result.count : 0, checksum: snapshot.checksum };
}