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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
pin the envelope-extension strip/pending/withhold lifecycle before refactoring it (#6844)
[._.]/ Adam Eivy4f4ad86e2bfe
diff --git a/server/services/sharing/peerSync.test.js b/server/services/sharing/peerSync.test.jsindex 5f75bf068f..e110e4154f 100644--- a/server/services/sharing/peerSync.test.js+++ b/server/services/sharing/peerSync.test.js@@ -250,6 +250,7 @@ import {} from './peerSync.js';import { buildPushPayload } from './peerSyncPush.js';+import { ENVELOPE_EXTENSIONS } from './peerSyncShared.js';import { getPeers } from '../instances.js';import { getInstanceId } from '../instanceIdentity.js';import { getUniverse, mergeUniversesFromSync, listUniverses } from '../universeBuilder.js';@@ -4624,6 +4625,269 @@ describe('peerSync', () => {// -------------------------------------------------------------------------// mediaCollection push + receiver// -------------------------------------------------------------------------+ // -------------------------------------------------------------------------+ // #6844 — the optional envelope keys share ONE strip / pending / withhold+ // lifecycle, declared once in ENVELOPE_EXTENSIONS. Every case below is driven+ // off that table so a row added later is exercised on both sides of the wire+ // without a hand-written twin of each flag.+ describe('envelope extensions (#6844)', () => {+ const SIDECARS = ENVELOPE_EXTENSIONS.filter((e) => e.pendingKey);+ const SELF_RECONCILING = ENVELOPE_EXTENSIONS.filter((e) => !e.pendingKey);+ const PENDING_KEYS = SIDECARS.map((e) => e.pendingKey);++ const legacyRejects = (key) => {+ const rejection = { ok: false, status: 400, json: async () => ({+ code: 'VALIDATION_ERROR',+ message: 'Validation failed',+ context: { details: [{ path: '', message: `Unrecognized key(s) in object: '${key}'` }] },+ }) };+ rejection.clone = () => rejection;+ return rejection;+ };+ const accepts = (body = {}) => ({ ok: true, status: 200, json: async () => body });+ // A peer whose strict schema predates `key`: rejects every push carrying+ // it, accepts the stripped retry.+ const mockPeerRejecting = (key) => {+ vi.mocked(peerFetch).mockImplementation(async (_url, init) => (+ key in JSON.parse(init.body) ? legacyRejects(key) : accepts()+ ));+ };+ const armSeries = () => {+ vi.mocked(getSeries).mockResolvedValue({ id: 's1', name: 'Series' });+ vi.mocked(listIssuesForSeries).mockResolvedValue([{ id: 'i1', seriesId: 's1', number: 1 }]);+ };+ // How the sender comes to carry each key on the wire. Every table row MUST+ // have a carrier (guarded below) — that is what makes this suite grow with+ // the table instead of silently skipping a new extension.+ const CARRIERS = {+ portosMeta: {+ recordKind: 'universe', recordId: 'u1',+ arm: async () => { vi.mocked(getUniverse).mockResolvedValue({ id: 'u1', name: 'Foo' }); },+ },+ catalogBundle: {+ recordKind: 'universe', recordId: 'u1',+ arm: async () => {+ vi.mocked(getBackendName).mockReturnValue('postgres');+ vi.mocked(getUniverse).mockResolvedValue({ id: 'u1', name: 'Foo' });+ vi.mocked(getCatalogBundleForRef).mockResolvedValue({+ ingredients: [{ id: 'cat-chr-1', type: 'character', name: 'Hero', updatedAt: '2026-01-02T00:00:00Z' }],+ refs: [{ ingredientId: 'cat-chr-1', refKind: 'universe', refId: 'u1', role: 'canon-character', createdAt: '2026-01-02T00:00:00Z' }],+ });+ },+ },+ manuscriptReview: {+ recordKind: 'series', recordId: 's1',+ arm: async () => {+ armSeries();+ vi.mocked(getReview).mockResolvedValue({+ schemaVersion: 1,+ comments: [{ id: 'mrc-1', problem: 'pacing', status: 'open', updatedAt: '2026-06-02T00:00:00Z' }],+ });+ },+ },+ reverseOutline: {+ recordKind: 'series', recordId: 's1',+ arm: async () => {+ armSeries();+ vi.mocked(getStoredOutline).mockResolvedValue({+ seriesId: 's1', schemaVersion: 1, status: 'complete', generatedAt: '2026-06-02T00:00:00Z',+ plotlines: [{ id: 'a', label: 'A', kind: 'main', color: '#3b82f6' }],+ scenes: [{ id: 'scene-001', sequence: 0, summary: 'opening', plotlineId: 'a' }],+ });+ },+ },+ linkedTrack: {+ recordKind: 'musicVideoProject', recordId: 'mv-1',+ arm: async () => {+ vi.mocked(getPeers).mockResolvedValue([{+ instanceId: 'peer-a', name: 'Peer A', host: null, address: '10.0.0.2', port: 5555,+ enabled: true, syncEnabled: true, directions: ['outbound', 'inbound'],+ syncCategories: { musicVideoProjects: true },+ }]);+ await writeFile(join(PATHS.music, 'linked.mp3'), Buffer.from('linked track audio'));+ vi.mocked(getTrack).mockResolvedValue({+ id: 'track-1', title: 'Anthem', audioFilename: 'linked.mp3',+ updatedAt: '2026-06-28T00:00:00Z', deleted: false, deletedAt: null,+ });+ vi.mocked(getMusicVideoProject).mockResolvedValue({+ id: 'mv-1', name: 'Linked', mode: 'director', trackId: 'track-1',+ uploadedAudioFilename: null, scenes: [],+ updatedAt: '2026-06-28T00:00:00Z', deleted: false, deletedAt: null,+ });+ },+ },+ };+ const claim = ({ recordKind, recordId }) => subscribePeer(+ { peerId: 'peer-a', recordKind, recordId },+ { adoptedFromReverse: true },+ );+ const refresh = ({ recordKind, recordId }) => findPeerSubscription('peer-a', recordKind, recordId);+ const payloadOf = (call) => JSON.parse(call[1].body);++ it('pins the wire vocabulary older peers depend on', () => {+ // Older senders and receivers read exactly these names off the wire — a+ // rename here is a cross-version break, not a refactor.+ expect(ENVELOPE_EXTENSIONS.map(({ key, pendingKey }) => [key, pendingKey])).toEqual([+ ['portosMeta', null],+ ['catalogBundle', null],+ ['manuscriptReview', 'reviewSyncPending'],+ ['reverseOutline', 'outlineSyncPending'],+ ['linkedTrack', 'trackSyncPending'],+ ]);+ expect(Object.isFrozen(ENVELOPE_EXTENSIONS)).toBe(true);+ });++ it('has a sender-side carrier for every table row (a new extension must be exercised here)', () => {+ expect(Object.keys(CARRIERS).sort()).toEqual(ENVELOPE_EXTENSIONS.map((e) => e.key).sort());+ });++ describe.each(ENVELOPE_EXTENSIONS)('$key — strip', ({ key }) => {+ it('is dropped — and only it — on the one retry a legacy peer\'s strict-schema 400 earns', async () => {+ const carrier = CARRIERS[key];+ await carrier.arm();+ mockPeerRejecting(key);+ const result = await pushRecordToPeer(await claim(carrier));+ expect(result.pushed).toBe(true);+ const calls = vi.mocked(peerFetch).mock.calls;+ expect(calls).toHaveLength(2);+ const first = payloadOf(calls[0]);+ const retry = payloadOf(calls[1]);+ expect(first[key]).toBeDefined();+ expect(retry[key]).toBeUndefined();+ // Surgical: every other extension the first push carried survives, so+ // a peer that supports portosMeta but not this key keeps its handshake.+ for (const other of ENVELOPE_EXTENSIONS) {+ if (other.key !== key && other.key in first) expect(retry[other.key]).toEqual(first[other.key]);+ }+ expect(retry.record.id).toBe(carrier.recordId);+ });+ });++ describe.each(SIDECARS)('$key — no independent cycle ($pendingKey)', ({ key, pendingKey }) => {+ it('withholds lastPushedHash and water-marks lastPushedLegacyHash after a stripped push (#3928)', async () => {+ const carrier = CARRIERS[key];+ await carrier.arm();+ mockPeerRejecting(key);+ const result = await pushRecordToPeer(await claim(carrier));+ expect(result.pushed).toBe(true);+ const refreshed = await refresh(carrier);+ expect(refreshed.lastPushedHash).toBeFalsy();+ expect(refreshed.lastPushedLegacyHash).toBe(result.hash);+ // An unchanged record settles instead of re-running the 400 + retry pair.+ const second = await pushRecordToPeer(refreshed);+ expect(second.reason).toBe('unchanged-legacy-stripped');+ expect(vi.mocked(peerFetch).mock.calls).toHaveLength(2);+ });++ it('withholds the hash WITHOUT a legacy water-mark when the receiver reports it pending (transient merge failure)', async () => {+ const carrier = CARRIERS[key];+ await carrier.arm();+ vi.mocked(peerFetch).mockResolvedValue(accepts({ [pendingKey]: true }));+ const result = await pushRecordToPeer(await claim(carrier));+ expect(result.pushed).toBe(true);+ expect(vi.mocked(peerFetch).mock.calls).toHaveLength(1);+ const refreshed = await refresh(carrier);+ expect(refreshed.lastPushedHash).toBeFalsy();+ expect(refreshed.lastPushedLegacyHash).toBeFalsy();+ // The next cycle must genuinely re-push, not short-circuit.+ vi.mocked(peerFetch).mockClear();+ vi.mocked(peerFetch).mockResolvedValue(accepts());+ const second = await pushRecordToPeer(refreshed);+ expect(second.pushed).toBe(true);+ expect(vi.mocked(peerFetch).mock.calls).toHaveLength(1);+ });+ });++ describe.each(SELF_RECONCILING)('$key — own reconciliation cycle', ({ key }) => {+ it('saves lastPushedHash normally after a stripped push (nothing is owed through this hash)', async () => {+ const carrier = CARRIERS[key];+ await carrier.arm();+ mockPeerRejecting(key);+ const result = await pushRecordToPeer(await claim(carrier));+ expect(result.pushed).toBe(true);+ const refreshed = await refresh(carrier);+ expect(refreshed.lastPushedHash).toBe(result.hash);+ expect(refreshed.lastPushedLegacyHash).toBeFalsy();+ const second = await pushRecordToPeer(refreshed);+ expect(second.reason).toBe('unchanged');+ });+ });++ it('leaves the bundled-track GC floor unstamped when linkedTrack was stripped for a legacy peer (#1922)', async () => {+ // The project landed (its own confirmed floor advances) but the peer+ // never got the track, so the bundle-specific floor tombstoneGc's+ // `track` cutoff reads off musicVideoProject rows must stay unset.+ const carrier = CARRIERS.linkedTrack;+ await carrier.arm();+ mockPeerRejecting('linkedTrack');+ const sub = await claim(carrier);+ expect(sub.lastConfirmedTrackBundleAtMs).toBeNull();+ const result = await pushRecordToPeer(sub);+ expect(result.pushed).toBe(true);+ const refreshed = await refresh(carrier);+ expect(refreshed.lastConfirmedPushedAt).toBeTruthy();+ expect(refreshed.lastConfirmedTrackBundleAtMs).toBeNull();+ });++ describe('receiver — applyIncomingPush', () => {+ // How each sidecar reaches the receiver, and the merge that can throw.+ const RECEIVE_CARRIERS = {+ manuscriptReview: {+ merge: mergeReviewFromSync,+ payload: () => ({+ kind: 'series',+ record: { id: 's1', name: 'S', deleted: false, deletedAt: null },+ issues: [],+ manuscriptReview: { schemaVersion: 1, comments: [{ id: 'mrc-1', problem: 'x', status: 'open', updatedAt: '2026-06-02T00:00:00Z' }] },+ assetManifest: [],+ sourceInstanceId: 'peer-a',+ }),+ },+ reverseOutline: {+ merge: mergeOutlineFromSync,+ payload: () => ({+ kind: 'series',+ record: { id: 's1', name: 'S', deleted: false, deletedAt: null },+ issues: [],+ reverseOutline: {+ schemaVersion: 1, status: 'complete', generatedAt: '2026-06-02T00:00:00Z',+ plotlines: [{ id: 'a', label: 'A', kind: 'main' }],+ scenes: [{ id: 'scene-001', sequence: 0, summary: 'opening', plotlineId: 'a' }],+ },+ assetManifest: [],+ sourceInstanceId: 'peer-a',+ }),+ },+ linkedTrack: {+ merge: mergeTracksFromSync,+ payload: () => ({+ kind: 'musicVideoProject',+ record: { id: 'mv-9', trackId: 'track-9', deleted: false, deletedAt: null },+ linkedTrack: { id: 'track-9', audioFilename: 'linked.mp3' },+ assetManifest: [],+ sourceInstanceId: 'peer-a',+ }),+ },+ };++ it('has a receiver-side carrier for every sidecar row', () => {+ expect(Object.keys(RECEIVE_CARRIERS).sort()).toEqual(SIDECARS.map((e) => e.key).sort());+ });++ it.each(SIDECARS)('raises exactly $pendingKey when the bundled $key merge throws, and no flag when it succeeds', async ({ key, pendingKey }) => {+ const { merge, payload } = RECEIVE_CARRIERS[key];+ const clean = await applyIncomingPush(payload());+ for (const flag of PENDING_KEYS) expect(clean).not.toHaveProperty(flag);+ vi.mocked(merge).mockRejectedValueOnce(new Error('disk full'));+ const failed = await applyIncomingPush(payload());+ expect(failed[pendingKey]).toBe(true);+ for (const flag of PENDING_KEYS) {+ if (flag !== pendingKey) expect(failed).not.toHaveProperty(flag);+ }+ });+ });+ });+describe('collectCollectionAssetReferences', () => {it('maps items to image/video refs with an empty directImageRefFilenames', () => {const refs = collectCollectionAssetReferences({ items: [diff --git a/server/services/sharing/peerSyncShared.js b/server/services/sharing/peerSyncShared.jsindex a8585a92c6..94669eb7fd 100644--- a/server/services/sharing/peerSyncShared.js+++ b/server/services/sharing/peerSyncShared.js@@ -17,6 +17,55 @@ import { getPeers, resolveEffectiveCategories } from '../instances.js';export const PEER_SUBSCRIBABLE_KINDS = Object.freeze(['universe', 'series', 'mediaCollection', 'author', 'artist', 'album', 'track', 'creativeDirectorProject', 'moodBoard', 'fableLoom', 'writersRoomWork', 'writersRoomFolder', 'writersRoomExercise', 'musicVideoProject', 'commissionFeedback', 'creativeCommission']);+/**+ * The optional top-level keys a NEWER sender may attach to a push envelope+ * beyond the `{ kind, record, assetManifest, sourceInstanceId }` core. These+ * are the mechanism the schema-version design relies on for "degrades+ * gracefully on older peers" (`server/lib/schemaVersions.js`), and every row+ * shares ONE lifecycle that both sides of the wire drive off this table+ * (#6844) instead of a hand-written flag pair per key:+ *+ * 1. STRIP — an older receiver's `.strict()` push schema predates the key,+ * so it 400-rejects the whole envelope at Zod, naming the key. The+ * sender drops whichever listed keys the receiver named and retries+ * once so the record still lands; the stripped feature reaches that+ * peer once it upgrades, because the next push re-includes the key.+ * 2. PENDING — `pendingKey` is the response flag the RECEIVER raises when+ * that bundle's merge threw after the record itself merged (2xx). The+ * sender reads the same flag back, and counts its own strip as pending.+ * 3. WITHHOLD — while a row is pending the sender withholds `lastPushedHash`+ * so the next cycle re-sends instead of short-circuiting as `unchanged`+ * (`resolvePushWatermarks` in peerSyncPush.js, including #3928's+ * `lastPushedLegacyHash` for the stable strip case).+ *+ * `pendingKey: null` marks a key with its OWN reconciliation cycle — the+ * version-gate handshake re-includes `portosMeta` on every push, and catalog+ * sync reconciles `catalogBundle`'s rows — so a strip there is not pending+ * and the hash saves normally. The three sidecar docs have no independent+ * cycle; they are the ones a saved hash would strand.+ *+ * The wire names are frozen: older senders and receivers read exactly these+ * `pendingKey`s, and `server/lib/peerSyncValidation.js` validates the+ * envelope keys. Adding a bundled doc is one row here plus its build/merge+ * hooks — never a new flag pair. `peerSync.test.js` ("envelope extensions")+ * fails until every row has a carrier on both sides.+ */+export const ENVELOPE_EXTENSIONS = Object.freeze([+ Object.freeze({ key: 'portosMeta', pendingKey: null }),+ Object.freeze({ key: 'catalogBundle', pendingKey: null }),+ Object.freeze({ key: 'manuscriptReview', pendingKey: 'reviewSyncPending' }),+ Object.freeze({ key: 'reverseOutline', pendingKey: 'outlineSyncPending' }),+ Object.freeze({ key: 'linkedTrack', pendingKey: 'trackSyncPending' }),+]);++/**+ * Envelope key → the receiver's pending flag, for the sidecar rows only+ * (derived from ENVELOPE_EXTENSIONS; the self-reconciling keys are absent).+ */+export const ENVELOPE_PENDING_KEYS = Object.freeze(Object.fromEntries(+ ENVELOPE_EXTENSIONS.filter((e) => e.pendingKey).map((e) => [e.key, e.pendingKey]),+));+/*** Cross-cutting event bus for the peer-sync receiver. The asset-pull worker* emits `asset-arrived` ({ filename, kind, peerId }) when a previously-missing