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 (claude): compare mirrored function BODIES, not just signatures
[._.]/ Adam Eivy4a55497e5aaf
diff --git a/server/lib/mirrorParity.js b/server/lib/mirrorParity.jsindex bdfeafc304..8269cbdef8 100644--- a/server/lib/mirrorParity.js+++ b/server/lib/mirrorParity.js@@ -31,13 +31,37 @@ export function stripCommentsAndNormalize(src) {.trim();}+/**+ * Index of the delimiter matching the opener at `openIdx`, or -1 if unbalanced.+ * Counts `{}`, `()` and `[]` together — good enough for the declarations we+ * mirror, and the parity diff is textual anyway.+ */+function matchDelimiter(src, openIdx) {+ let depth = 0;+ for (let i = openIdx; i < src.length; i++) {+ const ch = src[i];+ if (ch === '{' || ch === '(' || ch === '[') depth++;+ else if (ch === '}' || ch === ')' || ch === ']') {+ depth--;+ if (depth === 0) return i;+ }+ }+ return -1;+}+/*** Extract a top-level declaration by name — `function` / `async function` /- * `const` (including `Object.freeze(…)` and array/object literal bodies).+ * `const` (regex literal, array/object initializer, `Object.freeze(…)`, arrow+ * function). Returns the declaration text, or `null` when the name is absent+ * or its source is unbalanced.*- * Walks from the declaration to the first opening delimiter and returns the- * slice through its match, tracking `{}`, `()` and `[]` together. Returns the- * declaration text, or `null` when the name is absent.+ * The two forms terminate differently, and conflating them is a trap worth+ * spelling out: a naive "walk to the first time depth returns to 0" returns+ * `function f(a)` — the closing paren of the PARAMETER LIST — so the entire+ * body escapes the parity diff and a gutted mirror passes green. Likewise it+ * truncates `const RE = /(?:a|b):\/\//i` at the non-capturing group's `)`.+ * So: a function is walked to the matching brace of its BODY, and a const to+ * its terminating `;`.*/export function extractDeclaration(src, name) {const startRe = new RegExp(`(?:export\\s+)?(?:async\\s+function|function|const)\\s+${name}[\\s=(]`);@@ -45,18 +69,30 @@ export function extractDeclaration(src, name) {if (!match) return null;const start = match.index;- let depth = 0;- let foundOpen = false;+ if (/(?:async\s+)?function\s/.test(match[0])) {+ // Skip the parameter list, then return through the body's matching brace.+ const parenOpen = src.indexOf('(', start);+ if (parenOpen === -1) return null;+ const parenClose = matchDelimiter(src, parenOpen);+ if (parenClose === -1) return null;+ const braceOpen = src.indexOf('{', parenClose);+ if (braceOpen === -1) return null;+ const braceClose = matchDelimiter(src, braceOpen);+ return braceClose === -1 ? null : src.slice(start, braceClose + 1);+ }++ // `const`: run to the statement's terminating `;` at depth 0. This ends a+ // regex literal, an array/object initializer, and an arrow function alike.+ // Caveat: a `;` inside a top-level string/regex literal would end the slice+ // early — no mirrored declaration does that, and a full lexer isn't worth it+ // here. Every mirrored `const` must be semicolon-terminated.+ let depth = 0;for (let i = start; i < src.length; i++) {const ch = src[i];- if (ch === '{' || ch === '(' || ch === '[') {- depth++;- foundOpen = true;- } else if (ch === '}' || ch === ')' || ch === ']') {- depth--;- if (foundOpen && depth === 0) return src.slice(start, i + 1);- }+ if (ch === '{' || ch === '(' || ch === '[') depth++;+ else if (ch === '}' || ch === ')' || ch === ']') depth--;+ else if (ch === ';' && depth === 0) return src.slice(start, i + 1);}return null;}diff --git a/server/lib/mirrorParity.test.js b/server/lib/mirrorParity.test.jsnew file mode 100644index 0000000000..03b985710c--- /dev/null+++ b/server/lib/mirrorParity.test.js@@ -0,0 +1,95 @@+import { describe, it, expect } from 'vitest';+import { stripCommentsAndNormalize, extractDeclaration, compareDeclaration } from './mirrorParity.js';++describe('stripCommentsAndNormalize', () => {+ it('drops block and line comments and collapses whitespace', () => {+ const src = `+ /* header */+ const A = 1; // trailing+ `;+ expect(stripCommentsAndNormalize(src)).toBe('const A = 1;');+ });++ it('lets the two sides differ in commentary while comparing equal', () => {+ expect(stripCommentsAndNormalize('// server note\nconst A = 1;'))+ .toBe(stripCommentsAndNormalize('/* client note */\nconst A = 1;'));+ });+});++describe('extractDeclaration', () => {+ // The regression this helper's own test exists for: a naive "return at the+ // first depth-0" walk stops at the closing paren of the PARAMETER LIST, so+ // the body never reaches the parity diff and a gutted mirror passes green.+ it('includes a function BODY, not just its signature', () => {+ const src = 'export function f(a) {\n return a + 1;\n}\n';+ const out = extractDeclaration(src, 'f');+ expect(out).toContain('return a + 1;');+ expect(out.endsWith('}')).toBe(true);+ });++ it('includes the whole regex literal, not just up to a group\'s close paren', () => {+ const src = 'const RE = /(?:a|b):\\/\\/|\\bx::/i;\n';+ const out = extractDeclaration(src, 'RE');+ expect(out).toContain('\\bx::');+ expect(out.endsWith(';')).toBe(true);+ });++ it('handles async functions', () => {+ const src = 'export async function g(a) {\n await a;\n}\n';+ expect(extractDeclaration(src, 'g')).toContain('await a;');+ });++ it('handles a function whose params contain a nested call/default', () => {+ const src = 'function h(a = fn(1), { b } = {}) {\n return [a, b];\n}\n';+ expect(extractDeclaration(src, 'h')).toContain('return [a, b];');+ });++ it('handles array, Object.freeze and arrow-function const initializers', () => {+ expect(extractDeclaration('const A = [1, [2, 3]];\n', 'A')).toBe('const A = [1, [2, 3]];');+ expect(extractDeclaration('const S = Object.freeze({ a: { b: 1 } });\n', 'S'))+ .toBe('const S = Object.freeze({ a: { b: 1 } });');+ expect(extractDeclaration('const m = (p) => `${p}${x()}`;\n', 'm'))+ .toBe('const m = (p) => `${p}${x()}`;');+ });++ it('does not stop at a `;` nested inside the initializer', () => {+ const src = 'const F = (a) => { const b = a; return b; };\n';+ expect(extractDeclaration(src, 'F')).toBe('const F = (a) => { const b = a; return b; };');+ });++ it('returns null for an absent name', () => {+ expect(extractDeclaration('const A = 1;', 'B')).toBeNull();+ });++ it('returns null rather than a truncated slice when the source is unbalanced', () => {+ expect(extractDeclaration('function f(a) {\n return a;\n', 'f')).toBeNull();+ expect(extractDeclaration('const A = [1, 2\n', 'A')).toBeNull();+ });+});++describe('compareDeclaration', () => {+ const server = 'export function f(a) {\n return a + 1;\n}\n';++ it('matches when only commentary differs', () => {+ const client = '// mirrored from the server\nexport function f(a) {\n /* same */ return a + 1;\n}\n';+ expect(compareDeclaration(server, client, 'f').match).toBe(true);+ });++ it('reports a mismatch when only the BODY diverges (signature identical)', () => {+ // The exact drift the signature-only walk used to miss.+ const gutted = 'export function f(a) {\n return 0;\n}\n';+ expect(compareDeclaration(server, gutted, 'f').match).toBe(false);+ });++ it('reports a mismatch when a regex tail diverges', () => {+ const a = 'const RE = /(?:x|y):\\/\\//i;\n';+ const b = 'const RE = /(?:x|y):TOTALLY-DIFFERENT/i;\n';+ expect(compareDeclaration(a, b, 'RE').match).toBe(false);+ });++ it('does not report a match when the declaration is missing from a side', () => {+ const { clientDecl, match } = compareDeclaration(server, 'const other = 1;', 'f');+ expect(clientDecl).toBeNull();+ expect(match).toBe(false);+ });+});