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: fire federation side-effects on insertXxxWithId tombstone resurrection

Adam Eivy41565dee2466
diff --git a/server/services/pipeline/issues.js b/server/services/pipeline/issues.js
index 39b23fc078..ae8cb45ef4 100644
--- a/server/services/pipeline/issues.js
+++ b/server/services/pipeline/issues.js
@@ -669,9 +669,10 @@ export function insertIssueWithId(input = {}) {
if (existingIdx >= 0 && !state.issues[existingIdx].deleted) {
throw makeErr(`Issue id already exists: ${input.id}`, ERR_DUPLICATE);
}
+ const wasResurrection = existingIdx >= 0;
const next = sanitizeIssue({ ...input, seriesId, title });
if (!next) throw makeErr('Invalid issue payload', ERR_VALIDATION);
- if (existingIdx >= 0) {
+ if (wasResurrection) {
console.warn(`♻️ insertIssueWithId: overwriting tombstone for ${input.id}`);
state.issues[existingIdx] = next;
} else {
@@ -681,6 +682,9 @@ export function insertIssueWithId(input = {}) {
// comes from (volume order, arcPosition) of the local state.
await renumberInline(state, seriesId, next.seasonId || UNSCOPED_ANCHOR);
await saveIssuesNow(state.issues.filter((i) => i.seriesId === seriesId));
+ // Mirror createIssue's federation side-effect on tombstone-overwrite:
+ // issues ride series-level events, so notify peers via the parent series.
+ if (wasResurrection) emitRecordUpdated('series', next.seriesId);
return next;
});
}
diff --git a/server/services/pipeline/issues.test.js b/server/services/pipeline/issues.test.js
index db0e4dfb37..0eca166d24 100644
--- a/server/services/pipeline/issues.test.js
+++ b/server/services/pipeline/issues.test.js
@@ -34,6 +34,7 @@ vi.mock('../sharing/peerSync.js', () => mockNoPeerSync());
const svc = await import('./issues.js');
const seriesSvc = await import('./series.js');
const seasonsSvc = await import('./seasons.js');
+const { recordEvents } = await import('../sharing/recordEvents.js');
describe('pipeline issues service', () => {
beforeEach(() => {
@@ -733,6 +734,31 @@ describe('pipeline issues service', () => {
expect(restored).toMatchObject({ id, title: 'Restored', deleted: false });
});
+ it('insertIssueWithId resurrection fires emitRecordUpdated on the parent series', async () => {
+ const id = 'iss-550e8400-e29b-41d4-a716-44665544abcd';
+ await svc.insertIssueWithId({ id, seriesId: 'ser-resurrect', title: 'ToResurrect' });
+ await svc.deleteIssue(id);
+
+ const emitSpy = vi.spyOn(recordEvents, 'emit');
+
+ await svc.insertIssueWithId({ id, seriesId: 'ser-resurrect', title: 'Resurrected' });
+
+ expect(emitSpy).toHaveBeenCalledWith('updated', { recordKind: 'series', recordId: 'ser-resurrect' });
+ emitSpy.mockRestore();
+ });
+
+ it('insertIssueWithId fresh insert does NOT fire emitRecordUpdated', async () => {
+ const id = 'iss-550e8400-e29b-41d4-a716-44665544abe0';
+ const emitSpy = vi.spyOn(recordEvents, 'emit');
+
+ await svc.insertIssueWithId({ id, seriesId: 'ser-fresh', title: 'Brand New' });
+
+ // insertIssueWithId (unlike createIssue) does not fire emitRecordUpdated
+ // on a fresh insert — only on tombstone resurrection.
+ expect(emitSpy).not.toHaveBeenCalledWith('updated', { recordKind: 'series', recordId: 'ser-fresh' });
+ emitSpy.mockRestore();
+ });
+
it('insertIssueWithId still rejects DUPLICATE on a LIVE record', async () => {
const id = 'iss-550e8400-e29b-41d4-a716-44665544abcf';
await svc.insertIssueWithId({ id, seriesId: 'ser-1', title: 'First' });
diff --git a/server/services/pipeline/series.js b/server/services/pipeline/series.js
index 4ae9dbf3d6..aa882376c2 100644
--- a/server/services/pipeline/series.js
+++ b/server/services/pipeline/series.js
@@ -232,7 +232,7 @@ export async function insertSeriesWithId(input = {}) {
}
const name = trimTo(input.name, NAME_MAX);
if (!name) throw makeErr(`Series name is required (1..${NAME_MAX} chars)`, ERR_VALIDATION);
- return store().queueRecordWrite(input.id, async () => {
+ const { next, wasResurrection } = await store().queueRecordWrite(input.id, async () => {
// Tombstone-overwrite: same contract as universeBuilder.insertUniverseWithId —
// re-import undeletes; peer-sync resurrection is prevented at the merge
// path via LWW, not here.
@@ -240,14 +240,26 @@ export async function insertSeriesWithId(input = {}) {
if (existing && !existing.deleted) {
throw makeErr(`Series id already exists: ${input.id}`, ERR_DUPLICATE);
}
+ const wasResurrection = !!existing;
const next = sanitizeSeries({ ...input, name });
if (!next) throw makeErr('Invalid series payload', ERR_VALIDATION);
- if (existing) {
+ if (wasResurrection) {
console.warn(`♻️ insertSeriesWithId: overwriting tombstone for ${input.id}`);
}
await store().saveOneNow(next.id, next);
- return next;
+ return { next, wasResurrection };
});
+ // Mirror createSeries's federation side-effects on tombstone-overwrite:
+ // peers that still have the deleted record need the resurrection propagated.
+ if (wasResurrection && !next.ephemeral) {
+ emitRecordUpdated('series', next.id);
+ import('../sharing/peerSync.js').then(({ autoSubscribeRecordToAllPeers }) =>
+ autoSubscribeRecordToAllPeers('series', next.id)
+ ).catch((err) => {
+ console.log(`⚠️ series: auto-subscribe after resurrection failed: ${err.message}`);
+ });
+ }
+ return next;
}
export async function updateSeries(id, patch = {}) {
diff --git a/server/services/pipeline/series.test.js b/server/services/pipeline/series.test.js
index 8564d80e6d..f808a922d7 100644
--- a/server/services/pipeline/series.test.js
+++ b/server/services/pipeline/series.test.js
@@ -21,6 +21,8 @@ vi.mock('../instances.js', () => mockNoPeers());
vi.mock('../sharing/peerSync.js', () => mockNoPeerSync());
const svc = await import('./series.js');
+const peerSyncMock = await import('../sharing/peerSync.js');
+const { recordEvents } = await import('../sharing/recordEvents.js');
describe('pipeline series service', () => {
beforeEach(() => {
@@ -121,6 +123,35 @@ describe('pipeline series service', () => {
expect((await svc.listSeries()).map((s) => s.id)).toContain(id);
});
+ it('insertSeriesWithId resurrection fires emitRecordUpdated + autoSubscribeRecordToAllPeers', async () => {
+ const id = 'ser-550e8400-e29b-41d4-a716-44665544abcf';
+ await svc.insertSeriesWithId({ id, name: 'ToResurrect' });
+ await svc.deleteSeries(id);
+
+ const emitSpy = vi.spyOn(recordEvents, 'emit');
+ const subscribeSpy = vi.spyOn(peerSyncMock, 'autoSubscribeRecordToAllPeers');
+
+ await svc.insertSeriesWithId({ id, name: 'Resurrected' });
+ // Allow the fire-and-forget peerSync dynamic import to settle.
+ await new Promise((r) => setTimeout(r, 0));
+
+ expect(emitSpy).toHaveBeenCalledWith('updated', { recordKind: 'series', recordId: id });
+ expect(subscribeSpy).toHaveBeenCalledWith('series', id);
+
+ emitSpy.mockRestore();
+ subscribeSpy.mockRestore();
+ });
+
+ it('insertSeriesWithId fresh insert does NOT fire emitRecordUpdated', async () => {
+ const id = 'ser-550e8400-e29b-41d4-a716-44665544abd0';
+ const emitSpy = vi.spyOn(recordEvents, 'emit');
+
+ await svc.insertSeriesWithId({ id, name: 'Fresh' });
+
+ expect(emitSpy).not.toHaveBeenCalledWith('updated', { recordKind: 'series', recordId: id });
+ emitSpy.mockRestore();
+ });
+
it('insertSeriesWithId still rejects DUPLICATE on a LIVE record', async () => {
const id = 'ser-550e8400-e29b-41d4-a716-44665544abce';
await svc.insertSeriesWithId({ id, name: 'First' });
diff --git a/server/services/universeBuilder.js b/server/services/universeBuilder.js
index 9a1150e819..1234ea62d3 100644
--- a/server/services/universeBuilder.js
+++ b/server/services/universeBuilder.js
@@ -958,7 +958,7 @@ export async function insertUniverseWithId(input = {}) {
const name = trimTo(input.name, NAME_MAX_LENGTH);
if (!name) throw makeErr(`Universe name is required (1..${NAME_MAX_LENGTH} chars)`, ERR_VALIDATION);
const s = store();
- return s.queueRecordWrite(input.id, async () => {
+ const { next, wasResurrection } = await s.queueRecordWrite(input.id, async () => {
// Tombstone-overwrite: a previously-deleted record with the same id is
// overwritten (effectively undeleted) — this keeps the share-bucket
// re-import flow idempotent (deleting then re-importing the same manifest
@@ -969,15 +969,27 @@ export async function insertUniverseWithId(input = {}) {
if (existing && !existing.deleted) {
throw makeErr(`Universe id already exists: ${input.id}`, ERR_DUPLICATE);
}
+ const wasResurrection = !!existing;
const next = sanitizeTemplate({ ...input, name });
if (!next) throw makeErr('Invalid universe payload', ERR_VALIDATION);
- if (existing) {
+ if (wasResurrection) {
console.warn(`♻️ insertUniverseWithId: overwriting tombstone for ${input.id}`);
}
await ensureDir(s.recordDir(next.id));
await atomicWrite(s.recordPath(next.id), next);
- return next;
+ return { next, wasResurrection };
});
+ // Mirror createUniverse's federation side-effects on tombstone-overwrite:
+ // peers that still have the deleted record need the resurrection propagated.
+ if (wasResurrection && !next.ephemeral) {
+ emitRecordUpdated('universe', next.id);
+ import('./sharing/peerSync.js').then(({ autoSubscribeRecordToAllPeers }) =>
+ autoSubscribeRecordToAllPeers('universe', next.id)
+ ).catch((err) => {
+ console.log(`⚠️ universe: auto-subscribe after resurrection failed: ${err.message}`);
+ });
+ }
+ return next;
}
export async function updateUniverse(id, patchOrMutator = {}) {
diff --git a/server/services/universeBuilder.test.js b/server/services/universeBuilder.test.js
index 270a9db24b..c5f585a1c3 100644
--- a/server/services/universeBuilder.test.js
+++ b/server/services/universeBuilder.test.js
@@ -80,6 +80,8 @@ vi.mock("crypto", async () => {
});
const svc = await import("./universeBuilder.js");
+const peerSyncMock = await import("./sharing/peerSync.js");
+const { recordEvents } = await import("./sharing/recordEvents.js");
// Default universe with non-empty influences. Override `influences` for tests
// that need isolation from the seed tokens.
@@ -630,6 +632,35 @@ describe("universeBuilder service", () => {
expect((await svc.listUniverses()).map((u) => u.id)).toContain(id);
});
+ it("insertUniverseWithId resurrection fires emitRecordUpdated + autoSubscribeRecordToAllPeers", async () => {
+ const id = "550e8400-e29b-41d4-a716-44665544abcf";
+ await svc.insertUniverseWithId({ id, name: "ToResurrect" });
+ await svc.deleteUniverse(id);
+
+ const emitSpy = vi.spyOn(recordEvents, "emit");
+ const subscribeSpy = vi.spyOn(peerSyncMock, "autoSubscribeRecordToAllPeers");
+
+ await svc.insertUniverseWithId({ id, name: "Resurrected" });
+ // Allow the fire-and-forget peerSync dynamic import to settle.
+ await new Promise((r) => setTimeout(r, 0));
+
+ expect(emitSpy).toHaveBeenCalledWith("updated", { recordKind: "universe", recordId: id });
+ expect(subscribeSpy).toHaveBeenCalledWith("universe", id);
+
+ emitSpy.mockRestore();
+ subscribeSpy.mockRestore();
+ });
+
+ it("insertUniverseWithId fresh insert does NOT fire emitRecordUpdated", async () => {
+ const id = "550e8400-e29b-41d4-a716-44665544abd0";
+ const emitSpy = vi.spyOn(recordEvents, "emit");
+
+ await svc.insertUniverseWithId({ id, name: "Fresh" });
+
+ expect(emitSpy).not.toHaveBeenCalledWith("updated", { recordKind: "universe", recordId: id });
+ emitSpy.mockRestore();
+ });
+
it("insertUniverseWithId still rejects DUPLICATE on a LIVE record", async () => {
const id = "550e8400-e29b-41d4-a716-44665544abce";
await svc.insertUniverseWithId({ id, name: "First" });