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

fix: tolerate empty/non-GUID forPeer on the sync-status probe instead of 500ing every cycle

[._.]/ Adam Eivy0c9bbcd45b1c
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index c226275bf9..39e98e0e36 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -60,6 +60,8 @@
## Federation
+- **A peer probe with an empty or non-GUID instance id no longer spams the log with a 500 every sync cycle.** `GET /api/instances/sync-status` validated the `?forPeer=<id>` query param with `z.string().guid()` — far stricter than the `getSyncStatus()` service it feeds, which only ever uses the value as a cursor-cache key. The endpoint is reachable by any tailnet machine, and a prober running an older PortOS, carrying the `unknown` instance-id sentinel, or emitting a bare `?forPeer=` (empty string, which `.guid()` rejects) made the route throw `ServerError: Invalid GUID` on every probe — even though the sibling `/api/sync/*` checksum/snapshot routes already accept the same id leniently. The route now mirrors `dataSync.js`'s `forPeerOf` handling (trim + 128-char cap, blank/array → unscoped), so the probe degrades to the unscoped global response instead of a repeating 500. (`server/routes/instances.js`)
+
- **[issue-1562] New "Full mirror" mode lets a peer mirror *everything* with one toggle.** Federation used to require enabling each sync category by hand, and records that existed *before* a peer was added were never back-subscribed — so you could never be sure a node pair was a true replica. The Instances page now has a per-peer **Full mirror** switch: turning it on enables every sync category at once (shown locked-on), back-subscribes all of your existing universes, series, media collections, authors, and music records to that peer, and is reciprocated automatically so the other machine mirrors you back. It's future-proof — any new kind of record added in a later version is covered with no per-peer change — and a live **"Fully mirrored ✓ / N pending"** indicator (counted from records actually confirmed-delivered, not internal cursors) tells you the real replication state at a glance. A **"Default new peers to full mirror"** switch on *This Instance* makes every peer you add from now on start in full-mirror mode.
- **[issue-1636] Reciprocal sync (including full mirror) is now truly two-directional when one machine added the other.** When machine A added peer B and asked B to mirror back — but the user on B never explicitly added A — B adopted the sync categories yet still never pushed its *own* existing universes, series, authors, and music back to A, so the "mirror" was silently one-directional until someone on B also connected to A. B now treats A's explicit reciprocal-sync request as permission to push back and starts mirroring its existing records to A right away. Machines that mutually added each other were already fine; this only fixes the asymmetric "A added B" setup.
diff --git a/server/routes/instances.js b/server/routes/instances.js
index 1e7c8473c8..e611e5e264 100644
--- a/server/routes/instances.js
+++ b/server/routes/instances.js
@@ -129,8 +129,22 @@ router.get('/', asyncHandler(async (req, res) => {
// into its data (`cursorForYou`) — how far we've pulled from it. That cursor is
// the peer's push-frontier toward us, so it can render an outbound "N to push"
// count. Older peers omit the param and get the legacy (inbound-only) shape.
+//
+// `forPeer` is consumed by getSyncStatus() purely as a cursor-cache KEY
+// (`cursors[forPeer]`), never as a structurally-validated id — so we must NOT
+// demand canonical GUID format here. The endpoint is reachable by ANY tailnet
+// machine (not just peers in our list), and a prober running an older PortOS,
+// carrying the `unknown` instance-id sentinel, or emitting a bare `?forPeer=`
+// (empty string — which `.guid()` rejects) legitimately hits us; rejecting it
+// threw a 500 ServerError every probe cycle instead of degrading to the
+// unscoped (global) response. Mirror the lenient trim + length-cap the sibling
+// /api/sync/* routes already apply (forPeerOf in dataSync.js): any non-empty
+// string is a valid key; absent/blank/array → unscoped.
const syncStatusQuerySchema = z.object({
- forPeer: z.string().guid().optional()
+ forPeer: z.preprocess(
+ (v) => (typeof v === 'string' && v.trim().length > 0 ? v.trim().slice(0, 128) : undefined),
+ z.string().optional()
+ )
});
router.get('/sync-status', asyncHandler(async (req, res) => {
const { forPeer } = syncStatusQuerySchema.parse(req.query);
diff --git a/server/routes/instances.test.js b/server/routes/instances.test.js
new file mode 100644
index 0000000000..f315d9cc97
--- /dev/null
+++ b/server/routes/instances.test.js
@@ -0,0 +1,97 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import express from 'express';
+import { request } from '../lib/testHelper.js';
+import { errorMiddleware } from '../lib/errorHandler.js';
+
+// Only GET /api/instances/sync-status is exercised here — it's the forPeer
+// validation that regressed (a non-GUID peer probe threw a 500 every sync
+// cycle). Heavy service deps are mocked so importing the router is cheap.
+vi.mock('../services/syncOrchestrator.js', () => ({
+ getSyncStatus: vi.fn(),
+ syncWithPeer: vi.fn(),
+}));
+vi.mock('../services/instances.js', () => ({}));
+vi.mock('../services/sharing/peerSync.js', () => ({
+ getFullSyncCoverageForPeer: vi.fn(),
+}));
+vi.mock('../services/certProvisioner.js', () => ({
+ provisionTailscaleCert: vi.fn(),
+}));
+vi.mock('../lib/tailscale.js', () => ({
+ findTailscale: vi.fn(() => null),
+}));
+
+import { getSyncStatus } from '../services/syncOrchestrator.js';
+import instancesRoutes from './instances.js';
+
+const buildApp = () => {
+ const app = express();
+ app.use(express.json());
+ app.use('/api/instances', instancesRoutes);
+ app.use(errorMiddleware);
+ return app;
+};
+
+describe('GET /api/instances/sync-status — forPeer scoping', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ getSyncStatus.mockResolvedValue({ local: { brainSeq: 0, memorySeq: 0, checksums: {} } });
+ });
+
+ it('threads a canonical GUID forPeer through to getSyncStatus', async () => {
+ const id = '191aaece-a492-41ee-a66d-d4661eadc132';
+ const res = await request(buildApp()).get(`/api/instances/sync-status?forPeer=${id}`);
+ expect(res.status).toBe(200);
+ expect(getSyncStatus).toHaveBeenCalledWith({ includeChecksums: true, forPeer: id });
+ });
+
+ it('accepts a non-GUID forPeer (older PortOS / `unknown` sentinel) instead of throwing a 500', async () => {
+ // Regression: an older PortOS or a prober carrying the `unknown` instance-id
+ // sentinel hits us with a non-GUID value. The route must degrade
+ // gracefully, not throw a ServerError on every probe cycle.
+ const res = await request(buildApp()).get('/api/instances/sync-status?forPeer=unknown');
+ expect(res.status).toBe(200);
+ expect(getSyncStatus).toHaveBeenCalledWith({ includeChecksums: true, forPeer: 'unknown' });
+ });
+
+ it('accepts a bare empty ?forPeer= (the value z.string().guid() used to 500 on) → undefined', async () => {
+ const res = await request(buildApp()).get('/api/instances/sync-status?forPeer=');
+ expect(res.status).toBe(200);
+ expect(getSyncStatus).toHaveBeenCalledWith({ includeChecksums: true, forPeer: undefined });
+ });
+
+ it('trims whitespace and caps an oversized forPeer at 128 chars', async () => {
+ const long = 'x'.repeat(200);
+ const res = await request(buildApp()).get(`/api/instances/sync-status?forPeer=%20${long}%20`);
+ expect(res.status).toBe(200);
+ expect(getSyncStatus).toHaveBeenCalledWith({ includeChecksums: true, forPeer: 'x'.repeat(128) });
+ });
+
+ it('drops a blank/whitespace-only forPeer to undefined (unscoped self-view)', async () => {
+ const res = await request(buildApp()).get('/api/instances/sync-status?forPeer=%20%20');
+ expect(res.status).toBe(200);
+ expect(getSyncStatus).toHaveBeenCalledWith({ includeChecksums: true, forPeer: undefined });
+ });
+
+ it('drops a repeated forPeer (array) to undefined so only a scalar id scopes', async () => {
+ const res = await request(buildApp()).get('/api/instances/sync-status?forPeer=a&forPeer=b');
+ expect(res.status).toBe(200);
+ expect(getSyncStatus).toHaveBeenCalledWith({ includeChecksums: true, forPeer: undefined });
+ });
+
+ it('omits forPeer entirely (legacy inbound-only shape) → undefined', async () => {
+ const res = await request(buildApp()).get('/api/instances/sync-status');
+ expect(res.status).toBe(200);
+ expect(getSyncStatus).toHaveBeenCalledWith({ includeChecksums: true, forPeer: undefined });
+ });
+
+ it('surfaces cursorForYou only when getSyncStatus returns one', async () => {
+ getSyncStatus.mockResolvedValue({
+ local: { brainSeq: 3, memorySeq: 5, checksums: { universe: 'abc' } },
+ cursorForYou: 42,
+ });
+ const res = await request(buildApp()).get('/api/instances/sync-status?forPeer=peer-1');
+ expect(res.status).toBe(200);
+ expect(res.body).toMatchObject({ brainSeq: 3, memorySeq: 5, cursorForYou: 42 });
+ });
+});