46d204ee9b
* fix: strip privacy tags from last_assistant_message in summarize path (cherry picked from commit bd68bfcc3cfe9d82977d5bdb87cf7e91a7258489) * fix: preserve Chroma relevance ordering in SQLite hydration When ChromaSearchStrategy queries by vector similarity with orderBy='relevance', SessionStore.getObservationsByIds and related methods silently coerced undefined to 'date_desc', destroying the semantic ranking. Add 'relevance' as a valid orderBy value that skips SQL ORDER BY and preserves caller-provided ID order. Fixes #2153 (cherry picked from commit 9fedf8fc165c01cc3a8a8cdb8c057ea980bf511e) * test(privacy): mock executeWithWorkerFallback and loadFromFileOnce Update the cherry-picked privacy-tag stripping test from swithek's fork to match current main: - Mock executeWithWorkerFallback / isWorkerFallback (the handler now uses these instead of workerHttpRequest directly). - Mock loadFromFileOnce in hook-settings.js (called by shouldTrackProject) so the handler resolves CLAUDE_MEM_EXCLUDED_PROJECTS to a string. - Switch the workerCallLog shape to record { path, method, body } and accept either object or JSON-string bodies. 10/10 tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: pass relevance through to SessionStore in ChromaSearchStrategy The Chroma strategy was coercing orderBy='relevance' to undefined before calling SessionStore. Combined with SessionStore's date_desc default for undefined, this destroyed the semantic ranking that Chroma had just computed. Pair this with the SessionStore-side fix from rogerdigital (commit 37c8988f) which now accepts 'relevance' as a valid orderBy and preserves caller-provided ID order. Adds a regression test asserting that getObservationsByIds returns rows in caller-provided order when orderBy='relevance', and continues to return date_desc order when orderBy is omitted. Closes #2153 Co-Authored-By: Roger Deng <13251150+rogerdigital@users.noreply.github.com> Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: isolate SDK boundary — settingSources, strictMcpConfig, cloud-provider env, observation cap Single architectural fix at the three @anthropic-ai/claude-agent-sdk query() call sites (SDKAgent.startSession, KnowledgeAgent.prime, KnowledgeAgent .executeQuery) plus the env sanitizer and ingest gate. Closes 6 issues: - #2155 settings.json bleed-through into observer SDK subprocess: pass settingSources: [] so user/project/local settings aren't inherited. - #2159 / #2171 / #2194 user MCP servers leak into observer SDK: pass strictMcpConfig: true alongside the existing mcpServers: {}. - #2199 Bedrock/Vertex env vars dropped: extend ENV_PRESERVE in src/supervisor/env-sanitizer.ts to keep CLAUDE_CODE_USE_BEDROCK, CLAUDE_CODE_USE_VERTEX, AWS_*, ANTHROPIC_VERTEX_PROJECT_ID, etc. - #2201 runaway tokens (345M/day reported): extend default CLAUDE_MEM_SKIP_TOOLS with exec_command, write_stdin, apply_patch and add a configurable CLAUDE_MEM_MAX_OBSERVATION_BYTES (default 64 KB) cap at the ingest gate. SDK option names verified against node_modules/@anthropic-ai/claude-agent-sdk/sdk.d.ts: settingSources?: SettingSource[] (SettingSource = 'user'|'project'|'local') strictMcpConfig?: boolean Anti-pattern guards observed: - Did not modify the proxy strip (#2099/#2115). - Did not skip Read/Write/Edit/Bash — those remain the primary observation surface; only added high-volume agentic-tool names (exec_command, write_stdin, apply_patch). - Did not invent SDK options. Closes #2155, #2159, #2171, #2194, #2199, #2201 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: restore Windows spawn fix from PR #751 + add Windows CI Re-applies the PowerShell Start-Process -WindowStyle Hidden daemon spawn that PR #751 (e6ae0176) introduced and commitd13662d5reverted. Also fixes the bun-runner cmd /c popup, sets detached:false on Windows for SDK subprocesses (so windowsHide actually works and claude.exe doesn't outlive the worker), and adds windows-latest CI to prevent regression. - ProcessManager.spawnDaemon: PowerShell -EncodedCommand branch back. Returns 0 sentinel on success — callers MUST use pid === undefined for failure detection, never falsy checks. - bun-runner.js: drop "cmd /c" wrapper. shell:true lets Node resolve bun.cmd via PATHEXT and respects windowsHide (the explicit cmd.exe wrapper was popping a visible window per hook — #2150, #2186). - process-registry.ts spawnSdkProcess: detached:false on Windows. Mixing detached:true with windowsHide:true is documented-undefined on Windows; with detached:false, windowsHide actually hides claude.exe and the SDK subprocess dies with the parent (#2190, #2198). - .github/workflows/windows.yml: smoke test counts visible cmd windows before/after spawn + grep guard that the Start-Process branch survives. WSL bash stdin (#2188) is acknowledged but deferred — the bash → node pipe boundary needs a real Windows VM to test, beyond this PR's scope. PTY for Claude CLI SDK mode (#2173, #2177) is also deferred per plan. Closes #2150, #2169, #2186, #2187, #2190, #2198 Refs #2183 (Windows perf — same root cause) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: Codex transcript ingestion + queue self-deadlock on Windows Three Windows-specific bugs surfaced by @MakaveliGER in #2192: A. Glob path normalization path.join(homedir(), ...) emits backslashes on Windows. globSync treats backslashes as escape characters, not separators, so it silently fails to match transcript files. Normalize backslashes to forward slashes before passing to globSync (only affects Windows; Unix paths unchanged). B. Live appends not picked up Per-file fs.watch on Windows ReFS/SMB misses appends to live JSONL files; the recursive root watcher is the only signal we can trust there. Expose FileTailer.poke() and call it from the root-watcher event when the file is already tailed, instead of returning early. Also normalize the resolved path so the tailer-map key matches what globSync stored. C. Queue self-deadlock on abort When the SDK generator aborts (idle timeout, user cancel, shutdown) with rows already claimed and yielded but not yet confirmed by ResponseProcessor, those rows sit in 'processing' under THIS worker's PID. The self-healing claim predicate skips them because the worker is still alive — the queue deadlocks until the worker restarts. In the .finally() block, walk the in-flight ids through markFailed so the retry ladder requeues them as 'pending' (or terminates them if retries are exhausted). Includes regression test tests/codex-transcript-watcher-windows.test.ts that asserts each fix at the source level so future refactors can't silently revert them. Co-Authored-By: MakaveliGER <noreply@github.com> Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Closes #2192 * fix: standalone batch — npm peer-deps overrides, marketplace self-heal warning, cache prune - Add `overrides: { tree-sitter: ^0.25.0 }` to the generated plugin/package.json so `npm install --production` resolves cleanly without --legacy-peer-deps. Fixes the ERESOLVE between grammar packages declaring three different majors of tree-sitter as peer deps. Closes #2147. - mcp-server.ts: emit a single loud, actionable warning when MCP boots but the marketplace directory at ~/.claude/plugins/marketplaces/<source>/ is missing. IDE plugin loaders silently skip claude-mem hooks in this state while MCP keeps working — the user has no way to know memory capture is dead. We don't run an installer from MCP startup (different permission model), but we tell the user exactly which command to run. Closes #2174. - smart-install.js (both root and plugin variants): prune older claude-mem version directories from ~/.claude/plugins/cache/thedotmack/claude-mem/. Claude Code resolves and caches hook commands per session, so a stale 12.x directory keeps the old hook path alive across restarts even after upgrade. Pruning makes the stale path physically unreachable. Closes #2172 (stale version reference). Note: the issue's secondary claim that @anthropic-ai/claude-agent-sdk is missing from package.json is no longer true — it was added at line 115 in v12.4.x. - #2170 ("ToolUseContext is required for prompt hooks") triaged as upstream: the string does not appear anywhere in this repo. The error originates in Claude Code's hook framework, which we don't own. No code change here. Co-Authored-By: Amadan04 <amadan04@users.noreply.github.com> Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: remove stale macOS binary, regen plugin artifacts (build/bundle drift) The committed plugin/scripts/claude-mem (63 MB Mach-O) was last built at v10.3.2 (Feb 2026). It baked in BUILT_IN_VERSION="10.3.1", dev paths (/Users/alexnewman/Scripts/claude-mem/...), and a now-removed POST /api/sessions/complete client + handler (deleted by PR #2136). That meant macOS users running the cached binary hit 404s every time the SessionEnd hook fired (issue #2200), the /api/health endpoint reported a two-major-versions-ago version (issue #2158), and the binary embedded a Zod copy that drifted from the worker bundle (issue #2154). - Delete plugin/scripts/claude-mem and gitignore it. The npm package already excludes it from the "files" allowlist, so no consumer change. The JS fallback (bun-runner.js → worker-service.cjs) covers all functionality on every platform per the existing checkBinaryPlatformCompatibility comment in smart-install.js. - Add npm run build:cli-binary for users who want the macOS speedup back. Produces it on demand from current source — no drift. - Regenerate plugin/scripts/{worker-service,mcp-server}.cjs and plugin/ui/viewer-bundle.js so the shipped artifacts match HEAD. Closes #2158, #2200, #2154. * fix(ci): Windows workflow — install without lockfile (project uses Bun) actions/setup-node@v4 cache: npm requires a package-lock.json and this project uses Bun (only bunfig.toml exists at root). Drop the cache directive, switch npm ci to npm install --no-audit --no-fund, and narrow the build step to npm run build — build-and-sync also runs a marketplace sync + worker restart that hardcodes ~/.claude/plugins, which doesn't exist on CI. * fix: harden observation cap parsing + safe stringify in debug logger CodeRabbit majors on #2206: - shared.ts: validate parsed cap is finite and > 0 before use; wrap JSON.stringify(payload.toolResponse) in try/catch and skip with reason 'payload_unserializable' on circular/throwing payloads, so ingestion never crashes on a bad tool response shape. - logger.ts: the debug-mode JSON dump for objects was unguarded; wrap stringify in try/catch and fall back to formatData on cycles. This is the source the bundled plugin/scripts/context-generator.cjs is built from. * fix(ci+windows): quote bun-runner shell:true args; replace dynamic smoke with static guards CodeRabbit majors on #2208: 1. plugin/scripts/bun-runner.js — shell:true with separate spawnArgs triggers DEP0190 on Node 22+ and breaks paths/args containing spaces. Build a single fully-quoted command string (mirroring findBun()'s 'where bun' approach) and pass spawnArgs=[]. 2. .github/workflows/windows.yml — the dynamic smoke step that counted visible cmd windows around 'claude-mem start' exits 1 on 'claude-mem is not installed' before exercising the spawn path, AND PowerShell try/catch doesn't suppress native exit codes regardless. Replace with three static regression guards covering the exact patterns PR #2208 protects: - PowerShell Start-Process + WindowStyle Hidden in spawnDaemon - bun-runner shell:true with empty spawnArgs (DEP0190 guard) - windowsHide set on SDK spawn factory (issue #2190) * fix(2210): cross-platform paths — Windows USERPROFILE + XDG cache symmetry Greptile P2s on #2210: - mcp-server.ts checkMarketplaceMarker: switch from process.env.HOME ?? '' to os.homedir(). HOME is unset on Windows; the empty fallback resolves relative to cwd, silently no-opping the canary on every Windows install. Also probe both ~/.claude/ and ~/.config/claude/ for the cache check so XDG users get the same warning behavior. - smart-install.js pruneStaleVersionCache (both root + plugin copies): scan both ~/.claude/plugins/cache/thedotmack/ and ~/.config/claude/... paths so users on XDG don't keep stale dirs re-triggering #2172. Greptile's third P2 (mtime vs semver sort for current version) deferred: mtime works correctly for the common case and the directory names start with versions that lexicographically sort the same way mtime does for sequentially-installed versions; semver sort would be a separate change. Refs PR #2210 * fix(2211): drop hardcoded --target from build:cli-binary Greptile P2: the npm script was pinned to bun-darwin-arm64, so an Intel Mac user (or anyone on Linux/Windows running this script manually) got a cross-compiled arm64 binary that runs only via Rosetta on x64 macOS and not at all elsewhere. Bun's --compile defaults to the host platform when --target is omitted. Drop the flag so the script produces a binary that matches whoever runs it. CI builds that need a specific target can still pass --target explicitly. Refs PR #2211 * ci(windows): drop static-grep tripwires, keep real Windows build The "Anti-regression" steps grep ProcessManager.ts/bun-runner.js/process-registry.ts for specific strings (Start-Process, WindowStyle Hidden, shell:true, windowsHide). Tripwires aren't fixes — they make refactoring harder forever and verify nothing the actual Windows build doesn't already verify. The npm install + npm run build on windows-latest is the real guard. * revert: drop byte cap and skip-list extension band-aids Strips two band-aid mechanisms from the SDK boundary fix, keeping only the genuine isolation flags (settingSources: [], strictMcpConfig: true) and the cloud-provider env preservation. Removed: - CLAUDE_MEM_MAX_OBSERVATION_BYTES (default 65536) — dropped oversize observations entirely. The structural fix is to chunk/summarize oversize tool results, not punish the data flow with an invented byte threshold. Tracked separately. - exec_command, write_stdin, apply_patch added to default skip list — static taste decision baked into defaults for everyone. Users can still set CLAUDE_MEM_SKIP_TOOLS themselves. The data flows again. Real fix is a follow-up. * revert: drop pruneStaleVersionCache walker Removes the cache walker that scans plugin cache dirs and deletes "old" version directories by inferred staleness. The structural fix for #2172 is for the installer to delete the prior version when it writes the new one — not for a separate walker to wake up later and guess which directories are stale. Keeps: - npm peer-dep override for tree-sitter (#2147) - Marketplace marker startup probe (#2174) - Cross-platform path handling Tracked separately as a follow-up. * build: regenerate bundled artifacts after merge Rebuilt plugin/scripts/*.cjs from src after merging #2211, #2204, #2205, #2208, #2209, #2206 (post-strip), #2210 (post-strip). Conflicts during merge were resolved by accepting incoming bundled artifacts; this commit replaces them with a clean rebuild from the merged source. Verified: 0 references to MAX_OBSERVATION_BYTES, payload_too_large, or pruneStaleVersionCache in the rebuilt artifacts. --------- Co-authored-by: swithek <52840391+swithek@users.noreply.github.com> Co-authored-by: Roger Deng <13251150+rogerdigital@users.noreply.github.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: Amadan04 <amadan04@users.noreply.github.com>
450 lines
18 KiB
JavaScript
450 lines
18 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Build script for claude-mem hooks
|
|
* Bundles TypeScript hooks into individual standalone executables using esbuild
|
|
*/
|
|
|
|
import { build } from 'esbuild';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
const WORKER_SERVICE = {
|
|
name: 'worker-service',
|
|
source: 'src/services/worker-service.ts'
|
|
};
|
|
|
|
const MCP_SERVER = {
|
|
name: 'mcp-server',
|
|
source: 'src/servers/mcp-server.ts'
|
|
};
|
|
|
|
const CONTEXT_GENERATOR = {
|
|
name: 'context-generator',
|
|
source: 'src/services/context-generator.ts'
|
|
};
|
|
|
|
/**
|
|
* Strip hardcoded __dirname/__filename from bundled CJS output.
|
|
*
|
|
* When esbuild converts ESM TypeScript source to CJS format, it inlines
|
|
* __dirname and __filename as static strings based on the SOURCE file paths
|
|
* at build time. These `var __dirname = "/build/machine/path/..."` declarations
|
|
* shadow the runtime's native __dirname (provided by Bun/Node's CJS module
|
|
* wrapper), causing path resolution to fail on end-user machines.
|
|
*
|
|
* This post-build step removes those hardcoded assignments so the runtime
|
|
* globals are used instead.
|
|
*
|
|
* See: https://github.com/thedotmack/claude-mem/issues/1410
|
|
*/
|
|
function stripHardcodedDirname(filePath) {
|
|
let content = fs.readFileSync(filePath, 'utf-8');
|
|
const before = content.length;
|
|
|
|
// Match both double-quoted and single-quoted string literals.
|
|
// esbuild currently emits double quotes, but single quotes are handled
|
|
// defensively in case future versions change quoting style.
|
|
const str = `(?:"[^"]*"|'[^']*')`;
|
|
|
|
for (const id of ['__dirname', '__filename']) {
|
|
// Remove `var <id> = "...", rest` → `var rest`
|
|
content = content.replace(new RegExp(`\\bvar ${id}\\s*=\\s*${str},\\s*`, 'g'), 'var ');
|
|
// Remove standalone `var <id> = "...";`
|
|
content = content.replace(new RegExp(`\\bvar ${id}\\s*=\\s*${str};\\s*`, 'g'), '');
|
|
// Remove `, <id> = "..."` from mid/end of var declarations
|
|
content = content.replace(new RegExp(`,\\s*${id}\\s*=\\s*${str}`, 'g'), '');
|
|
}
|
|
|
|
// Clean up dangling `var ;` left when __dirname was the sole declarator
|
|
content = content.replace(/\bvar\s*;/g, '');
|
|
|
|
const removed = before - content.length;
|
|
if (removed > 0) {
|
|
fs.writeFileSync(filePath, content);
|
|
console.log(` ✓ Stripped hardcoded __dirname/__filename paths (${removed} bytes)`);
|
|
}
|
|
}
|
|
|
|
async function buildHooks() {
|
|
console.log('🔨 Building claude-mem hooks and worker service...\n');
|
|
|
|
try {
|
|
// Read version from package.json
|
|
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
|
|
const version = packageJson.version;
|
|
console.log(`📌 Version: ${version}`);
|
|
|
|
// Create output directories
|
|
console.log('\n📦 Preparing output directories...');
|
|
const hooksDir = 'plugin/scripts';
|
|
const uiDir = 'plugin/ui';
|
|
|
|
if (!fs.existsSync(hooksDir)) {
|
|
fs.mkdirSync(hooksDir, { recursive: true });
|
|
}
|
|
if (!fs.existsSync(uiDir)) {
|
|
fs.mkdirSync(uiDir, { recursive: true });
|
|
}
|
|
console.log('✓ Output directories ready');
|
|
|
|
// Generate plugin/package.json for cache directory dependency installation
|
|
// Note: bun:sqlite is a Bun built-in, no external dependencies needed for SQLite
|
|
console.log('\n📦 Generating plugin package.json...');
|
|
const pluginPackageJson = {
|
|
name: 'claude-mem-plugin',
|
|
version: version,
|
|
private: true,
|
|
description: 'Runtime dependencies for claude-mem bundled hooks',
|
|
type: 'module',
|
|
dependencies: {
|
|
// Externalized from mcp-server.cjs to avoid Zod version conflicts when
|
|
// OpenCode's Bun bundler assembles hook scripts (#2113). MCP SDK
|
|
// transitively imports Zod; loading it via node_modules at runtime
|
|
// ensures OpenCode controls the version.
|
|
'zod': '^4.3.6',
|
|
'tree-sitter-cli': '^0.26.5',
|
|
'tree-sitter-c': '^0.24.1',
|
|
'tree-sitter-cpp': '^0.23.4',
|
|
'tree-sitter-go': '^0.25.0',
|
|
'tree-sitter-java': '^0.23.5',
|
|
'tree-sitter-javascript': '^0.25.0',
|
|
'tree-sitter-python': '^0.25.0',
|
|
'tree-sitter-ruby': '^0.23.1',
|
|
'tree-sitter-rust': '^0.24.0',
|
|
'tree-sitter-typescript': '^0.23.2',
|
|
'tree-sitter-kotlin': '^0.3.8',
|
|
'tree-sitter-swift': '^0.7.1',
|
|
'tree-sitter-php': '^0.24.2',
|
|
'tree-sitter-elixir': '^0.3.5',
|
|
'@tree-sitter-grammars/tree-sitter-lua': '^0.4.1',
|
|
'tree-sitter-scala': '^0.24.0',
|
|
'tree-sitter-bash': '^0.25.1',
|
|
'tree-sitter-haskell': '^0.23.1',
|
|
'@tree-sitter-grammars/tree-sitter-zig': '^1.1.2',
|
|
'tree-sitter-css': '^0.25.0',
|
|
'tree-sitter-scss': '^1.0.0',
|
|
'@tree-sitter-grammars/tree-sitter-toml': '^0.7.0',
|
|
'@tree-sitter-grammars/tree-sitter-yaml': '^0.7.1',
|
|
'@derekstride/tree-sitter-sql': '^0.3.11',
|
|
'@tree-sitter-grammars/tree-sitter-markdown': '^0.3.2',
|
|
},
|
|
// The grammar packages above declare three different majors of `tree-sitter`
|
|
// as peer deps (^0.21, ^0.22, ^0.25). Bun and pnpm are lenient enough to
|
|
// pick one and move on, but plain `npm install --production` aborts with
|
|
// ERESOLVE. Pinning a single version via `overrides` lets npm resolve a
|
|
// working tree without `--legacy-peer-deps`. Closes #2147.
|
|
overrides: {
|
|
'tree-sitter': '^0.25.0'
|
|
},
|
|
engines: {
|
|
node: '>=18.0.0',
|
|
bun: '>=1.0.0'
|
|
}
|
|
};
|
|
fs.writeFileSync('plugin/package.json', JSON.stringify(pluginPackageJson, null, 2) + '\n');
|
|
console.log('✓ plugin/package.json generated');
|
|
|
|
// Build React viewer
|
|
console.log('\n📋 Building React viewer...');
|
|
const { spawn } = await import('child_process');
|
|
const viewerBuild = spawn('node', ['scripts/build-viewer.js'], { stdio: 'inherit' });
|
|
await new Promise((resolve, reject) => {
|
|
viewerBuild.on('exit', (code) => {
|
|
if (code === 0) {
|
|
resolve();
|
|
} else {
|
|
reject(new Error(`Viewer build failed with exit code ${code}`));
|
|
}
|
|
});
|
|
});
|
|
|
|
// Build worker service
|
|
console.log(`\n🔧 Building worker service...`);
|
|
await build({
|
|
entryPoints: [WORKER_SERVICE.source],
|
|
bundle: true,
|
|
platform: 'node',
|
|
target: 'node18',
|
|
format: 'cjs',
|
|
outfile: `${hooksDir}/${WORKER_SERVICE.name}.cjs`,
|
|
minify: true,
|
|
logLevel: 'error', // Suppress warnings (import.meta warning is benign)
|
|
external: [
|
|
'bun:sqlite',
|
|
// Optional chromadb embedding providers
|
|
'cohere-ai',
|
|
'ollama',
|
|
// Default embedding function with native binaries
|
|
'@chroma-core/default-embed',
|
|
'onnxruntime-node'
|
|
],
|
|
define: {
|
|
'__DEFAULT_PACKAGE_VERSION__': `"${version}"`
|
|
},
|
|
banner: {
|
|
js: [
|
|
'#!/usr/bin/env bun',
|
|
'var __filename = __filename || require("node:path").resolve(process.argv[1] || "");',
|
|
'var __dirname = __dirname || require("node:path").dirname(__filename);'
|
|
].join('\n')
|
|
}
|
|
});
|
|
|
|
// Fix hardcoded __dirname/__filename in bundled output (#1410)
|
|
stripHardcodedDirname(`${hooksDir}/${WORKER_SERVICE.name}.cjs`);
|
|
|
|
// Make worker service executable
|
|
fs.chmodSync(`${hooksDir}/${WORKER_SERVICE.name}.cjs`, 0o755);
|
|
const workerStats = fs.statSync(`${hooksDir}/${WORKER_SERVICE.name}.cjs`);
|
|
console.log(`✓ worker-service built (${(workerStats.size / 1024).toFixed(2)} KB)`);
|
|
|
|
// Build MCP server
|
|
console.log(`\n🔧 Building MCP server...`);
|
|
await build({
|
|
entryPoints: [MCP_SERVER.source],
|
|
bundle: true,
|
|
platform: 'node',
|
|
target: 'node18',
|
|
format: 'cjs',
|
|
outfile: `${hooksDir}/${MCP_SERVER.name}.cjs`,
|
|
minify: true,
|
|
logLevel: 'error',
|
|
external: [
|
|
'bun:sqlite',
|
|
// Externalize Zod to avoid version conflicts when OpenCode's Bun bundler
|
|
// assembles hook scripts (see #2113). The MCP server transitively imports
|
|
// Zod via @modelcontextprotocol/sdk; bundling it caused two Zod versions
|
|
// to coexist at runtime and the v4 ↔ v3 _zod.def access crashed.
|
|
'zod',
|
|
'tree-sitter-cli',
|
|
'tree-sitter-javascript',
|
|
'tree-sitter-typescript',
|
|
'tree-sitter-python',
|
|
'tree-sitter-go',
|
|
'tree-sitter-rust',
|
|
'tree-sitter-ruby',
|
|
'tree-sitter-java',
|
|
'tree-sitter-c',
|
|
'tree-sitter-cpp',
|
|
'tree-sitter-kotlin',
|
|
'tree-sitter-swift',
|
|
'tree-sitter-php',
|
|
'tree-sitter-elixir',
|
|
'@tree-sitter-grammars/tree-sitter-lua',
|
|
'tree-sitter-scala',
|
|
'tree-sitter-bash',
|
|
'tree-sitter-haskell',
|
|
'@tree-sitter-grammars/tree-sitter-zig',
|
|
'tree-sitter-css',
|
|
'tree-sitter-scss',
|
|
'@tree-sitter-grammars/tree-sitter-toml',
|
|
'@tree-sitter-grammars/tree-sitter-yaml',
|
|
'@derekstride/tree-sitter-sql',
|
|
'@tree-sitter-grammars/tree-sitter-markdown',
|
|
],
|
|
define: {
|
|
'__DEFAULT_PACKAGE_VERSION__': `"${version}"`
|
|
},
|
|
banner: {
|
|
js: '#!/usr/bin/env node'
|
|
}
|
|
});
|
|
|
|
// Fix hardcoded __dirname/__filename in bundled output (#1410)
|
|
stripHardcodedDirname(`${hooksDir}/${MCP_SERVER.name}.cjs`);
|
|
|
|
// Make MCP server executable
|
|
fs.chmodSync(`${hooksDir}/${MCP_SERVER.name}.cjs`, 0o755);
|
|
const mcpServerStats = fs.statSync(`${hooksDir}/${MCP_SERVER.name}.cjs`);
|
|
console.log(`✓ mcp-server built (${(mcpServerStats.size / 1024).toFixed(2)} KB)`);
|
|
|
|
// GUARDRAIL (#1645): The MCP server runs under Node, but the entire `bun:`
|
|
// module namespace (bun:sqlite, bun:ffi, bun:test, etc.) is Bun-only. If
|
|
// any transitive import in mcp-server.ts ever pulls one in, the bundle
|
|
// will crash on first require under Node — which is exactly the regression
|
|
// PR #1645 fixed for `bun:sqlite`. Fail the build instead of shipping a
|
|
// broken bundle so future contributors get an immediate signal.
|
|
//
|
|
// Only flag actual `require("bun:...")` / `require('bun:...')` calls, not
|
|
// the bare string — error messages and inline comments may legitimately
|
|
// mention `bun:sqlite` by name without re-introducing the import.
|
|
const mcpBundleContent = fs.readFileSync(`${hooksDir}/${MCP_SERVER.name}.cjs`, 'utf-8');
|
|
const bunRequireRegex = /require\(\s*["']bun:[a-z][a-z0-9_-]*["']\s*\)/;
|
|
const bunRequireMatch = mcpBundleContent.match(bunRequireRegex);
|
|
if (bunRequireMatch) {
|
|
throw new Error(
|
|
`mcp-server.cjs contains a Bun-only ${bunRequireMatch[0]} call. This means a transitive import in src/servers/mcp-server.ts pulled in code from worker-service.ts (or another module that touches DatabaseManager/ChromaSync). The MCP server runs under Node and cannot load bun:* modules. Audit recent imports in src/servers/mcp-server.ts and src/services/worker-spawner.ts — the spawner module is intentionally lightweight and MUST NOT import anything that touches SQLite or other Bun-only modules. See PR #1645 for context.`
|
|
);
|
|
}
|
|
|
|
// SECONDARY GUARDRAIL (#1645 round 11): bundle size budget. The bun:sqlite
|
|
// regex above catches the specific regression class we already know about,
|
|
// but esbuild could in theory change how it emits external module specifiers
|
|
// and silently slip past the regex. A bundle-size budget catches the
|
|
// structural symptom (worker-service.ts dragged into the bundle blew the
|
|
// size from ~358KB to ~1.96MB) regardless of how the imports look.
|
|
//
|
|
// 600KB is a generous ceiling — current size is ~384KB, the broken v12.0.0
|
|
// bundle was ~1920KB, and there's plenty of headroom for legitimate growth
|
|
// before we'd want to revisit this number.
|
|
const MCP_SERVER_MAX_BYTES = 600 * 1024;
|
|
if (mcpServerStats.size > MCP_SERVER_MAX_BYTES) {
|
|
throw new Error(
|
|
`mcp-server.cjs is ${(mcpServerStats.size / 1024).toFixed(2)} KB, exceeding the ${(MCP_SERVER_MAX_BYTES / 1024).toFixed(0)} KB budget. This usually means a transitive import pulled worker-service.ts (or another heavy module) into the MCP bundle. The MCP server is supposed to be a thin HTTP wrapper — audit recent imports in src/servers/mcp-server.ts and src/services/worker-spawner.ts. See PR #1645 for context on why this guardrail exists.`
|
|
);
|
|
}
|
|
|
|
// Build context generator
|
|
console.log(`\n🔧 Building context generator...`);
|
|
await build({
|
|
entryPoints: [CONTEXT_GENERATOR.source],
|
|
bundle: true,
|
|
platform: 'node',
|
|
target: 'node18',
|
|
format: 'cjs',
|
|
outfile: `${hooksDir}/${CONTEXT_GENERATOR.name}.cjs`,
|
|
minify: true,
|
|
logLevel: 'error',
|
|
external: ['bun:sqlite', 'zod'],
|
|
define: {
|
|
'__DEFAULT_PACKAGE_VERSION__': `"${version}"`
|
|
},
|
|
// No banner needed: CJS files under Node.js have __dirname/__filename natively
|
|
});
|
|
|
|
// Fix hardcoded __dirname/__filename in bundled output (#1410)
|
|
stripHardcodedDirname(`${hooksDir}/${CONTEXT_GENERATOR.name}.cjs`);
|
|
|
|
const contextGenStats = fs.statSync(`${hooksDir}/${CONTEXT_GENERATOR.name}.cjs`);
|
|
console.log(`✓ context-generator built (${(contextGenStats.size / 1024).toFixed(2)} KB)`);
|
|
|
|
// Build NPX CLI (pure Node.js — no Bun dependency)
|
|
console.log(`\n🔧 Building NPX CLI...`);
|
|
const npxCliOutDir = 'dist/npx-cli';
|
|
if (!fs.existsSync(npxCliOutDir)) {
|
|
fs.mkdirSync(npxCliOutDir, { recursive: true });
|
|
}
|
|
await build({
|
|
entryPoints: ['src/npx-cli/index.ts'],
|
|
bundle: true,
|
|
platform: 'node',
|
|
target: 'node18',
|
|
format: 'esm',
|
|
outfile: `${npxCliOutDir}/index.js`,
|
|
banner: { js: '#!/usr/bin/env node' },
|
|
minify: true,
|
|
logLevel: 'error',
|
|
external: [
|
|
'fs', 'fs/promises', 'path', 'os', 'child_process', 'url',
|
|
'crypto', 'http', 'https', 'net', 'stream', 'util', 'events',
|
|
'buffer', 'querystring', 'readline', 'tty', 'assert',
|
|
],
|
|
define: {
|
|
'__DEFAULT_PACKAGE_VERSION__': `"${version}"`
|
|
},
|
|
});
|
|
|
|
// Make NPX CLI executable
|
|
fs.chmodSync(`${npxCliOutDir}/index.js`, 0o755);
|
|
const npxCliStats = fs.statSync(`${npxCliOutDir}/index.js`);
|
|
console.log(`✓ npx-cli built (${(npxCliStats.size / 1024).toFixed(2)} KB)`);
|
|
|
|
// Build OpenClaw plugin (self-contained, only Node builtins external)
|
|
if (fs.existsSync('openclaw/src/index.ts')) {
|
|
console.log(`\n🔧 Building OpenClaw plugin...`);
|
|
const openclawOutDir = 'openclaw/dist';
|
|
if (!fs.existsSync(openclawOutDir)) {
|
|
fs.mkdirSync(openclawOutDir, { recursive: true });
|
|
}
|
|
await build({
|
|
entryPoints: ['openclaw/src/index.ts'],
|
|
bundle: true,
|
|
platform: 'node',
|
|
target: 'node18',
|
|
format: 'esm',
|
|
outfile: `${openclawOutDir}/index.js`,
|
|
minify: true,
|
|
logLevel: 'error',
|
|
external: [
|
|
'fs', 'fs/promises', 'path', 'os', 'child_process', 'url',
|
|
'crypto', 'http', 'https', 'net', 'stream', 'util', 'events',
|
|
],
|
|
});
|
|
|
|
const openclawStats = fs.statSync(`${openclawOutDir}/index.js`);
|
|
console.log(`✓ openclaw plugin built (${(openclawStats.size / 1024).toFixed(2)} KB)`);
|
|
}
|
|
|
|
// Build OpenCode plugin (self-contained, Node.js ESM — Bun-compatible)
|
|
if (fs.existsSync('src/integrations/opencode-plugin/index.ts')) {
|
|
console.log(`\n🔧 Building OpenCode plugin...`);
|
|
const opencodeOutDir = 'dist/opencode-plugin';
|
|
if (!fs.existsSync(opencodeOutDir)) {
|
|
fs.mkdirSync(opencodeOutDir, { recursive: true });
|
|
}
|
|
await build({
|
|
entryPoints: ['src/integrations/opencode-plugin/index.ts'],
|
|
bundle: true,
|
|
platform: 'node',
|
|
target: 'node18',
|
|
format: 'esm',
|
|
outfile: `${opencodeOutDir}/index.js`,
|
|
minify: true,
|
|
logLevel: 'error',
|
|
external: [
|
|
'fs', 'fs/promises', 'path', 'os', 'child_process', 'url',
|
|
'crypto', 'http', 'https', 'net', 'stream', 'util', 'events',
|
|
],
|
|
});
|
|
|
|
const opencodeStats = fs.statSync(`${opencodeOutDir}/index.js`);
|
|
console.log(`✓ opencode plugin built (${(opencodeStats.size / 1024).toFixed(2)} KB)`);
|
|
}
|
|
|
|
// Verify critical distribution files exist (skills are source files, not build outputs)
|
|
console.log('\n📋 Verifying distribution files...');
|
|
const requiredDistributionFiles = [
|
|
'plugin/skills/mem-search/SKILL.md',
|
|
'plugin/skills/smart-explore/SKILL.md',
|
|
'plugin/hooks/hooks.json',
|
|
'plugin/.claude-plugin/plugin.json',
|
|
];
|
|
for (const filePath of requiredDistributionFiles) {
|
|
if (!fs.existsSync(filePath)) {
|
|
throw new Error(`Missing required distribution file: ${filePath}`);
|
|
}
|
|
}
|
|
console.log('✓ All required distribution files present');
|
|
|
|
console.log('\n✅ All build targets compiled successfully!');
|
|
console.log(` Output: ${hooksDir}/`);
|
|
console.log(` - Worker: worker-service.cjs`);
|
|
console.log(` - MCP Server: mcp-server.cjs`);
|
|
console.log(` - Context Generator: context-generator.cjs`);
|
|
console.log(` Output: ${npxCliOutDir}/`);
|
|
console.log(` - NPX CLI: index.js`);
|
|
if (fs.existsSync('openclaw/dist/index.js')) {
|
|
console.log(` Output: openclaw/dist/`);
|
|
console.log(` - OpenClaw Plugin: index.js`);
|
|
}
|
|
if (fs.existsSync('dist/opencode-plugin/index.js')) {
|
|
console.log(` Output: dist/opencode-plugin/`);
|
|
console.log(` - OpenCode Plugin: index.js`);
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('\n❌ Build failed:', error.message);
|
|
if (error.errors) {
|
|
console.error('\nBuild errors:');
|
|
error.errors.forEach(err => console.error(` - ${err.text}`));
|
|
}
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
buildHooks();
|