konoha-mcp 2.1.4 → 2.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/.agents/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/abusing-shadow-credentials-for-privesc/SKILL.md +97 -135
  2. package/.agents/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/abusing-shadow-credentials-for-privesc/references/api-reference.md +30 -47
  3. package/.agents/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/abusing-shadow-credentials-for-privesc/references/standards.md +7 -6
  4. package/.agents/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/abusing-shadow-credentials-for-privesc/scripts/agent.py +65 -57
  5. package/.agents/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/exploiting-active-directory-certificate-services-esc1/scripts/agent.py +36 -17
  6. package/.agents/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/exploiting-active-directory-certificate-services-esc1/scripts/process.py +25 -13
  7. package/.agents/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/exploiting-adcs-with-certipy/scripts/agent.py +35 -19
  8. package/.agents/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/exploiting-constrained-delegation-abuse/scripts/agent.py +30 -8
  9. package/.agents/skills.fingerprint +1 -1
  10. package/README.md +4 -4
  11. package/apps/web/build/adapter-node.js +1 -1
  12. package/apps/web/build/client/_app/immutable/entry/{app.DwlgEXmV.js → app.DLayTcYu.js} +2 -2
  13. package/apps/web/build/client/_app/immutable/entry/app.DLayTcYu.js.br +0 -0
  14. package/apps/web/build/client/_app/immutable/entry/app.DLayTcYu.js.gz +0 -0
  15. package/apps/web/build/client/_app/immutable/nodes/{9.CVBlvZqX.js → 9.CGMmeMVC.js} +1 -1
  16. package/apps/web/build/client/_app/immutable/nodes/9.CGMmeMVC.js.br +0 -0
  17. package/apps/web/build/client/_app/immutable/nodes/9.CGMmeMVC.js.gz +0 -0
  18. package/apps/web/build/client/_app/version.json +1 -1
  19. package/apps/web/build/client/_app/version.json.br +0 -0
  20. package/apps/web/build/client/_app/version.json.gz +0 -0
  21. package/apps/web/build/server/.vite/manifest.json +15 -15
  22. package/apps/web/build/server/adapter-index.js +1 -1
  23. package/apps/web/build/server/adapter-index.js.map +1 -1
  24. package/apps/web/build/server/adapter-node-env.js +1 -1
  25. package/apps/web/build/server/adapter-node-env.js.map +1 -1
  26. package/apps/web/build/server/adapter-node-handler.js +4 -4
  27. package/apps/web/build/server/adapter-node-handler.js.map +1 -1
  28. package/apps/web/build/server/chunks/exports.js +9 -9
  29. package/apps/web/build/server/chunks/exports.js.map +1 -1
  30. package/apps/web/build/server/chunks/functions.js +1 -1
  31. package/apps/web/build/server/chunks/functions.js.map +1 -1
  32. package/apps/web/build/server/chunks/instance.js +33 -33
  33. package/apps/web/build/server/chunks/instance.js.map +1 -1
  34. package/apps/web/build/server/chunks/internal.js +19 -19
  35. package/apps/web/build/server/chunks/internal.js.map +1 -1
  36. package/apps/web/build/server/chunks/server-errors.js +2 -2
  37. package/apps/web/build/server/chunks/server-errors.js.map +1 -1
  38. package/apps/web/build/server/chunks/server.js.map +1 -1
  39. package/apps/web/build/server/chunks/shared.js +1 -1
  40. package/apps/web/build/server/chunks/shared.js.map +1 -1
  41. package/apps/web/build/server/chunks/state.js +1 -1
  42. package/apps/web/build/server/chunks/state.js.map +1 -1
  43. package/apps/web/build/server/chunks/sweetAlert.svelte.js.map +1 -1
  44. package/apps/web/build/server/chunks/uiState.svelte.js.map +1 -1
  45. package/apps/web/build/server/entries/fallbacks/error.svelte.js +1 -1
  46. package/apps/web/build/server/entries/fallbacks/error.svelte.js.map +1 -1
  47. package/apps/web/build/server/entries/pages/_layout.svelte.js.map +1 -1
  48. package/apps/web/build/server/entries/pages/bridges/_page.svelte.js.map +1 -1
  49. package/apps/web/build/server/entries/pages/clients/_page.svelte.js.map +1 -1
  50. package/apps/web/build/server/entries/pages/detector/_page.svelte.js.map +1 -1
  51. package/apps/web/build/server/entries/pages/docs/_page.svelte.js +1 -1
  52. package/apps/web/build/server/entries/pages/docs/_page.svelte.js.map +1 -1
  53. package/apps/web/build/server/entries/pages/persona/_page.svelte.js.map +1 -1
  54. package/apps/web/build/server/entries/pages/search/_page.svelte.js.map +1 -1
  55. package/apps/web/build/server/entries/pages/semble/_page.svelte.js.map +1 -1
  56. package/apps/web/build/server/entries/pages/skills/_page.svelte.js.map +1 -1
  57. package/apps/web/build/server/entries/pages/tasks/_page.svelte.js.map +1 -1
  58. package/apps/web/build/server/index.js +1 -1
  59. package/apps/web/build/server/index.js.map +1 -1
  60. package/apps/web/build/server/manifest-full.js +1 -1
  61. package/apps/web/build/server/manifest.js +1 -1
  62. package/apps/web/build/server/nodes/9.js +1 -1
  63. package/apps/web/build/server/remote-entry.js +7 -7
  64. package/apps/web/build/server/remote-entry.js.map +1 -1
  65. package/apps/web/build/server/server.js +1 -1
  66. package/apps/web/package.json +2 -2
  67. package/bin/cli.js +6 -6
  68. package/bin/lib/ui_commands.js +4 -3
  69. package/docs/SecurityCompliance/qa_report_2.1.5_2026-10-07.md +65 -0
  70. package/docs/SecurityCompliance/security_compliance_report_google_policy_2.1.5_2026-10-07.md +74 -0
  71. package/docs/SecurityCompliance/security_compliance_report_google_policy_2.1.6_2026-10-07.md +78 -0
  72. package/docs/SecurityCompliance/socket_baseline_2026-10-07.md +60 -0
  73. package/docs/SecurityCompliance/socket_burndown_2.1.5_2026-10-07.md +73 -0
  74. package/docs/SecurityCompliance/socket_report_2.1.5_2026-10-07.md +54 -0
  75. package/docs/architecture/TOKEN-BASELINE.md +1 -1
  76. package/package.json +3 -17
  77. package/src/antigravity_manager.js +81 -15
  78. package/src/db.js +2 -2
  79. package/src/db_agents.js +1 -1
  80. package/src/db_bridges.js +1 -1
  81. package/src/db_savings.js +1 -1
  82. package/src/db_stats.js +1 -1
  83. package/src/deploy_utils.js +4 -5
  84. package/src/doctor.js +10 -0
  85. package/src/migrate.js +1 -1
  86. package/src/persona_memory.js +1 -1
  87. package/src/sqlite_driver.js +211 -0
  88. package/src/templates/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/abusing-shadow-credentials-for-privesc/SKILL.md +97 -135
  89. package/src/templates/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/abusing-shadow-credentials-for-privesc/references/api-reference.md +30 -47
  90. package/src/templates/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/abusing-shadow-credentials-for-privesc/references/standards.md +7 -6
  91. package/src/templates/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/abusing-shadow-credentials-for-privesc/scripts/agent.py +65 -57
  92. package/src/templates/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/exploiting-active-directory-certificate-services-esc1/scripts/agent.py +36 -17
  93. package/src/templates/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/exploiting-active-directory-certificate-services-esc1/scripts/process.py +25 -13
  94. package/src/templates/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/exploiting-adcs-with-certipy/scripts/agent.py +35 -19
  95. package/src/templates/skills/anbu-skill/references/anthropic-cybersecurity-skills-assets/skills/exploiting-constrained-delegation-abuse/scripts/agent.py +30 -8
  96. package/src/vector_search.js +1 -1
  97. package/apps/web/build/client/_app/immutable/entry/app.DwlgEXmV.js.br +0 -0
  98. package/apps/web/build/client/_app/immutable/entry/app.DwlgEXmV.js.gz +0 -0
  99. package/apps/web/build/client/_app/immutable/nodes/9.CVBlvZqX.js.br +0 -0
  100. package/apps/web/build/client/_app/immutable/nodes/9.CVBlvZqX.js.gz +0 -0
