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
address review (round 8): single-source bounds + shared event constant
Adam Eivy1d59b14f6d8d
diff --git a/client/src/components/CmdKSearch.jsx b/client/src/components/CmdKSearch.jsxindex ea2299933d..221ce4d819 100644--- a/client/src/components/CmdKSearch.jsx+++ b/client/src/components/CmdKSearch.jsx@@ -7,6 +7,7 @@ import { useScrollLock } from '../hooks/useScrollLock';import { search, getPaletteManifest, runPaletteAction, getDashboardLayouts, setActiveDashboardLayout } from '../services/api';import toast from './ui/Toast';import { modKey } from '../utils/platform';+import { DASHBOARD_LAYOUT_CHANGED } from '../constants/events.js';const ICON_MAP = { Brain, Cpu, Package, History, HeartPulse };@@ -210,7 +211,7 @@ export default function CmdKSearch() {// navigate('/') is a no-op if the user is already there.const ok = await setActiveDashboardLayout(item.layoutId).then(() => true, () => false);if (!ok) { close(); return; }- window.dispatchEvent(new CustomEvent('portos:dashboard-layout-changed'));+ window.dispatchEvent(new CustomEvent(DASHBOARD_LAYOUT_CHANGED));navigate('/');toast.success(`Switched to "${item.layoutName}"`);close();diff --git a/client/src/constants/events.js b/client/src/constants/events.jsnew file mode 100644index 0000000000..ba6f30a8a3--- /dev/null+++ b/client/src/constants/events.js@@ -0,0 +1,4 @@+// DOM CustomEvent names PortOS dispatches across component trees. Centralized+// so the publisher and listener can't drift on spelling.++export const DASHBOARD_LAYOUT_CHANGED = 'portos:dashboard-layout-changed';diff --git a/client/src/pages/Dashboard.jsx b/client/src/pages/Dashboard.jsxindex 5bbe722134..7d245a0789 100644--- a/client/src/pages/Dashboard.jsx+++ b/client/src/pages/Dashboard.jsx@@ -4,6 +4,7 @@ import BrailleSpinner from '../components/BrailleSpinner';import LayoutPicker from '../components/dashboard/LayoutPicker';import LayoutEditor from '../components/dashboard/LayoutEditor';import { WIDGETS_BY_ID, WIDTH_CLASS } from '../components/dashboard/widgetRegistry.jsx';+import { DASHBOARD_LAYOUT_CHANGED } from '../constants/events.js';import { Monitor } from 'lucide-react';import * as api from '../services/api';import socket from '../services/socket';@@ -56,8 +57,8 @@ export default function Dashboard() {// to the server so the Dashboard re-syncs even when already on `/`// (where navigate('/') would be a no-op and no remount happens).const handleLayoutChanged = () => fetchLayouts();- window.addEventListener('portos:dashboard-layout-changed', handleLayoutChanged);- return () => window.removeEventListener('portos:dashboard-layout-changed', handleLayoutChanged);+ window.addEventListener(DASHBOARD_LAYOUT_CHANGED, handleLayoutChanged);+ return () => window.removeEventListener(DASHBOARD_LAYOUT_CHANGED, handleLayoutChanged);}, []);const sortedApps = useMemo(() =>diff --git a/server/routes/dashboardLayouts.js b/server/routes/dashboardLayouts.jsindex fad5a4a2e5..e33674d622 100644--- a/server/routes/dashboardLayouts.js+++ b/server/routes/dashboardLayouts.js@@ -13,16 +13,16 @@ import * as svc from '../services/dashboardLayouts.js';const router = Router();-// Strict kebab: alphanumeric segments joined by single dashes, no leading-// or trailing dashes, no runs of dashes. Matches `default`, `morning-review`.-const idSchema = z.string().min(1).max(60).regex(/^[a-z0-9]+(-[a-z0-9]+)*$/, 'id must be lowercase kebab');+// Bounds are sourced from the service so sanitization on read and validation+// at the API boundary agree by construction.+const idSchema = z.string().min(1).max(svc.ID_MAX_LENGTH).regex(svc.ID_PATTERN, 'id must be lowercase kebab');const layoutSchema = z.object({id: idSchema,- name: z.string().min(1).max(80),+ name: z.string().min(1).max(svc.NAME_MAX_LENGTH),widgets: z- .array(z.string().min(1).max(80))- .max(50)+ .array(z.string().min(1).max(svc.WIDGET_ID_MAX_LENGTH))+ .max(svc.WIDGETS_MAX).refine((w) => new Set(w).size === w.length, { message: 'widgets must be unique' }),});diff --git a/server/routes/dashboardLayouts.test.js b/server/routes/dashboardLayouts.test.jsindex e499392aa9..22cd32485a 100644--- a/server/routes/dashboardLayouts.test.js+++ b/server/routes/dashboardLayouts.test.js@@ -6,6 +6,11 @@ import { errorMiddleware } from '../lib/errorHandler.js';vi.mock('../services/dashboardLayouts.js', () => ({ERR_NOT_FOUND: 'NOT_FOUND',ERR_BUILTIN_PROTECTED: 'BUILTIN_PROTECTED',+ ID_PATTERN: /^[a-z0-9]+(-[a-z0-9]+)*$/,+ ID_MAX_LENGTH: 60,+ NAME_MAX_LENGTH: 80,+ WIDGETS_MAX: 50,+ WIDGET_ID_MAX_LENGTH: 80,getState: vi.fn(),setActiveLayout: vi.fn(),saveLayout: vi.fn(),diff --git a/server/services/dashboardLayouts.js b/server/services/dashboardLayouts.jsindex fa4fad4dbf..ea16651c88 100644--- a/server/services/dashboardLayouts.js+++ b/server/services/dashboardLayouts.js@@ -63,10 +63,15 @@ const DEFAULT_STATE = {};const BUILTIN_IDS = new Set(DEFAULT_LAYOUTS.map((l) => l.id));-// Kept in lockstep with routes/dashboardLayouts.js#idSchema — both layers-// must reject ids the other can't round-trip, or the client ends up with-// layouts it can display but not activate/delete.-const ID_RE = /^[a-z0-9]+(-[a-z0-9]+)*$/;++// Shape constraints shared with routes/dashboardLayouts.js#layoutSchema.+// Exported so routes build their Zod schema from the same source; edits+// here automatically flow to the API boundary.+export const ID_PATTERN = /^[a-z0-9]+(-[a-z0-9]+)*$/;+export const ID_MAX_LENGTH = 60;+export const NAME_MAX_LENGTH = 80;+export const WIDGETS_MAX = 50;+export const WIDGET_ID_MAX_LENGTH = 80;// Sanitize a single layout entry — protect against hand-edits that produce// non-object elements, missing fields, non-array widget lists, or duplicate@@ -75,19 +80,22 @@ const ID_RE = /^[a-z0-9]+(-[a-z0-9]+)*$/;// flag can't downgrade a built-in into a deletable user layout.const sanitizeLayout = (l) => {if (!l || typeof l !== 'object') return null;- if (typeof l.id !== 'string' || !ID_RE.test(l.id) || l.id.length > 60) return null;+ if (typeof l.id !== 'string' || !ID_PATTERN.test(l.id) || l.id.length > ID_MAX_LENGTH) return null;if (typeof l.name !== 'string' || !l.name) return null;+ const name = l.name.slice(0, NAME_MAX_LENGTH);const widgets = [];const seen = new Set();if (Array.isArray(l.widgets)) {for (const w of l.widgets) {- if (typeof w === 'string' && !seen.has(w)) {- seen.add(w);- widgets.push(w);- }+ if (typeof w !== 'string') continue;+ if (!w || w.length > WIDGET_ID_MAX_LENGTH) continue;+ if (seen.has(w)) continue;+ seen.add(w);+ widgets.push(w);+ if (widgets.length >= WIDGETS_MAX) break;}}- return { id: l.id, name: l.name, builtIn: BUILTIN_IDS.has(l.id), widgets };+ return { id: l.id, name, builtIn: BUILTIN_IDS.has(l.id), widgets };};export async function getState() {