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
/**
* Digital Twin — Twin Personas (M34 P7)
*
* A persona is a named context variant (Professional, Casual, Family, …) whose
* `instructions` are prepended to the embodied-twin context so the twin
* modulates voice/behavior per context without forking the underlying
* documents. One persona can be marked "active" (`meta.settings.activePersonaId`);
* the CoS agent prompt builder asks for the active persona, while the
* identity-building paths (enrichment, analysis) stay persona-free.
*
* Personas live in `meta.personas`, mirroring how testHistory / enrichment
* already live in meta.json.
*/
import { generateId, now } from './digital-twin-helpers.js';
import { loadMeta, saveMeta, withMetaLock } from './digital-twin-meta.js';
import { recordTombstone } from '../lib/tombstones.js';
export async function getPersonas() {
const meta = await loadMeta();
return Array.isArray(meta.personas) ? meta.personas : [];
}
export async function getPersonaById(id) {
const personas = await getPersonas();
return personas.find(p => p.id === id) || null;
}
export const createPersona = (...args) => withMetaLock(() => createPersonaLocked(...args));
async function createPersonaLocked({ name, description, instructions, traitAdjustments }) {
const meta = await loadMeta();
if (!Array.isArray(meta.personas)) meta.personas = [];
const timestamp = now();
const persona = {
id: generateId(),
name,
...(description ? { description } : {}),
instructions,
...(traitAdjustments ? { traitAdjustments } : {}),
createdAt: timestamp,
updatedAt: timestamp
};
meta.personas.push(persona);
await saveMeta(meta);
console.log(`🎭 Created persona: ${name}`);
return persona;
}
export const updatePersona = (...args) => withMetaLock(() => updatePersonaLocked(...args));
async function updatePersonaLocked(id, updates) {
const meta = await loadMeta();
const persona = (meta.personas || []).find(p => p.id === id);
if (!persona) {
throw new Error(`Persona ${id} not found`);
}
// Only apply keys that were actually provided so an omitted field preserves
// the original (and description: '' can intentionally clear it).
if (updates.name !== undefined) persona.name = updates.name;
if (updates.description !== undefined) persona.description = updates.description;
if (updates.instructions !== undefined) persona.instructions = updates.instructions;
// traitAdjustments: absent (undefined) preserves the original; an explicit
// null clears it back to an instructions-only persona; an object replaces it.
if (updates.traitAdjustments !== undefined) {
if (updates.traitAdjustments === null) {
delete persona.traitAdjustments;
} else {
persona.traitAdjustments = updates.traitAdjustments;
}
}
persona.updatedAt = now();
await saveMeta(meta);
return persona;
}
export const deletePersona = (...args) => withMetaLock(() => deletePersonaLocked(...args));
async function deletePersonaLocked(id) {
const meta = await loadMeta();
const before = (meta.personas || []).length;
meta.personas = (meta.personas || []).filter(p => p.id !== id);
const deleted = before !== meta.personas.length;
// Tombstone the id so peer sync — which unions personas add-only — can't
// resurrect it from a machine that still has the persona (#3533). The
// tombstone also propagates the delete to those machines. Only recorded when
// something was actually removed: a delete for an unknown id is a no-op, and
// writing a tombstone for it would just consume the capped list.
if (deleted) {
meta.deletedPersonas = recordTombstone(meta.deletedPersonas, id, { keyField: 'id' });
}
// Clearing the active persona when it's the one being deleted keeps the
// settings pointer from dangling at a non-existent id.
if (meta.settings?.activePersonaId === id) {
meta.settings.activePersonaId = null;
}
await saveMeta(meta);
return { deleted };
}
/**
* Set (or clear, with personaId === null) the active persona. Validates the id
* exists so the settings pointer can't dangle.
*/
export const setActivePersona = (...args) => withMetaLock(() => setActivePersonaLocked(...args));
async function setActivePersonaLocked(personaId) {
const meta = await loadMeta();
if (personaId !== null && !(meta.personas || []).some(p => p.id === personaId)) {
throw new Error(`Persona ${personaId} not found`);
}
if (!meta.settings) meta.settings = {};
meta.settings.activePersonaId = personaId;
await saveMeta(meta);
return meta.settings;
}
export async function getActivePersona() {
const meta = await loadMeta();
const id = meta.settings?.activePersonaId;
if (!id) return null;
return (meta.personas || []).find(p => p.id === id) || null;
}