@@ -446,6 +446,59 @@ function ensureAntigravityMcpSchemas() {
446
446
  }
447
447
  } catch { /* intentional best-effort fallback: failure here must never crash the CLI/MCP runtime */ }
448
448
  }
449
+
450
+ // Ensure token-optimized auxiliary MCP schemas for semble and aislop (prevent token drift)
451
+ try {
452
+ const sembleDir = path.join(HOME, '.gemini', 'antigravity-cli', 'mcp', 'semble');
453
+ const searchJson = path.join(sembleDir, 'search.json');
454
+ const findRelatedJson = path.join(sembleDir, 'find_related.json');
455
+ if (fs.existsSync(searchJson) && fs.existsSync(findRelatedJson)) {
456
+ const s1 = JSON.parse(fs.readFileSync(searchJson, 'utf8'));
457
+ const s2 = JSON.parse(fs.readFileSync(findRelatedJson, 'utf8'));
458
+ if (s1.description !== 'Search codebase with focused query. Returns file paths and line numbers. Pass local path or git URL as repo.') {
459
+ s1.description = 'Search codebase with focused query. Returns file paths and line numbers. Pass local path or git URL as repo.';
460
+ if (s1.parameters?.properties?.max_snippet_lines) s1.parameters.properties.max_snippet_lines.description = 'Lines of source to include per result (default: 10, 0: location only).';
461
+ if (s1.parameters?.properties?.repo) s1.parameters.properties.repo.description = 'Local directory path or git URL to index and search.';
462
+ fs.writeFileSync(searchJson, JSON.stringify(s1, null, 2) + '\n', 'utf8');
463
+ }
464
+ if (s2.description !== 'Find code similar to a known location. Pass file_path and line from prior search result.') {
465
+ s2.description = 'Find code similar to a known location. Pass file_path and line from prior search result.';
466
+ if (s2.parameters?.properties?.max_snippet_lines) s2.parameters.properties.max_snippet_lines.description = 'Lines of source per result (default: 10, 0: location only).';
467
+ if (s2.parameters?.properties?.repo) s2.parameters.properties.repo.description = 'Local directory path or git URL to index and search.';
468
+ fs.writeFileSync(findRelatedJson, JSON.stringify(s2, null, 2) + '\n', 'utf8');
469
+ }
470
+ }
471
+
472
+ const aislopDir = path.join(HOME, '.gemini', 'antigravity-cli', 'mcp', 'aislop');
473
+ const scanJson = path.join(aislopDir, 'aislop_scan.json');
474
+ const fixJson = path.join(aislopDir, 'aislop_fix.json');
475
+ const baselineJson = path.join(aislopDir, 'aislop_baseline.json');
476
+ const whyJson = path.join(aislopDir, 'aislop_why.json');
477
+ if (fs.existsSync(scanJson) && fs.existsSync(fixJson) && fs.existsSync(baselineJson) && fs.existsSync(whyJson)) {
478
+ const s = JSON.parse(fs.readFileSync(scanJson, 'utf8'));
479
+ const f = JSON.parse(fs.readFileSync(fixJson, 'utf8'));
480
+ const b = JSON.parse(fs.readFileSync(baselineJson, 'utf8'));
481
+ const w = JSON.parse(fs.readFileSync(whyJson, 'utf8'));
482
+
483
+ b.description = 'Read project baseline score. Returns score, lastScanAt, fileCount, or null.';
484
+ if (b.parameters?.properties?.path) b.parameters.properties.path.description = 'Project directory path.';
485
+
486
+ f.description = 'Apply mechanical fixes (formatting, unused/duplicate imports). Returns counts before/after.';
487
+ if (f.parameters?.properties?.path) f.parameters.properties.path.description = 'Project directory path.';
488
+ if (f.parameters?.properties?.force) f.parameters.properties.force.description = 'Run aggressive fixes.';
489
+
490
+ s.description = 'Scan project with aislop engines. Returns 0-100 score and top findings.';
491
+ if (s.parameters?.properties?.path) s.parameters.properties.path.description = 'Project directory path.';
492
+
493
+ w.description = 'Explain an aislop rule, rationale, severity, and auto-fixability.';
494
+ if (w.parameters?.properties?.rule_id) w.parameters.properties.rule_id.description = 'Full rule ID.';
495
+
496
+ fs.writeFileSync(scanJson, JSON.stringify(s, null, 2) + '\n', 'utf8');
497
+ fs.writeFileSync(fixJson, JSON.stringify(f, null, 2) + '\n', 'utf8');
498
+ fs.writeFileSync(baselineJson, JSON.stringify(b, null, 2) + '\n', 'utf8');
499
+ fs.writeFileSync(whyJson, JSON.stringify(w, null, 2) + '\n', 'utf8');
500
+ }
501
+ } catch { /* intentional best-effort fallback: failure here must never crash the CLI/MCP runtime */ }
449
502
  }
