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

refactor: collapse the two world-stage seed migrations into one multi-stage slot (#2175)

[._.]/ Adam Eivy18f4f697a62f
diff --git a/scripts/migrations/165-editorial-world-doctrine-stages.js b/scripts/migrations/165-editorial-world-doctrine-stages.js
new file mode 100644
index 0000000000..b0f1c2ecbd
--- /dev/null
+++ b/scripts/migrations/165-editorial-world-doctrine-stages.js
@@ -0,0 +1,20 @@
+/**
+ * Seed the worldbuilding-doctrine editorial stages into existing installs (#2175):
+ * - pipeline-editorial-world-unforeshadowed-solution
+ * - pipeline-editorial-world-cost-free-power
+ *
+ * Mirrors the multi-stage seed precedent (091/094/095/107): copies each `.md`
+ * template from `data.reference/prompts/stages/` when missing and merges its
+ * stage-config entry when absent, in a single migration slot. Boot runs migrations
+ * (server/index.js) but NOT `setup-data.js`, so an upgrade that pulls + `pm2
+ * restart`s (rather than running `update.sh`) would otherwise leave these stages
+ * unseeded and the `world.unforeshadowed-solution` / `world.cost-free-power`
+ * editorial checks would throw "Stage not found" the first time they run.
+ */
+
+import { makeSeedMigrations } from './_seedStageHelpers.js';
+
+export default makeSeedMigrations([
+ 'pipeline-editorial-world-unforeshadowed-solution',
+ 'pipeline-editorial-world-cost-free-power',
+]);
diff --git a/scripts/migrations/165-editorial-world-doctrine-stages.test.js b/scripts/migrations/165-editorial-world-doctrine-stages.test.js
new file mode 100644
index 0000000000..6bf9ff6d04
--- /dev/null
+++ b/scripts/migrations/165-editorial-world-doctrine-stages.test.js
@@ -0,0 +1,106 @@
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { mkdtempSync, rmSync, mkdirSync, writeFileSync, readFileSync, existsSync } from 'fs';
+import { tmpdir } from 'os';
+import { join, dirname } from 'path';
+import { fileURLToPath } from 'url';
+
+import migration from './165-editorial-world-doctrine-stages.js';
+
+// scripts/migrations/<this> → ../.. is the repo root (matches _testHelpers.js).
+const repoRoot = join(dirname(fileURLToPath(import.meta.url)), '..', '..');
+
+// First-shipment multi-stage seed migration (091/094/095/107 family) — no MD5
+// bookkeeping, so the seed / no-clobber behavior is asserted directly here.
+
+const FILES = [
+ { filename: 'pipeline-editorial-world-unforeshadowed-solution.md', stageKey: 'pipeline-editorial-world-unforeshadowed-solution', body: '# Unforeshadowed solution\n\nshipped body\n' },
+ { filename: 'pipeline-editorial-world-cost-free-power.md', stageKey: 'pipeline-editorial-world-cost-free-power', body: '# Cost-free power\n\nshipped body\n' },
+];
+
+describe('migration 165 — seed editorial worldbuilding-doctrine stages', () => {
+ let rootDir;
+ let stagesDir;
+ let refStagesDir;
+ let installedConfigPath;
+
+ const seedReference = ({ withConfig = true } = {}) => {
+ for (const { filename, body } of FILES) {
+ writeFileSync(join(refStagesDir, filename), body);
+ }
+ if (withConfig) {
+ const refConfigPath = join(rootDir, 'data.reference', 'prompts', 'stage-config.json');
+ const stages = {};
+ for (const { stageKey } of FILES) {
+ stages[stageKey] = { name: stageKey, description: `${stageKey} desc`, model: 'default', returnsJson: true, variables: [] };
+ }
+ writeFileSync(refConfigPath, JSON.stringify({ stages }, null, 2) + '\n');
+ }
+ };
+
+ beforeEach(() => {
+ rootDir = mkdtempSync(join(tmpdir(), 'migration-165-'));
+ stagesDir = join(rootDir, 'data', 'prompts', 'stages');
+ refStagesDir = join(rootDir, 'data.reference', 'prompts', 'stages');
+ installedConfigPath = join(rootDir, 'data', 'prompts', 'stage-config.json');
+ mkdirSync(stagesDir, { recursive: true });
+ mkdirSync(refStagesDir, { recursive: true });
+ });
+
+ afterEach(() => {
+ rmSync(rootDir, { recursive: true, force: true });
+ });
+
+ it('seeds both .md templates and merges both stage-config entries on a fresh install', async () => {
+ seedReference();
+
+ await expect(migration.up({ rootDir })).resolves.not.toThrow();
+
+ for (const { filename, body } of FILES) {
+ expect(readFileSync(join(stagesDir, filename), 'utf8')).toBe(body);
+ }
+ const installed = JSON.parse(readFileSync(installedConfigPath, 'utf8'));
+ for (const { stageKey } of FILES) {
+ expect(installed.stages[stageKey]).toMatchObject({ name: stageKey, returnsJson: true });
+ }
+ });
+
+ it('is idempotent and never clobbers a customized .md or an existing stage-config entry', async () => {
+ seedReference();
+ const customBody = '# CUSTOMIZED — do not overwrite\n';
+ for (const { filename } of FILES) {
+ writeFileSync(join(stagesDir, filename), customBody);
+ }
+ writeFileSync(
+ installedConfigPath,
+ JSON.stringify({ stages: { [FILES[0].stageKey]: { name: 'user-tuned', model: 'custom' } } }, null, 2) + '\n',
+ );
+
+ await migration.up({ rootDir });
+
+ for (const { filename } of FILES) {
+ expect(readFileSync(join(stagesDir, filename), 'utf8')).toBe(customBody);
+ }
+ const installed = JSON.parse(readFileSync(installedConfigPath, 'utf8'));
+ // Existing entry preserved verbatim; the missing second one is added.
+ expect(installed.stages[FILES[0].stageKey]).toEqual({ name: 'user-tuned', model: 'custom' });
+ expect(installed.stages[FILES[1].stageKey]).toMatchObject({ name: FILES[1].stageKey });
+ });
+
+ it('does not throw when the data.reference samples are missing', async () => {
+ await expect(migration.up({ rootDir })).resolves.not.toThrow();
+ for (const { filename } of FILES) {
+ expect(existsSync(join(stagesDir, filename))).toBe(false);
+ }
+ });
+
+ it('matches the live shipped stage-config keys + prompt files (drift catch)', () => {
+ const refConfig = JSON.parse(
+ readFileSync(join(repoRoot, 'data.reference', 'prompts', 'stage-config.json'), 'utf8'),
+ );
+ for (const { stageKey, filename } of FILES) {
+ expect(refConfig.stages[stageKey], `stage-config missing ${stageKey}`).toBeTruthy();
+ expect(refConfig.stages[stageKey].returnsJson).toBe(true);
+ expect(existsSync(join(repoRoot, 'data.reference', 'prompts', 'stages', filename))).toBe(true);
+ }
+ });
+});
diff --git a/scripts/migrations/165-editorial-world-unforeshadowed-solution-stage.js b/scripts/migrations/165-editorial-world-unforeshadowed-solution-stage.js
deleted file mode 100644
index 2bdd1608d6..0000000000
--- a/scripts/migrations/165-editorial-world-unforeshadowed-solution-stage.js
+++ /dev/null
@@ -1,16 +0,0 @@
-/**
- * Seed the `pipeline-editorial-world-unforeshadowed-solution` stage into existing
- * installs (#2175).
- *
- * Mirrors `130-editorial-character-consistency-stage.js`: copies the `.md` template
- * from `data.reference/prompts/stages/` and merges the stage-config entry into
- * `data/prompts/stage-config.json`. Boot runs migrations (server/index.js) but NOT
- * `setup-data.js`, so an upgrade that pulls + `pm2 restart`s (rather than running
- * `update.sh`) would otherwise leave the new stage unseeded and the
- * `world.unforeshadowed-solution` editorial check would throw "Stage not found" the
- * first time it runs.
- */
-
-import { makeSeedMigration } from './_seedStageHelpers.js';
-
-export default makeSeedMigration('pipeline-editorial-world-unforeshadowed-solution');
diff --git a/scripts/migrations/165-editorial-world-unforeshadowed-solution-stage.test.js b/scripts/migrations/165-editorial-world-unforeshadowed-solution-stage.test.js
deleted file mode 100644
index b295881b6b..0000000000
--- a/scripts/migrations/165-editorial-world-unforeshadowed-solution-stage.test.js
+++ /dev/null
@@ -1,84 +0,0 @@
-import { describe, it, expect, beforeEach, afterEach } from 'vitest';
-import { mkdtempSync, rmSync, mkdirSync, writeFileSync, readFileSync, existsSync } from 'fs';
-import { tmpdir } from 'os';
-import { join, dirname } from 'path';
-import { fileURLToPath } from 'url';
-
-import migration from './165-editorial-world-unforeshadowed-solution-stage.js';
-
-// scripts/migrations/<this> → ../.. is the repo root (matches _testHelpers.js).
-const repoRoot = join(dirname(fileURLToPath(import.meta.url)), '..', '..');
-
-// First-shipment seed migration (mirrors 130) — no MD5 bookkeeping, so the
-// seed / no-clobber behavior is asserted directly.
-
-const FILENAME = 'pipeline-editorial-world-unforeshadowed-solution.md';
-const STAGE_KEY = 'pipeline-editorial-world-unforeshadowed-solution';
-const BODY = '# Unforeshadowed solution\n\nshipped body\n';
-
-describe('migration 165 — seed editorial-world-unforeshadowed-solution stage', () => {
- let rootDir;
- let stagesDir;
- let refStagesDir;
- let installedConfigPath;
-
- const seedReference = () => {
- writeFileSync(join(refStagesDir, FILENAME), BODY);
- const refConfigPath = join(rootDir, 'data.reference', 'prompts', 'stage-config.json');
- writeFileSync(
- refConfigPath,
- JSON.stringify({ stages: { [STAGE_KEY]: { name: STAGE_KEY, description: 'desc', model: 'default', returnsJson: true, variables: [] } } }, null, 2) + '\n',
- );
- };
-
- beforeEach(() => {
- rootDir = mkdtempSync(join(tmpdir(), 'migration-165-'));
- stagesDir = join(rootDir, 'data', 'prompts', 'stages');
- refStagesDir = join(rootDir, 'data.reference', 'prompts', 'stages');
- installedConfigPath = join(rootDir, 'data', 'prompts', 'stage-config.json');
- mkdirSync(stagesDir, { recursive: true });
- mkdirSync(refStagesDir, { recursive: true });
- });
-
- afterEach(() => {
- rmSync(rootDir, { recursive: true, force: true });
- });
-
- it('seeds the .md template and merges the stage-config entry on a fresh install', async () => {
- seedReference();
- await expect(migration.up({ rootDir })).resolves.not.toThrow();
- expect(readFileSync(join(stagesDir, FILENAME), 'utf8')).toBe(BODY);
- const installed = JSON.parse(readFileSync(installedConfigPath, 'utf8'));
- expect(installed.stages[STAGE_KEY]).toMatchObject({ returnsJson: true });
- });
-
- it('is idempotent and never clobbers a customized .md or an existing stage-config entry', async () => {
- seedReference();
- const customBody = '# CUSTOMIZED — do not overwrite\n';
- writeFileSync(join(stagesDir, FILENAME), customBody);
- writeFileSync(
- installedConfigPath,
- JSON.stringify({ stages: { [STAGE_KEY]: { name: 'user-tuned', model: 'custom' } } }, null, 2) + '\n',
- );
-
- await migration.up({ rootDir });
-
- expect(readFileSync(join(stagesDir, FILENAME), 'utf8')).toBe(customBody);
- const installed = JSON.parse(readFileSync(installedConfigPath, 'utf8'));
- expect(installed.stages[STAGE_KEY]).toEqual({ name: 'user-tuned', model: 'custom' });
- });
-
- it('does not throw when the data.reference samples are missing', async () => {
- await expect(migration.up({ rootDir })).resolves.not.toThrow();
- expect(existsSync(join(stagesDir, FILENAME))).toBe(false);
- });
-
- it('matches the live shipped stage-config key + prompt file (drift catch)', () => {
- const refConfig = JSON.parse(
- readFileSync(join(repoRoot, 'data.reference', 'prompts', 'stage-config.json'), 'utf8'),
- );
- expect(refConfig.stages[STAGE_KEY], `stage-config missing ${STAGE_KEY}`).toBeTruthy();
- expect(refConfig.stages[STAGE_KEY].returnsJson).toBe(true);
- expect(existsSync(join(repoRoot, 'data.reference', 'prompts', 'stages', FILENAME))).toBe(true);
- });
-});
diff --git a/scripts/migrations/166-editorial-world-cost-free-power-stage.js b/scripts/migrations/166-editorial-world-cost-free-power-stage.js
deleted file mode 100644
index 19727ac7fb..0000000000
--- a/scripts/migrations/166-editorial-world-cost-free-power-stage.js
+++ /dev/null
@@ -1,16 +0,0 @@
-/**
- * Seed the `pipeline-editorial-world-cost-free-power` stage into existing installs
- * (#2175).
- *
- * Mirrors `130-editorial-character-consistency-stage.js`: copies the `.md` template
- * from `data.reference/prompts/stages/` and merges the stage-config entry into
- * `data/prompts/stage-config.json`. Boot runs migrations (server/index.js) but NOT
- * `setup-data.js`, so an upgrade that pulls + `pm2 restart`s (rather than running
- * `update.sh`) would otherwise leave the new stage unseeded and the
- * `world.cost-free-power` editorial check would throw "Stage not found" the first
- * time it runs.
- */
-
-import { makeSeedMigration } from './_seedStageHelpers.js';
-
-export default makeSeedMigration('pipeline-editorial-world-cost-free-power');
diff --git a/scripts/migrations/166-editorial-world-cost-free-power-stage.test.js b/scripts/migrations/166-editorial-world-cost-free-power-stage.test.js
deleted file mode 100644
index 3fbdf6b68c..0000000000
--- a/scripts/migrations/166-editorial-world-cost-free-power-stage.test.js
+++ /dev/null
@@ -1,84 +0,0 @@
-import { describe, it, expect, beforeEach, afterEach } from 'vitest';
-import { mkdtempSync, rmSync, mkdirSync, writeFileSync, readFileSync, existsSync } from 'fs';
-import { tmpdir } from 'os';
-import { join, dirname } from 'path';
-import { fileURLToPath } from 'url';
-
-import migration from './166-editorial-world-cost-free-power-stage.js';
-
-// scripts/migrations/<this> → ../.. is the repo root (matches _testHelpers.js).
-const repoRoot = join(dirname(fileURLToPath(import.meta.url)), '..', '..');
-
-// First-shipment seed migration (mirrors 130) — no MD5 bookkeeping, so the
-// seed / no-clobber behavior is asserted directly.
-
-const FILENAME = 'pipeline-editorial-world-cost-free-power.md';
-const STAGE_KEY = 'pipeline-editorial-world-cost-free-power';
-const BODY = '# Cost-free power\n\nshipped body\n';
-
-describe('migration 166 — seed editorial-world-cost-free-power stage', () => {
- let rootDir;
- let stagesDir;
- let refStagesDir;
- let installedConfigPath;
-
- const seedReference = () => {
- writeFileSync(join(refStagesDir, FILENAME), BODY);
- const refConfigPath = join(rootDir, 'data.reference', 'prompts', 'stage-config.json');
- writeFileSync(
- refConfigPath,
- JSON.stringify({ stages: { [STAGE_KEY]: { name: STAGE_KEY, description: 'desc', model: 'default', returnsJson: true, variables: [] } } }, null, 2) + '\n',
- );
- };
-
- beforeEach(() => {
- rootDir = mkdtempSync(join(tmpdir(), 'migration-166-'));
- stagesDir = join(rootDir, 'data', 'prompts', 'stages');
- refStagesDir = join(rootDir, 'data.reference', 'prompts', 'stages');
- installedConfigPath = join(rootDir, 'data', 'prompts', 'stage-config.json');
- mkdirSync(stagesDir, { recursive: true });
- mkdirSync(refStagesDir, { recursive: true });
- });
-
- afterEach(() => {
- rmSync(rootDir, { recursive: true, force: true });
- });
-
- it('seeds the .md template and merges the stage-config entry on a fresh install', async () => {
- seedReference();
- await expect(migration.up({ rootDir })).resolves.not.toThrow();
- expect(readFileSync(join(stagesDir, FILENAME), 'utf8')).toBe(BODY);
- const installed = JSON.parse(readFileSync(installedConfigPath, 'utf8'));
- expect(installed.stages[STAGE_KEY]).toMatchObject({ returnsJson: true });
- });
-
- it('is idempotent and never clobbers a customized .md or an existing stage-config entry', async () => {
- seedReference();
- const customBody = '# CUSTOMIZED — do not overwrite\n';
- writeFileSync(join(stagesDir, FILENAME), customBody);
- writeFileSync(
- installedConfigPath,
- JSON.stringify({ stages: { [STAGE_KEY]: { name: 'user-tuned', model: 'custom' } } }, null, 2) + '\n',
- );
-
- await migration.up({ rootDir });
-
- expect(readFileSync(join(stagesDir, FILENAME), 'utf8')).toBe(customBody);
- const installed = JSON.parse(readFileSync(installedConfigPath, 'utf8'));
- expect(installed.stages[STAGE_KEY]).toEqual({ name: 'user-tuned', model: 'custom' });
- });
-
- it('does not throw when the data.reference samples are missing', async () => {
- await expect(migration.up({ rootDir })).resolves.not.toThrow();
- expect(existsSync(join(stagesDir, FILENAME))).toBe(false);
- });
-
- it('matches the live shipped stage-config key + prompt file (drift catch)', () => {
- const refConfig = JSON.parse(
- readFileSync(join(repoRoot, 'data.reference', 'prompts', 'stage-config.json'), 'utf8'),
- );
- expect(refConfig.stages[STAGE_KEY], `stage-config missing ${STAGE_KEY}`).toBeTruthy();
- expect(refConfig.stages[STAGE_KEY].returnsJson).toBe(true);
- expect(existsSync(join(repoRoot, 'data.reference', 'prompts', 'stages', FILENAME))).toBe(true);
- });
-});