Multi-topic batch. The big-ticket item is the skills audit; the rest are smaller patches that compounded during the audit work. ## Skills audit (rules→recipes split) Vendored all 26 skills from /home/samkintop/opt/skills/ into data/skills/ (the boocode-repo-local skill library — see docker-compose change below). Audited via 5 parallel Claude Code agent-teams running the mgechev/skills-best-practices 4-step protocol (Discovery → Logic → Edge Case → self-Architecture-Refinement) per skill, ~2 min wall-clock vs the ~3.7-hour serial estimate. Result: 14 skills surviving (renamed to gerund form, frontmatter matched), 11 deleted (duplicates, BooCode-irrelevant patterns, Claude-already-does- natively), 1 migrated to BOOCHAT.md/BOOCODER.md as an always-true rule (verification-before-completion). Each surviving skill had its description refined to fix specific trigger gaps surfaced by the protocol — 4 real-bug findings landed (dead refs, stale tags, broken sub-file references in the original vendored content). Audit decisions documented in openspec/changes/v1.13.12-skills-audit/ audit-notes.md. Convention codified in BOOCHAT.md/BOOCODER.md "rules vs recipes" sections — future workflow rules go to those files (100% present), recipes stay in data/skills/ (~6% invoke rate in multi-turn per the Codeminer42 measurement). ## Token tracking + stale-stream banner fix (same root cause) ws-frames.ts IsoTimestamp was z.string().min(1) but postgres returns timestamp columns as JS Date objects. Every message_complete / session_updated / chat_updated frame was failing the v1.13.11 Zod gate and being silently dropped. Symptoms: token tracking blank in the UI (no usage frames landed); the 60s no-token-activity timer tripped the stale-stream banner because the frontend's local message state never saw status='streaming' flip to 'complete'. Fix: z.preprocess(v => v instanceof Date ? v.toISOString() : v, z.string().min(1)) applied to the IsoTimestamp primitive. Centralized, no publisher changes, works identically server + web (the parity test still passes). ## Codecontext .codecontextignore auto-install services/codecontext_client.ts now copies the codecontext/.codecontextignore.template into any project's root on the first call to that project if no .codecontextignore exists. One file written per project, idempotent (in-memory Set guard + access-check), silent fallback on read-only project. Stops the upstream empty-source- file parser crash on foreign projects' node_modules — previously required manually copying the template per project. ## Tool-call budget cap 30 → 50 services/inference/budget.ts: BUDGET_READ_ONLY and BUDGET_NO_AGENT bumped to 50 (from 30). BUDGET_NON_READ_ONLY stays at 10 (no write tools landed yet). Real recon sessions were hitting 30 with ~3 turns wasted on codecontext parse failures; legitimate need was ~27, and Architect-class system overviews want deeper recon. Headroom of 20 absorbs failure-retry turns without changing the safety floor — the doom-loop guard (3 identical calls → abort) catches the actual failure mode this cap was guarding against. v1.14 (Phase C outer agent loop) will supersede this via per-agent agent.steps. Throwaway-ish patch but unblocks deeper recon today. ## UI cleanups - ChatPane queued-message dropdown removed. Each queued message now has three buttons: edit (pop back into ChatInput via sendToChat event), force-send (was the dropdown's only useful action), and cancel. Default behavior (send when streaming completes) needs no UI — it's the implicit do-nothing path. - ChatThroughput removed from desktop tab strip (ChatTabBar.tsx). Mobile tab switcher still shows it. ## Plumbing - .gitignore: data/* + !data/AGENTS.md + !data/skills/ negation patterns so the vendored skill library + agent registry become git-tracked while session DB state stays out. - docker-compose.yml: removed /opt/skills:/data/skills override mount. Skills now live in the boocode repo at data/skills/, auditable per-batch. The host-level /opt/skills/ is preserved untouched for any other tools that read from it. - .codecontextignore at repo root: auto-installed when codecontext was first called against /opt/boocode itself; matches the template. - CLAUDE.md: updated to document the v1.13.11 publishFrame wrapper + message_parts table + tool_cost_stats view + DB-integration test pattern + host-side smoke endpoint quirk. (Pre-existing in working tree before this batch; shipped here for completeness.) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
355 lines
11 KiB
JavaScript
355 lines
11 KiB
JavaScript
const crypto = require('crypto');
|
|
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// ========== WebSocket Protocol (RFC 6455) ==========
|
|
|
|
const OPCODES = { TEXT: 0x01, CLOSE: 0x08, PING: 0x09, PONG: 0x0A };
|
|
const WS_MAGIC = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
|
|
|
function computeAcceptKey(clientKey) {
|
|
return crypto.createHash('sha1').update(clientKey + WS_MAGIC).digest('base64');
|
|
}
|
|
|
|
function encodeFrame(opcode, payload) {
|
|
const fin = 0x80;
|
|
const len = payload.length;
|
|
let header;
|
|
|
|
if (len < 126) {
|
|
header = Buffer.alloc(2);
|
|
header[0] = fin | opcode;
|
|
header[1] = len;
|
|
} else if (len < 65536) {
|
|
header = Buffer.alloc(4);
|
|
header[0] = fin | opcode;
|
|
header[1] = 126;
|
|
header.writeUInt16BE(len, 2);
|
|
} else {
|
|
header = Buffer.alloc(10);
|
|
header[0] = fin | opcode;
|
|
header[1] = 127;
|
|
header.writeBigUInt64BE(BigInt(len), 2);
|
|
}
|
|
|
|
return Buffer.concat([header, payload]);
|
|
}
|
|
|
|
function decodeFrame(buffer) {
|
|
if (buffer.length < 2) return null;
|
|
|
|
const secondByte = buffer[1];
|
|
const opcode = buffer[0] & 0x0F;
|
|
const masked = (secondByte & 0x80) !== 0;
|
|
let payloadLen = secondByte & 0x7F;
|
|
let offset = 2;
|
|
|
|
if (!masked) throw new Error('Client frames must be masked');
|
|
|
|
if (payloadLen === 126) {
|
|
if (buffer.length < 4) return null;
|
|
payloadLen = buffer.readUInt16BE(2);
|
|
offset = 4;
|
|
} else if (payloadLen === 127) {
|
|
if (buffer.length < 10) return null;
|
|
payloadLen = Number(buffer.readBigUInt64BE(2));
|
|
offset = 10;
|
|
}
|
|
|
|
const maskOffset = offset;
|
|
const dataOffset = offset + 4;
|
|
const totalLen = dataOffset + payloadLen;
|
|
if (buffer.length < totalLen) return null;
|
|
|
|
const mask = buffer.slice(maskOffset, dataOffset);
|
|
const data = Buffer.alloc(payloadLen);
|
|
for (let i = 0; i < payloadLen; i++) {
|
|
data[i] = buffer[dataOffset + i] ^ mask[i % 4];
|
|
}
|
|
|
|
return { opcode, payload: data, bytesConsumed: totalLen };
|
|
}
|
|
|
|
// ========== Configuration ==========
|
|
|
|
const PORT = process.env.BRAINSTORM_PORT || (49152 + Math.floor(Math.random() * 16383));
|
|
const HOST = process.env.BRAINSTORM_HOST || '127.0.0.1';
|
|
const URL_HOST = process.env.BRAINSTORM_URL_HOST || (HOST === '127.0.0.1' ? 'localhost' : HOST);
|
|
const SESSION_DIR = process.env.BRAINSTORM_DIR || '/tmp/brainstorm';
|
|
const CONTENT_DIR = path.join(SESSION_DIR, 'content');
|
|
const STATE_DIR = path.join(SESSION_DIR, 'state');
|
|
let ownerPid = process.env.BRAINSTORM_OWNER_PID ? Number(process.env.BRAINSTORM_OWNER_PID) : null;
|
|
|
|
const MIME_TYPES = {
|
|
'.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript',
|
|
'.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
|
|
'.jpeg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml'
|
|
};
|
|
|
|
// ========== Templates and Constants ==========
|
|
|
|
const WAITING_PAGE = `<!DOCTYPE html>
|
|
<html>
|
|
<head><meta charset="utf-8"><title>Brainstorm Companion</title>
|
|
<style>body { font-family: system-ui, sans-serif; padding: 2rem; max-width: 800px; margin: 0 auto; }
|
|
h1 { color: #333; } p { color: #666; }</style>
|
|
</head>
|
|
<body><h1>Brainstorm Companion</h1>
|
|
<p>Waiting for the agent to push a screen...</p></body></html>`;
|
|
|
|
const frameTemplate = fs.readFileSync(path.join(__dirname, 'frame-template.html'), 'utf-8');
|
|
const helperScript = fs.readFileSync(path.join(__dirname, 'helper.js'), 'utf-8');
|
|
const helperInjection = '<script>\n' + helperScript + '\n</script>';
|
|
|
|
// ========== Helper Functions ==========
|
|
|
|
function isFullDocument(html) {
|
|
const trimmed = html.trimStart().toLowerCase();
|
|
return trimmed.startsWith('<!doctype') || trimmed.startsWith('<html');
|
|
}
|
|
|
|
function wrapInFrame(content) {
|
|
return frameTemplate.replace('<!-- CONTENT -->', content);
|
|
}
|
|
|
|
function getNewestScreen() {
|
|
const files = fs.readdirSync(CONTENT_DIR)
|
|
.filter(f => f.endsWith('.html'))
|
|
.map(f => {
|
|
const fp = path.join(CONTENT_DIR, f);
|
|
return { path: fp, mtime: fs.statSync(fp).mtime.getTime() };
|
|
})
|
|
.sort((a, b) => b.mtime - a.mtime);
|
|
return files.length > 0 ? files[0].path : null;
|
|
}
|
|
|
|
// ========== HTTP Request Handler ==========
|
|
|
|
function handleRequest(req, res) {
|
|
touchActivity();
|
|
if (req.method === 'GET' && req.url === '/') {
|
|
const screenFile = getNewestScreen();
|
|
let html = screenFile
|
|
? (raw => isFullDocument(raw) ? raw : wrapInFrame(raw))(fs.readFileSync(screenFile, 'utf-8'))
|
|
: WAITING_PAGE;
|
|
|
|
if (html.includes('</body>')) {
|
|
html = html.replace('</body>', helperInjection + '\n</body>');
|
|
} else {
|
|
html += helperInjection;
|
|
}
|
|
|
|
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
res.end(html);
|
|
} else if (req.method === 'GET' && req.url.startsWith('/files/')) {
|
|
const fileName = req.url.slice(7);
|
|
const filePath = path.join(CONTENT_DIR, path.basename(fileName));
|
|
if (!fs.existsSync(filePath)) {
|
|
res.writeHead(404);
|
|
res.end('Not found');
|
|
return;
|
|
}
|
|
const ext = path.extname(filePath).toLowerCase();
|
|
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
|
|
res.writeHead(200, { 'Content-Type': contentType });
|
|
res.end(fs.readFileSync(filePath));
|
|
} else {
|
|
res.writeHead(404);
|
|
res.end('Not found');
|
|
}
|
|
}
|
|
|
|
// ========== WebSocket Connection Handling ==========
|
|
|
|
const clients = new Set();
|
|
|
|
function handleUpgrade(req, socket) {
|
|
const key = req.headers['sec-websocket-key'];
|
|
if (!key) { socket.destroy(); return; }
|
|
|
|
const accept = computeAcceptKey(key);
|
|
socket.write(
|
|
'HTTP/1.1 101 Switching Protocols\r\n' +
|
|
'Upgrade: websocket\r\n' +
|
|
'Connection: Upgrade\r\n' +
|
|
'Sec-WebSocket-Accept: ' + accept + '\r\n\r\n'
|
|
);
|
|
|
|
let buffer = Buffer.alloc(0);
|
|
clients.add(socket);
|
|
|
|
socket.on('data', (chunk) => {
|
|
buffer = Buffer.concat([buffer, chunk]);
|
|
while (buffer.length > 0) {
|
|
let result;
|
|
try {
|
|
result = decodeFrame(buffer);
|
|
} catch (e) {
|
|
socket.end(encodeFrame(OPCODES.CLOSE, Buffer.alloc(0)));
|
|
clients.delete(socket);
|
|
return;
|
|
}
|
|
if (!result) break;
|
|
buffer = buffer.slice(result.bytesConsumed);
|
|
|
|
switch (result.opcode) {
|
|
case OPCODES.TEXT:
|
|
handleMessage(result.payload.toString());
|
|
break;
|
|
case OPCODES.CLOSE:
|
|
socket.end(encodeFrame(OPCODES.CLOSE, Buffer.alloc(0)));
|
|
clients.delete(socket);
|
|
return;
|
|
case OPCODES.PING:
|
|
socket.write(encodeFrame(OPCODES.PONG, result.payload));
|
|
break;
|
|
case OPCODES.PONG:
|
|
break;
|
|
default: {
|
|
const closeBuf = Buffer.alloc(2);
|
|
closeBuf.writeUInt16BE(1003);
|
|
socket.end(encodeFrame(OPCODES.CLOSE, closeBuf));
|
|
clients.delete(socket);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
socket.on('close', () => clients.delete(socket));
|
|
socket.on('error', () => clients.delete(socket));
|
|
}
|
|
|
|
function handleMessage(text) {
|
|
let event;
|
|
try {
|
|
event = JSON.parse(text);
|
|
} catch (e) {
|
|
console.error('Failed to parse WebSocket message:', e.message);
|
|
return;
|
|
}
|
|
touchActivity();
|
|
console.log(JSON.stringify({ source: 'user-event', ...event }));
|
|
if (event.choice) {
|
|
const eventsFile = path.join(STATE_DIR, 'events');
|
|
fs.appendFileSync(eventsFile, JSON.stringify(event) + '\n');
|
|
}
|
|
}
|
|
|
|
function broadcast(msg) {
|
|
const frame = encodeFrame(OPCODES.TEXT, Buffer.from(JSON.stringify(msg)));
|
|
for (const socket of clients) {
|
|
try { socket.write(frame); } catch (e) { clients.delete(socket); }
|
|
}
|
|
}
|
|
|
|
// ========== Activity Tracking ==========
|
|
|
|
const IDLE_TIMEOUT_MS = 30 * 60 * 1000; // 30 minutes
|
|
let lastActivity = Date.now();
|
|
|
|
function touchActivity() {
|
|
lastActivity = Date.now();
|
|
}
|
|
|
|
// ========== File Watching ==========
|
|
|
|
const debounceTimers = new Map();
|
|
|
|
// ========== Server Startup ==========
|
|
|
|
function startServer() {
|
|
if (!fs.existsSync(CONTENT_DIR)) fs.mkdirSync(CONTENT_DIR, { recursive: true });
|
|
if (!fs.existsSync(STATE_DIR)) fs.mkdirSync(STATE_DIR, { recursive: true });
|
|
|
|
// Track known files to distinguish new screens from updates.
|
|
// macOS fs.watch reports 'rename' for both new files and overwrites,
|
|
// so we can't rely on eventType alone.
|
|
const knownFiles = new Set(
|
|
fs.readdirSync(CONTENT_DIR).filter(f => f.endsWith('.html'))
|
|
);
|
|
|
|
const server = http.createServer(handleRequest);
|
|
server.on('upgrade', handleUpgrade);
|
|
|
|
const watcher = fs.watch(CONTENT_DIR, (eventType, filename) => {
|
|
if (!filename || !filename.endsWith('.html')) return;
|
|
|
|
if (debounceTimers.has(filename)) clearTimeout(debounceTimers.get(filename));
|
|
debounceTimers.set(filename, setTimeout(() => {
|
|
debounceTimers.delete(filename);
|
|
const filePath = path.join(CONTENT_DIR, filename);
|
|
|
|
if (!fs.existsSync(filePath)) return; // file was deleted
|
|
touchActivity();
|
|
|
|
if (!knownFiles.has(filename)) {
|
|
knownFiles.add(filename);
|
|
const eventsFile = path.join(STATE_DIR, 'events');
|
|
if (fs.existsSync(eventsFile)) fs.unlinkSync(eventsFile);
|
|
console.log(JSON.stringify({ type: 'screen-added', file: filePath }));
|
|
} else {
|
|
console.log(JSON.stringify({ type: 'screen-updated', file: filePath }));
|
|
}
|
|
|
|
broadcast({ type: 'reload' });
|
|
}, 100));
|
|
});
|
|
watcher.on('error', (err) => console.error('fs.watch error:', err.message));
|
|
|
|
function shutdown(reason) {
|
|
console.log(JSON.stringify({ type: 'server-stopped', reason }));
|
|
const infoFile = path.join(STATE_DIR, 'server-info');
|
|
if (fs.existsSync(infoFile)) fs.unlinkSync(infoFile);
|
|
fs.writeFileSync(
|
|
path.join(STATE_DIR, 'server-stopped'),
|
|
JSON.stringify({ reason, timestamp: Date.now() }) + '\n'
|
|
);
|
|
watcher.close();
|
|
clearInterval(lifecycleCheck);
|
|
server.close(() => process.exit(0));
|
|
}
|
|
|
|
function ownerAlive() {
|
|
if (!ownerPid) return true;
|
|
try { process.kill(ownerPid, 0); return true; } catch (e) { return e.code === 'EPERM'; }
|
|
}
|
|
|
|
// Check every 60s: exit if owner process died or idle for 30 minutes
|
|
const lifecycleCheck = setInterval(() => {
|
|
if (!ownerAlive()) shutdown('owner process exited');
|
|
else if (Date.now() - lastActivity > IDLE_TIMEOUT_MS) shutdown('idle timeout');
|
|
}, 60 * 1000);
|
|
lifecycleCheck.unref();
|
|
|
|
// Validate owner PID at startup. If it's already dead, the PID resolution
|
|
// was wrong (common on WSL, Tailscale SSH, and cross-user scenarios).
|
|
// Disable monitoring and rely on the idle timeout instead.
|
|
if (ownerPid) {
|
|
try { process.kill(ownerPid, 0); }
|
|
catch (e) {
|
|
if (e.code !== 'EPERM') {
|
|
console.log(JSON.stringify({ type: 'owner-pid-invalid', pid: ownerPid, reason: 'dead at startup' }));
|
|
ownerPid = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
server.listen(PORT, HOST, () => {
|
|
const info = JSON.stringify({
|
|
type: 'server-started', port: Number(PORT), host: HOST,
|
|
url_host: URL_HOST, url: 'http://' + URL_HOST + ':' + PORT,
|
|
screen_dir: CONTENT_DIR, state_dir: STATE_DIR
|
|
});
|
|
console.log(info);
|
|
fs.writeFileSync(path.join(STATE_DIR, 'server-info'), info + '\n');
|
|
});
|
|
}
|
|
|
|
if (require.main === module) {
|
|
startServer();
|
|
}
|
|
|
|
module.exports = { computeAcceptKey, encodeFrame, decodeFrame, OPCODES };
|