450
503
 
451
504
  // isRtkInstalled is imported from platform_utils
@@ -807,24 +860,37 @@ function removeAntigravityConfig(silent = true, options = {}) {
807
860
  function ensureAntigravitySetup(options = {}) {
808
861
  const { silent = true } = options;
809
862
  const home = options.home || process.env.HOME || os.homedir();
810
- const mcpConfigPath = options.configPath || path.join(home, '.gemini', 'config', 'mcp_config.json');
811
- const dir = path.dirname(mcpConfigPath);
812
- if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
813
-
814
- let config = { mcpServers: {} };
815
- if (fs.existsSync(mcpConfigPath)) {
816
- try {
817
- config = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf8'));
818
- if (!config.mcpServers) config.mcpServers = {};
819
- } catch {
820
- config = { mcpServers: {} };
821
- }
822
- }
863
+ const targetPaths = [
864
+ options.configPath || path.join(home, '.gemini', 'config', 'mcp_config.json'),
865
+ path.join(home, '.gemini', 'antigravity-ide', 'mcp_config.json'),
866
+ path.join(home, '.gemini', 'antigravity', 'mcp_config.json')
867
+ ];
823
868
 
824
869
  const { buildStdioMcpServers } = require('./mcp_clients_manager');
825
870
  const servers = buildStdioMcpServers({ client: 'antigravity' });
826
- Object.assign(config.mcpServers, servers);
827
- fs.writeFileSync(mcpConfigPath, JSON.stringify(config, null, 2) + '\n');
871
+
872
+ for (const mcpConfigPath of targetPaths) {
873
+ const dir = path.dirname(mcpConfigPath);
874
+ if (!fs.existsSync(dir)) {
875
+ if (mcpConfigPath === targetPaths[0]) {
876
+ fs.mkdirSync(dir, { recursive: true });
877
+ } else {
878
+ continue;
879
+ }
880
+ }
881
+
882
+ let config = { mcpServers: {} };
883
+ if (fs.existsSync(mcpConfigPath)) {
884
+ try {
885
+ config = JSON.parse(fs.readFileSync(mcpConfigPath, 'utf8'));
886
+ if (!config.mcpServers) config.mcpServers = {};
887
+ } catch {
888
+ config = { mcpServers: {} };
889
+ }
890
+ }
891
+ Object.assign(config.mcpServers, servers);
892
+ fs.writeFileSync(mcpConfigPath, JSON.stringify(config, null, 2) + '\n');
893
+ }
828
894
 
829
895
  ensureAntigravityPermissions(silent);
830
896
  ensureAntigravityMcpSchemas();
package/src/db.js CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
- * Canonical database access layer for Konoha (skills.db) using better-sqlite3.
2
+ * Canonical database access layer for Konoha (skills.db) using the built-in node:sqlite driver.
3
3
  * Owns DB_PATH, connection pragmas, vector extension loading, and the unified schema.
4
4
  */
5
5
 
6
6
  const fs = require('fs');
7
7
  const path = require('path');
8
8
  const os = require('os');
9
- const Database = require('better-sqlite3');
9
+ const Database = require('./sqlite_driver');
10
10
 
11
11
  // Single canonical declaration of DB_PATH.
12
12
  // KONOHA_DB_PATH env override (set by the test runner / test suites) isolates
package/src/db_agents.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
3
  * Agent and model storage management for Konoha via SQLite (skills.db).
4
- * Pure Node.js replacement for db_agents.py using better-sqlite3 and yaml_utils.
4
+ * Pure Node.js replacement for db_agents.py using the node:sqlite driver and yaml_utils.
5
5
  */
6
6
 
7
7
  const fs = require('fs');
package/src/db_bridges.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
3
  * Bridge storage management for Konoha via SQLite (skills.db).
4
- * Pure Node.js replacement for db_bridges.py using better-sqlite3.
4
+ * Pure Node.js replacement for db_bridges.py using the node:sqlite driver.
5
5
  */
6
6
 
7
7
  const fs = require('fs');
package/src/db_savings.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
3
  * Helper script and in-process module to query token savings and tool call statistics as JSON.
4
- * Pure Node.js replacement for db_savings.py using better-sqlite3.
4
+ * Pure Node.js replacement for db_savings.py using the node:sqlite driver.
5
5
  */
6
6
 
7
7
  const fs = require('fs');
package/src/db_stats.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
3
  * Helper script and in-process module to return database stats as JSON.
4
- * Replaces db_stats.py with pure Node.js better-sqlite3.
4
+ * Replaces db_stats.py with pure Node.js the node:sqlite driver.
5
5
  */
6
6
 
7
7
 
@@ -66,13 +66,13 @@ function resolveWebUiDir(opts = {}) {
66
66
  try {
67
67
  const parsed = JSON.parse(fs.readFileSync(pkg, "utf8"));
68
68
  if (parsed.name === "konoha-web") {
69
- const hasRoutes = fs.existsSync(path.join(dir, "src", "routes"));
69
+ const hasRoutes = fs.existsSync(path.join(dir, "src", "routes")) || fs.existsSync(path.join(dir, "public", "index.html"));
70
70
  const hasBuildScript = Boolean(parsed.scripts && parsed.scripts.build);
71
71
  hasSources = hasRoutes && hasBuildScript;
72
72
  }
73
73
  } catch (_) { /* intentional best-effort fallback: failure here must never crash the CLI/MCP runtime */ }
74
74
  }
75
- const hasBuild = fs.existsSync(path.join(dir, "build", "handler.js"));
75
+ const hasBuild = fs.existsSync(path.join(dir, "build", "handler.js")) || fs.existsSync(path.join(dir, "public", "index.html"));
76
76
  const qualifies = hasSources || hasBuild;
77
77
  if (qualifies) qualifying.push({ dir, hasSources, hasBuild });
78
78
  } catch (_) { /* intentional best-effort fallback: failure here must never crash the CLI/MCP runtime */ }
@@ -537,7 +537,6 @@ function installFileTools(silent = true, pythonCmd = null) {
537
537
  private: true,
538
538
  dependencies: {
539
539
  "@bufbuild/protobuf": "^2.11.0",
540
- "better-sqlite3": "^13.0.3",
541
540
  "@huggingface/transformers": "^4.2.0"
542
541
  },
543
542
  },
@@ -546,7 +545,7 @@ function installFileTools(silent = true, pythonCmd = null) {
546
545
  ) + "\n",
547
546
  );
