Persistent project knowledge graph for coding agents. MCP server with semantic search, in-process embeddings, and web explorer.
TypeScript agentic-codingaiai-agentsclaude-codecode-contextcoding-agentembeddingsknowledge-graphllmmcpmcp-serveropencodepersistent-memorysqlitetypescript
fix: restore full stats command, bump to v1.3.3
0xk3vin8f6ddb2ed448
diff --git a/package-lock.json b/package-lock.jsonindex 5090359..b3070e1 100644--- a/package-lock.json+++ b/package-lock.json@@ -1,12 +1,12 @@{"name": "megamemory",- "version": "1.3.2",+ "version": "1.3.3","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "megamemory",- "version": "1.3.2",+ "version": "1.3.3","license": "MIT","dependencies": {"@modelcontextprotocol/sdk": "^1.12.1",diff --git a/package.json b/package.jsonindex 5f2a03b..aaf9062 100644--- a/package.json+++ b/package.json@@ -1,6 +1,6 @@{"name": "megamemory",- "version": "1.3.2",+ "version": "1.3.3","description": "MCP server that gives coding agents persistent project understanding via a knowledge graph","type": "module","main": "dist/index.js",diff --git a/src/index.ts b/src/index.tsindex 66f5bb2..821727d 100644--- a/src/index.ts+++ b/src/index.ts@@ -85,7 +85,7 @@ switch (cmd) {case "stats": {const { runStats } = await import("./stats.js");- runStats(process.argv.slice(3));+ await runStats(process.argv.slice(3));process.exit(0);break;}diff --git a/src/stats.ts b/src/stats.tsindex 7ec8b10..99d49bf 100644--- a/src/stats.ts+++ b/src/stats.ts@@ -52,87 +52,119 @@ function sortedKinds(kinds: Record<string, number>): Array<[string, number]> {return [...preferred, ...extra];}-function estimateTokens(jsonText: string): number {- // Rough estimate: ~4 characters per token- return Math.ceil(jsonText.length / 4);-}+export async function runStats(args: string[]): Promise<void> {+ const dbPath = path.resolve(getFlag(args, "--db") ?? getDefaultDbPath());-function getDatabaseFileSize(dbPath: string): number {- try {- return fs.statSync(dbPath).size;- } catch {- return 0;- }-}--export function runStats(args: string[]): void {- let dbPath = getFlag(args, "--db");- if (!dbPath) {- dbPath = getDefaultDbPath();+ if (!fs.existsSync(dbPath)) {+ errorBold(`Database not found: ${dbPath}`);+ process.exit(1);}- if (!fs.existsSync(dbPath)) {- errorBold(`Database not found at ${pc.dim(dbPath)}`);- console.log(pc.dim(" Run megamemory in a project that has been used with the MCP server,"));- console.log(pc.dim(" or use --db to specify the database path."));- return;+ let dbStat: fs.Stats;+ try {+ dbStat = fs.statSync(dbPath);+ } catch (err) {+ errorBold(`Failed to stat database file: ${err instanceof Error ? err.message : String(err)}`);+ process.exit(1);}const db = new KnowledgeDB(dbPath);- const dbStats = db.getStats();- const kindStats = db.getKindsBreakdown();- const fileSize = getDatabaseFileSize(dbPath);-- console.log();- console.log(` ${pc.bold(pc.cyan("megamemory"))} ${pc.dim("database stats")}`);- console.log();-- // Database file- printRow("File size", formatBytes(fileSize), dbPath);- printRow("Nodes", formatNumber(dbStats.nodes));- printRow("Edges", formatNumber(dbStats.edges));- printRow("Removed", formatNumber(dbStats.removed));- console.log();-- // Kind breakdown- if (Object.keys(kindStats).length > 0) {- console.log(` ${pc.cyan("Kind breakdown:")}`);- for (const [kind, count] of sortedKinds(kindStats)) {- const percentage = ((count / dbStats.nodes) * 100).toFixed(1);- printRow(` ${kind}`, formatNumber(count), `${percentage}%`);+ try {+ const stats = db.getStats();+ const rootCount = db.getRootNodes().length;+ const timelineCount = db.getTimelineBounds().count;+ const kinds = db.getKindsBreakdown();+ const nodes = db.getAllActiveNodes();++ const childCount = nodes.filter((node) => node.parent_id !== null).length;++ // Per-node size analysis+ let totalChars = 0;+ let totalBytes = 0;+ let totalTokens = 0;+ let largest: { name: string; tokens: number } | null = null;+ let smallest: { name: string; tokens: number } | null = null;++ for (const node of nodes) {+ const payload = JSON.stringify({+ name: node.name,+ summary: node.summary,+ why: node.why ?? "",+ file_refs: node.file_refs ?? "",+ });+ const chars = payload.length;+ const bytes = Buffer.byteLength(payload, "utf8");+ const tokens = Math.ceil(chars / 4);++ totalChars += chars;+ totalBytes += bytes;+ totalTokens += tokens;++ if (!largest || tokens > largest.tokens) {+ largest = { name: node.name, tokens };+ }+ if (!smallest || tokens < smallest.tokens) {+ smallest = { name: node.name, tokens };+ }}++ const avgTokens = nodes.length > 0 ? Math.ceil(totalTokens / nodes.length) : 0;++ // list_roots payload analysis (matches what MCP server actually sends)+ const listRootsResult = listRoots(db);+ const listRootsPayload = JSON.stringify({ ...listRootsResult, stats: db.getStats() }, null, 2);+ const listRootsChars = listRootsPayload.length;+ const listRootsTokens = Math.ceil(listRootsChars / 4);+ const listRootsNodeCount = listRootsResult.roots.reduce(+ (sum, r) => sum + 1 + r.children.length,+ 0+ );++ // Print output+ console.log(pc.bold(pc.cyan("megamemory stats")));console.log();- }- // list_roots token cost analysis- try {- const rootsOutput = listRoots(db);- const rootsJson = JSON.stringify(rootsOutput, null, 2);- const estimatedTokens = estimateTokens(rootsJson);-- console.log(` ${pc.cyan("list_roots payload:")}`);- printRow("JSON size", formatBytes(rootsJson.length));- printRow("Est. tokens", formatNumber(estimatedTokens));-- // Calculate what the old format would have cost- const oldFormatSize = rootsOutput.roots.reduce((total, root) => {- // Estimate: each child as full object vs just name- const childrenAsObjects = root.children.length * 200; // rough estimate per object- const childrenAsStrings = root.children.reduce((sum, name) => sum + name.length + 2, 0); // +2 for quotes- return total + childrenAsObjects - childrenAsStrings;- }, rootsJson.length);-- const oldTokens = estimateTokens(JSON.stringify({ length: oldFormatSize }));- const savings = Math.round(((oldTokens - estimatedTokens) / oldTokens) * 100);-- if (savings > 0) {- printRow("Token savings", `~${savings}%`, "vs full children objects");+ console.log(` ${pc.bold("Database")}`);+ printRow("Path", dbPath);+ printRow("File size", formatBytes(dbStat.size), `(${formatNumber(dbStat.size)} bytes)`);+ console.log();++ console.log(` ${pc.bold("Graph")}`);+ printRow("Nodes", formatNumber(stats.nodes), `(${formatNumber(rootCount)} root, ${formatNumber(childCount)} children)`);+ printRow("Edges", formatNumber(stats.edges));+ printRow("Removed", formatNumber(stats.removed));+ printRow("Timeline", `${formatNumber(timelineCount)} entries`);+ console.log();++ console.log(` ${pc.bold("list_roots")}`);+ printRow("Nodes", formatNumber(listRootsNodeCount), `(${listRootsResult.roots.length} roots + ${listRootsNodeCount - listRootsResult.roots.length} children)`);+ printRow("Response size", `~${formatNumber(listRootsTokens)} tokens`, `(${formatNumber(listRootsChars)} chars)`);+ console.log();++ console.log(` ${pc.bold("Kinds")}`);+ for (const [kind, count] of sortedKinds(kinds)) {+ printRow(kind, formatNumber(count));}- } catch (error) {- console.log(` ${pc.yellow("Warning:")} Could not analyze list_roots payload`);- console.log(` ${pc.dim(`Error: ${error instanceof Error ? error.message : error}`)}`);- }+ if (Object.keys(kinds).length === 0) {+ printRow("-", "0");+ }+ console.log();- console.log();- db.close();-}\ No newline at end of file+ console.log(` ${pc.bold("Size")}`);+ printRow("Total tokens", `~${formatNumber(totalTokens)}`);+ printRow("Total chars", formatNumber(totalChars), `(${formatNumber(totalBytes)} bytes)`);+ printRow("Avg node tokens", `~${formatNumber(avgTokens)}`);+ if (largest) {+ printRow("Largest", `${largest.name} (${formatNumber(largest.tokens)} tokens)`);+ } else {+ printRow("Largest", "N/A");+ }+ if (smallest) {+ printRow("Smallest", `${smallest.name} (${formatNumber(smallest.tokens)} tokens)`);+ } else {+ printRow("Smallest", "N/A");+ }+ } finally {+ db.close();+ }+}