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
fix: stop fixture cleanup on failures and preserve retryable parents (#11041)
Adam Eivy28b585ce7b95
diff --git a/scripts/cleanup-test-data.js b/scripts/cleanup-test-data.jsindex fd19ba0d43..d5424b8123 100644--- a/scripts/cleanup-test-data.js+++ b/scripts/cleanup-test-data.js@@ -42,9 +42,7 @@* records that happen to share a substring (e.g. a real series called* "Commit Universe Tour").*/-import { listUniverses, deleteUniverse } from '../server/services/universeBuilder.js';-import { listSeries, deleteSeries } from '../server/services/pipeline/series.js';-import { listIssues, deleteIssue } from '../server/services/pipeline/issues.js';+import { isDirectlyInvoked } from './lib/directInvocation.js';// Real records are ALWAYS minted with randomUUID() — universes get a bare UUID,// series an `ser-<uuid>`, issues an `iss-<uuid>` (server/services/pipeline/{series,@@ -104,15 +102,15 @@ function parseArgs(argv) {return out;}-async function main() {- const { apply } = parseArgs(process.argv);+async function cleanup({ apply, services, output }) {+ const { listUniverses, deleteUniverse, listSeries, deleteSeries, listIssues, deleteIssue } = services;if (!apply) {- console.log('🧪 DRY-RUN — pass --apply to actually soft-delete\n');+ output.log('🧪 DRY-RUN — pass --apply to actually soft-delete\n');} else {// Service-layer write tails are in-process — a concurrent portos-server// can clobber tombstones (last-writer-wins on the JSON file). Warn loudly.- console.log('⚠️ --apply: STOP the server before continuing or tombstones may be clobbered');- console.log('⚠️ Run `pm2 stop portos-server` first, then `pm2 start portos-server` to propagate\n');+ output.log('⚠️ --apply: STOP the server before continuing or tombstones may be clobbered');+ output.log('⚠️ Run `pm2 stop portos-server` first, then `pm2 start portos-server` to propagate\n');}// A record is a fixture target on EITHER signal: an impossible (non-minted)@@ -130,9 +128,9 @@ async function main() {const universeTargets = universes.map(u => ({ rec: u, reason: universeReason(u) })).filter(t => t.reason);- console.log(`🗑️ Universes to delete: ${universeTargets.length}`);+ output.log(`🗑️ Universes to delete: ${universeTargets.length}`);for (const { rec: u, reason } of universeTargets) {- console.log(` - ${u.id.slice(0, 8)} "${u.name}" (${reason}, updated ${u.updatedAt})`);+ output.log(` - ${u.id.slice(0, 8)} "${u.name}" (${reason}, updated ${u.updatedAt})`);}// Series + their child issues. Resolve children up front so the dry-run@@ -145,72 +143,99 @@ async function main() {// Map series.id → array of live child issues for that series.const childIssuesBySeries = new Map();for (const { rec: s } of seriesTargets) {- const children = await listIssues({ seriesId: s.id, includeDeleted: false }).catch(() => []);+ const children = await listIssues({ seriesId: s.id, includeDeleted: false });childIssuesBySeries.set(s.id, children);}const totalChildIssues = [...childIssuesBySeries.values()].reduce((n, arr) => n + arr.length, 0);- console.log(`\n🗑️ Series to delete: ${seriesTargets.length} (cascading ${totalChildIssues} child issue${totalChildIssues === 1 ? '' : 's'})`);+ output.log(`\n🗑️ Series to delete: ${seriesTargets.length} (cascading ${totalChildIssues} child issue${totalChildIssues === 1 ? '' : 's'})`);for (const { rec: s, reason } of seriesTargets) {const children = childIssuesBySeries.get(s.id) || [];- console.log(` - ${s.id.slice(0, 12)} "${s.name}" (${reason}, updated ${s.updatedAt}, ${children.length} child issue${children.length === 1 ? '' : 's'})`);+ output.log(` - ${s.id.slice(0, 12)} "${s.name}" (${reason}, updated ${s.updatedAt}, ${children.length} child issue${children.length === 1 ? '' : 's'})`);}// Orphan fixture issues: a non-uuid-id issue whose parent series is NOT itself// a target (so it won't be reached by the cascade above). These are direct// test-fixture inserts (`iss-live`, `iss-dup`, …) that would otherwise survive.- const allIssues = await listIssues({ includeDeleted: false }).catch(() => []);+ const allIssues = await listIssues({ includeDeleted: false });const orphanIssueTargets = allIssues.filter(i => isIssueFixtureId(i.id) && !seriesTargetIds.has(i.seriesId),);- console.log(`\n🗑️ Orphan fixture issues (non-uuid id, parent not targeted): ${orphanIssueTargets.length}`);+ output.log(`\n🗑️ Orphan fixture issues (non-uuid id, parent not targeted): ${orphanIssueTargets.length}`);for (const i of orphanIssueTargets) {- console.log(` - ${i.id} (series ${i.seriesId}, #${i.number})`);+ output.log(` - ${i.id} (series ${i.seriesId}, #${i.number})`);}if (!apply) {- console.log('\n🧪 DRY-RUN complete — re-run with --apply to write tombstones');+ output.log('\n🧪 DRY-RUN complete — re-run with --apply to write tombstones');return;}// Apply phase — universes first (their child series carry universeId// pointers, but the tombstone on the universe doesn't cascade to series;// series with this universeId orphan-but-live, which is fine).- console.log('\n🗑️ Applying universe tombstones...');+ output.log('\n🗑️ Applying universe tombstones...');for (const { rec: u } of universeTargets) {- await deleteUniverse(u.id).catch(err =>- console.error(` ⚠️ failed to delete universe ${u.id.slice(0, 8)}: ${err.message}`),- );+ await deleteUniverse(u.id).catch(err => {+ throw new Error(`Failed to delete universe ${u.id.slice(0, 8)}: ${err.message}`, { cause: err });+ });}// Series: tombstone the children FIRST so a re-run can't see them after// the parent series tombstone is gone (listIssues filters by seriesId so// an orphan child wouldn't surface in the dry-run either — children must// go via this script's pass, not the parent's GC). Then tombstone the series.- console.log('🗑️ Applying issue + series tombstones...');+ output.log('🗑️ Applying issue + series tombstones...');for (const { rec: s } of seriesTargets) {const children = childIssuesBySeries.get(s.id) || [];for (const i of children) {- await deleteIssue(i.id).catch(err =>- console.error(` ⚠️ failed to delete issue ${i.id.slice(0, 12)} (child of ${s.id.slice(0, 12)}): ${err.message}`),- );+ await deleteIssue(i.id).catch(err => {+ throw new Error(`Failed to delete issue ${i.id.slice(0, 12)} (child of ${s.id.slice(0, 12)}): ${err.message}`, { cause: err });+ });}- await deleteSeries(s.id).catch(err =>- console.error(` ⚠️ failed to delete series ${s.id.slice(0, 12)}: ${err.message}`),- );+ await deleteSeries(s.id).catch(err => {+ throw new Error(`Failed to delete series ${s.id.slice(0, 12)}: ${err.message}`, { cause: err });+ });}// Orphan fixture issues whose parent series wasn't itself a target.for (const i of orphanIssueTargets) {- await deleteIssue(i.id).catch(err =>- console.error(` ⚠️ failed to delete orphan issue ${i.id}: ${err.message}`),- );+ await deleteIssue(i.id).catch(err => {+ throw new Error(`Failed to delete orphan issue ${i.id}: ${err.message}`, { cause: err });+ });}- console.log(`\n✅ Tombstoned ${universeTargets.length} universe(s) + ${seriesTargets.length} series + ${totalChildIssues + orphanIssueTargets.length} issue(s).`);- console.log('🔄 Now restart the PM2 server to propagate the tombstones to peers:');- console.log(' pm2 restart portos-server');- console.log(' (peer:online will re-fire every per-record sub and the new tombstone');- console.log(' hashes will diverge from lastPushedHash so the deletes push.)');+ output.log(`\n✅ Tombstoned ${universeTargets.length} universe(s) + ${seriesTargets.length} series + ${totalChildIssues + orphanIssueTargets.length} issue(s).`);+ output.log('🔄 Now restart the PM2 server to propagate the tombstones to peers:');+ output.log(' pm2 restart portos-server');+ output.log(' (peer:online will re-fire every per-record sub and the new tombstone');+ output.log(' hashes will diverge from lastPushedHash so the deletes push.)');}-main().catch(err => {- console.error('❌ cleanup-test-data failed:', err);- process.exit(1);-});+/**+ * CLI boundary: injected services keep workflow tests out of the live DB and+ * peer-sync graph. Production imports happen only when running without them.+ * Returns the process exit code; already-written tombstones survive failures.+ */+export async function runCleanupTestData({ argv = process.argv, services, output = console } = {}) {+ try {+ if (!services) {+ const [universes, series, issues] = await Promise.all([+ import('../server/services/universeBuilder.js'),+ import('../server/services/pipeline/series.js'),+ import('../server/services/pipeline/issues.js'),+ ]);+ services = {+ listUniverses: universes.listUniverses, deleteUniverse: universes.deleteUniverse,+ listSeries: series.listSeries, deleteSeries: series.deleteSeries,+ listIssues: issues.listIssues, deleteIssue: issues.deleteIssue,+ };+ }+ await cleanup({ ...parseArgs(argv), services, output });+ return 0;+ } catch (err) {+ output.error(`❌ cleanup-test-data failed: ${err.message}`);+ output.error('❌ Cleanup incomplete; any earlier tombstones are retained. Re-run after fixing the failure.');+ return 1;+ }+}++if (isDirectlyInvoked(import.meta.url)) {+ process.exitCode = await runCleanupTestData();+}diff --git a/scripts/cleanup-test-data.test.js b/scripts/cleanup-test-data.test.jsnew file mode 100644index 0000000000..0b7b83ad52--- /dev/null+++ b/scripts/cleanup-test-data.test.js@@ -0,0 +1,172 @@+import { describe, expect, it, vi } from 'vitest';+import { runCleanupTestData } from './cleanup-test-data.js';++// Importing or exercising the injected workflow must never load production+// storage, provider, or peer-sync dependencies.+vi.mock('../server/services/universeBuilder.js', () => { throw new Error('Live universe service loaded'); });+vi.mock('../server/services/pipeline/series.js', () => { throw new Error('Live series service loaded'); });+vi.mock('../server/services/pipeline/issues.js', () => { throw new Error('Live issue service loaded'); });++const uuid = n => `00000000-0000-4000-8000-${String(n).padStart(12, '0')}`;+const U = uuid(1);+const S = `ser-${uuid(2)}`;+const REAL_S = `ser-${uuid(3)}`;+const CHILD_A = `iss-${uuid(4)}`;+const CHILD_B = `iss-${uuid(5)}`;+const REAL_I = `iss-${uuid(6)}`;++function fixtureStore() {+ const universes = [+ { id: U, name: ' Test Work ' },+ { id: 'fixture-universe', name: 'Example Universe' },+ { id: uuid(7), name: 'Commit Universe Tour' },+ { id: uuid(8), name: 'Test Work', deleted: true },+ ];+ const series = [+ { id: S, name: ' Commit S ' },+ { id: 'ser-fixture', name: 'Example Series' },+ { id: REAL_S, name: 'Commit Series Tour' },+ { id: `ser-${uuid(9)}`, name: 'Commit S', deleted: true },+ ];+ const issues = [+ { id: CHILD_A, seriesId: S, number: 1 },+ { id: CHILD_B, seriesId: S, number: 2 },+ { id: 'iss-fixture-child', seriesId: 'ser-fixture', number: 1 },+ { id: 'iss-fixture-orphan', seriesId: REAL_S, number: 1 },+ { id: REAL_I, seriesId: REAL_S, number: 2 },+ { id: 'iss-already-deleted', seriesId: S, deleted: true },+ ];+ const failures = new Set();+ const attempts = [];+ const completed = [];+ const list = (records, key, options) => {+ expect(options.includeDeleted).toBe(false);+ if (failures.has(key)) throw new Error(`Enumeration unavailable: ${key}`);+ return records.filter(r => !r.deleted);+ };+ const remove = (records, kind) => async id => {+ const key = `${kind}:${id}`;+ attempts.push(key);+ if (failures.has(key)) throw new Error(`Delete unavailable: ${key}`);+ const record = records.find(r => r.id === id);+ if (!record || record.deleted) throw new Error('Repeated or missing tombstone');+ record.deleted = true;+ completed.push(key);+ };+ const services = {+ listUniverses: async options => list(universes, 'universes', options),+ listSeries: async options => list(series, 'series', options),+ listIssues: async options => list(issues, options.seriesId ? 'children' : 'all-issues', options)+ .filter(i => !options.seriesId || i.seriesId === options.seriesId),+ deleteUniverse: remove(universes, 'universe'),+ deleteSeries: remove(series, 'series'),+ deleteIssue: remove(issues, 'issue'),+ };+ const run = async (apply = true) => {+ const logs = [];+ const errors = [];+ const code = await runCleanupTestData({+ argv: ['node', 'cleanup-test-data.js', ...(apply ? ['--apply'] : [])],+ services,+ output: { log: line => logs.push(line), error: line => errors.push(line) },+ });+ return { code, logs: logs.join('\n'), errors: errors.join('\n') };+ };+ return { universes, series, issues, failures, attempts, completed, run };+}++const ORDER = [+ `universe:${U}`,+ 'universe:fixture-universe',+ `issue:${CHILD_A}`,+ `issue:${CHILD_B}`,+ `series:${S}`,+ 'issue:iss-fixture-child',+ 'series:ser-fixture',+ 'issue:iss-fixture-orphan',+];++function expectFailure(result) {+ expect(result.code).toBe(1);+ expect(result.errors).toContain('cleanup-test-data failed');+ expect(result.logs).not.toMatch(/✅|DRY-RUN complete|Now restart/);+}++describe('fixture cleanup CLI workflow', () => {+ it('defaults to dry-run, preserves selection and real records, and applies children before parents exactly once', async () => {+ const store = fixtureStore();+ const dry = await store.run(false);+ expect(dry.code).toBe(0);+ expect(dry.errors).toBe('');+ expect(store.attempts).toEqual([]);+ expect(dry.logs).toContain('DRY-RUN complete');++ const applied = await store.run();+ expect(applied.code).toBe(0);+ expect(applied.errors).toBe('');+ const targetLines = result => result.logs.split('\n').filter(line => /to delete:|cascading|Orphan fixture|^ - /.test(line));+ expect(targetLines(applied)).toEqual(targetLines(dry));+ expect(store.completed).toEqual(ORDER);+ expect(applied.logs).toContain('✅ Tombstoned 2 universe(s) + 2 series + 4 issue(s).');+ expect(store.universes.find(u => u.id === uuid(7)).deleted).toBeUndefined();+ expect(store.series.find(s => s.id === REAL_S).deleted).toBeUndefined();+ expect(store.issues.find(i => i.id === REAL_I).deleted).toBeUndefined();++ const rerun = await store.run();+ expect(rerun.code).toBe(0);+ expect(rerun.logs).toContain('✅ Tombstoned 0 universe(s) + 0 series + 0 issue(s).');+ expect(store.attempts).toEqual(ORDER);+ });++ it.each(['universes', 'series', 'children', 'all-issues'])('refuses all writes when %s enumeration fails', async key => {+ const store = fixtureStore();+ store.failures.add(key);+ expectFailure(await store.run());+ expect(store.attempts).toEqual([]);+ expect(store.completed).toEqual([]);+ expectFailure(await store.run(false));+ expect(store.attempts).toEqual([]);+ });++ it('keeps the parent and remaining UUID child live after a child failure, then retries only remaining work', async () => {+ const store = fixtureStore();+ store.failures.add(`issue:${CHILD_B}`);+ const failed = await store.run();+ expectFailure(failed);+ expect(failed.errors).toContain('Failed to delete issue');+ expect(store.attempts).toEqual(ORDER.slice(0, 4));+ expect(store.completed).toEqual(ORDER.slice(0, 3));+ expect(store.issues.find(i => i.id === CHILD_B).deleted).toBeUndefined();+ expect(store.series.find(s => s.id === S).deleted).toBeUndefined();++ store.failures.clear();+ const retry = await store.run();+ expect(retry.code).toBe(0);+ expect(retry.logs).toContain('✅ Tombstoned 0 universe(s) + 2 series + 3 issue(s).');+ expect(store.completed).toEqual(ORDER);+ expect(store.attempts.slice(4)).toEqual(ORDER.slice(3));+ expect((await store.run()).code).toBe(0);+ expect(store.completed).toEqual(ORDER);+ });++ it.each([+ ['universe', 1],+ ['series', 4],+ ['orphan issue', 7],+ ])('stops on failed %s deletion without counting it, and safely resumes', async (kind, index) => {+ const store = fixtureStore();+ store.failures.add(ORDER[index]);+ const failed = await store.run();+ expectFailure(failed);+ expect(failed.errors).toContain(`Failed to delete ${kind}`);+ expect(store.attempts).toEqual(ORDER.slice(0, index + 1));+ expect(store.completed).toEqual(ORDER.slice(0, index));+ store.failures.clear();+ const retry = await store.run();+ expect(retry.code).toBe(0);+ expect(store.attempts.slice(index + 1)).toEqual(ORDER.slice(index));+ expect(store.completed).toEqual(ORDER);+ expect((await store.run()).code).toBe(0);+ expect(store.completed).toEqual(ORDER);+ });+});