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,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/diagnostics/doctorEngine.mjs
|
|
3
|
+
* Contract Self-Check & Bounded Hook Runtime Diagnostics Engine (`ah-claude doctor` — `ISS-065`).
|
|
4
|
+
*
|
|
5
|
+
* Reads `<cwd>/.claude/cache/ah_diagnostics.jsonl` via `readRuntimeDiagnostics` and surfaces
|
|
6
|
+
* `hookDiagnostics` (`recentErrorCount` and up to the last 5 redacted `recentErrors`).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
fs,
|
|
11
|
+
path,
|
|
12
|
+
os,
|
|
13
|
+
fileURLToPath,
|
|
14
|
+
readJsonSafe,
|
|
15
|
+
getDiagnosticsFilePath,
|
|
16
|
+
readRuntimeDiagnostics
|
|
17
|
+
} from '../utils/nodeIo.mjs';
|
|
18
|
+
import {
|
|
19
|
+
isKnownCatalogModel,
|
|
20
|
+
loadModelCatalog
|
|
21
|
+
} from '../core-isomorphic/config/catalogLoader.mjs';
|
|
22
|
+
import { loadHorizonConfig } from '../core-isomorphic/config/horizonConfig.mjs';
|
|
23
|
+
import { inspectClaudeBinaryCapabilities } from './claudeHookCapabilities.mjs';
|
|
24
|
+
import { readModelSwitchTelemetry } from '../utils/modelSwitchTelemetry.mjs';
|
|
25
|
+
import { auditAntigravityRulesIntegrity } from '../core/layer5_hygiene.mjs';
|
|
26
|
+
import { auditClaudeAutoMemory } from '../core/shield_manager.mjs';
|
|
27
|
+
import { auditProseRuleHookCandidates } from '../core/bucket_b_runtime.mjs';
|
|
28
|
+
import { auditWorkspaceRuleClashes } from '../core/rsl_clash_auditor.mjs';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Redacts the user's OS home directory from any diagnostic string.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} text
|
|
34
|
+
* @param {string} [homeDir]
|
|
35
|
+
* @returns {string}
|
|
36
|
+
*/
|
|
37
|
+
export function redactDoctorPaths(text, homeDir) {
|
|
38
|
+
const raw = String(text || '');
|
|
39
|
+
const home = homeDir || os?.homedir?.() || '';
|
|
40
|
+
if (!home) return raw;
|
|
41
|
+
const escapedWin = home.replace(/\//g, '\\').replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
42
|
+
const posixHome = home.replace(/\\/g, '/').replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
43
|
+
return raw
|
|
44
|
+
.replace(new RegExp(escapedWin, 'gi'), '~')
|
|
45
|
+
.replace(new RegExp(posixHome, 'gi'), '~');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Resolves the target `settings.json` path for `--project` or `--global`.
|
|
50
|
+
*
|
|
51
|
+
* @param {object} [options={}]
|
|
52
|
+
* @returns {string}
|
|
53
|
+
*/
|
|
54
|
+
function resolveDoctorSettingsPath(options = {}) {
|
|
55
|
+
if (typeof options.settingsPath === 'string' && options.settingsPath.trim()) {
|
|
56
|
+
return options.settingsPath.trim();
|
|
57
|
+
}
|
|
58
|
+
const scope = options.scope === 'global' || options.global ? 'global' : 'project';
|
|
59
|
+
if (scope === 'global') {
|
|
60
|
+
const home = options.homeDir || os.homedir() || '.';
|
|
61
|
+
return path.join(home, '.claude', 'settings.json');
|
|
62
|
+
}
|
|
63
|
+
const cwd = options.cwd || globalThis.process?.cwd?.() || '.';
|
|
64
|
+
return path.join(cwd, '.claude', 'settings.json');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Reads `<cwd>/.claude/cache/ah_diagnostics.jsonl` and returns a zero-PII summary
|
|
69
|
+
* with total `recentErrorCount` and up to the newest 5 redacted `recentErrors` (`ISS-065`).
|
|
70
|
+
*
|
|
71
|
+
* @param {object} [options={}]
|
|
72
|
+
* @returns {{
|
|
73
|
+
* diagnosticsPath: string,
|
|
74
|
+
* recentErrorCount: number,
|
|
75
|
+
* recentErrors: Array<{ timestamp: string, source: string, operation: string, name: string, message: string }>
|
|
76
|
+
* }}
|
|
77
|
+
*/
|
|
78
|
+
export function inspectHookDiagnostics(options = {}) {
|
|
79
|
+
const cwd = options.cwd || globalThis.process?.cwd?.() || '.';
|
|
80
|
+
const homeDir = options.homeDir || os?.homedir?.() || '';
|
|
81
|
+
const entries = readRuntimeDiagnostics({ ...options, cwd });
|
|
82
|
+
const recentErrors = entries.slice(-5).map((entry) => ({
|
|
83
|
+
timestamp: String(entry.timestamp || ''),
|
|
84
|
+
source: String(entry.source || 'unknown'),
|
|
85
|
+
operation: String(entry.operation || 'unknown'),
|
|
86
|
+
name: String(entry.name || 'Error'),
|
|
87
|
+
message: redactDoctorPaths(entry.message || '', homeDir).slice(0, 300)
|
|
88
|
+
}));
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
diagnosticsPath: getDiagnosticsFilePath({ ...options, cwd }),
|
|
92
|
+
recentErrorCount: entries.length,
|
|
93
|
+
recentErrors
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Runs host contract & runtime diagnostic checks (`npx ah-claude doctor`).
|
|
99
|
+
*
|
|
100
|
+
* @param {object} [options={}]
|
|
101
|
+
* @returns {{
|
|
102
|
+
* ok: boolean,
|
|
103
|
+
* nodeVersion: string,
|
|
104
|
+
* checks: Array<{ id: string, ok: boolean, detail: string }>,
|
|
105
|
+
* hookDiagnostics: {
|
|
106
|
+
* diagnosticsPath: string,
|
|
107
|
+
* recentErrorCount: number,
|
|
108
|
+
* recentErrors: Array<{ timestamp: string, source: string, operation: string, name: string, message: string }>
|
|
109
|
+
* }
|
|
110
|
+
* }}
|
|
111
|
+
*/
|
|
112
|
+
export function runDoctorDiagnostics(options = {}) {
|
|
113
|
+
const cwd = options.cwd || globalThis.process?.cwd?.() || '.';
|
|
114
|
+
const nodeVersion = globalThis.process?.versions?.node || '0.0.0';
|
|
115
|
+
const major = Number(nodeVersion.split('.')[0] || 0);
|
|
116
|
+
const checks = [];
|
|
117
|
+
|
|
118
|
+
checks.push({
|
|
119
|
+
id: 'node_version',
|
|
120
|
+
ok: major >= 20,
|
|
121
|
+
detail: `Node.js v${nodeVersion} (requires >= 20.0.0)`
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
const catalog = loadModelCatalog(options);
|
|
126
|
+
const modelCount = Object.keys(catalog.models || {}).length;
|
|
127
|
+
checks.push({
|
|
128
|
+
id: 'model_catalog',
|
|
129
|
+
ok: modelCount >= 6,
|
|
130
|
+
detail: `Loaded ${modelCount} Anthropic model rate cards (Haiku 5.5 100k cliff enabled)`
|
|
131
|
+
});
|
|
132
|
+
} catch (err) {
|
|
133
|
+
checks.push({
|
|
134
|
+
id: 'model_catalog',
|
|
135
|
+
ok: false,
|
|
136
|
+
detail: `Failed to load catalog: ${err?.message || String(err)}`
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const pluginRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
|
|
141
|
+
const registerPath = path.join(pluginRoot, 'hooks', 'register.js');
|
|
142
|
+
const hooksJsonPath = path.join(pluginRoot, 'hooks', 'hooks.json');
|
|
143
|
+
const pluginJsonPath = path.join(pluginRoot, '.claude-plugin', 'plugin.json');
|
|
144
|
+
|
|
145
|
+
checks.push({
|
|
146
|
+
id: 'mod_entrypoint',
|
|
147
|
+
ok: Boolean(fs.existsSync(registerPath)),
|
|
148
|
+
detail: `hooks/register.js present at ${registerPath}`
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const parsedHooksJson = readJsonSafe(hooksJsonPath, null);
|
|
152
|
+
const validHooksSchema = Boolean(
|
|
153
|
+
parsedHooksJson &&
|
|
154
|
+
typeof parsedHooksJson.hooks === 'object' &&
|
|
155
|
+
Array.isArray(parsedHooksJson.hooks.PreToolUse) &&
|
|
156
|
+
Array.isArray(parsedHooksJson.hooks.PreToolUse[0]?.hooks)
|
|
157
|
+
);
|
|
158
|
+
checks.push({
|
|
159
|
+
id: 'hooks_manifest',
|
|
160
|
+
ok: Boolean(fs.existsSync(hooksJsonPath) && validHooksSchema),
|
|
161
|
+
detail: `hooks/hooks.json valid (nested hooks schema)`
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const parsedPluginJson = readJsonSafe(pluginJsonPath, null);
|
|
165
|
+
const validPluginSchema = Boolean(
|
|
166
|
+
parsedPluginJson &&
|
|
167
|
+
parsedPluginJson.entrypoint === undefined &&
|
|
168
|
+
Array.isArray(parsedPluginJson.commands) &&
|
|
169
|
+
parsedPluginJson.commands.every((c) => typeof c === 'string')
|
|
170
|
+
);
|
|
171
|
+
checks.push({
|
|
172
|
+
id: 'plugin_manifest',
|
|
173
|
+
ok: Boolean(fs.existsSync(pluginJsonPath) && validPluginSchema),
|
|
174
|
+
detail: `.claude-plugin/plugin.json valid`
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const settingsPath = resolveDoctorSettingsPath({ ...options, cwd });
|
|
178
|
+
const settingsInstalled = Boolean(fs.existsSync(settingsPath));
|
|
179
|
+
checks.push({
|
|
180
|
+
id: 'settings_status',
|
|
181
|
+
ok: true,
|
|
182
|
+
detail: settingsInstalled
|
|
183
|
+
? `settings.json present at ${settingsPath}`
|
|
184
|
+
: `settings.json not yet created at ${settingsPath} (run 'ah-claude install')`
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const config = loadHorizonConfig({ ...options, cwd });
|
|
188
|
+
const customDetectors = Object.entries(config.detectors || {}).filter(
|
|
189
|
+
([, mode]) => mode !== 'default'
|
|
190
|
+
).length;
|
|
191
|
+
checks.push({
|
|
192
|
+
id: 'horizon_config',
|
|
193
|
+
ok: Boolean(config && config.detectors && Object.keys(config.detectors).length === 20),
|
|
194
|
+
detail: `20 detectors configured (FlightMode=${config.flightMode}, overrides=${customDetectors})`
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const agRulesAudit = auditAntigravityRulesIntegrity({ workspacePath: cwd });
|
|
198
|
+
const claudeMemAudit = auditClaudeAutoMemory({
|
|
199
|
+
cwd,
|
|
200
|
+
homeDir: options.homeDir,
|
|
201
|
+
memoryDir: options.memoryDir,
|
|
202
|
+
memoryFilePath: options.memoryFilePath
|
|
203
|
+
});
|
|
204
|
+
const memoryRuleIntegrity = {
|
|
205
|
+
ok: !options.strictRulesCheck || !(agRulesAudit.triggered || claudeMemAudit.triggered),
|
|
206
|
+
warning: Boolean(agRulesAudit.triggered || claudeMemAudit.triggered),
|
|
207
|
+
issues: [...(agRulesAudit.issues || []), ...(claudeMemAudit.issues || [])],
|
|
208
|
+
antigravityRules: agRulesAudit,
|
|
209
|
+
claudeAutoMemory: claudeMemAudit
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const proseHookCandidates = auditProseRuleHookCandidates(cwd);
|
|
213
|
+
const rslClashAudit = auditWorkspaceRuleClashes(cwd);
|
|
214
|
+
const hookDiagnostics = inspectHookDiagnostics({ ...options, cwd });
|
|
215
|
+
|
|
216
|
+
// Item 1.2: Warn cleanly when the configured or active session model has no price catalog entry
|
|
217
|
+
const candidateModelSources = [
|
|
218
|
+
options.model,
|
|
219
|
+
options.activeModel,
|
|
220
|
+
config?.model,
|
|
221
|
+
readJsonSafe(path.join(cwd, '.claude', 'settings.local.json'), null, options)?.model,
|
|
222
|
+
readJsonSafe(path.join(cwd, '.claude', 'settings.json'), null, options)?.model,
|
|
223
|
+
readJsonSafe(
|
|
224
|
+
path.join(options.homeDir || os.homedir() || '', '.claude', 'settings.json'),
|
|
225
|
+
null,
|
|
226
|
+
options
|
|
227
|
+
)?.model,
|
|
228
|
+
options.env?.ANTHROPIC_MODEL,
|
|
229
|
+
options.env?.CLAUDE_MODEL,
|
|
230
|
+
globalThis.process?.env?.ANTHROPIC_MODEL,
|
|
231
|
+
globalThis.process?.env?.CLAUDE_MODEL
|
|
232
|
+
].filter((m) => typeof m === 'string' && m.trim().length > 0);
|
|
233
|
+
const unknownConfiguredModels = Array.from(
|
|
234
|
+
new Set(
|
|
235
|
+
candidateModelSources
|
|
236
|
+
.map((m) => m.trim())
|
|
237
|
+
.filter((m) => !isKnownCatalogModel(m, { ...options, cwd }))
|
|
238
|
+
)
|
|
239
|
+
);
|
|
240
|
+
const warnings = [];
|
|
241
|
+
if (unknownConfiguredModels.length > 0) {
|
|
242
|
+
const warnDetail = `Configured model(s) [${unknownConfiguredModels.join(', ')}] not in price catalog; dollar figures and price-based holds are disabled (unknown model state).`;
|
|
243
|
+
warnings.push(warnDetail);
|
|
244
|
+
checks.push({
|
|
245
|
+
id: 'model_catalog_coverage',
|
|
246
|
+
status: 'WARN',
|
|
247
|
+
ok: true,
|
|
248
|
+
detail: warnDetail
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const modelSwitchTelemetry = readModelSwitchTelemetry({
|
|
253
|
+
homeDir: options.homeDir,
|
|
254
|
+
telemetryPath: options.telemetryPath
|
|
255
|
+
});
|
|
256
|
+
const claudeCapabilities = inspectClaudeBinaryCapabilities({
|
|
257
|
+
...options,
|
|
258
|
+
cwd,
|
|
259
|
+
recordDiagnostic: false
|
|
260
|
+
});
|
|
261
|
+
if (rslClashAudit.clashCount > 0) {
|
|
262
|
+
const clashMsg = `${rslClashAudit.clashCount} RSL rule conflict(s) detected: ${rslClashAudit.clashes.slice(0, 2).map((c) => c.reason).join('; ')}`;
|
|
263
|
+
warnings.push(clashMsg);
|
|
264
|
+
checks.push({
|
|
265
|
+
id: 'rsl_rule_clashes',
|
|
266
|
+
status: 'WARN',
|
|
267
|
+
ok: true,
|
|
268
|
+
detail: clashMsg
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
const ok =
|
|
272
|
+
checks.every((c) => c.ok) &&
|
|
273
|
+
memoryRuleIntegrity.ok &&
|
|
274
|
+
(!options.strictClaudeCheck || Boolean(claudeCapabilities.found && claudeCapabilities.contractValid));
|
|
275
|
+
|
|
276
|
+
return {
|
|
277
|
+
ok,
|
|
278
|
+
nodeVersion,
|
|
279
|
+
checks,
|
|
280
|
+
warnings,
|
|
281
|
+
claudeCapabilities,
|
|
282
|
+
hookDiagnostics,
|
|
283
|
+
modelSwitchTelemetry,
|
|
284
|
+
memoryRuleIntegrity,
|
|
285
|
+
proseHookCandidates,
|
|
286
|
+
rslRuleClashes: rslClashAudit.clashCount,
|
|
287
|
+
rslClashes: rslClashAudit.clashes.slice(0, 5)
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export default {
|
|
292
|
+
redactDoctorPaths,
|
|
293
|
+
inspectHookDiagnostics,
|
|
294
|
+
runDoctorDiagnostics
|
|
295
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/engine-node/db/index.mjs
|
|
3
|
+
* Unified database client factory for Highbeam storage engines.
|
|
4
|
+
*
|
|
5
|
+
* Dynamically resolves to native read-only node:sqlite on Node >= 22.5,
|
|
6
|
+
* or pure-JS JsonlDatabaseFallbackWrapper on Node 20 LTS without warnings.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { isNodeSqliteAvailable, getDatabaseSyncClass, suppressSqliteWarning } from './probe.mjs';
|
|
10
|
+
import { SqliteDatabaseSyncWrapper } from './sqlite_wrapper.mjs';
|
|
11
|
+
import { JsonlDatabaseFallbackWrapper } from './jsonl_fallback.mjs';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Returns a unified read-only database client.
|
|
15
|
+
*
|
|
16
|
+
* @param {object} options
|
|
17
|
+
* @param {string} options.dbPath - Path to database or session transcript
|
|
18
|
+
* @param {boolean} options.readOnly - Must strictly be true
|
|
19
|
+
* @param {boolean} [options.forceFallback=false] - Force JSONL fallback wrapper
|
|
20
|
+
* @returns {SqliteDatabaseSyncWrapper | JsonlDatabaseFallbackWrapper}
|
|
21
|
+
*/
|
|
22
|
+
export function getDatabaseClient(options = {}) {
|
|
23
|
+
if (!options || typeof options !== 'object') {
|
|
24
|
+
throw new TypeError('getDatabaseClient requires an options object');
|
|
25
|
+
}
|
|
26
|
+
if (options.readOnly !== true) {
|
|
27
|
+
throw new Error('[Zero-Trust SQLite Guard] getDatabaseClient strictly requires { readOnly: true }');
|
|
28
|
+
}
|
|
29
|
+
if (!options.dbPath || typeof options.dbPath !== 'string') {
|
|
30
|
+
throw new TypeError('getDatabaseClient requires a non-empty dbPath string');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const { dbPath, forceFallback = false } = options;
|
|
34
|
+
|
|
35
|
+
if (!forceFallback && isNodeSqliteAvailable()) {
|
|
36
|
+
const DatabaseSync = getDatabaseSyncClass();
|
|
37
|
+
return new SqliteDatabaseSyncWrapper(dbPath, { ...options, DatabaseSync });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return new JsonlDatabaseFallbackWrapper(dbPath, options);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export {
|
|
44
|
+
isNodeSqliteAvailable,
|
|
45
|
+
getDatabaseSyncClass,
|
|
46
|
+
suppressSqliteWarning,
|
|
47
|
+
SqliteDatabaseSyncWrapper,
|
|
48
|
+
JsonlDatabaseFallbackWrapper
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export default {
|
|
52
|
+
getDatabaseClient,
|
|
53
|
+
isNodeSqliteAvailable,
|
|
54
|
+
getDatabaseSyncClass,
|
|
55
|
+
suppressSqliteWarning,
|
|
56
|
+
SqliteDatabaseSyncWrapper,
|
|
57
|
+
JsonlDatabaseFallbackWrapper
|
|
58
|
+
};
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/engine-node/db/jsonl_fallback.mjs
|
|
3
|
+
* Pure-JS JSONL transcript reader and query emulator for Node 20 LTS environments.
|
|
4
|
+
*
|
|
5
|
+
* Provides equivalent get(), all(), and searchFts() interface for session transcripts,
|
|
6
|
+
* strictly enforcing the { readOnly: true } invariant and rejecting mutation queries.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
|
|
12
|
+
const MUTATION_SQL_RE = /^\s*(INSERT|UPDATE|DELETE|DROP|ALTER|CREATE|REPLACE|ATTACH|DETACH|VACUUM|REINDEX)\b/i;
|
|
13
|
+
|
|
14
|
+
export class JsonlDatabaseFallbackWrapper {
|
|
15
|
+
/**
|
|
16
|
+
* @param {string} dbPath
|
|
17
|
+
* @param {object} options
|
|
18
|
+
* @param {boolean} options.readOnly - Must strictly be true
|
|
19
|
+
* @param {Array<object>} [options.records] - Direct pre-loaded records
|
|
20
|
+
*/
|
|
21
|
+
constructor(dbPath, options = {}) {
|
|
22
|
+
if (!options || options.readOnly !== true) {
|
|
23
|
+
throw new Error('[Zero-Trust SQLite Guard] JsonlDatabaseFallbackWrapper strictly requires { readOnly: true }');
|
|
24
|
+
}
|
|
25
|
+
this.dbPath = dbPath;
|
|
26
|
+
this.readOnly = true;
|
|
27
|
+
this.isOpen = true;
|
|
28
|
+
this.records = [];
|
|
29
|
+
this._loadRecords(options);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
_loadRecords(options = {}) {
|
|
33
|
+
if (options.records && Array.isArray(options.records)) {
|
|
34
|
+
this.records = [...options.records];
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (!this.dbPath || typeof this.dbPath !== 'string') return;
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const targetPath = path.resolve(this.dbPath);
|
|
41
|
+
if (fs.existsSync(targetPath)) {
|
|
42
|
+
const stat = fs.statSync(targetPath);
|
|
43
|
+
if (stat.isFile() && targetPath.endsWith('.jsonl')) {
|
|
44
|
+
this._parseJsonlFile(targetPath);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const sessionDir = stat.isDirectory() ? targetPath : path.dirname(targetPath);
|
|
49
|
+
const cand1 = path.join(sessionDir, '.system_generated', 'logs', 'transcript_full.jsonl');
|
|
50
|
+
if (fs.existsSync(cand1)) {
|
|
51
|
+
this._parseJsonlFile(cand1);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const cand2 = path.join(sessionDir, '.system_generated', 'logs', 'transcript.jsonl');
|
|
55
|
+
if (fs.existsSync(cand2)) {
|
|
56
|
+
this._parseJsonlFile(cand2);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const cand3 = path.join(sessionDir, 'transcript.jsonl');
|
|
60
|
+
if (fs.existsSync(cand3)) {
|
|
61
|
+
this._parseJsonlFile(cand3);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
} catch {
|
|
66
|
+
// Graceful fail-open on path lookup
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
_parseJsonlFile(filePath) {
|
|
71
|
+
try {
|
|
72
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
73
|
+
const lines = content.split(/\r?\n/);
|
|
74
|
+
for (const line of lines) {
|
|
75
|
+
const trimmed = line.trim();
|
|
76
|
+
if (!trimmed) continue;
|
|
77
|
+
try {
|
|
78
|
+
const parsed = JSON.parse(trimmed);
|
|
79
|
+
if (parsed !== null && parsed !== undefined) this.records.push(parsed);
|
|
80
|
+
} catch {
|
|
81
|
+
// Ignore malformed individual JSONL line
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
} catch {
|
|
85
|
+
// File read error caught gracefully
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
_assertNotMutating(sql) {
|
|
90
|
+
if (MUTATION_SQL_RE.test(sql)) {
|
|
91
|
+
throw new Error(`[Zero-Trust SQLite Guard] Mutation query not permitted on read-only database: ${sql}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
get(sql, params = []) {
|
|
96
|
+
const rows = this.all(sql, params);
|
|
97
|
+
return rows.length > 0 ? rows[0] : null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
all(sql, params = []) {
|
|
101
|
+
if (!this.isOpen) throw new Error('Database handle is closed');
|
|
102
|
+
this._assertNotMutating(sql);
|
|
103
|
+
|
|
104
|
+
// Handle SELECT COUNT(*)
|
|
105
|
+
if (/SELECT\s+COUNT\(\*\)\s+(?:AS\s+(\w+))?/i.test(sql)) {
|
|
106
|
+
const match = sql.match(/SELECT\s+COUNT\(\*\)\s+(?:AS\s+(\w+))?/i);
|
|
107
|
+
const alias = match ? match[1] || 'cnt' : 'cnt';
|
|
108
|
+
return [{ [alias]: this.records.length }];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let filtered = this.records;
|
|
112
|
+
|
|
113
|
+
// Handle WHERE column = ? or literal
|
|
114
|
+
const whereMatch = sql.match(/WHERE\s+([a-zA-Z0-9_]+)\s*=\s*(?:\?|'([^']*)'|(\d+))/i);
|
|
115
|
+
if (whereMatch) {
|
|
116
|
+
const col = whereMatch[1];
|
|
117
|
+
const expectedVal = whereMatch[2] !== undefined
|
|
118
|
+
? whereMatch[2]
|
|
119
|
+
: whereMatch[3] !== undefined
|
|
120
|
+
? Number(whereMatch[3])
|
|
121
|
+
: (Array.isArray(params) ? params[0] : params);
|
|
122
|
+
|
|
123
|
+
filtered = filtered.filter(r => r && (r[col] === expectedVal || String(r[col]) === String(expectedVal)));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Handle LIMIT N
|
|
127
|
+
const limitMatch = sql.match(/LIMIT\s+(\d+)/i);
|
|
128
|
+
if (limitMatch) {
|
|
129
|
+
const lim = parseInt(limitMatch[1], 10);
|
|
130
|
+
filtered = filtered.slice(0, lim);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return filtered;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
searchFts(query, limit = 10) {
|
|
137
|
+
if (!this.isOpen) throw new Error('Database handle is closed');
|
|
138
|
+
if (!query || typeof query !== 'string') return [];
|
|
139
|
+
|
|
140
|
+
const terms = query.toLowerCase().split(/\s+/).filter(Boolean);
|
|
141
|
+
if (terms.length === 0) return [];
|
|
142
|
+
|
|
143
|
+
const results = [];
|
|
144
|
+
for (const r of this.records) {
|
|
145
|
+
if (!r) continue;
|
|
146
|
+
const text = typeof r === 'string'
|
|
147
|
+
? r
|
|
148
|
+
: Object.values(r).map(v => (typeof v === 'object' && v !== null ? JSON.stringify(v) : String(v ?? ''))).join(' ');
|
|
149
|
+
const lower = text.toLowerCase();
|
|
150
|
+
let score = 0;
|
|
151
|
+
let firstIdx = -1;
|
|
152
|
+
|
|
153
|
+
for (const term of terms) {
|
|
154
|
+
let idx = 0;
|
|
155
|
+
let count = 0;
|
|
156
|
+
while ((idx = lower.indexOf(term, idx)) !== -1) {
|
|
157
|
+
count++;
|
|
158
|
+
if (firstIdx === -1 || idx < firstIdx) firstIdx = idx;
|
|
159
|
+
idx += term.length;
|
|
160
|
+
}
|
|
161
|
+
if (count > 0) {
|
|
162
|
+
score += count * (term.length / Math.max(1, lower.length) + 1);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (score > 0) {
|
|
167
|
+
const start = Math.max(0, firstIdx - 20);
|
|
168
|
+
const end = Math.min(text.length, firstIdx + 60);
|
|
169
|
+
const snippet = (start > 0 ? '...' : '') + text.slice(start, end) + (end < text.length ? '...' : '');
|
|
170
|
+
results.push({ ...r, snippet, score });
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
results.sort((a, b) => b.score - a.score);
|
|
175
|
+
return results.slice(0, limit);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
close() {
|
|
179
|
+
this.isOpen = false;
|
|
180
|
+
this.records = [];
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export default JsonlDatabaseFallbackWrapper;
|