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([issue-722]): parallelize applyPromptReplaceMigration per-file scan

[._.]/ Adam Eivyd6010176bf70
diff --git a/scripts/migrations/_lib.js b/scripts/migrations/_lib.js
index 7be211c9d0..a6790e0b51 100644
--- a/scripts/migrations/_lib.js
+++ b/scripts/migrations/_lib.js
@@ -280,13 +280,13 @@ export async function applyPromptReplaceMigration({
const stagesDir = join(rootDir, 'data', 'prompts', 'stages');
const sampleDir = join(rootDir, 'data.reference', 'prompts', 'stages');
- let updated = 0;
- let alreadyCurrent = 0;
- let skipped = 0;
- let created = 0;
- let retired = 0;
-
- for (const filename of Object.keys(accepted)) {
+ // Per-file scan runs in parallel — each file's read/compare/write is
+ // independent (no cross-file shared state), so the only cost of the old
+ // serial loop was wall-clock time on a multi-file migration. Each task
+ // returns the name of the single counter it bumped (or null for a no-op);
+ // we sum them post-flight, so counter accumulation never races (it happens
+ // after every task settles, not from inside the async work).
+ const outcomes = await Promise.all(Object.keys(accepted).map(async (filename) => {
const dataPath = join(stagesDir, filename);
const samplePath = join(sampleDir, filename);
@@ -301,12 +301,11 @@ export async function applyPromptReplaceMigration({
if (sampleContent != null) {
await writeFile(dataPath, sampleContent);
console.log(`📄 created ${label}: ${filename}`);
- created++;
- continue;
+ return 'created';
}
}
console.log(`📄 ${label} ${filename}: not present in data/, will be created by setup-data.js`);
- continue;
+ return null;
}
const existingMd5 = md5(existing);
@@ -328,21 +327,18 @@ export async function applyPromptReplaceMigration({
if (matchesAcceptedOld || matchesCurrent) {
await unlink(dataPath);
console.log(`🗑️ ${label} ${filename} was renamed/retired upstream — removed unmodified copy from data/`);
- retired++;
- } else {
- console.warn(
- `⚠️ ${label} ${filename} was renamed/retired upstream but your local copy has been customized.\n` +
- ` Check data.reference/prompts/stages/ for the replacement file and merge any custom edits manually.`,
- );
- skipped++;
+ return 'retired';
}
- continue;
+ console.warn(
+ `⚠️ ${label} ${filename} was renamed/retired upstream but your local copy has been customized.\n` +
+ ` Check data.reference/prompts/stages/ for the replacement file and merge any custom edits manually.`,
+ );
+ return 'skipped';
}
}
if (matchesCurrent) {
- alreadyCurrent++;
- continue;
+ return 'alreadyCurrent';
}
if (!matchesAcceptedOld) {
@@ -350,17 +346,21 @@ export async function applyPromptReplaceMigration({
`⚠️ ${label} ${filename} has been customized — skipping auto-update.\n` +
customizedHint(filename),
);
- skipped++;
- continue;
+ return 'skipped';
}
const sampleContent = await readFile(samplePath, 'utf-8');
await writeFile(dataPath, sampleContent);
console.log(`✅ updated ${label}: ${filename}`);
- updated++;
+ return 'updated';
+ }));
+
+ const counts = { updated: 0, alreadyCurrent: 0, skipped: 0, created: 0, retired: 0 };
+ for (const outcome of outcomes) {
+ if (outcome) counts[outcome]++;
}
- return { updated, alreadyCurrent, skipped, created, retired };
+ return counts;
}
/**
diff --git a/scripts/migrations/_lib.test.js b/scripts/migrations/_lib.test.js
index 8f955abd1e..3e7aaddcc8 100644
--- a/scripts/migrations/_lib.test.js
+++ b/scripts/migrations/_lib.test.js
@@ -107,6 +107,46 @@ describe('applyPromptReplaceMigration opt-ins', () => {
).rejects.toThrow(/ENOENT/);
});
});
+
+ describe('multi-file scan', () => {
+ // The per-file scan runs in parallel (Promise.all). These assert the
+ // post-flight counter accumulation stays correct across files that land in
+ // different branches — distinct outcomes must each be counted exactly once.
+ const NAMES = ['pipeline-a.md', 'pipeline-b.md', 'pipeline-c.md'];
+ const multiOpts = {
+ accepted: Object.fromEntries(NAMES.map((n) => [n, [md5(BODY_OLD)]])),
+ current: Object.fromEntries(NAMES.map((n) => [n, md5(BODY_NEW)])),
+ label: 'multi',
+ customizedHint: () => '',
+ };
+
+ it('accumulates each file into its own counter across distinct outcomes', async () => {
+ // a → old hash + sample present → updated
+ writeFileSync(join(stagesDir, 'pipeline-a.md'), BODY_OLD);
+ writeFileSync(join(sampleDir, 'pipeline-a.md'), BODY_NEW);
+ // b → already at current hash → alreadyCurrent
+ writeFileSync(join(stagesDir, 'pipeline-b.md'), BODY_NEW);
+ // c → customized (matches neither) → skipped
+ writeFileSync(join(stagesDir, 'pipeline-c.md'), BODY_CUSTOM);
+
+ const result = await applyPromptReplaceMigration({ rootDir, ...multiOpts });
+ expect(result).toEqual({ updated: 1, alreadyCurrent: 1, skipped: 1, created: 0, retired: 0 });
+ expect(readFileSync(join(stagesDir, 'pipeline-a.md'), 'utf-8')).toBe(BODY_NEW);
+ expect(readFileSync(join(stagesDir, 'pipeline-c.md'), 'utf-8')).toBe(BODY_CUSTOM);
+ });
+
+ it('updates every file when all are at the accepted-old hash', async () => {
+ for (const name of NAMES) {
+ writeFileSync(join(stagesDir, name), BODY_OLD);
+ writeFileSync(join(sampleDir, name), BODY_NEW);
+ }
+ const result = await applyPromptReplaceMigration({ rootDir, ...multiOpts });
+ expect(result).toEqual({ updated: 3, alreadyCurrent: 0, skipped: 0, created: 0, retired: 0 });
+ for (const name of NAMES) {
+ expect(readFileSync(join(stagesDir, name), 'utf-8')).toBe(BODY_NEW);
+ }
+ });
+ });
});
describe('readLayoutsDoc / writeLayoutsDoc', () => {