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: round-trip structured universe canon fields

[._.]/ Adam Eivyadda099e05cc
diff --git a/server/lib/universeMarkdown.js b/server/lib/universeMarkdown.js
index 2d2d4865e0..bec48add6b 100644
--- a/server/lib/universeMarkdown.js
+++ b/server/lib/universeMarkdown.js
@@ -35,6 +35,14 @@ const ENTRY_METADATA_FIELDS = new Set([
'deleted', 'deletedAt', 'schemaVersion', 'locked', 'missingFromProse',
]);
+const stripEntryMetadata = (value) => {
+ if (Array.isArray(value)) return value.map(stripEntryMetadata);
+ if (!value || typeof value !== 'object') return value;
+ return Object.fromEntries(Object.entries(value)
+ .filter(([key]) => !ENTRY_METADATA_FIELDS.has(key))
+ .map(([key, nested]) => [key, stripEntryMetadata(nested)]));
+};
+
const CANON_SECTIONS = Object.freeze([
['characters', 'Characters'],
['places', 'Places'],
@@ -51,15 +59,6 @@ const compareNames = (left, right) => {
return rawA < rawB ? -1 : rawA > rawB ? 1 : 0;
};
-const hasContent = (value) => {
- if (value === null || value === undefined) return false;
- if (typeof value === 'string') return Boolean(value.trim());
- if (typeof value === 'number' || typeof value === 'boolean') return true;
- if (Array.isArray(value)) return value.some(hasContent);
- if (typeof value === 'object') return Object.values(value).some(hasContent);
- return false;
-};
-
const formatFieldLabel = (key) => String(key)
.replace(/([a-z])([A-Z])/g, '$1 $2')
.replace(/[_-]+/g, ' ')
@@ -133,8 +132,10 @@ const renderEntry = (kind, entry, fallback) => {
const lines = [`### ${entryName(entry, fallback)}`];
for (const key of orderedEntryKeys(kind, entry)) {
const value = entry[key];
- if (!hasContent(value)) continue;
- const rendered = formatBlockValue(value);
+ if (value === null || value === undefined || (typeof value === 'string' && !value.trim())) continue;
+ const rendered = typeof value === 'string'
+ ? formatBlockValue(value)
+ : `\`\`\`portos-json\n${JSON.stringify(stripEntryMetadata(value), null, 2).replace(/`/g, '\\u0060')}\n\`\`\``;
if (!rendered) continue;
lines.push(`**${formatFieldLabel(key)}:** ${rendered}`);
}
@@ -401,14 +402,25 @@ const splitSubsections = (lines) => {
const parseCanonEntries = (kind, lines) => splitSubsections(lines).map(({ title, lines: entryLines }) => {
const { fields, freeText } = readFieldBlocks(
entryLines,
- (key) => STRING_FIELDS[kind].has(key) || ARRAY_FIELDS[kind].has(key),
+ (key) => key !== 'name' && !ENTRY_METADATA_FIELDS.has(key),
);
const entry = { ...(kind === 'places' && /^(?:int\.?\s*\/\s*ext\.?|ext\.?|int\.?)(?:\s|\.)/iu.test(title)
? { slugline: title }
: { name: title }) };
for (const [key, value] of Object.entries(fields)) {
+ const structured = value.match(/^```portos-json\n([\s\S]*?)\n```$/u);
+ if (structured) {
+ try {
+ entry[key] = JSON.parse(structured[1]);
+ continue;
+ } catch {
+ // Keep malformed hand-authored metadata legible instead of silently
+ // discarding the field; normal universe validation still applies.
+ }
+ }
if (STRING_FIELDS[kind].has(key)) entry[key] = value;
else if (ARRAY_FIELDS[kind].has(key)) entry[key] = splitArrayValue(value);
+ else entry[key] = value;
}
if (freeText && !entry.notes) entry.notes = freeText;
return entry;
diff --git a/server/lib/universeMarkdown.test.js b/server/lib/universeMarkdown.test.js
index 1081cb7c59..f3e42b2f39 100644
--- a/server/lib/universeMarkdown.test.js
+++ b/server/lib/universeMarkdown.test.js
@@ -135,6 +135,42 @@ describe('universe markdown filenames', () => {
});
describe('parseUniverseMarkdown', () => {
+ it('round-trips structured canon fields and empty collections without exporting nested metadata', () => {
+ const source = {
+ name: 'Structured World',
+ characters: [{
+ name: 'Mira',
+ psychology: { theoryOfControl: 'Carry the map and no one gets lost.', drives: { status: { desire: 'a title', fear: 'being surplus' } } },
+ sliders: { proactivity: 7, competence: 4 },
+ relationshipLinks: [{ id: 'local-link-id', targetCharacterId: 'example-character', type: 'ally', description: 'Shares the route.' }],
+ stats: { agility: 5 },
+ colorPalette: [{ name: 'Brass', hex: '#b08d57', role: 'coat' }],
+ props: [{ id: 'local-prop-id', name: 'Compass', description: 'Points north.' }],
+ expressions: [{ name: 'Wary', description: 'One brow raised.' }],
+ handGestures: [{ name: 'Wait', description: 'Palm out.' }],
+ wardrobes: [{ name: 'Travel coat', description: 'Weathered canvas.' }],
+ customProfile: { nested: ['kept', 'in order'] },
+ emptyCollection: [],
+ }],
+ places: [{ name: 'The Archive', palette: ['copper', 'ink'] }],
+ objects: [{ name: 'Sun Compass', attachments: [{ characterId: 'example-character', role: 'keepsake' }] }],
+ };
+
+ const markdown = universeToMarkdown(source);
+ const imported = parseUniverseMarkdown(markdown);
+
+ expect(imported).toEqual({
+ ...source,
+ characters: [{
+ ...source.characters[0],
+ relationshipLinks: [{ targetCharacterId: 'example-character', type: 'ally', description: 'Shares the route.' }],
+ props: [{ name: 'Compass', description: 'Points north.' }],
+ }],
+ });
+ expect(markdown).not.toContain('local-link-id');
+ expect(markdown).not.toContain('local-prop-id');
+ });
+
it('reads the exported editable fields, including multiline values and escaped headings', () => {
const markdown = universeToMarkdown({
name: 'The Bright World',