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: atomic backup and full marker-collision refusal in health array repair (#13185)

Adam Eivy40824db3ebce
diff --git a/scripts/migrations/438-repair-legacy-health-array-files.js b/scripts/migrations/438-repair-legacy-health-array-files.js
index 3d105dba1f..c9f966778b 100644
--- a/scripts/migrations/438-repair-legacy-health-array-files.js
+++ b/scripts/migrations/438-repair-legacy-health-array-files.js
@@ -32,9 +32,9 @@
* This is a frozen transform: it does not import the live services. The repair
* is derived from the install's own rows, so it ships no `data.reference` seed.
*/
-import { copyFile, readFile } from 'fs/promises';
+import { readFile } from 'fs/promises';
import { join } from 'path';
-import { writeJsonAtomic } from './_lib.js';
+import { writeJsonAtomic, writeTextAtomic } from './_lib.js';
const TARGETS = [
{ file: 'blood-tests.json', key: 'tests', extra: () => ({ referenceRanges: {} }), flattenMarkers: true },
@@ -44,7 +44,8 @@ const TARGETS = [
];
const BACKUP_SUFFIX = '.pre-438.json';
-// A marker named like a row field would silently overwrite or be overwritten.
+// A marker named like a row field would silently overwrite or be overwritten;
+// the reserved names are refused even when this particular row lacks them.
const RESERVED_ROW_KEYS = ['id', 'date', 'createdAt', 'updatedAt', 'aliasIds', 'markers'];
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
@@ -56,7 +57,8 @@ function flattenBloodRow(row) {
return rest;
}
const { markers, ...rest } = row;
- if (!isObject(markers) || RESERVED_ROW_KEYS.some((key) => Object.hasOwn(markers, key))) return null;
+ if (!isObject(markers) || RESERVED_ROW_KEYS.some((key) => Object.hasOwn(markers, key))
+ || Object.keys(markers).some((key) => Object.hasOwn(rest, key))) return null;
return { ...markers, ...rest };
}
@@ -92,7 +94,7 @@ async function repairFile(dir, { file, key, extra, flattenMarkers }) {
const backupPath = `${path}${BACKUP_SUFFIX}`;
const existingBackup = await readBytes(backupPath);
if (existingBackup === null) {
- await copyFile(path, backupPath);
+ await writeTextAtomic(backupPath, bytes); // temp + rename: a torn backup would wedge every retry
} else if (!existingBackup.equals(bytes)) {
console.log(`⏭️ ${file}: a different ${file}${BACKUP_SUFFIX} already exists; left untouched`);
return;
diff --git a/scripts/migrations/438-repair-legacy-health-array-files.test.js b/scripts/migrations/438-repair-legacy-health-array-files.test.js
index 48b6991790..41148b45b5 100644
--- a/scripts/migrations/438-repair-legacy-health-array-files.test.js
+++ b/scripts/migrations/438-repair-legacy-health-array-files.test.js
@@ -71,7 +71,7 @@ describe('migration 438 — legacy bare-array health files', () => {
it('refuses malformed JSON, non-object rows and ambiguous markers without overwriting', async () => {
put('eyes.json', '[{"id":"e1"');
put('epigenetic-tests.json', [{ id: 'p1' }, 'stray']);
- put('blood-tests.json', [{ id: 'b1', markers: { id: 'clash' } }]);
+ put('blood-tests.json', [{ id: 'b1', lab: 'Acme', markers: { lab: 'clash' } }]);
put('health-metrics.json', '"just a string"');
const before = ['eyes.json', 'epigenetic-tests.json', 'blood-tests.json', 'health-metrics.json']
.map((name) => [name, readFileSync(p(name), 'utf-8')]);
@@ -81,6 +81,9 @@ describe('migration 438 — legacy bare-array health files', () => {
for (const [name, text] of before) expect(readFileSync(p(name), 'utf-8')).toBe(text);
expect(existsSync(p('eyes.json.pre-438.json'))).toBe(false);
expect(existsSync(p('blood-tests.json.pre-438.json'))).toBe(false);
+ put('blood-tests.json', [{ id: 'b2', markers: { id: 'clash' } }]);
+ await migration.up({ rootDir });
+ expect(read('blood-tests.json')).toEqual([{ id: 'b2', markers: { id: 'clash' } }]);
});
it('is safe to repeat and resumes after an interruption that left only the backup', async () => {