ctxtop 0.0.0-stage → 4.1.0
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.
- package/.antigravity-plugin/plugin.json +36 -0
- package/.claude-plugin/plugin.json +18 -0
- package/CHECKSUMS.sha256 +166 -0
- package/LICENSE +27 -0
- package/README.md +350 -2
- package/SECURITY.md +22 -0
- package/agents/ah-flash-scout.md +25 -0
- package/assets/advanced_view_loaded_420px.png +0 -0
- package/assets/hero-demo.svg +188 -0
- package/assets/logo.svg +19 -0
- package/assets/simple_view_lean_420px.png +0 -0
- package/assets/simple_view_loaded_420px.png +0 -0
- package/assets/social-preview.svg +72 -0
- package/assets/zone_c_step3_shield_full.png +0 -0
- package/bin/ah-statusline.mjs +175 -0
- package/bin/cli.mjs +3 -0
- package/bin/ctxtop.mjs +1799 -0
- package/commands/cost-radar.md +11 -0
- package/commands/evolve.md +10 -0
- package/commands/handoff.md +10 -0
- package/commands/horizon.md +10 -0
- package/commands/inspect.md +10 -0
- package/commands/precheck.md +10 -0
- package/config/default_flightcheck.json +133 -0
- package/config/modelCatalog.json +785 -0
- package/config/platformQuirks.json +19 -0
- package/hooks/handlers/compact-lifecycle.mjs +577 -0
- package/hooks/handlers/config-change.mjs +402 -0
- package/hooks/handlers/hookIo.mjs +311 -0
- package/hooks/handlers/hookIpcClient.mjs +83 -0
- package/hooks/handlers/post-model-switch.mjs +111 -0
- package/hooks/handlers/post-tool-use.mjs +366 -0
- package/hooks/handlers/pre-model-switch.mjs +1048 -0
- package/hooks/handlers/pre-tool-use.mjs +632 -0
- package/hooks/handlers/stop-gate.mjs +526 -0
- package/hooks/handlers/user-prompt-submit.mjs +367 -0
- package/hooks/hooks.json +131 -0
- package/hooks/post_invocation.mjs +13 -0
- package/hooks/post_tool_use.mjs +25 -0
- package/hooks/pre_invocation.mjs +14 -0
- package/hooks/pre_tool_use.mjs +97 -0
- package/hooks/register.js +726 -0
- package/hooks/session_start.mjs +24 -0
- package/hooks/stop.mjs +23 -0
- package/hooks.json +56 -0
- package/package.json +110 -4
- package/packages/antigravity/package.json +14 -0
- package/packages/antigravity/src/index.mjs +1 -0
- package/packages/claude-code/package.json +14 -0
- package/packages/claude-code/src/index.mjs +2 -0
- package/packages/core/package.json +14 -0
- package/packages/core/src/index.mjs +1 -0
- package/packages/engine-node/package.json +14 -0
- package/packages/engine-node/src/index.mjs +1 -0
- package/packages/mcp/package.json +17 -0
- package/packages/mcp/src/server.mjs +396 -0
- package/packages/pro/LICENSE-COMMERCIAL.md +19 -0
- package/packages/pro/README-SIGNING.md +30 -0
- package/packages/pro/package.json +16 -0
- package/packages/pro/src/index.mjs +3 -0
- package/packages/pro/src/team_finops_pack.mjs +241 -0
- package/packages/pro/workers/polar_webhook_worker.mjs +215 -0
- package/plugin.json +36 -0
- package/rules/AGENTS.md +10 -0
- package/sidecars/horizon-daemon/ipcServer.mjs +24 -0
- package/sidecars/horizon-daemon/server.mjs +2038 -0
- package/sidecars/horizon-daemon/sidecar.json +19 -0
- package/skills/cost-radar/SKILL.md +21 -0
- package/skills/evolve/SKILL.md +21 -0
- package/skills/handoff/SKILL.md +50 -0
- package/skills/horizon/SKILL.md +17 -0
- package/skills/inspect/SKILL.md +18 -0
- package/skills/precheck/SKILL.md +44 -0
- package/src/adapters/antigravity/index.mjs +109 -0
- package/src/adapters/claude-code/index.mjs +194 -0
- package/src/cli/cost_radar.mjs +70 -0
- package/src/cli/doctor.mjs +351 -0
- package/src/cli/horizon_hud.mjs +888 -0
- package/src/cli/init.mjs +425 -0
- package/src/cli/postmortem.mjs +515 -0
- package/src/coach/cacheGravityRouter.mjs +12 -0
- package/src/config/modelCatalog.json +704 -0
- package/src/config/platformQuirks.json +19 -0
- package/src/core/action_queue.mjs +264 -0
- package/src/core/ag_bridge.mjs +346 -0
- package/src/core/atomic_io.mjs +124 -0
- package/src/core/baseline.mjs +607 -0
- package/src/core/blindspot_sensors.mjs +399 -0
- package/src/core/bucket_b_runtime.mjs +1200 -0
- package/src/core/classifier.mjs +1757 -0
- package/src/core/coach/circuit_breakers.mjs +650 -0
- package/src/core/coach/contradiction_auditor.mjs +239 -0
- package/src/core/coach/effort_advisor.mjs +143 -0
- package/src/core/coach/handoff_hydrator.mjs +483 -0
- package/src/core/coach/progressive_checkpointer.mjs +551 -0
- package/src/core/coach/prompt_decomposer_coach.mjs +141 -0
- package/src/core/coach/reflector_queue.mjs +505 -0
- package/src/core/coach/saved_quota_ledger.mjs +615 -0
- package/src/core/coach/subagent_ipi_sanitizer.mjs +200 -0
- package/src/core/coach/subagent_phantom_auditor.mjs +261 -0
- package/src/core/coach/task_quota_router.mjs +955 -0
- package/src/core/coach/turn_zero_gate.mjs +574 -0
- package/src/core/compaction_coach.mjs +1126 -0
- package/src/core/config.mjs +214 -0
- package/src/core/constants.mjs +1987 -0
- package/src/core/detectors_det01_to_16.mjs +1523 -0
- package/src/core/doc_indexer.mjs +338 -0
- package/src/core/episodic_memory.mjs +135 -0
- package/src/core/fts5_output_compressor.mjs +359 -0
- package/src/core/git_ledger.mjs +189 -0
- package/src/core/handoff_audit.mjs +558 -0
- package/src/core/history.mjs +221 -0
- package/src/core/hook_diagnostics.mjs +158 -0
- package/src/core/horizon_classifier.mjs +326 -0
- package/src/core/horizon_config.mjs +802 -0
- package/src/core/host_watchdog.mjs +200 -0
- package/src/core/layer5_hygiene.mjs +1084 -0
- package/src/core/log_resolver.mjs +231 -0
- package/src/core/mcp_auditor.mjs +138 -0
- package/src/core/micro_skills_store.mjs +91 -0
- package/src/core/offenders.mjs +161 -0
- package/src/core/parser.mjs +1997 -0
- package/src/core/path_canonicalizer.mjs +73 -0
- package/src/core/paths.mjs +311 -0
- package/src/core/prefix_cache_shield.mjs +386 -0
- package/src/core/pro_license.mjs +385 -0
- package/src/core/prompt_rubric.mjs +518 -0
- package/src/core/pruner.mjs +433 -0
- package/src/core/quota.mjs +1860 -0
- package/src/core/rsi_instinct_engine.mjs +1197 -0
- package/src/core/rsl_clash_auditor.mjs +423 -0
- package/src/core/rsl_distiller.mjs +372 -0
- package/src/core/rsl_ledger.mjs +969 -0
- package/src/core/rsl_triage.mjs +909 -0
- package/src/core/sanitizer.mjs +121 -0
- package/src/core/session_auditor.mjs +658 -0
- package/src/core/session_forensics.mjs +303 -0
- package/src/core/session_timeline.mjs +358 -0
- package/src/core/settings.mjs +99 -0
- package/src/core/shared/detectors.mjs +1342 -0
- package/src/core/shared/fnv1a64.mjs +85 -0
- package/src/core/shared/handoff_schema.mjs +123 -0
- package/src/core/shared/index.mjs +5 -0
- package/src/core/shared/ipi_sanitizer.mjs +165 -0
- package/src/core/shared/micro_skills.mjs +1 -0
- package/src/core/shared/plugin_validator.mjs +202 -0
- package/src/core/shared/prompt_hygiene_governor.mjs +1 -0
- package/src/core/shield_manager.mjs +1417 -0
- package/src/core/sidecar_store.mjs +302 -0
- package/src/core/sprint_metrics.mjs +366 -0
- package/src/core/sqlite_client.mjs +283 -0
- package/src/core/storage_doctor.mjs +358 -0
- package/src/core/subagents.mjs +609 -0
- package/src/core/swarm_topology.mjs +360 -0
- package/src/core/task_quota_router.mjs +1656 -0
- package/src/core/telemetry/cache_ttl_watchdog.mjs +284 -0
- package/src/core/telemetry/scope_lock_registry.mjs +578 -0
- package/src/core/telemetry/shadow_file_detector.mjs +291 -0
- package/src/core/telemetry/trajectory_watcher.mjs +259 -0
- package/src/core/timeline_svg.mjs +236 -0
- package/src/core/tool_surgeon.mjs +305 -0
- package/src/core/trajectory_stitcher.mjs +645 -0
- package/src/core/u_shaped_optimizer.mjs +789 -0
- package/src/core/wal_hygiene.mjs +233 -0
- package/src/core-isomorphic/catalog/detectorCatalog.mjs +558 -0
- package/src/core-isomorphic/catalog/index.mjs +1 -0
- package/src/core-isomorphic/coach/cacheGravityRouter.mjs +1547 -0
- package/src/core-isomorphic/coach/microSkills.mjs +1 -0
- package/src/core-isomorphic/coach/promptHygieneGovernor.mjs +1 -0
- package/src/core-isomorphic/coach/repoCalibration.mjs +254 -0
- package/src/core-isomorphic/coach/resumeToolsetAuditor.mjs +321 -0
- package/src/core-isomorphic/coach/savedQuotaLedger.mjs +527 -0
- package/src/core-isomorphic/coach/smartCompactAdvisor.mjs +305 -0
- package/src/core-isomorphic/coach/stateBatonHydrator.mjs +810 -0
- package/src/core-isomorphic/coach/ttlWatchdog.mjs +464 -0
- package/src/core-isomorphic/config/anthropicModelCatalog.json +606 -0
- package/src/core-isomorphic/config/catalogLoader.mjs +1268 -0
- package/src/core-isomorphic/config/horizonConfig.mjs +1055 -0
- package/src/core-isomorphic/control/callTree.mjs +338 -0
- package/src/core-isomorphic/control/coachLatches.mjs +126 -0
- package/src/core-isomorphic/control/controlStateMachine.mjs +205 -0
- package/src/core-isomorphic/control/index.mjs +49 -0
- package/src/core-isomorphic/control/swarmWorktree.mjs +109 -0
- package/src/core-isomorphic/control/toolSuspension.mjs +197 -0
- package/src/core-isomorphic/diagnostics/drift_guard.mjs +382 -0
- package/src/core-isomorphic/diagnostics/index.mjs +9 -0
- package/src/core-isomorphic/diagnostics/micro_skills.mjs +471 -0
- package/src/core-isomorphic/diagnostics/prompt_hygiene_governor.mjs +888 -0
- package/src/core-isomorphic/economics/billingBlocks.mjs +251 -0
- package/src/core-isomorphic/economics/cacheGravity.mjs +369 -0
- package/src/core-isomorphic/economics/cliffBreaker.mjs +256 -0
- package/src/core-isomorphic/economics/driftLatency.mjs +433 -0
- package/src/core-isomorphic/economics/dualTtl.mjs +198 -0
- package/src/core-isomorphic/economics/index.mjs +47 -0
- package/src/core-isomorphic/economics/retentionRadar.mjs +348 -0
- package/src/core-isomorphic/guards/bucketBAlphaBeta.mjs +860 -0
- package/src/core-isomorphic/guards/bucketBGamma.mjs +442 -0
- package/src/core-isomorphic/guards/bucketBHeuristics.mjs +37 -0
- package/src/core-isomorphic/guards/circuitBreakers.mjs +1202 -0
- package/src/core-isomorphic/guards/det01_03_shell_secret.mjs +1184 -0
- package/src/core-isomorphic/guards/det04_12_thrash_rot.mjs +572 -0
- package/src/core-isomorphic/guards/det13_20_mcp_scope.mjs +913 -0
- package/src/core-isomorphic/guards/docIndexCore.mjs +446 -0
- package/src/core-isomorphic/guards/losslessPostSanitizer.mjs +317 -0
- package/src/core-isomorphic/guards/slopsquatGate.mjs +551 -0
- package/src/core-isomorphic/index.js +27 -0
- package/src/core-isomorphic/telemetry/otelExporter.mjs +187 -0
- package/src/core-isomorphic/telemetry/scopeLockRegistry.mjs +522 -0
- package/src/core-isomorphic/telemetry/sprintMetrics.mjs +327 -0
- package/src/core-isomorphic/telemetry/swarmTopology.mjs +152 -0
- package/src/core-isomorphic/telemetry/telemetryBridge.mjs +1156 -0
- package/src/core-isomorphic/ui/brailleSparkline.mjs +142 -0
- package/src/core-isomorphic/ui/costRadarSvg.mjs +276 -0
- package/src/core-isomorphic/ui/horizonGrid.mjs +475 -0
- package/src/core-isomorphic/ui/horizonPanelHtml.mjs +1237 -0
- package/src/core-isomorphic/ui/modViews.mjs +1504 -0
- package/src/core-isomorphic/ui/pdfReceiptGenerator.mjs +329 -0
- package/src/core-isomorphic/ustf/index.mjs +7 -0
- package/src/core-isomorphic/ustf/normalizer.mjs +512 -0
- package/src/core-isomorphic/ustf/schema.mjs +186 -0
- package/src/core-isomorphic/utils/runtime.mjs +962 -0
- package/src/demo/synthetic_states.mjs +661 -0
- package/src/diagnostics/bugReportGenerator.mjs +253 -0
- package/src/diagnostics/claudeHookCapabilities.mjs +396 -0
- package/src/diagnostics/doctorEngine.mjs +295 -0
- package/src/engine-node/db/index.mjs +58 -0
- package/src/engine-node/db/jsonl_fallback.mjs +184 -0
- package/src/engine-node/db/probe.mjs +342 -0
- package/src/engine-node/db/sqlite_wrapper.mjs +122 -0
- package/src/engine-node/index.mjs +12 -0
- package/src/engine-node/ipc/client.mjs +447 -0
- package/src/engine-node/ipc/index.mjs +22 -0
- package/src/engine-node/ipc/protocol.mjs +177 -0
- package/src/engine-node/ipc/server.mjs +445 -0
- package/src/engine-node/offloader/index.mjs +6 -0
- package/src/engine-node/offloader/tool_offloader.mjs +241 -0
- package/src/engine-node/sensors/blindspots.mjs +376 -0
- package/src/engine-node/sensors/git_merge_tree.mjs +223 -0
- package/src/engine-node/sensors/index.mjs +7 -0
- package/src/hooks/antigravity_rsl_hook.mjs +250 -0
- package/src/hooks/claude_rsl_hook.mjs +288 -0
- package/src/hooks/portfolio_runtime.mjs +858 -0
- package/src/hooks/post_invocation.mjs +611 -0
- package/src/hooks/pre_invocation.mjs +1725 -0
- package/src/hooks/session_start.mjs +199 -0
- package/src/shims/agy_cli_compressor.mjs +705 -0
- package/src/shims/agy_git_guard.mjs +749 -0
- package/src/shims/ah_log_query.mjs +39 -0
- package/src/ui/costRadarSvg.mjs +6 -0
- package/src/ui/horizonGrid.mjs +1 -0
- package/src/ui/horizonPanelHtml.mjs +1 -0
- package/src/ui/horizon_panel.css +167 -0
- package/src/ui/horizon_panel.js +212 -0
- package/src/ui/modViews.mjs +1 -0
- package/src/utils/modelSwitchTelemetry.mjs +308 -0
- package/src/utils/nodeIo.mjs +59 -0
- package/src/utils/runtime.mjs +1 -0
- package/src/utils/sessionCalibration.mjs +657 -0
- package/src/webview/advanced_view.html +1808 -0
- package/src/webview/horizon_panel.html +12397 -0
- package/src/webview/registry.mjs +80 -0
- package/src/webview/simple_view.html +1370 -0
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/engine-node/db/probe.mjs
|
|
3
|
+
* Dynamic runtime probe for node:sqlite builtin module.
|
|
4
|
+
*
|
|
5
|
+
* Enables full read-only SQLite & FTS5 on Node >= 22.5, with automatic fallback
|
|
6
|
+
* to JSONL parsing on Node 20 LTS with zero --no-warnings flags.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
let warningSuppressed = false;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Suppresses experimental SQLite warning from process.emitWarning.
|
|
13
|
+
* Allows Node >= 22.5 to load node:sqlite with 0 stderr noise and zero --no-warnings CLI flags.
|
|
14
|
+
*/
|
|
15
|
+
export function suppressSqliteWarning({ force = false } = {}) {
|
|
16
|
+
if ((warningSuppressed && !force) || typeof process === 'undefined') return;
|
|
17
|
+
warningSuppressed = true;
|
|
18
|
+
if (typeof process.emitWarning === 'function') {
|
|
19
|
+
const orig = process.emitWarning.bind(process);
|
|
20
|
+
process.emitWarning = function (warning, ...args) {
|
|
21
|
+
const msg = typeof warning === 'string' ? warning : (warning && warning.message) || '';
|
|
22
|
+
const type = typeof args[0] === 'string' ? args[0] : (args[0] && args[0].type) || (warning && warning.name) || '';
|
|
23
|
+
if ((type === 'ExperimentalWarning' && /SQLite/i.test(msg)) || /SQLite is an experimental feature/i.test(msg)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
return orig(warning, ...args);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let probedModule = undefined;
|
|
32
|
+
|
|
33
|
+
function extractFtsTokens(rawQuery = '') {
|
|
34
|
+
const cleaned = String(rawQuery || '')
|
|
35
|
+
.replace(/\bNEAR\s*\([^)]*\)/gi, ' ')
|
|
36
|
+
.replace(/[A-Za-z0-9_]+\s*:/g, ' ')
|
|
37
|
+
.replace(/['"*()]/g, ' ')
|
|
38
|
+
.trim();
|
|
39
|
+
if (!cleaned) return { mode: 'EMPTY', include: [], exclude: [] };
|
|
40
|
+
|
|
41
|
+
if (/\s+NOT\s+/i.test(cleaned)) {
|
|
42
|
+
const [incPart, ...excParts] = cleaned.split(/\s+NOT\s+/i);
|
|
43
|
+
const include = incPart.split(/\s+(?:AND\s+|OR\s+)?/i).map(t => t.trim().toLowerCase()).filter(Boolean);
|
|
44
|
+
const exclude = excParts.join(' ').split(/\s+/).map(t => t.trim().toLowerCase()).filter(Boolean);
|
|
45
|
+
return { mode: 'AND', include, exclude };
|
|
46
|
+
}
|
|
47
|
+
if (/\s+OR\s+/i.test(cleaned)) {
|
|
48
|
+
const include = cleaned.split(/\s+OR\s+|\s+/i).map(t => t.trim().toLowerCase()).filter(t => t && t !== 'or' && t !== 'and');
|
|
49
|
+
return { mode: 'OR', include, exclude: [] };
|
|
50
|
+
}
|
|
51
|
+
const include = cleaned.split(/\s+AND\s+|\s+/i).map(t => t.trim().toLowerCase()).filter(t => t && t !== 'and');
|
|
52
|
+
return { mode: 'AND', include, exclude: [] };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function evaluateFtsMatch(haystack = '', rawQuery = '') {
|
|
56
|
+
const text = String(haystack || '').toLowerCase();
|
|
57
|
+
const { mode, include, exclude } = extractFtsTokens(rawQuery);
|
|
58
|
+
if (mode === 'EMPTY' || include.length === 0) return 0;
|
|
59
|
+
for (const ex of exclude) {
|
|
60
|
+
if (text.includes(ex)) return 0;
|
|
61
|
+
}
|
|
62
|
+
if (mode === 'OR') {
|
|
63
|
+
return include.some(tok => text.includes(tok)) ? 1 : 0;
|
|
64
|
+
}
|
|
65
|
+
return include.every(tok => text.includes(tok)) ? 1 : 0;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function evaluateFtsBm25(haystack = '', rawQuery = '') {
|
|
69
|
+
const text = String(haystack || '').toLowerCase();
|
|
70
|
+
const { include } = extractFtsTokens(rawQuery);
|
|
71
|
+
if (include.length === 0) return 0.0;
|
|
72
|
+
let score = 0;
|
|
73
|
+
for (const tok of include) {
|
|
74
|
+
let idx = 0;
|
|
75
|
+
while ((idx = text.indexOf(tok, idx)) !== -1) {
|
|
76
|
+
score += 1;
|
|
77
|
+
idx += tok.length;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return score > 0 ? -score : 0.0;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function evaluateFtsSnippet(haystack = '', rawQuery = '', openTag = '[', closeTag = ']', ellipsis = '...', maxTokens = 16) {
|
|
84
|
+
const rawStr = String(haystack || '');
|
|
85
|
+
const { include } = extractFtsTokens(rawQuery);
|
|
86
|
+
const segments = rawStr.includes('\x1f') ? rawStr.split('\x1f') : [rawStr];
|
|
87
|
+
let chosenSegment = segments[segments.length - 1] || '';
|
|
88
|
+
if (include.length > 0) {
|
|
89
|
+
for (const seg of segments) {
|
|
90
|
+
const lowerSeg = seg.toLowerCase();
|
|
91
|
+
if (include.some(tok => lowerSeg.includes(tok))) {
|
|
92
|
+
chosenSegment = seg;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (!chosenSegment || include.length === 0) return chosenSegment.slice(0, 120);
|
|
98
|
+
let out = chosenSegment;
|
|
99
|
+
for (const tok of include) {
|
|
100
|
+
const escaped = tok.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
101
|
+
const re = new RegExp(`(${escaped})`, 'gi');
|
|
102
|
+
if (re.test(out)) {
|
|
103
|
+
out = out.replace(re, `${openTag}$1${closeTag}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const words = out.split(/\s+/);
|
|
107
|
+
const limit = Math.max(4, Number(maxTokens) || 16);
|
|
108
|
+
if (words.length > limit) {
|
|
109
|
+
let matchWordIdx = 0;
|
|
110
|
+
const lowerInclude = include.map(t => t.toLowerCase());
|
|
111
|
+
for (let i = 0; i < words.length; i++) {
|
|
112
|
+
const wLower = words[i].toLowerCase();
|
|
113
|
+
if (lowerInclude.some(tok => wLower.includes(tok))) {
|
|
114
|
+
matchWordIdx = i;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const startIdx = Math.max(0, Math.min(words.length - limit, matchWordIdx - Math.floor(limit / 3)));
|
|
119
|
+
const endIdx = startIdx + limit;
|
|
120
|
+
const prefix = startIdx > 0 ? ellipsis : '';
|
|
121
|
+
const suffix = endIdx < words.length ? ellipsis : '';
|
|
122
|
+
return `${prefix}${words.slice(startIdx, endIdx).join(' ')}${suffix}`;
|
|
123
|
+
}
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function createFts5PolyfillClass(RawDatabaseSync) {
|
|
128
|
+
const Fts5PolyfillDatabaseSync = class DatabaseSync extends RawDatabaseSync {
|
|
129
|
+
constructor(...args) {
|
|
130
|
+
super(...args);
|
|
131
|
+
this._activeFtsQuery = '';
|
|
132
|
+
this._ftsColsCache = new Map();
|
|
133
|
+
try {
|
|
134
|
+
if (typeof this.function === 'function') {
|
|
135
|
+
this.function('__hz_fts_match', (haystack, q) => {
|
|
136
|
+
this._activeFtsQuery = String(q ?? '');
|
|
137
|
+
return evaluateFtsMatch(haystack, q);
|
|
138
|
+
});
|
|
139
|
+
this.function('__hz_fts_bm25', (haystack) => {
|
|
140
|
+
return evaluateFtsBm25(haystack, this._activeFtsQuery);
|
|
141
|
+
});
|
|
142
|
+
this.function('__hz_fts_snippet', (haystack, openTag, closeTag, ellipsis, maxTok) => {
|
|
143
|
+
return evaluateFtsSnippet(haystack, this._activeFtsQuery, openTag, closeTag, ellipsis, maxTok);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
} catch {
|
|
147
|
+
// Ignore if custom functions cannot be registered
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
_getTableColumns(tableName) {
|
|
152
|
+
if (this._ftsColsCache.has(tableName)) return this._ftsColsCache.get(tableName);
|
|
153
|
+
try {
|
|
154
|
+
const rows = super.prepare(`PRAGMA table_info(${tableName})`).all();
|
|
155
|
+
const cols = rows.map(r => r.name).filter(Boolean);
|
|
156
|
+
if (cols.length > 0) {
|
|
157
|
+
this._ftsColsCache.set(tableName, cols);
|
|
158
|
+
return cols;
|
|
159
|
+
}
|
|
160
|
+
} catch {}
|
|
161
|
+
return [];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
_buildConcatExpr(tableName) {
|
|
165
|
+
const cols = this._getTableColumns(tableName);
|
|
166
|
+
if (cols.length === 0) return `''`;
|
|
167
|
+
return `(${cols.map(c => `COALESCE(CAST(${tableName}.${c} AS TEXT), '')`).join(" || char(31) || ")})`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
exec(sql) {
|
|
171
|
+
let rewritten = String(sql || '');
|
|
172
|
+
// Strip FTS5 optimize command on polyfilled tables
|
|
173
|
+
rewritten = rewritten.replace(
|
|
174
|
+
/INSERT\s+INTO\s+([A-Za-z0-9_]+)\s*\(\s*\1\s*\)\s*VALUES\s*\(\s*'optimize'\s*\)\s*;?/gi,
|
|
175
|
+
''
|
|
176
|
+
);
|
|
177
|
+
// Rewrite CREATE VIRTUAL TABLE ... USING fts5(...) into standard SQLite table preserving /* fts5 */ marker
|
|
178
|
+
rewritten = rewritten.replace(
|
|
179
|
+
/CREATE\s+VIRTUAL\s+TABLE\s+(IF\s+NOT\s+EXISTS\s+)?([A-Za-z0-9_]+)\s+USING\s+fts5\s*\(([\s\S]*?)\)/gi,
|
|
180
|
+
(_, ifNotExists = '', tableName, rawCols) => {
|
|
181
|
+
const cols = rawCols
|
|
182
|
+
.split(',')
|
|
183
|
+
.map(part => part.trim())
|
|
184
|
+
.filter(part => part && !part.includes('='))
|
|
185
|
+
.map(part => part.replace(/\bUNINDEXED\b/gi, '').trim().split(/\s+/)[0])
|
|
186
|
+
.filter(Boolean);
|
|
187
|
+
this._ftsColsCache.set(tableName, cols);
|
|
188
|
+
const colDefs = cols.map(c => `${c} TEXT`).join(', ');
|
|
189
|
+
return `CREATE TABLE ${ifNotExists}${tableName} (/* fts5 */ ${colDefs})`;
|
|
190
|
+
}
|
|
191
|
+
);
|
|
192
|
+
if (!rewritten.trim()) return;
|
|
193
|
+
return super.exec(rewritten);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
prepare(sql) {
|
|
197
|
+
let rewritten = String(sql || '');
|
|
198
|
+
const hasFtsSyntax = /\bMATCH\s+\?|\bbm25\s*\(|\bsnippet\s*\(/i.test(rewritten);
|
|
199
|
+
if (hasFtsSyntax) {
|
|
200
|
+
rewritten = rewritten.replace(
|
|
201
|
+
/snippet\s*\(\s*([A-Za-z0-9_]+)\s*,\s*(-?\d+)\s*,\s*('[^']*')\s*,\s*('[^']*')\s*,\s*('[^']*')\s*,\s*(\d+)\s*\)/gi,
|
|
202
|
+
(_, tbl, colIdxStr, openTag, closeTag, ellipsis, maxTok) => {
|
|
203
|
+
const colIdx = Number(colIdxStr);
|
|
204
|
+
const cols = this._getTableColumns(tbl);
|
|
205
|
+
const targetExpr = (colIdx >= 0 && cols[colIdx])
|
|
206
|
+
? `COALESCE(CAST(${tbl}.${cols[colIdx]} AS TEXT), '')`
|
|
207
|
+
: this._buildConcatExpr(tbl);
|
|
208
|
+
return `__hz_fts_snippet(${targetExpr}, ${openTag}, ${closeTag}, ${ellipsis}, ${maxTok})`;
|
|
209
|
+
}
|
|
210
|
+
);
|
|
211
|
+
rewritten = rewritten.replace(
|
|
212
|
+
/bm25\s*\(\s*([A-Za-z0-9_]+)\s*\)/gi,
|
|
213
|
+
(_, tbl) => `__hz_fts_bm25(${this._buildConcatExpr(tbl)})`
|
|
214
|
+
);
|
|
215
|
+
rewritten = rewritten.replace(
|
|
216
|
+
/([A-Za-z0-9_]+)\s+MATCH\s+\?/gi,
|
|
217
|
+
(_, tbl) => `__hz_fts_match(${this._buildConcatExpr(tbl)}, ?)`
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const stmt = super.prepare(rewritten);
|
|
222
|
+
if (!hasFtsSyntax) return stmt;
|
|
223
|
+
|
|
224
|
+
const self = this;
|
|
225
|
+
const origAll = stmt.all.bind(stmt);
|
|
226
|
+
const origGet = stmt.get.bind(stmt);
|
|
227
|
+
stmt.all = (...args) => {
|
|
228
|
+
if (args.length > 0) self._activeFtsQuery = String(args[0] ?? '');
|
|
229
|
+
return origAll(...args);
|
|
230
|
+
};
|
|
231
|
+
stmt.get = (...args) => {
|
|
232
|
+
if (args.length > 0) self._activeFtsQuery = String(args[0] ?? '');
|
|
233
|
+
return origGet(...args);
|
|
234
|
+
};
|
|
235
|
+
return stmt;
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
Object.defineProperty(Fts5PolyfillDatabaseSync, 'name', { value: 'DatabaseSync' });
|
|
239
|
+
return Fts5PolyfillDatabaseSync;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function ensureFts5CapableModule(mod) {
|
|
243
|
+
if (!mod || typeof mod.DatabaseSync !== 'function') return mod;
|
|
244
|
+
const RawDb = mod.DatabaseSync;
|
|
245
|
+
let hasNativeFts5 = false;
|
|
246
|
+
if (typeof process === 'undefined' || process.env?.CTXTOP_FORCE_FTS5_POLYFILL !== '1') {
|
|
247
|
+
try {
|
|
248
|
+
const mem = new RawDb(':memory:');
|
|
249
|
+
mem.exec('CREATE VIRTUAL TABLE __fts5_probe USING fts5(c);');
|
|
250
|
+
mem.close();
|
|
251
|
+
hasNativeFts5 = true;
|
|
252
|
+
} catch {
|
|
253
|
+
hasNativeFts5 = false;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (hasNativeFts5) return mod;
|
|
257
|
+
const PolyfilledDb = createFts5PolyfillClass(RawDb);
|
|
258
|
+
return {
|
|
259
|
+
...mod,
|
|
260
|
+
DatabaseSync: PolyfilledDb
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Probes for built-in node:sqlite module via process.getBuiltinModule.
|
|
266
|
+
*
|
|
267
|
+
* @param {object} [options={}]
|
|
268
|
+
* @param {boolean} [options.forceFallback=false]
|
|
269
|
+
* @param {Function|null} [options.getBuiltinModuleOverride=null]
|
|
270
|
+
* @returns {object|null}
|
|
271
|
+
*/
|
|
272
|
+
export function probeSqliteBuiltin({ forceFallback = false, getBuiltinModuleOverride = null } = {}) {
|
|
273
|
+
if (
|
|
274
|
+
forceFallback ||
|
|
275
|
+
(typeof process !== 'undefined' && process.env?.CTXTOP_FORCE_NO_SQLITE === '1')
|
|
276
|
+
) {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
if (probedModule !== undefined && !getBuiltinModuleOverride) return probedModule;
|
|
280
|
+
|
|
281
|
+
suppressSqliteWarning();
|
|
282
|
+
|
|
283
|
+
const getBuiltin = getBuiltinModuleOverride ||
|
|
284
|
+
(typeof process !== 'undefined' && typeof process.getBuiltinModule === 'function'
|
|
285
|
+
? process.getBuiltinModule.bind(process)
|
|
286
|
+
: null);
|
|
287
|
+
|
|
288
|
+
if (!getBuiltin) {
|
|
289
|
+
if (!getBuiltinModuleOverride) probedModule = null;
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
try {
|
|
294
|
+
const rawMod = getBuiltin('node:sqlite');
|
|
295
|
+
if (rawMod && typeof rawMod.DatabaseSync === 'function') {
|
|
296
|
+
const mod = getBuiltinModuleOverride ? rawMod : ensureFts5CapableModule(rawMod);
|
|
297
|
+
if (!getBuiltinModuleOverride) probedModule = mod;
|
|
298
|
+
return mod;
|
|
299
|
+
}
|
|
300
|
+
} catch {
|
|
301
|
+
// Fail-open: probe never throws
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (!getBuiltinModuleOverride) probedModule = null;
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Returns true if built-in node:sqlite with DatabaseSync is available in this runtime.
|
|
310
|
+
*
|
|
311
|
+
* @param {object} [options={}]
|
|
312
|
+
* @returns {boolean}
|
|
313
|
+
*/
|
|
314
|
+
export function isNodeSqliteAvailable(options = {}) {
|
|
315
|
+
return Boolean(probeSqliteBuiltin(options));
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Returns the DatabaseSync constructor if available, or null.
|
|
320
|
+
*
|
|
321
|
+
* @param {object} [options={}]
|
|
322
|
+
* @returns {Function|null}
|
|
323
|
+
*/
|
|
324
|
+
export function getDatabaseSyncClass(options = {}) {
|
|
325
|
+
const mod = probeSqliteBuiltin(options);
|
|
326
|
+
return mod?.DatabaseSync || null;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Resets probe cache for testing environments.
|
|
331
|
+
*/
|
|
332
|
+
export function resetProbeCacheForTesting() {
|
|
333
|
+
probedModule = undefined;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export default {
|
|
337
|
+
suppressSqliteWarning,
|
|
338
|
+
probeSqliteBuiltin,
|
|
339
|
+
isNodeSqliteAvailable,
|
|
340
|
+
getDatabaseSyncClass,
|
|
341
|
+
resetProbeCacheForTesting
|
|
342
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/engine-node/db/sqlite_wrapper.mjs
|
|
3
|
+
* Read-only wrapper around node:sqlite DatabaseSync.
|
|
4
|
+
*
|
|
5
|
+
* Strictly enforces { readOnly: true } invariant and rejects any mutating SQL.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { getDatabaseSyncClass } from './probe.mjs';
|
|
10
|
+
|
|
11
|
+
const MUTATION_SQL_RE = /^\s*(INSERT|UPDATE|DELETE|DROP|ALTER|CREATE|REPLACE|ATTACH|DETACH|VACUUM|REINDEX)\b/i;
|
|
12
|
+
|
|
13
|
+
export class SqliteDatabaseSyncWrapper {
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} dbPath
|
|
16
|
+
* @param {object} options
|
|
17
|
+
* @param {boolean} options.readOnly - Must strictly be true
|
|
18
|
+
* @param {Function} [options.DatabaseSync] - Optional constructor override
|
|
19
|
+
*/
|
|
20
|
+
constructor(dbPath, options = {}) {
|
|
21
|
+
if (!options || options.readOnly !== true) {
|
|
22
|
+
throw new Error('[Zero-Trust SQLite Guard] SqliteDatabaseSyncWrapper strictly requires { readOnly: true }');
|
|
23
|
+
}
|
|
24
|
+
if (!dbPath || typeof dbPath !== 'string') {
|
|
25
|
+
throw new TypeError('SqliteDatabaseSyncWrapper requires a non-empty dbPath string');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
this.dbPath = (dbPath === ':memory:' || dbPath.startsWith('file:')) ? dbPath : path.resolve(dbPath);
|
|
29
|
+
this.readOnly = true;
|
|
30
|
+
this.isOpen = true;
|
|
31
|
+
|
|
32
|
+
const DbCtor = options.DatabaseSync || getDatabaseSyncClass(options);
|
|
33
|
+
if (!DbCtor) {
|
|
34
|
+
throw new Error('DatabaseSync constructor is unavailable in this runtime');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
this.db = new DbCtor(this.dbPath, { readOnly: true, readonly: true });
|
|
38
|
+
try {
|
|
39
|
+
if (typeof this.db.exec === 'function') {
|
|
40
|
+
this.db.exec('PRAGMA busy_timeout = 3000;');
|
|
41
|
+
}
|
|
42
|
+
} catch {
|
|
43
|
+
// Ignore pragma rejection on immutable/locked handles
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
_assertNotMutating(sql) {
|
|
48
|
+
if (!sql || typeof sql !== 'string') return;
|
|
49
|
+
const stripped = sql
|
|
50
|
+
.replace(/\/\*[\s\S]*?\*\//g, ' ')
|
|
51
|
+
.replace(/--.*$/gm, ' ')
|
|
52
|
+
.trim();
|
|
53
|
+
|
|
54
|
+
const statements = stripped.split(';').map(s => s.trim()).filter(Boolean);
|
|
55
|
+
for (const stmt of statements) {
|
|
56
|
+
if (MUTATION_SQL_RE.test(stmt)) {
|
|
57
|
+
throw new Error(`[Zero-Trust SQLite Guard] Mutation query not permitted on read-only database: ${sql}`);
|
|
58
|
+
}
|
|
59
|
+
if (/^\s*WITH\b/i.test(stmt) && /\b(DELETE|INSERT|UPDATE|DROP|ALTER|REPLACE)\b/i.test(stmt)) {
|
|
60
|
+
throw new Error(`[Zero-Trust SQLite Guard] Mutation query not permitted on read-only database: ${sql}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get(sql, params = []) {
|
|
66
|
+
if (!this.isOpen) throw new Error('Database handle is closed');
|
|
67
|
+
this._assertNotMutating(sql);
|
|
68
|
+
const stmt = this.db.prepare(sql);
|
|
69
|
+
if (Array.isArray(params)) return stmt.get(...params);
|
|
70
|
+
if (params !== undefined && params !== null) return stmt.get(params);
|
|
71
|
+
return stmt.get();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
all(sql, params = []) {
|
|
75
|
+
if (!this.isOpen) throw new Error('Database handle is closed');
|
|
76
|
+
this._assertNotMutating(sql);
|
|
77
|
+
const stmt = this.db.prepare(sql);
|
|
78
|
+
if (Array.isArray(params)) return stmt.all(...params);
|
|
79
|
+
if (params !== undefined && params !== null) return stmt.all(params);
|
|
80
|
+
return stmt.all();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
searchFts(query, limit = 10, { table = null } = {}) {
|
|
84
|
+
if (!this.isOpen) throw new Error('Database handle is closed');
|
|
85
|
+
if (!query || typeof query !== 'string') return [];
|
|
86
|
+
|
|
87
|
+
let targetTable = table;
|
|
88
|
+
if (!targetTable) {
|
|
89
|
+
try {
|
|
90
|
+
const tables = this.all("SELECT name FROM sqlite_master WHERE type='table' AND sql LIKE '%fts5%'");
|
|
91
|
+
if (tables.length > 0) targetTable = tables[0].name;
|
|
92
|
+
} catch {
|
|
93
|
+
// Fall back to null
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!targetTable) return [];
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
const sanitized = query.replace(/['"*]/g, ' ').trim();
|
|
101
|
+
if (!sanitized) return [];
|
|
102
|
+
const sql = `SELECT *, snippet(${targetTable}, -1, '[', ']', '...', 16) AS snippet, bm25(${targetTable}) AS score ` +
|
|
103
|
+
`FROM ${targetTable} WHERE ${targetTable} MATCH ? ORDER BY score LIMIT ?`;
|
|
104
|
+
return this.all(sql, [sanitized, limit]);
|
|
105
|
+
} catch {
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
close() {
|
|
111
|
+
if (this.isOpen) {
|
|
112
|
+
this.isOpen = false;
|
|
113
|
+
try {
|
|
114
|
+
if (typeof this.db.close === 'function') this.db.close();
|
|
115
|
+
} catch {
|
|
116
|
+
// Safe close
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export default SqliteDatabaseSyncWrapper;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/engine-node/index.mjs
|
|
3
|
+
* Highbeam Node.js runtime engines barrel.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export * from './db/index.mjs';
|
|
7
|
+
export * from './sensors/index.mjs';
|
|
8
|
+
export * from './offloader/index.mjs';
|
|
9
|
+
export * from './ipc/index.mjs';
|
|
10
|
+
export * from '../core/rsl_triage.mjs';
|
|
11
|
+
export * from '../core/rsl_distiller.mjs';
|
|
12
|
+
export * from '../core/rsl_ledger.mjs';
|