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: only confirm/ack a bundled track delivery when it actually applied

[._.]/ Adam Eivy7100854be6ae
diff --git a/server/services/sharing/peerSync.test.js b/server/services/sharing/peerSync.test.js
index ff474037d9..a13a256ce8 100644
--- a/server/services/sharing/peerSync.test.js
+++ b/server/services/sharing/peerSync.test.js
@@ -2481,6 +2481,51 @@ describe('peerSync', () => {
expect(refreshed.lastConfirmedPushedAt).toBeTruthy();
});
+ it('does NOT stamp lastConfirmedTrackBundleAtMs when the project still has a trackId but the sender omitted linkedTrack (#1922)', async () => {
+ // Regression (codex review round 3): a project can still carry a
+ // `trackId` while buildPushPayload's own getTrack() lookup transiently
+ // returns null/throws, so `linkedTrack` is omitted from the payload
+ // entirely. The receiver never sees a linkedTrack key, so it can't
+ // report trackSyncPending either — `!trackSyncPending` alone would
+ // wrongly look "confirmed". A track is still owed (trackId is set), so
+ // the bundle floor must stay unstamped until a future push actually
+ // ships the track.
+ enableMusicVideoPeer();
+ vi.mocked(getTrack).mockResolvedValue(null); // transient lookup failure
+ vi.mocked(getMusicVideoProject).mockResolvedValue({
+ id: 'mv-12', name: 'Lookup failed', mode: 'director', trackId: 'track-12',
+ uploadedAudioFilename: null, scenes: [],
+ updatedAt: '2026-06-28T00:00:00Z', deleted: false, deletedAt: null,
+ });
+ vi.mocked(peerFetch).mockResolvedValue({ ok: true, json: async () => ({ missingAssets: [] }) });
+ const sub = await subscribePeer(
+ { peerId: 'peer-a', recordKind: 'musicVideoProject', recordId: 'mv-12' },
+ { adoptedFromReverse: true },
+ );
+ await pushRecordToPeer(sub);
+ const refreshed = await findPeerSubscription('peer-a', 'musicVideoProject', 'mv-12');
+ expect(refreshed.lastConfirmedTrackBundleAtMs).toBeNull();
+ expect(refreshed.lastConfirmedPushedAt).toBeTruthy();
+ });
+
+ it('DOES stamp lastConfirmedTrackBundleAtMs when the project has no trackId at all (nothing owed)', async () => {
+ enableMusicVideoPeer();
+ vi.mocked(getMusicVideoProject).mockResolvedValue({
+ id: 'mv-13', name: 'No track', mode: 'director', trackId: null,
+ uploadedAudioFilename: 'upload.mp3', scenes: [],
+ updatedAt: '2026-06-28T00:00:00Z', deleted: false, deletedAt: null,
+ });
+ vi.mocked(peerFetch).mockResolvedValue({ ok: true, json: async () => ({ missingAssets: [] }) });
+ const before = Date.now();
+ const sub = await subscribePeer(
+ { peerId: 'peer-a', recordKind: 'musicVideoProject', recordId: 'mv-13' },
+ { adoptedFromReverse: true },
+ );
+ await pushRecordToPeer(sub);
+ const refreshed = await findPeerSubscription('peer-a', 'musicVideoProject', 'mv-13');
+ expect(refreshed.lastConfirmedTrackBundleAtMs).toBeGreaterThanOrEqual(before);
+ });
+
it('omits linkedTrack when the project has no linked track', async () => {
enableMusicVideoPeer();
vi.mocked(getMusicVideoProject).mockResolvedValue({
@@ -3038,6 +3083,53 @@ describe('peerSync', () => {
expect(result.ackedDeletesUpTo).toBe(0);
});
+ it('does NOT fold a mismatched-id linkedTrack tombstone into ackedDeletesUpTo (merge gate refused it, never applied)', async () => {
+ // Regression (codex review round 3 on #1922): the merge gate refuses a
+ // linkedTrack whose id doesn't match the project's trackId (anti-
+ // smuggling guard), so mergeTracksFromSync never runs. Folding its
+ // deletedAt into ackedDeletesUpTo anyway would tell the sender "applied"
+ // for a tombstone this receiver never actually merged.
+ const result = await applyIncomingPush({
+ kind: 'musicVideoProject',
+ record: { id: 'mv-14', trackId: 'track-A', deleted: false, deletedAt: null },
+ linkedTrack: { id: 'track-SMUGGLED', deleted: true, deletedAt: '2026-04-02T00:00:00Z' },
+ assetManifest: [],
+ sourceInstanceId: 'peer-a',
+ });
+ expect(mergeTracksFromSync).not.toHaveBeenCalled();
+ expect(result.ackedDeletesUpTo).toBe(0);
+ });
+
+ it('does NOT fold a bundled linkedTrack tombstone into ackedDeletesUpTo when its merge fails (trackSyncPending)', async () => {
+ // Regression (codex review round 3 on #1922): a thrown merge means the
+ // receiver did NOT actually apply the tombstone — acking it anyway would
+ // let the sender's cursor advance past a deletion that still needs a
+ // retry (the exact failure mode #1922 closes for the success path).
+ vi.mocked(mergeTracksFromSync).mockRejectedValueOnce(new Error('disk full'));
+ const result = await applyIncomingPush({
+ kind: 'musicVideoProject',
+ record: { id: 'mv-15', trackId: 'track-15', deleted: false, deletedAt: null },
+ linkedTrack: { id: 'track-15', deleted: true, deletedAt: '2026-04-03T00:00:00Z' },
+ assetManifest: [],
+ sourceInstanceId: 'peer-a',
+ });
+ expect(result.trackSyncPending).toBe(true);
+ expect(result.ackedDeletesUpTo).toBe(0);
+ });
+
+ it('does NOT fold a bundled linkedTrack tombstone into ackedDeletesUpTo for a local-ephemeral project (merge skipped, opted out)', async () => {
+ vi.mocked(getMusicVideoProject).mockResolvedValueOnce({ id: 'mv-16', ephemeral: true });
+ const result = await applyIncomingPush({
+ kind: 'musicVideoProject',
+ record: { id: 'mv-16', trackId: 'track-16', deleted: false, deletedAt: null },
+ linkedTrack: { id: 'track-16', deleted: true, deletedAt: '2026-04-04T00:00:00Z' },
+ assetManifest: [],
+ sourceInstanceId: 'peer-a',
+ });
+ expect(mergeTracksFromSync).not.toHaveBeenCalled();
+ expect(result.ackedDeletesUpTo).toBe(0);
+ });
+
it('auto-creates a reverse subscription back to the sender', async () => {
// The merge path actually landed the record locally, so the
// classifyLocalRecord('universe', 'u1') call inside
diff --git a/server/services/sharing/peerSyncPush.js b/server/services/sharing/peerSyncPush.js
index 91558a1f22..fed078ffe3 100644
--- a/server/services/sharing/peerSyncPush.js
+++ b/server/services/sharing/peerSyncPush.js
@@ -431,9 +431,24 @@ export async function pushRecordToPeer(sub, options = {}) {
// latter advances even when the bundled track merge failed or was stripped
// for a legacy peer (`trackSyncPending`) — using it here would let GC treat
// an unconfirmed bundled tombstone as delivered.
+ //
+ // `!trackSyncPending` alone isn't enough: `buildPushPayload` can OMIT
+ // `linkedTrack` even though the project still has a `trackId` (the sender's
+ // `getTrack()` lookup returned null or threw — transient or the track was
+ // hard-deleted out from under it) — the receiver never sees a `linkedTrack`
+ // key at all in that case, so it can't report `trackSyncPending` either.
+ // `payload.record` keeps `trackId` for both live AND tombstoned
+ // musicVideoProject pushes (whole-record LWW, no tombstone field-stripping —
+ // see `sanitizeRecordForWire`'s `musicVideoProject` case), so gate on: no
+ // track is owed at all (`trackId` absent), OR a `linkedTrack` was actually
+ // included in this push.
+ const trackOwed = isStr(payload.record?.trackId);
+ const trackBundleConfirmed = sub.recordKind === 'musicVideoProject'
+ && !trackSyncPending
+ && (!trackOwed || Boolean(payload.linkedTrack));
await persistPushSuccess(sub.id, (missingCount > 0 || reviewSyncPending || outlineSyncPending || trackSyncPending) ? null : hash, {
confirmedAtMs: Date.now(),
- trackBundleConfirmed: sub.recordKind === 'musicVideoProject' && !trackSyncPending,
+ trackBundleConfirmed,
});
if (Number.isFinite(body?.ackedDeletesUpTo) && body.ackedDeletesUpTo > 0) {
await ackDeletesUpTo(sub.peerId, body.ackedDeletesUpTo).catch(() => {});
diff --git a/server/services/sharing/peerSyncReceive.js b/server/services/sharing/peerSyncReceive.js
index 34098c2a3a..b44c1f74fe 100644
--- a/server/services/sharing/peerSyncReceive.js
+++ b/server/services/sharing/peerSyncReceive.js
@@ -260,6 +260,13 @@ export async function applyIncomingPush(payload) {
// so a swallowed merge failure would strand the record the receiver needs to
// render. Signal so the sender withholds lastPushedHash and re-sends.
let trackSyncPending = false;
+ // #1922: true only once the bundled linkedTrack merge actually RAN and
+ // succeeded — gates whether `linkedTrack` may contribute to
+ // `ackedDeletesUpTo` below. Stays false on every skip path (mismatched id,
+ // local-ephemeral, tombstoned project) and on a merge failure
+ // (`trackSyncPending`), so the sender can never be told "the receiver
+ // applied this track tombstone" when it didn't.
+ let linkedTrackApplied = false;
// Set true when a writersRoomWork merge accepted the remote (insert/remote-won)
// — gates whether a present-but-different local draft body may be overwritten.
let workMergeApplied = false;
@@ -341,7 +348,9 @@ export async function applyIncomingPush(payload) {
// music-video push (the project only needs the track it actually links).
if (!localEphemeral && record.deleted !== true && isPlainObject(linkedTrack)
&& linkedTrack.id === record.trackId) {
- await mergeTracksFromSync([linkedTrack], { source }).catch((err) => {
+ await mergeTracksFromSync([linkedTrack], { source }).then(() => {
+ linkedTrackApplied = true;
+ }).catch((err) => {
console.log(`⚠️ peerSync: linkedTrack merge failed: ${err.message}`);
trackSyncPending = true;
});
@@ -405,6 +414,12 @@ export async function applyIncomingPush(payload) {
// independent `track` subscription would never ack a bundled track delete
// through ANY cursor, so the sender's track-tombstone GC could prune the
// tombstone before a transient delivery failure got a chance to retry).
+ // Only pass `linkedTrack` through when `linkedTrackApplied` — i.e. the
+ // merge gate above actually ran AND succeeded. Acking it unconditionally
+ // would tell the sender "delivered" for a track that was skipped (mismatched
+ // id, local-ephemeral, tombstoned project) or whose merge threw
+ // (`trackSyncPending`), letting the sender's cursor advance past a deletion
+ // this receiver never actually applied.
// We return this to the sender so THEY can advance THEIR cursor (which
// tracks what we — the receiver — have acked of THEIR pushes). We do
// NOT call ackDeletesUpTo here: that would write
@@ -416,7 +431,7 @@ export async function applyIncomingPush(payload) {
// seen them — even though peer-A is just telling us about ITS own
// tombstones. In bidirectional sync, that lets peer-A's stale live
// records resurrect after GC drops our tombstones.
- const ackedDeletesUpTo = computeAckedDeletesFromPayload(record, issues, linkedTrack);
+ const ackedDeletesUpTo = computeAckedDeletesFromPayload(record, issues, linkedTrackApplied ? linkedTrack : null);
// Best-effort reverse subscription. Failures don't fail the push — the
// record is already merged and the response will tell the sender what