The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
fix: keep activity dashboard executable and bound log reads
haelyra846c4506587c
diff --git a/scripts/dashboard-web.js b/scripts/dashboard-web.jsindex 1ad612510..91454abe6 100644--- a/scripts/dashboard-web.js+++ b/scripts/dashboard-web.js@@ -857,7 +857,7 @@ handleRoute();'<span class="act-pill act-pill-agent">Agent: ' + agentCount + '</span>' +'</div>';if (!entries.length) {- panel.innerHTML = summary + '<div class="act-empty">No recorded tool calls yet — this reads ~/.claude/metrics/tool-usage.jsonl, written by the session-activity-tracker PostToolUse hook. Needs that hook active in the session you\'re watching.</div>';+ panel.innerHTML = summary + '<div class="act-empty">No recorded tool calls yet. This reads ~/.claude/metrics/tool-usage.jsonl, written by the session-activity-tracker PostToolUse hook. Requires that hook in the active session.</div>';return;}panel.innerHTML = summary + '<div class="act-feed">' + entries.map(rowHtml).join('') + '</div>';@@ -918,20 +918,36 @@ function sendHtml(res, statusCode, html) {res.end(html);}-function loadActivity(limit = 200) {- const logPath = path.join(os.homedir(), '.claude', 'metrics', 'tool-usage.jsonl');+const MAX_ACTIVITY_LOG_BYTES = 256 * 1024;++function loadActivity(limit = 200, logPath = path.join(os.homedir(), '.claude', 'metrics', 'tool-usage.jsonl')) {+ let fd;let text;try {- text = fs.readFileSync(logPath, 'utf8');+ fd = fs.openSync(logPath, 'r');+ const stat = fs.fstatSync(fd);+ if (!stat.isFile()) return [];+ const length = Math.min(stat.size, MAX_ACTIVITY_LOG_BYTES);+ const start = stat.size - length;+ const buffer = Buffer.alloc(length);+ const bytesRead = fs.readSync(fd, buffer, 0, length, start);+ text = buffer.toString('utf8', 0, bytesRead);+ // A bounded tail may start inside a JSON row or a multibyte character.+ if (start > 0) text = text.slice(text.indexOf('\n') + 1);} catch {return [];+ } finally {+ if (fd !== undefined) fs.closeSync(fd);}const lines = text.split('\n').filter(Boolean);const start = Math.max(0, lines.length - limit);const out = [];for (let i = lines.length - 1; i >= start; i -= 1) {try {- out.push(JSON.parse(lines[i]));+ const row = JSON.parse(lines[i]);+ if (row && !Array.isArray(row) && typeof row.session_id === 'string' && typeof row.tool_name === 'string') {+ out.push(row);+ }} catch {// Skip unparseable lines rather than fail the whole feed.}@@ -1063,6 +1079,7 @@ module.exports = {createDashboardServer,listenDashboardServer,loadAgents,+ loadActivity,loadCommands,loadHooks,loadMcps,diff --git a/tests/scripts/dashboard-web.test.js b/tests/scripts/dashboard-web.test.jsindex d9a125dbe..b3809def4 100644--- a/tests/scripts/dashboard-web.test.js+++ b/tests/scripts/dashboard-web.test.js@@ -8,6 +8,7 @@ const os = require('os');const path = require('path');const http = require('http');const net = require('net');+const vm = require('vm');const SCRIPT = path.join(__dirname, '..', '..', 'scripts', 'dashboard-web.js');@@ -17,6 +18,37 @@ let testFailed = 0;const asyncTests = [];const REQUEST_TIMEOUT_MS = 5000;+test('renderHTML emits executable scripts without syntax errors', () => {+ const { renderHTML } = require(SCRIPT);+ const html = renderHTML({ agents: [], skills: [], commands: [], rules: [], mcps: [], hooks: [] });+ const scripts = [...html.matchAll(/<script>([\s\S]*?)<\/script>/g)];+ assert.ok(scripts.length > 0);+ for (const script of scripts) new vm.Script(script[1]);+});++test('activity loading bounds large logs and ignores malformed rows', () => {+ const { loadActivity } = require(SCRIPT);+ withTempDir('ecc-activity-', dir => {+ const log = path.join(dir, 'usage.jsonl');+ const row = name => JSON.stringify({ session_id: 'fixture', tool_name: name });+ fs.writeFileSync(log, 'x'.repeat(1024 * 1024) + '\n' + row('Read') + '\nnull\n{"session_id":42,"tool_name":"Read"}\nnot-json\n' + row('Skill') + '\n');+ const originalRead = fs.readSync;+ let bytesRequested = 0;+ fs.readSync = (...args) => {+ bytesRequested += args[3];+ return originalRead(...args);+ };+ let rows;+ try { rows = loadActivity(200, log); }+ finally { fs.readSync = originalRead; }+ assert.ok(bytesRequested <= 256 * 1024);+ assert.deepStrictEqual(rows.map(entry => entry.tool_name), ['Skill', 'Read']);+ fs.writeFileSync(log, row('Edit') + '\n');+ assert.deepStrictEqual(loadActivity(200, log).map(entry => entry.tool_name), ['Edit']);+ assert.deepStrictEqual(loadActivity(200, path.join(dir, 'missing')), []);+ });+});+function test(name, fn) {try {fn();