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,705 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { performance } from 'node:perf_hooks';
|
|
6
|
+
import { suppressSqliteExperimentalWarning } from '../core/paths.mjs';
|
|
7
|
+
import { openLocalHorizonDb } from '../core/sqlite_client.mjs';
|
|
8
|
+
import { sanitizeForensicPayload } from '../core/sanitizer.mjs';
|
|
9
|
+
import { SavedQuotaReceiptLedger, SAVINGS_EVENT_TYPES } from '../core/coach/saved_quota_ledger.mjs';
|
|
10
|
+
import { compressCliOutputPure, redactSecretsPure } from '../core/shared/index.mjs';
|
|
11
|
+
|
|
12
|
+
suppressSqliteExperimentalWarning();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Phase 3.2 / Ring 1 Hardening: CLI Output Compressor with Critical Error-Line Recovery
|
|
16
|
+
* (`src/shims/agy_cli_compressor.mjs`)
|
|
17
|
+
*
|
|
18
|
+
* - Pre-Threshold ANSI & Spinner Stripping: Strips ANSI CSI/OSC escape codes and collapses
|
|
19
|
+
* carriage-return `\r` overwrites before threshold check and FTS5 SQLite storage.
|
|
20
|
+
* - Expands Command Matchers: Dedicated noise profiles for `cargo` (`cargo test|check|clippy`)
|
|
21
|
+
* and `npm` / `pnpm` / `yarn` (`install|ci|add`).
|
|
22
|
+
* - Intercepts noisy CLI stdout (`> 4096` chars).
|
|
23
|
+
* - Archives sanitized, full unredacted output into `.agents/cache/cli_logs.sqlite` (SQLite FTS5).
|
|
24
|
+
* - Critical Error-Line Recovery Backstop: Diffs the compressed summary against raw stdout.
|
|
25
|
+
* Any line matching `/(Error|Exception|FAIL|error\[E\d+\]|panic:|ERR!|ERESOLVE|AssertionError|SyntaxError)/i`
|
|
26
|
+
* omitted by summarization is forcibly recovered and appended under:
|
|
27
|
+
* `[horizon-cli-shim: N critical error line(s) preserved]`
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
export const CLI_SHIM_CONSTANTS = Object.freeze({
|
|
31
|
+
MIN_CHARS_TO_COMPRESS: 4096,
|
|
32
|
+
TARGET_CLI_REGEX:
|
|
33
|
+
/\b(pytest|npm\s+(?:test|run\s+test|install|add|i|ci|build|update)|pnpm\s+(?:test|run\s+test|install|add|i|ci|build|update)|yarn(?:\s+(?:test|run\s+test|install|add|build|check))?|cargo\s+(?:test|check|clippy|build|run)|tsc|git\s+log|jest|vitest|node\s+--test)\b/i,
|
|
34
|
+
CRITICAL_ERROR_LINE_REGEX:
|
|
35
|
+
/(Error|Exception|FAIL|error\[E\d+\]|panic:|ERR!|ERESOLVE|AssertionError|SyntaxError)/i
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export function getDefaultCliLogsSqlitePath(workspaceDir = process.cwd()) {
|
|
39
|
+
return path.join(workspaceDir, '.agents', 'cache', 'cli_logs.sqlite');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function isTargetNoisyCliCommand(command = '') {
|
|
43
|
+
return CLI_SHIM_CONSTANTS.TARGET_CLI_REGEX.test(String(command || ''));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Strips all ANSI CSI/OSC escape codes and collapses `\r` carriage-return progress-bar overwrites.
|
|
48
|
+
*/
|
|
49
|
+
export function stripAnsiAndCarriageReturns(rawText = '') {
|
|
50
|
+
if (!rawText) return '';
|
|
51
|
+
// 1. Strip ANSI CSI/OSC and color/control escape sequences
|
|
52
|
+
const strippedAnsi = String(rawText).replace(
|
|
53
|
+
/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]|\].*?(?:\x07|\x1B\\))/g,
|
|
54
|
+
''
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
// 2. Collapse \r carriage-return progress-bar overwrites per line
|
|
58
|
+
const lines = strippedAnsi.split(/\r?\n/);
|
|
59
|
+
const collapsedLines = lines.map((line) => {
|
|
60
|
+
if (!line.includes('\r')) return line;
|
|
61
|
+
const parts = line.split('\r').filter((p) => p.length > 0);
|
|
62
|
+
return parts.length > 0 ? parts[parts.length - 1] : '';
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return collapsedLines.join('\n');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Builds the compact ~115-token structural summary (header, head/tail framing, and FTS5 BM25 pointer).
|
|
70
|
+
*/
|
|
71
|
+
export function buildCompact115TokenSummary(rawStdout = '', {
|
|
72
|
+
command = 'npm test',
|
|
73
|
+
exitCode = 1,
|
|
74
|
+
logId = 'log-1',
|
|
75
|
+
dbPath = '.agents/cache/cli_logs.sqlite'
|
|
76
|
+
} = {}) {
|
|
77
|
+
const lines = String(rawStdout || '').split(/\r?\n/);
|
|
78
|
+
const headLines = lines.slice(0, 3).map((l) => l.trim()).filter(Boolean);
|
|
79
|
+
const tailLines = lines.slice(-3).map((l) => l.trim()).filter(Boolean);
|
|
80
|
+
|
|
81
|
+
const summaryLines = [
|
|
82
|
+
`[horizon-cli-compressor] cmd="${command}" exit=${exitCode} chars=${rawStdout.length} lines=${lines.length}`,
|
|
83
|
+
`[archive] ${dbPath} (id="${logId}" • FTS5 BM25 query: SELECT snippet(cli_logs, 4, '[', ']', '...', 16) FROM cli_logs WHERE cli_logs MATCH '<term>' ORDER BY bm25(cli_logs))`,
|
|
84
|
+
`--- Head (${headLines.length} lines) ---`,
|
|
85
|
+
...headLines.map((l) => ` ${l.slice(0, 120)}`),
|
|
86
|
+
`--- Tail (${tailLines.length} lines) ---`,
|
|
87
|
+
...tailLines.map((l) => ` ${l.slice(0, 120)}`)
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
return summaryLines.join('\n');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Specialized noise profile for cargo commands (cargo test, cargo check, cargo clippy).
|
|
95
|
+
* Collapses Downloading, Compiling, Checking, and passing tests while retaining error[E\d+],
|
|
96
|
+
* failure sections, file/line spans, and panic traces.
|
|
97
|
+
*/
|
|
98
|
+
export function compressCargoOutput(cleanStdout = '', {
|
|
99
|
+
command = 'cargo test',
|
|
100
|
+
exitCode = 0,
|
|
101
|
+
logId = 'log-1',
|
|
102
|
+
dbPath = '.agents/cache/cli_logs.sqlite'
|
|
103
|
+
} = {}) {
|
|
104
|
+
const lines = String(cleanStdout || '').split(/\r?\n/);
|
|
105
|
+
let compilingCount = 0;
|
|
106
|
+
let downloadingCount = 0;
|
|
107
|
+
let checkingCount = 0;
|
|
108
|
+
let passedTestsCount = 0;
|
|
109
|
+
const diagnosticLines = [];
|
|
110
|
+
let inFailuresSection = false;
|
|
111
|
+
|
|
112
|
+
for (let i = 0; i < lines.length; i++) {
|
|
113
|
+
const line = lines[i];
|
|
114
|
+
const trimmed = line.trim();
|
|
115
|
+
|
|
116
|
+
if (/^\s*(Downloading|Downloaded)\s+/i.test(trimmed)) {
|
|
117
|
+
downloadingCount++;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (/^\s*Compiling\s+/i.test(trimmed)) {
|
|
121
|
+
compilingCount++;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (/^\s*Checking\s+/i.test(trimmed)) {
|
|
125
|
+
checkingCount++;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (/^test\s+.*?\.\.\.\s+ok$/i.test(trimmed)) {
|
|
129
|
+
passedTestsCount++;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (/^failures:$/i.test(trimmed)) {
|
|
134
|
+
inFailuresSection = true;
|
|
135
|
+
diagnosticLines.push(line);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (inFailuresSection) {
|
|
140
|
+
if (/^test result:/i.test(trimmed)) {
|
|
141
|
+
inFailuresSection = false;
|
|
142
|
+
diagnosticLines.push(line);
|
|
143
|
+
} else {
|
|
144
|
+
diagnosticLines.push(line);
|
|
145
|
+
}
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (
|
|
150
|
+
/error\[E\d+\]:/i.test(trimmed) ||
|
|
151
|
+
/-->\s+\S+:\d+:\d+/i.test(trimmed) ||
|
|
152
|
+
/panicked at/i.test(trimmed) ||
|
|
153
|
+
/^test result:/i.test(trimmed)
|
|
154
|
+
) {
|
|
155
|
+
diagnosticLines.push(line);
|
|
156
|
+
let nextIdx = i + 1;
|
|
157
|
+
while (nextIdx < lines.length && /^\s{4,}/.test(lines[nextIdx]) && nextIdx <= i + 3) {
|
|
158
|
+
diagnosticLines.push(lines[nextIdx]);
|
|
159
|
+
nextIdx++;
|
|
160
|
+
}
|
|
161
|
+
i = nextIdx - 1;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const summaryLines = [
|
|
166
|
+
`[horizon-cli-compressor:cargo] cmd="${command}" exit=${exitCode} compiled=${compilingCount} downloaded=${downloadingCount} passed_tests=${passedTestsCount}`,
|
|
167
|
+
`[archive] ${dbPath} (id="${logId}" • FTS5 BM25 query: SELECT snippet(cli_logs, 4, '[', ']', '...', 16) FROM cli_logs WHERE cli_logs MATCH '<term>' ORDER BY bm25(cli_logs))`,
|
|
168
|
+
...(diagnosticLines.length > 0
|
|
169
|
+
? [`--- Diagnostics (${diagnosticLines.length} lines) ---`, ...diagnosticLines.slice(0, 40)]
|
|
170
|
+
: ['--- No compiler/test failures recorded ---'])
|
|
171
|
+
];
|
|
172
|
+
|
|
173
|
+
return summaryLines.join('\n');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Specialized noise profile for npm / pnpm / yarn package install commands.
|
|
178
|
+
* Strips progress spinners, funding notices, and verbose dependency trees while retaining
|
|
179
|
+
* the exit summary and ERR! / ERESOLVE conflict blocks.
|
|
180
|
+
*/
|
|
181
|
+
export function compressNpmInstallOutput(cleanStdout = '', {
|
|
182
|
+
command = 'npm install',
|
|
183
|
+
exitCode = 0,
|
|
184
|
+
logId = 'log-1',
|
|
185
|
+
dbPath = '.agents/cache/cli_logs.sqlite'
|
|
186
|
+
} = {}) {
|
|
187
|
+
const lines = String(cleanStdout || '').split(/\r?\n/);
|
|
188
|
+
const preservedLines = [];
|
|
189
|
+
let packageSummary = '';
|
|
190
|
+
|
|
191
|
+
for (const line of lines) {
|
|
192
|
+
const trimmed = line.trim();
|
|
193
|
+
if (/^[⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏]/i.test(trimmed)) continue;
|
|
194
|
+
if (/\bpackages? (?:are )?looking for funding\b/i.test(trimmed)) continue;
|
|
195
|
+
if (/\brun `npm fund`\b/i.test(trimmed)) continue;
|
|
196
|
+
if (/\brun `npm audit`\b/i.test(trimmed)) continue;
|
|
197
|
+
if (/^[+`|\\├└]--/.test(trimmed)) continue;
|
|
198
|
+
|
|
199
|
+
if (
|
|
200
|
+
/\b(?:added|removed|changed|audited)\s+\d+\s+packages?\b/i.test(trimmed) ||
|
|
201
|
+
/^up to date\b/i.test(trimmed)
|
|
202
|
+
) {
|
|
203
|
+
packageSummary = trimmed;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (
|
|
208
|
+
/\b(?:npm\s+ERR!|ERR!|ERESOLVE|warn\s+deprecated|error\[E\d+\])\b/i.test(trimmed) ||
|
|
209
|
+
CLI_SHIM_CONSTANTS.CRITICAL_ERROR_LINE_REGEX.test(trimmed)
|
|
210
|
+
) {
|
|
211
|
+
preservedLines.push(line);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const summaryLines = [
|
|
216
|
+
`[horizon-cli-compressor:npm] cmd="${command}" exit=${exitCode} summary="${packageSummary || 'install completed'}"`,
|
|
217
|
+
`[archive] ${dbPath} (id="${logId}" • FTS5 BM25 query: SELECT snippet(cli_logs, 4, '[', ']', '...', 16) FROM cli_logs WHERE cli_logs MATCH '<term>' ORDER BY bm25(cli_logs))`,
|
|
218
|
+
...(preservedLines.length > 0
|
|
219
|
+
? [`--- Retained Messages (${preservedLines.length} lines) ---`, ...preservedLines.slice(0, 30)]
|
|
220
|
+
: [])
|
|
221
|
+
];
|
|
222
|
+
|
|
223
|
+
return summaryLines.join('\n');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Diffs a compressed summary against clean stdout and recovers 100% of omitted lines matching
|
|
228
|
+
* `/(Error|Exception|FAIL|error\[E\d+\]|panic:|ERR!|ERESOLVE|AssertionError|SyntaxError)/i`.
|
|
229
|
+
*/
|
|
230
|
+
export function recoverOmittedCriticalErrorLines(cleanStdout = '', compressedSummary = '') {
|
|
231
|
+
const rawLines = String(cleanStdout || '').split(/\r?\n/);
|
|
232
|
+
const summaryText = String(compressedSummary || '');
|
|
233
|
+
const omittedCriticalLines = [];
|
|
234
|
+
const seen = new Set();
|
|
235
|
+
|
|
236
|
+
for (const rawLine of rawLines) {
|
|
237
|
+
const trimmed = rawLine.trim();
|
|
238
|
+
if (!trimmed) continue;
|
|
239
|
+
if (!CLI_SHIM_CONSTANTS.CRITICAL_ERROR_LINE_REGEX.test(trimmed)) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (!summaryText.includes(trimmed) && !seen.has(trimmed)) {
|
|
244
|
+
seen.add(trimmed);
|
|
245
|
+
omittedCriticalLines.push(trimmed);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (omittedCriticalLines.length === 0) {
|
|
250
|
+
return {
|
|
251
|
+
output: summaryText,
|
|
252
|
+
recoveredCount: 0,
|
|
253
|
+
recoveredLines: []
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const recoveryBlock = [
|
|
258
|
+
`[horizon-cli-shim: ${omittedCriticalLines.length} critical error line(s) preserved]`,
|
|
259
|
+
...omittedCriticalLines
|
|
260
|
+
].join('\n');
|
|
261
|
+
|
|
262
|
+
return {
|
|
263
|
+
output: `${summaryText}\n${recoveryBlock}`,
|
|
264
|
+
recoveredCount: omittedCriticalLines.length,
|
|
265
|
+
recoveredLines: omittedCriticalLines
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Compresses noisy CLI output (`> 4096` chars), indexes the unredacted stdout into
|
|
271
|
+
* `.agents/cache/cli_logs.sqlite` using FTS5 BM25, and applies the Critical Error-Line Recovery Backstop.
|
|
272
|
+
*/
|
|
273
|
+
export async function compressCliOutputWithRecovery({
|
|
274
|
+
command = 'pytest',
|
|
275
|
+
rawStdout = '',
|
|
276
|
+
stdout = undefined,
|
|
277
|
+
exitCode = 1,
|
|
278
|
+
sessionId = 'default-session',
|
|
279
|
+
turnId = 'turn-1',
|
|
280
|
+
workspaceDir = process.cwd(),
|
|
281
|
+
dbPath = null,
|
|
282
|
+
DatabaseSync = null,
|
|
283
|
+
summaryOverride = null,
|
|
284
|
+
forceCompress = false
|
|
285
|
+
} = {}) {
|
|
286
|
+
const startMs = performance.now();
|
|
287
|
+
const rawSource = String(rawStdout || stdout || '');
|
|
288
|
+
|
|
289
|
+
// 1. Pre-Threshold ANSI & Spinner Stripping
|
|
290
|
+
const cleanText = stripAnsiAndCarriageReturns(rawSource);
|
|
291
|
+
const charCount = cleanText.length;
|
|
292
|
+
const rawTokens = Math.ceil(charCount / 4);
|
|
293
|
+
|
|
294
|
+
const shouldCompress =
|
|
295
|
+
forceCompress ||
|
|
296
|
+
(charCount > CLI_SHIM_CONSTANTS.MIN_CHARS_TO_COMPRESS && isTargetNoisyCliCommand(command));
|
|
297
|
+
|
|
298
|
+
if (!shouldCompress) {
|
|
299
|
+
return {
|
|
300
|
+
compressed: false,
|
|
301
|
+
charCount,
|
|
302
|
+
rawTokens,
|
|
303
|
+
compressedTokens: rawTokens,
|
|
304
|
+
tokensSaved: 0,
|
|
305
|
+
recoveredCount: 0,
|
|
306
|
+
recoveredLines: [],
|
|
307
|
+
output: cleanText,
|
|
308
|
+
dbPath: null,
|
|
309
|
+
elapsedMs: Number((performance.now() - startMs).toFixed(3))
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const resolvedDbPath = dbPath || getDefaultCliLogsSqlitePath(workspaceDir);
|
|
314
|
+
const logId = `${sessionId}:${turnId}:${Date.now()}`;
|
|
315
|
+
let archivedInSqlite = false;
|
|
316
|
+
|
|
317
|
+
// 2. Archive sanitized clean output into SQLite FTS5 (WAL + busy_timeout=5000)
|
|
318
|
+
try {
|
|
319
|
+
const db = openLocalHorizonDb(resolvedDbPath, { readOnly: false, DatabaseSync });
|
|
320
|
+
try {
|
|
321
|
+
db.exec(`
|
|
322
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS cli_logs USING fts5(
|
|
323
|
+
log_id,
|
|
324
|
+
session_id,
|
|
325
|
+
turn_id,
|
|
326
|
+
command,
|
|
327
|
+
raw_output,
|
|
328
|
+
created_at
|
|
329
|
+
);
|
|
330
|
+
`);
|
|
331
|
+
db.prepare(`
|
|
332
|
+
INSERT INTO cli_logs (log_id, session_id, turn_id, command, raw_output, created_at)
|
|
333
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
334
|
+
`).run(
|
|
335
|
+
String(logId),
|
|
336
|
+
String(sessionId),
|
|
337
|
+
String(turnId),
|
|
338
|
+
String(command),
|
|
339
|
+
cleanText,
|
|
340
|
+
new Date().toISOString()
|
|
341
|
+
);
|
|
342
|
+
archivedInSqlite = true;
|
|
343
|
+
} finally {
|
|
344
|
+
db.close();
|
|
345
|
+
}
|
|
346
|
+
} catch {
|
|
347
|
+
archivedInSqlite = false;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// 3. Choose appropriate compressor profile
|
|
351
|
+
let baseSummary = '';
|
|
352
|
+
if (summaryOverride !== null) {
|
|
353
|
+
baseSummary = String(summaryOverride);
|
|
354
|
+
} else if (/cargo\s+(?:test|check|clippy|build|run)/i.test(command)) {
|
|
355
|
+
baseSummary = compressCargoOutput(cleanText, {
|
|
356
|
+
command,
|
|
357
|
+
exitCode,
|
|
358
|
+
logId,
|
|
359
|
+
dbPath: resolvedDbPath
|
|
360
|
+
});
|
|
361
|
+
} else if (/(?:npm|pnpm|yarn)\s+(?:install|add|i|ci|build|update)/i.test(command)) {
|
|
362
|
+
baseSummary = compressNpmInstallOutput(cleanText, {
|
|
363
|
+
command,
|
|
364
|
+
exitCode,
|
|
365
|
+
logId,
|
|
366
|
+
dbPath: resolvedDbPath
|
|
367
|
+
});
|
|
368
|
+
} else {
|
|
369
|
+
baseSummary = buildCompact115TokenSummary(cleanText, {
|
|
370
|
+
command,
|
|
371
|
+
exitCode,
|
|
372
|
+
logId,
|
|
373
|
+
dbPath: resolvedDbPath
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// 4. Critical Error-Line Recovery Backstop
|
|
378
|
+
const recovery = recoverOmittedCriticalErrorLines(cleanText, baseSummary);
|
|
379
|
+
const compressedTokens = Math.ceil(recovery.output.length / 4);
|
|
380
|
+
const tokensSaved = Math.max(0, rawTokens - compressedTokens);
|
|
381
|
+
|
|
382
|
+
return {
|
|
383
|
+
compressed: true,
|
|
384
|
+
archivedInSqlite,
|
|
385
|
+
logId,
|
|
386
|
+
dbPath: resolvedDbPath,
|
|
387
|
+
charCount,
|
|
388
|
+
rawTokens,
|
|
389
|
+
compressedTokens,
|
|
390
|
+
tokensSaved,
|
|
391
|
+
recoveredCount: recovery.recoveredCount,
|
|
392
|
+
recoveredLines: recovery.recoveredLines,
|
|
393
|
+
output: recovery.output,
|
|
394
|
+
elapsedMs: Number((performance.now() - startMs).toFixed(3))
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Queries `.agents/cache/cli_logs.sqlite` using SQLite FTS5 BM25 ranking.
|
|
400
|
+
*/
|
|
401
|
+
export async function searchCliLogsBm25(queryText, {
|
|
402
|
+
workspaceDir = process.cwd(),
|
|
403
|
+
dbPath = null,
|
|
404
|
+
limit = 5,
|
|
405
|
+
DatabaseSync = null
|
|
406
|
+
} = {}) {
|
|
407
|
+
const resolvedDbPath = dbPath || getDefaultCliLogsSqlitePath(workspaceDir);
|
|
408
|
+
if (!fs.existsSync(resolvedDbPath)) return [];
|
|
409
|
+
|
|
410
|
+
const db = openLocalHorizonDb(resolvedDbPath, { readOnly: true, DatabaseSync });
|
|
411
|
+
try {
|
|
412
|
+
const safeQuery = String(queryText || '').replace(/"/g, '""').trim();
|
|
413
|
+
if (!safeQuery) return [];
|
|
414
|
+
const stmt = db.prepare(`
|
|
415
|
+
SELECT
|
|
416
|
+
log_id AS logId,
|
|
417
|
+
session_id AS sessionId,
|
|
418
|
+
turn_id AS turnId,
|
|
419
|
+
command,
|
|
420
|
+
bm25(cli_logs) AS bm25Score,
|
|
421
|
+
snippet(cli_logs, 4, '[', ']', '...', 24) AS snippet
|
|
422
|
+
FROM cli_logs
|
|
423
|
+
WHERE cli_logs MATCH ?
|
|
424
|
+
ORDER BY bm25(cli_logs)
|
|
425
|
+
LIMIT ?
|
|
426
|
+
`);
|
|
427
|
+
return stmt.all(`"${safeQuery}"`, Number(limit || 5));
|
|
428
|
+
} finally {
|
|
429
|
+
db.close();
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
export const AH_CLI_ARCHIVE_MAX_BYTES = 5 * 1024 * 1024; // 5 MB ring cap
|
|
434
|
+
|
|
435
|
+
export function getDefaultAhCliArchiveSqlitePath(workspaceDir = process.cwd()) {
|
|
436
|
+
return path.join(workspaceDir, '.agents', 'cache', 'ah_cli_archive.sqlite');
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Rank 3 (DET-07): Archives sanitized >4,096-char CLI output into a WAL-enabled,
|
|
441
|
+
* 5 MB ring-capped SQLite FTS5 store (`.agents/cache/ah_cli_archive.sqlite`),
|
|
442
|
+
* preserves 30 head + 45 tail + 100% of error lines, appends BM25 query hint,
|
|
443
|
+
* and records exact byte-verified token savings in `SavedQuotaReceiptLedger`.
|
|
444
|
+
*/
|
|
445
|
+
export function archiveAndCompressDet07Output(rawOutput = '', {
|
|
446
|
+
command = '',
|
|
447
|
+
exitCode = 0,
|
|
448
|
+
sessionId = 'default-session',
|
|
449
|
+
workspaceDir = process.cwd(),
|
|
450
|
+
archiveDbPath = null,
|
|
451
|
+
ledgerDbPath = null,
|
|
452
|
+
maxArchiveBytes = AH_CLI_ARCHIVE_MAX_BYTES
|
|
453
|
+
} = {}) {
|
|
454
|
+
const rawStr = String(rawOutput ?? '');
|
|
455
|
+
const comp = compressCliOutputPure(rawStr, {
|
|
456
|
+
headCount: 30,
|
|
457
|
+
tailCount: 45,
|
|
458
|
+
archiveThresholdChars: CLI_SHIM_CONSTANTS.MIN_CHARS_TO_COMPRESS,
|
|
459
|
+
queryHint: 'node .agents/shims/ah_log_query.mjs "<term>"'
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
if (rawStr.length <= CLI_SHIM_CONSTANTS.MIN_CHARS_TO_COMPRESS) {
|
|
463
|
+
return {
|
|
464
|
+
detectorId: 'DET-07',
|
|
465
|
+
compressed: comp.compressed,
|
|
466
|
+
archived: false,
|
|
467
|
+
exitCode: Number(exitCode ?? 0),
|
|
468
|
+
output: comp.output,
|
|
469
|
+
rawBytes: Buffer.byteLength(rawStr, 'utf8'),
|
|
470
|
+
compressedBytes: Buffer.byteLength(comp.output, 'utf8'),
|
|
471
|
+
bytesSaved: 0,
|
|
472
|
+
tokensSaved: 0,
|
|
473
|
+
archiveDbPath: null
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Strip ANSI escape codes + scrub credentials via sanitizer.mjs + shared pure secret scrubber
|
|
478
|
+
const ansiClean = stripAnsiAndCarriageReturns(rawStr);
|
|
479
|
+
const { sanitized: forensicClean } = sanitizeForensicPayload(ansiClean);
|
|
480
|
+
const { sanitized: scrubbedText, redactedCount } = redactSecretsPure(forensicClean);
|
|
481
|
+
|
|
482
|
+
const resolvedArchiveDb = archiveDbPath || getDefaultAhCliArchiveSqlitePath(workspaceDir);
|
|
483
|
+
const capBytes = Math.max(65536, Number(maxArchiveBytes) || AH_CLI_ARCHIVE_MAX_BYTES);
|
|
484
|
+
const createdAt = new Date().toISOString();
|
|
485
|
+
const payloadBytes = Buffer.byteLength(scrubbedText, 'utf8');
|
|
486
|
+
let evictedRows = 0;
|
|
487
|
+
|
|
488
|
+
const db = openLocalHorizonDb(resolvedArchiveDb, { readOnly: false, busyTimeoutMs: 3000 });
|
|
489
|
+
try {
|
|
490
|
+
db.exec(`
|
|
491
|
+
CREATE TABLE IF NOT EXISTS ah_cli_meta (
|
|
492
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
493
|
+
session_id TEXT NOT NULL,
|
|
494
|
+
command TEXT NOT NULL,
|
|
495
|
+
exit_code INTEGER NOT NULL,
|
|
496
|
+
byte_len INTEGER NOT NULL,
|
|
497
|
+
created_at TEXT NOT NULL
|
|
498
|
+
);
|
|
499
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS ah_cli_fts USING fts5(
|
|
500
|
+
meta_id UNINDEXED,
|
|
501
|
+
command,
|
|
502
|
+
redacted_output
|
|
503
|
+
);
|
|
504
|
+
`);
|
|
505
|
+
|
|
506
|
+
// Clamp single-entry payload if a single giant dump exceeds 80% of the 5 MB ring cap
|
|
507
|
+
const maxSinglePayload = Math.floor(capBytes * 0.72);
|
|
508
|
+
const boundedText = payloadBytes > maxSinglePayload
|
|
509
|
+
? scrubbedText.slice(0, maxSinglePayload)
|
|
510
|
+
: scrubbedText;
|
|
511
|
+
const boundedBytes = Buffer.byteLength(boundedText, 'utf8');
|
|
512
|
+
|
|
513
|
+
// Evict oldest rows so cumulative payload + SQLite overhead stays strictly <= 5 MB
|
|
514
|
+
const targetPayloadBudget = Math.floor(capBytes * 0.78);
|
|
515
|
+
let totalRow = db.prepare('SELECT COALESCE(SUM(byte_len), 0) AS total FROM ah_cli_meta').get();
|
|
516
|
+
let currentTotal = Number(totalRow?.total || 0);
|
|
517
|
+
|
|
518
|
+
while (currentTotal + boundedBytes > targetPayloadBudget) {
|
|
519
|
+
const oldest = db.prepare('SELECT id, byte_len FROM ah_cli_meta ORDER BY id ASC LIMIT 5').all();
|
|
520
|
+
if (!oldest || oldest.length === 0) break;
|
|
521
|
+
const delMeta = db.prepare('DELETE FROM ah_cli_meta WHERE id = ?');
|
|
522
|
+
const delFts = db.prepare('DELETE FROM ah_cli_fts WHERE meta_id = ?');
|
|
523
|
+
for (const row of oldest) {
|
|
524
|
+
delFts.run(String(row.id));
|
|
525
|
+
delMeta.run(row.id);
|
|
526
|
+
currentTotal = Math.max(0, currentTotal - Number(row.byte_len || 0));
|
|
527
|
+
evictedRows += 1;
|
|
528
|
+
if (currentTotal + boundedBytes <= targetPayloadBudget) break;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
const insMeta = db.prepare(`
|
|
533
|
+
INSERT INTO ah_cli_meta (session_id, command, exit_code, byte_len, created_at)
|
|
534
|
+
VALUES (?, ?, ?, ?, ?)
|
|
535
|
+
`).run(String(sessionId), String(command), Number(exitCode ?? 0), boundedBytes, createdAt);
|
|
536
|
+
|
|
537
|
+
const metaId = String(insMeta.lastInsertRowid);
|
|
538
|
+
db.prepare(`
|
|
539
|
+
INSERT INTO ah_cli_fts (meta_id, command, redacted_output)
|
|
540
|
+
VALUES (?, ?, ?)
|
|
541
|
+
`).run(metaId, String(command), boundedText);
|
|
542
|
+
|
|
543
|
+
if (evictedRows > 0) {
|
|
544
|
+
db.exec("INSERT INTO ah_cli_fts(ah_cli_fts) VALUES('optimize');");
|
|
545
|
+
db.exec('PRAGMA wal_checkpoint(TRUNCATE);');
|
|
546
|
+
}
|
|
547
|
+
} finally {
|
|
548
|
+
db.close();
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// Enforce hard physical file ceiling <= capBytes via VACUUM if page freelist grew past cap
|
|
552
|
+
try {
|
|
553
|
+
if (fs.existsSync(resolvedArchiveDb) && fs.statSync(resolvedArchiveDb).size > capBytes) {
|
|
554
|
+
const vacDb = openLocalHorizonDb(resolvedArchiveDb, { readOnly: false, busyTimeoutMs: 3000 });
|
|
555
|
+
try {
|
|
556
|
+
vacDb.exec('PRAGMA wal_checkpoint(TRUNCATE);');
|
|
557
|
+
vacDb.exec('VACUUM;');
|
|
558
|
+
vacDb.exec('PRAGMA journal_mode = WAL;');
|
|
559
|
+
} finally {
|
|
560
|
+
vacDb.close();
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
} catch {}
|
|
564
|
+
|
|
565
|
+
const rawBytes = Buffer.byteLength(rawStr, 'utf8');
|
|
566
|
+
const compressedBytes = Buffer.byteLength(comp.output, 'utf8');
|
|
567
|
+
const bytesSaved = Math.max(0, rawBytes - compressedBytes);
|
|
568
|
+
const tokensSaved = Math.floor(bytesSaved / 4);
|
|
569
|
+
|
|
570
|
+
if (tokensSaved > 0) {
|
|
571
|
+
try {
|
|
572
|
+
const ledger = new SavedQuotaReceiptLedger({
|
|
573
|
+
...(ledgerDbPath ? { dbPath: ledgerDbPath } : {})
|
|
574
|
+
});
|
|
575
|
+
ledger.recordSaving({
|
|
576
|
+
sessionId,
|
|
577
|
+
eventType: SAVINGS_EVENT_TYPES.CLI_DUMP_COMPRESSED,
|
|
578
|
+
argonEquivTokensSaved: tokensSaved,
|
|
579
|
+
rawBytesSaved: bytesSaved,
|
|
580
|
+
detail: `DET-07 compressed "${String(command).slice(0, 60)}" (${bytesSaved} bytes saved)`
|
|
581
|
+
});
|
|
582
|
+
} catch {}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
return {
|
|
586
|
+
detectorId: 'DET-07',
|
|
587
|
+
compressed: true,
|
|
588
|
+
archived: true,
|
|
589
|
+
exitCode: Number(exitCode ?? 0),
|
|
590
|
+
output: comp.output,
|
|
591
|
+
rawBytes,
|
|
592
|
+
compressedBytes,
|
|
593
|
+
bytesSaved,
|
|
594
|
+
tokensSaved,
|
|
595
|
+
redactedCount,
|
|
596
|
+
evictedRows,
|
|
597
|
+
archiveDbPath: resolvedArchiveDb
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
export function queryAhCliArchiveBm25(searchTerm, {
|
|
602
|
+
workspaceDir = process.cwd(),
|
|
603
|
+
archiveDbPath = null,
|
|
604
|
+
limit = 8
|
|
605
|
+
} = {}) {
|
|
606
|
+
const resolvedDb = archiveDbPath || getDefaultAhCliArchiveSqlitePath(workspaceDir);
|
|
607
|
+
if (!fs.existsSync(resolvedDb)) return [];
|
|
608
|
+
const cleanTerm = String(searchTerm || '').replace(/"/g, () => '""').trim();
|
|
609
|
+
if (!cleanTerm) return [];
|
|
610
|
+
|
|
611
|
+
const db = openLocalHorizonDb(resolvedDb, { readOnly: true, busyTimeoutMs: 3000 });
|
|
612
|
+
try {
|
|
613
|
+
return db.prepare(`
|
|
614
|
+
SELECT
|
|
615
|
+
m.id AS id,
|
|
616
|
+
m.command AS command,
|
|
617
|
+
m.exit_code AS exitCode,
|
|
618
|
+
m.created_at AS createdAt,
|
|
619
|
+
bm25(ah_cli_fts) AS score,
|
|
620
|
+
snippet(ah_cli_fts, 2, '[', ']', '...', 24) AS snippet
|
|
621
|
+
FROM ah_cli_fts
|
|
622
|
+
JOIN ah_cli_meta m ON m.id = CAST(ah_cli_fts.meta_id AS INTEGER)
|
|
623
|
+
WHERE ah_cli_fts MATCH ?
|
|
624
|
+
ORDER BY bm25(ah_cli_fts)
|
|
625
|
+
LIMIT ?
|
|
626
|
+
`).all(`"${cleanTerm}"`, Math.max(1, Number(limit) || 8));
|
|
627
|
+
} finally {
|
|
628
|
+
db.close();
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Executes a real child command, captures stdout + stderr + exact exitCode,
|
|
634
|
+
* applies DET-07 compression + FTS5 archiving, and returns `{ exitCode, output, ... }`.
|
|
635
|
+
*/
|
|
636
|
+
export function executeAndCompressCommand(command, args = [], options = {}) {
|
|
637
|
+
const useShell = options.shell !== undefined ? Boolean(options.shell) : args.length === 0;
|
|
638
|
+
const child = spawnSync(command, args, {
|
|
639
|
+
cwd: options.cwd || process.cwd(),
|
|
640
|
+
env: options.env || process.env,
|
|
641
|
+
shell: useShell,
|
|
642
|
+
encoding: 'utf8',
|
|
643
|
+
maxBuffer: 32 * 1024 * 1024
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
const exactExitCode = typeof child.status === 'number'
|
|
647
|
+
? child.status
|
|
648
|
+
: (child.error ? 127 : 1);
|
|
649
|
+
const combinedRaw = [child.stdout || '', child.stderr || ''].filter(Boolean).join('\n');
|
|
650
|
+
const fullCmdLabel = args.length > 0 ? `${command} ${args.join(' ')}` : String(command);
|
|
651
|
+
|
|
652
|
+
const det07 = archiveAndCompressDet07Output(combinedRaw, {
|
|
653
|
+
command: fullCmdLabel,
|
|
654
|
+
exitCode: exactExitCode,
|
|
655
|
+
sessionId: options.sessionId || process.env.AGY_CONVERSATION_ID || 'default-session',
|
|
656
|
+
workspaceDir: options.workspaceDir || options.cwd || process.cwd(),
|
|
657
|
+
archiveDbPath: options.archiveDbPath || null,
|
|
658
|
+
ledgerDbPath: options.ledgerDbPath || null,
|
|
659
|
+
maxArchiveBytes: options.maxArchiveBytes || AH_CLI_ARCHIVE_MAX_BYTES
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
return {
|
|
663
|
+
...det07,
|
|
664
|
+
exitCode: exactExitCode
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function isShimCliMain(argv1 = process.argv[1]) {
|
|
669
|
+
if (!argv1) return false;
|
|
670
|
+
try {
|
|
671
|
+
return fs.realpathSync(argv1) === fs.realpathSync(fileURLToPath(import.meta.url));
|
|
672
|
+
} catch {
|
|
673
|
+
return path.resolve(argv1) === path.resolve(fileURLToPath(import.meta.url));
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
if (isShimCliMain()) {
|
|
678
|
+
const rawArgs = process.argv.slice(2);
|
|
679
|
+
const dashIdx = rawArgs.indexOf('--');
|
|
680
|
+
const cmdTokens = dashIdx >= 0 ? rawArgs.slice(dashIdx + 1) : rawArgs;
|
|
681
|
+
if (cmdTokens.length === 0) {
|
|
682
|
+
process.exit(0);
|
|
683
|
+
}
|
|
684
|
+
const [bin, ...rest] = cmdTokens;
|
|
685
|
+
const res = executeAndCompressCommand(bin, rest, { shell: rest.length === 0 });
|
|
686
|
+
if (res.output) {
|
|
687
|
+
process.stdout.write(res.output + (res.output.endsWith('\n') ? '' : '\n'));
|
|
688
|
+
}
|
|
689
|
+
process.exit(res.exitCode);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
export default {
|
|
693
|
+
CLI_SHIM_CONSTANTS,
|
|
694
|
+
AH_CLI_ARCHIVE_MAX_BYTES,
|
|
695
|
+
stripAnsiAndCarriageReturns,
|
|
696
|
+
compressCliOutputWithRecovery,
|
|
697
|
+
compressCargoOutput,
|
|
698
|
+
compressNpmInstallOutput,
|
|
699
|
+
recoverOmittedCriticalErrorLines,
|
|
700
|
+
buildCompact115TokenSummary,
|
|
701
|
+
searchCliLogsBm25,
|
|
702
|
+
archiveAndCompressDet07Output,
|
|
703
|
+
queryAhCliArchiveBm25,
|
|
704
|
+
executeAndCompressCommand
|
|
705
|
+
};
|