From 5e3863e5122dd5a6a8dabd1a2fc1208c0e3b9983 Mon Sep 17 00:00:00 2001 From: Darwin Pardave Date: Sun, 6 Sep 2026 20:35:50 -0500 Subject: [PATCH] perf: avoid duplicate skill file reads Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- scripts/dashboard-web.js | 17 ++++++++++++----- tests/scripts/dashboard-web.test.js | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/scripts/dashboard-web.js b/scripts/dashboard-web.js index 5524853bd..9aaba4f89 100644 --- a/scripts/dashboard-web.js +++ b/scripts/dashboard-web.js @@ -44,9 +44,7 @@ const PORT = parsePort(process.argv[2] || process.env.ECC_DASHBOARD_PORT || '345 const HOST = resolveDashboardHost(); const ROOT = path.resolve(__dirname, '..'); -function readFrontmatter(p) { - try { - const c = fs.readFileSync(p, 'utf8'); +function parseFrontmatter(c) { const m = c.match(/^---\n([\s\S]*?)\n---/); if (!m) return {}; const fm = {}; @@ -63,9 +61,18 @@ function readFrontmatter(p) { } fm._body = c.replace(/^---[\s\S]*?---\n*/, '').trim(); return fm; - } catch { return {}; } } -function readSkill(p) { try { const c = fs.readFileSync(p, 'utf8'); const fm = readFrontmatter(p); return { d: fm.description || '', b: c.replace(/^---[\s\S]*?---\n*/, '').trim() }; } catch { return { d: '', b: '' }; } } +function readFrontmatter(p) { + try { return parseFrontmatter(fs.readFileSync(p, 'utf8')); } + catch { return {}; } +} +function readSkill(p) { + try { + const c = fs.readFileSync(p, 'utf8'); + const fm = parseFrontmatter(c); + return { d: fm.description || '', b: c.replace(/^---[\s\S]*?---\n*/, '').trim() }; + } catch { return { d: '', b: '' }; } +} function loadAgents(_root) { const root = _root || ROOT; diff --git a/tests/scripts/dashboard-web.test.js b/tests/scripts/dashboard-web.test.js index d9a125dbe..bb523d255 100644 --- a/tests/scripts/dashboard-web.test.js +++ b/tests/scripts/dashboard-web.test.js @@ -361,6 +361,28 @@ test('readSkill parses skill frontmatter and body', () => { cleanup(testRoot); }); +test('readSkill reads each skill file only once', () => { + const { readSkill } = require(SCRIPT); + testRoot = createTempDir('ecc-test-'); + const skillPath = path.join(testRoot, 'SKILL.md'); + writeFile(testRoot, 'SKILL.md', '---\ndescription: A test skill\n---\nbody'); + const originalReadFileSync = fs.readFileSync; + let readCount = 0; + fs.readFileSync = (...args) => { + readCount++; + return originalReadFileSync(...args); + }; + + try { + readSkill(skillPath); + } finally { + fs.readFileSync = originalReadFileSync; + cleanup(testRoot); + } + + assert.strictEqual(readCount, 1); +}); + test('readSkill returns empty defaults for missing file', () => { const { readSkill } = require(SCRIPT); const skill = readSkill('/nonexistent/skill/SKILL.md');