548
547
  }
549
- if (!fileExists(nodeModulesPath) || !fileExists(path.join(nodeModulesPath, 'better-sqlite3'))) {
548
+ if (!fileExists(nodeModulesPath) || !fileExists(path.join(nodeModulesPath, '@bufbuild', 'protobuf'))) {
550
549
  const isWin = process.platform === "win32";
551
550
  // .cmd/.bat shims require shell:true since Node >= 18.20.2 (CVE-2024-27980);
552
551
  // fall back to npm when pnpm is unavailable
@@ -810,7 +809,7 @@ function cleanKonohaRuntimeDir(options = {}) {
810
809
  const activeDbPath = path.join(target, 'konoha.db');
811
810
  if (fs.existsSync(activeDbPath)) {
812
811
  try {
813
- const Database = require('better-sqlite3');
812
+ const Database = require('./sqlite_driver');
814
813
  const db = new Database(activeDbPath, { timeout: 5000 });
815
814
  try {
816
815
  db.pragma('wal_checkpoint(TRUNCATE)');
package/src/doctor.js CHANGED
@@ -96,12 +96,22 @@ function getDiagnostics(autoRepair = false) {
96
96
  const servers = cfg.mcpServers || {};
97
97
  if (servers.konoha && servers.semble && servers.aislop) {
98
98
  record('MCP Config (mcp_config.json)', 'HEALTHY', 'konoha, semble, and aislop are active');
99
+ } else if (autoRepair) {
100
+ require('./antigravity_manager').ensureAntigravitySetup({ silent: true });
101
+ record('MCP Config (mcp_config.json)', 'REPAIRED', 'Registered konoha, semble, and aislop in config');
99
102
  } else {
100
103
  record('MCP Config (mcp_config.json)', 'WARNING', 'Some MCP servers not registered');
101
104
  }
102
105
  } catch {
103
106
  record('MCP Config (mcp_config.json)', 'FAILED', 'Invalid JSON config');
104
107
  }
108
+ } else if (autoRepair) {
109
+ try {
110
+ require('./antigravity_manager').ensureAntigravitySetup({ silent: true });
111
+ record('MCP Config (mcp_config.json)', 'REPAIRED', 'Registered konoha, semble, and aislop in config');
112
+ } catch {
113
+ record('MCP Config (mcp_config.json)', 'FAILED', 'Missing mcp_config.json');
114
+ }
105
115
  } else {
106
116
  record('MCP Config (mcp_config.json)', 'FAILED', 'Missing mcp_config.json');
107
117
  }
package/src/migrate.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
3
  * Skills Migration Engine (v1.1.0 — Enhanced Token Optimization)
4
- * Pure Node.js replacement for migrate.py using better-sqlite3 and yaml_utils.
4
+ * Pure Node.js replacement for migrate.py using the node:sqlite driver and yaml_utils.
5
5
  * Migrates skill content from ~/.agents/skills/ into SQLite FTS5 database.
6
6
  */
7
7
 
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Persistent Persona Memory & Project Context Engine for Konoha.
3
- * Pure Node.js replacement for persona_memory.py using better-sqlite3 and crypto.
3
+ * Pure Node.js replacement for persona_memory.py using the node:sqlite driver and crypto.
4
4
  */
5
5
 
6
6
  const fs = require('fs');
@@ -0,0 +1,211 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Synchronous SQLite driver for Konoha built on Node's bundled `node:sqlite`
5
+ * (Node >= 22.16 ships SQLite with FTS5 enabled). Exposes the small
6
+ * better-sqlite3-style surface the codebase relies on — prepare, exec,
7
+ * pragma, transaction, loadExtension, close — so the runtime has zero native
8
+ * dependencies and nothing to compile at install time.
9
+ */
10
+
11
+ const fs = require('fs');
12
+
13
+ const SQLITE_EXPERIMENTAL_WARNING = 'SQLite is an experimental feature';
14
+ const NAMED_PARAMETER = /[:@$]([A-Za-z_][A-Za-z0-9_]*)/g;
15
+ const DEFAULT_BUSY_TIMEOUT_MS = 5000;
16
+
17
+ // node:sqlite prints an ExperimentalWarning to stderr when first loaded on
18
+ // Node 22/24. Filter only that warning, only while the module loads, so MCP
19
+ // stdio sessions and CLI output stay clean.
20
+ function loadNodeSqlite() {
21
+ const originalEmitWarning = process.emitWarning;
22
+ process.emitWarning = function emitWarningWithoutSqliteNotice(warning, ...rest) {
23
+ const text = typeof warning === 'string' ? warning : String((warning && warning.message) || '');
24
+ if (text.includes(SQLITE_EXPERIMENTAL_WARNING)) return undefined;
25
+ return originalEmitWarning.call(process, warning, ...rest);
26
+ };
27
+ try {
28
+ return require('node:sqlite');
29
+ } catch (err) {
30
+ throw new Error(`Konoha requires Node.js >= 22.16 with the built-in node:sqlite module (${err.message})`);
31
+ } finally {
32
+ process.emitWarning = originalEmitWarning;
33
+ }
34
+ }
35
+
36
+ const { DatabaseSync } = loadNodeSqlite();
37
+
38
+ function isNamedParameterObject(value) {
39
+ return value !== null
40
+ && typeof value === 'object'
41
+ && !ArrayBuffer.isView(value)
42
+ && !(value instanceof ArrayBuffer);
43
+ }
44
+
45
+ // better-sqlite3 binds `undefined` and booleans leniently; node:sqlite rejects them.
46
+ function toBindable(value) {
47
+ if (value === undefined) return null;
48
+ if (typeof value === 'boolean') return value ? 1 : 0;
49
+ return value;
50
+ }
51
+
52
+ // node:sqlite returns null-prototype rows and Uint8Array blobs; callers expect
53
+ // plain objects and Buffers (as better-sqlite3 returned).
54
+ function normalizeRow(row) {
55
+ if (row === undefined || row === null) return row;
56
+ const plain = {};
57
+ for (const key of Object.keys(row)) {
58
+ const value = row[key];
59
+ plain[key] = value instanceof Uint8Array && !Buffer.isBuffer(value)
60
+ ? Buffer.from(value.buffer, value.byteOffset, value.byteLength)
61
+ : value;
62
+ }
63
+ return plain;
64
+ }
65
+
66
+ class Statement {
67
+ constructor(statement) {
68
+ this._statement = statement;
69
+ this._parameterNames = null;
70
+ this.source = statement.sourceSQL;
71
+ }
72
+
73
+ _namesInSql() {
74
+ if (!this._parameterNames) {
75
+ this._parameterNames = new Set();
76
+ for (const match of this.source.matchAll(NAMED_PARAMETER)) {
77
+ this._parameterNames.add(match[1]);
78
+ }
79
+ }
80
+ return this._parameterNames;
81
+ }
82
+
83
+ // node:sqlite throws on object keys that are not parameters of the
84
+ // statement; better-sqlite3 ignored them. Keep only keys the SQL references.
85
+ _pickNamed(params) {
86
+ const names = this._namesInSql();
87
+ const picked = {};
88
+ for (const key of Object.keys(params)) {
89
+ if (names.has(key)) picked[key] = toBindable(params[key]);
90
+ }
91
+ return picked;
92
+ }
93
+
94
+ _bind(args) {
95
+ const positional = [];
96
+ let named = null;
97
+ for (const arg of args.flat()) {
98
+ if (isNamedParameterObject(arg)) {
99
+ named = this._pickNamed(arg);
100
+ } else {
101
+ positional.push(toBindable(arg));
102
+ }
103
+ }
104
+ return named ? [named, ...positional] : positional;
105
+ }
106
+
107
+ run(...args) {
108
+ return this._statement.run(...this._bind(args));
109
+ }
110
+
111
+ get(...args) {
112
+ return normalizeRow(this._statement.get(...this._bind(args)));
113
+ }
114
+
115
+ all(...args) {
116
+ return this._statement.all(...this._bind(args)).map(normalizeRow);
117
+ }
118
+
119
+ *iterate(...args) {
120
+ for (const row of this._statement.iterate(...this._bind(args))) {
121
+ yield normalizeRow(row);
122
+ }
123
+ }
124
+ }
125
+
126
+ class Database {
127
+ constructor(filename = ':memory:', options = {}) {
128
+ const readOnly = Boolean(options.readonly || options.readOnly);
129
+ if (options.fileMustExist && filename !== ':memory:' && !fs.existsSync(filename)) {
130
+ throw new Error(`unable to open database file: ${filename}`);
131
+ }
132
+ this._db = new DatabaseSync(filename, { readOnly, allowExtension: true });
133
+ this._savepointDepth = 0;
134
+ this.name = filename;
135
+ this.readonly = readOnly;
136
+ this.memory = filename === ':memory:';
137
+ const timeout = Number.isInteger(options.timeout) ? options.timeout : DEFAULT_BUSY_TIMEOUT_MS;
138
+ this._db.exec(`PRAGMA busy_timeout = ${timeout}`);
139
+ }
140
+
141
+ get open() {
142
+ return this._db.isOpen;
143
+ }
144
+
145
+ get inTransaction() {
146
+ return this._db.isTransaction;
147
+ }
148
+
149
+ prepare(sql) {
150
+ return new Statement(this._db.prepare(sql));
151
+ }
152
+
153
+ exec(sql) {
154
+ this._db.exec(sql);
155
+ return this;
156
+ }
157
+
158
+ pragma(source, options = {}) {
159
+ const rows = this.prepare(`PRAGMA ${source}`).all();
160
+ if (!options.simple) return rows;
161
+ return rows.length > 0 ? Object.values(rows[0])[0] : undefined;
162
+ }
163
+
164
+ transaction(fn) {
165
+ const database = this;
166
+ return function runTransaction(...args) {
167
+ return database._runInTransaction(fn, this, args);
168
+ };
169
+ }
170
+
171
+ _runInTransaction(fn, thisArg, args) {
172
+ const nested = this._db.isTransaction;
173
+ const savepoint = `konoha_sp_${this._savepointDepth}`;
174
+ this._db.exec(nested ? `SAVEPOINT ${savepoint}` : 'BEGIN');
175
+ this._savepointDepth += 1;
176
+ try {
177
+ const result = fn.apply(thisArg, args);
178
+ this._db.exec(nested ? `RELEASE ${savepoint}` : 'COMMIT');
179
+ return result;
180
+ } catch (err) {
181
+ if (this._db.isTransaction) {
182
+ this._db.exec(nested ? `ROLLBACK TO ${savepoint}; RELEASE ${savepoint}` : 'ROLLBACK');
183
+ }
184
+ throw err;
185
+ } finally {
186
+ this._savepointDepth -= 1;
187
+ }
188
+ }
189
+
190
+ loadExtension(file) {
191
+ if (typeof file !== 'string' || file.length === 0) {
192
+ throw new TypeError('Extension path must be a non-empty string');
193
+ }
194
+ this._db.enableLoadExtension(true);
195
+ try {
196
+ this._db.loadExtension(file);
197
+ } finally {
198
+ this._db.enableLoadExtension(false);
199
+ }
200
+ return this;
201
+ }
202
+
203
+ close() {
204
+ if (this._db.isOpen) this._db.close();
205
+ return this;
206
+ }
207
+ }
208
+
209
+ module.exports = Database;
210
+ module.exports.Database = Database;
211
+ module.exports.normalizeRow = normalizeRow;