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.
Files changed (262) hide show
  1. package/.antigravity-plugin/plugin.json +36 -0
  2. package/.claude-plugin/plugin.json +18 -0
  3. package/CHECKSUMS.sha256 +166 -0
  4. package/LICENSE +27 -0
  5. package/README.md +350 -2
  6. package/SECURITY.md +22 -0
  7. package/agents/ah-flash-scout.md +25 -0
  8. package/assets/advanced_view_loaded_420px.png +0 -0
  9. package/assets/hero-demo.svg +188 -0
  10. package/assets/logo.svg +19 -0
  11. package/assets/simple_view_lean_420px.png +0 -0
  12. package/assets/simple_view_loaded_420px.png +0 -0
  13. package/assets/social-preview.svg +72 -0
  14. package/assets/zone_c_step3_shield_full.png +0 -0
  15. package/bin/ah-statusline.mjs +175 -0
  16. package/bin/cli.mjs +3 -0
  17. package/bin/ctxtop.mjs +1799 -0
  18. package/commands/cost-radar.md +11 -0
  19. package/commands/evolve.md +10 -0
  20. package/commands/handoff.md +10 -0
  21. package/commands/horizon.md +10 -0
  22. package/commands/inspect.md +10 -0
  23. package/commands/precheck.md +10 -0
  24. package/config/default_flightcheck.json +133 -0
  25. package/config/modelCatalog.json +785 -0
  26. package/config/platformQuirks.json +19 -0
  27. package/hooks/handlers/compact-lifecycle.mjs +577 -0
  28. package/hooks/handlers/config-change.mjs +402 -0
  29. package/hooks/handlers/hookIo.mjs +311 -0
  30. package/hooks/handlers/hookIpcClient.mjs +83 -0
  31. package/hooks/handlers/post-model-switch.mjs +111 -0
  32. package/hooks/handlers/post-tool-use.mjs +366 -0
  33. package/hooks/handlers/pre-model-switch.mjs +1048 -0
  34. package/hooks/handlers/pre-tool-use.mjs +632 -0
  35. package/hooks/handlers/stop-gate.mjs +526 -0
  36. package/hooks/handlers/user-prompt-submit.mjs +367 -0
  37. package/hooks/hooks.json +131 -0
  38. package/hooks/post_invocation.mjs +13 -0
  39. package/hooks/post_tool_use.mjs +25 -0
  40. package/hooks/pre_invocation.mjs +14 -0
  41. package/hooks/pre_tool_use.mjs +97 -0
  42. package/hooks/register.js +726 -0
  43. package/hooks/session_start.mjs +24 -0
  44. package/hooks/stop.mjs +23 -0
  45. package/hooks.json +56 -0
  46. package/package.json +110 -4
  47. package/packages/antigravity/package.json +14 -0
  48. package/packages/antigravity/src/index.mjs +1 -0
  49. package/packages/claude-code/package.json +14 -0
  50. package/packages/claude-code/src/index.mjs +2 -0
  51. package/packages/core/package.json +14 -0
  52. package/packages/core/src/index.mjs +1 -0
  53. package/packages/engine-node/package.json +14 -0
  54. package/packages/engine-node/src/index.mjs +1 -0
  55. package/packages/mcp/package.json +17 -0
  56. package/packages/mcp/src/server.mjs +396 -0
  57. package/packages/pro/LICENSE-COMMERCIAL.md +19 -0
  58. package/packages/pro/README-SIGNING.md +30 -0
  59. package/packages/pro/package.json +16 -0
  60. package/packages/pro/src/index.mjs +3 -0
  61. package/packages/pro/src/team_finops_pack.mjs +241 -0
  62. package/packages/pro/workers/polar_webhook_worker.mjs +215 -0
  63. package/plugin.json +36 -0
  64. package/rules/AGENTS.md +10 -0
  65. package/sidecars/horizon-daemon/ipcServer.mjs +24 -0
  66. package/sidecars/horizon-daemon/server.mjs +2038 -0
  67. package/sidecars/horizon-daemon/sidecar.json +19 -0
  68. package/skills/cost-radar/SKILL.md +21 -0
  69. package/skills/evolve/SKILL.md +21 -0
  70. package/skills/handoff/SKILL.md +50 -0
  71. package/skills/horizon/SKILL.md +17 -0
  72. package/skills/inspect/SKILL.md +18 -0
  73. package/skills/precheck/SKILL.md +44 -0
  74. package/src/adapters/antigravity/index.mjs +109 -0
  75. package/src/adapters/claude-code/index.mjs +194 -0
  76. package/src/cli/cost_radar.mjs +70 -0
  77. package/src/cli/doctor.mjs +351 -0
  78. package/src/cli/horizon_hud.mjs +888 -0
  79. package/src/cli/init.mjs +425 -0
  80. package/src/cli/postmortem.mjs +515 -0
  81. package/src/coach/cacheGravityRouter.mjs +12 -0
  82. package/src/config/modelCatalog.json +704 -0
  83. package/src/config/platformQuirks.json +19 -0
  84. package/src/core/action_queue.mjs +264 -0
  85. package/src/core/ag_bridge.mjs +346 -0
  86. package/src/core/atomic_io.mjs +124 -0
  87. package/src/core/baseline.mjs +607 -0
  88. package/src/core/blindspot_sensors.mjs +399 -0
  89. package/src/core/bucket_b_runtime.mjs +1200 -0
  90. package/src/core/classifier.mjs +1757 -0
  91. package/src/core/coach/circuit_breakers.mjs +650 -0
  92. package/src/core/coach/contradiction_auditor.mjs +239 -0
  93. package/src/core/coach/effort_advisor.mjs +143 -0
  94. package/src/core/coach/handoff_hydrator.mjs +483 -0
  95. package/src/core/coach/progressive_checkpointer.mjs +551 -0
  96. package/src/core/coach/prompt_decomposer_coach.mjs +141 -0
  97. package/src/core/coach/reflector_queue.mjs +505 -0
  98. package/src/core/coach/saved_quota_ledger.mjs +615 -0
  99. package/src/core/coach/subagent_ipi_sanitizer.mjs +200 -0
  100. package/src/core/coach/subagent_phantom_auditor.mjs +261 -0
  101. package/src/core/coach/task_quota_router.mjs +955 -0
  102. package/src/core/coach/turn_zero_gate.mjs +574 -0
  103. package/src/core/compaction_coach.mjs +1126 -0
  104. package/src/core/config.mjs +214 -0
  105. package/src/core/constants.mjs +1987 -0
  106. package/src/core/detectors_det01_to_16.mjs +1523 -0
  107. package/src/core/doc_indexer.mjs +338 -0
  108. package/src/core/episodic_memory.mjs +135 -0
  109. package/src/core/fts5_output_compressor.mjs +359 -0
  110. package/src/core/git_ledger.mjs +189 -0
  111. package/src/core/handoff_audit.mjs +558 -0
  112. package/src/core/history.mjs +221 -0
  113. package/src/core/hook_diagnostics.mjs +158 -0
  114. package/src/core/horizon_classifier.mjs +326 -0
  115. package/src/core/horizon_config.mjs +802 -0
  116. package/src/core/host_watchdog.mjs +200 -0
  117. package/src/core/layer5_hygiene.mjs +1084 -0
  118. package/src/core/log_resolver.mjs +231 -0
  119. package/src/core/mcp_auditor.mjs +138 -0
  120. package/src/core/micro_skills_store.mjs +91 -0
  121. package/src/core/offenders.mjs +161 -0
  122. package/src/core/parser.mjs +1997 -0
  123. package/src/core/path_canonicalizer.mjs +73 -0
  124. package/src/core/paths.mjs +311 -0
  125. package/src/core/prefix_cache_shield.mjs +386 -0
  126. package/src/core/pro_license.mjs +385 -0
  127. package/src/core/prompt_rubric.mjs +518 -0
  128. package/src/core/pruner.mjs +433 -0
  129. package/src/core/quota.mjs +1860 -0
  130. package/src/core/rsi_instinct_engine.mjs +1197 -0
  131. package/src/core/rsl_clash_auditor.mjs +423 -0
  132. package/src/core/rsl_distiller.mjs +372 -0
  133. package/src/core/rsl_ledger.mjs +969 -0
  134. package/src/core/rsl_triage.mjs +909 -0
  135. package/src/core/sanitizer.mjs +121 -0
  136. package/src/core/session_auditor.mjs +658 -0
  137. package/src/core/session_forensics.mjs +303 -0
  138. package/src/core/session_timeline.mjs +358 -0
  139. package/src/core/settings.mjs +99 -0
  140. package/src/core/shared/detectors.mjs +1342 -0
  141. package/src/core/shared/fnv1a64.mjs +85 -0
  142. package/src/core/shared/handoff_schema.mjs +123 -0
  143. package/src/core/shared/index.mjs +5 -0
  144. package/src/core/shared/ipi_sanitizer.mjs +165 -0
  145. package/src/core/shared/micro_skills.mjs +1 -0
  146. package/src/core/shared/plugin_validator.mjs +202 -0
  147. package/src/core/shared/prompt_hygiene_governor.mjs +1 -0
  148. package/src/core/shield_manager.mjs +1417 -0
  149. package/src/core/sidecar_store.mjs +302 -0
  150. package/src/core/sprint_metrics.mjs +366 -0
  151. package/src/core/sqlite_client.mjs +283 -0
  152. package/src/core/storage_doctor.mjs +358 -0
  153. package/src/core/subagents.mjs +609 -0
  154. package/src/core/swarm_topology.mjs +360 -0
  155. package/src/core/task_quota_router.mjs +1656 -0
  156. package/src/core/telemetry/cache_ttl_watchdog.mjs +284 -0
  157. package/src/core/telemetry/scope_lock_registry.mjs +578 -0
  158. package/src/core/telemetry/shadow_file_detector.mjs +291 -0
  159. package/src/core/telemetry/trajectory_watcher.mjs +259 -0
  160. package/src/core/timeline_svg.mjs +236 -0
  161. package/src/core/tool_surgeon.mjs +305 -0
  162. package/src/core/trajectory_stitcher.mjs +645 -0
  163. package/src/core/u_shaped_optimizer.mjs +789 -0
  164. package/src/core/wal_hygiene.mjs +233 -0
  165. package/src/core-isomorphic/catalog/detectorCatalog.mjs +558 -0
  166. package/src/core-isomorphic/catalog/index.mjs +1 -0
  167. package/src/core-isomorphic/coach/cacheGravityRouter.mjs +1547 -0
  168. package/src/core-isomorphic/coach/microSkills.mjs +1 -0
  169. package/src/core-isomorphic/coach/promptHygieneGovernor.mjs +1 -0
  170. package/src/core-isomorphic/coach/repoCalibration.mjs +254 -0
  171. package/src/core-isomorphic/coach/resumeToolsetAuditor.mjs +321 -0
  172. package/src/core-isomorphic/coach/savedQuotaLedger.mjs +527 -0
  173. package/src/core-isomorphic/coach/smartCompactAdvisor.mjs +305 -0
  174. package/src/core-isomorphic/coach/stateBatonHydrator.mjs +810 -0
  175. package/src/core-isomorphic/coach/ttlWatchdog.mjs +464 -0
  176. package/src/core-isomorphic/config/anthropicModelCatalog.json +606 -0
  177. package/src/core-isomorphic/config/catalogLoader.mjs +1268 -0
  178. package/src/core-isomorphic/config/horizonConfig.mjs +1055 -0
  179. package/src/core-isomorphic/control/callTree.mjs +338 -0
  180. package/src/core-isomorphic/control/coachLatches.mjs +126 -0
  181. package/src/core-isomorphic/control/controlStateMachine.mjs +205 -0
  182. package/src/core-isomorphic/control/index.mjs +49 -0
  183. package/src/core-isomorphic/control/swarmWorktree.mjs +109 -0
  184. package/src/core-isomorphic/control/toolSuspension.mjs +197 -0
  185. package/src/core-isomorphic/diagnostics/drift_guard.mjs +382 -0
  186. package/src/core-isomorphic/diagnostics/index.mjs +9 -0
  187. package/src/core-isomorphic/diagnostics/micro_skills.mjs +471 -0
  188. package/src/core-isomorphic/diagnostics/prompt_hygiene_governor.mjs +888 -0
  189. package/src/core-isomorphic/economics/billingBlocks.mjs +251 -0
  190. package/src/core-isomorphic/economics/cacheGravity.mjs +369 -0
  191. package/src/core-isomorphic/economics/cliffBreaker.mjs +256 -0
  192. package/src/core-isomorphic/economics/driftLatency.mjs +433 -0
  193. package/src/core-isomorphic/economics/dualTtl.mjs +198 -0
  194. package/src/core-isomorphic/economics/index.mjs +47 -0
  195. package/src/core-isomorphic/economics/retentionRadar.mjs +348 -0
  196. package/src/core-isomorphic/guards/bucketBAlphaBeta.mjs +860 -0
  197. package/src/core-isomorphic/guards/bucketBGamma.mjs +442 -0
  198. package/src/core-isomorphic/guards/bucketBHeuristics.mjs +37 -0
  199. package/src/core-isomorphic/guards/circuitBreakers.mjs +1202 -0
  200. package/src/core-isomorphic/guards/det01_03_shell_secret.mjs +1184 -0
  201. package/src/core-isomorphic/guards/det04_12_thrash_rot.mjs +572 -0
  202. package/src/core-isomorphic/guards/det13_20_mcp_scope.mjs +913 -0
  203. package/src/core-isomorphic/guards/docIndexCore.mjs +446 -0
  204. package/src/core-isomorphic/guards/losslessPostSanitizer.mjs +317 -0
  205. package/src/core-isomorphic/guards/slopsquatGate.mjs +551 -0
  206. package/src/core-isomorphic/index.js +27 -0
  207. package/src/core-isomorphic/telemetry/otelExporter.mjs +187 -0
  208. package/src/core-isomorphic/telemetry/scopeLockRegistry.mjs +522 -0
  209. package/src/core-isomorphic/telemetry/sprintMetrics.mjs +327 -0
  210. package/src/core-isomorphic/telemetry/swarmTopology.mjs +152 -0
  211. package/src/core-isomorphic/telemetry/telemetryBridge.mjs +1156 -0
  212. package/src/core-isomorphic/ui/brailleSparkline.mjs +142 -0
  213. package/src/core-isomorphic/ui/costRadarSvg.mjs +276 -0
  214. package/src/core-isomorphic/ui/horizonGrid.mjs +475 -0
  215. package/src/core-isomorphic/ui/horizonPanelHtml.mjs +1237 -0
  216. package/src/core-isomorphic/ui/modViews.mjs +1504 -0
  217. package/src/core-isomorphic/ui/pdfReceiptGenerator.mjs +329 -0
  218. package/src/core-isomorphic/ustf/index.mjs +7 -0
  219. package/src/core-isomorphic/ustf/normalizer.mjs +512 -0
  220. package/src/core-isomorphic/ustf/schema.mjs +186 -0
  221. package/src/core-isomorphic/utils/runtime.mjs +962 -0
  222. package/src/demo/synthetic_states.mjs +661 -0
  223. package/src/diagnostics/bugReportGenerator.mjs +253 -0
  224. package/src/diagnostics/claudeHookCapabilities.mjs +396 -0
  225. package/src/diagnostics/doctorEngine.mjs +295 -0
  226. package/src/engine-node/db/index.mjs +58 -0
  227. package/src/engine-node/db/jsonl_fallback.mjs +184 -0
  228. package/src/engine-node/db/probe.mjs +342 -0
  229. package/src/engine-node/db/sqlite_wrapper.mjs +122 -0
  230. package/src/engine-node/index.mjs +12 -0
  231. package/src/engine-node/ipc/client.mjs +447 -0
  232. package/src/engine-node/ipc/index.mjs +22 -0
  233. package/src/engine-node/ipc/protocol.mjs +177 -0
  234. package/src/engine-node/ipc/server.mjs +445 -0
  235. package/src/engine-node/offloader/index.mjs +6 -0
  236. package/src/engine-node/offloader/tool_offloader.mjs +241 -0
  237. package/src/engine-node/sensors/blindspots.mjs +376 -0
  238. package/src/engine-node/sensors/git_merge_tree.mjs +223 -0
  239. package/src/engine-node/sensors/index.mjs +7 -0
  240. package/src/hooks/antigravity_rsl_hook.mjs +250 -0
  241. package/src/hooks/claude_rsl_hook.mjs +288 -0
  242. package/src/hooks/portfolio_runtime.mjs +858 -0
  243. package/src/hooks/post_invocation.mjs +611 -0
  244. package/src/hooks/pre_invocation.mjs +1725 -0
  245. package/src/hooks/session_start.mjs +199 -0
  246. package/src/shims/agy_cli_compressor.mjs +705 -0
  247. package/src/shims/agy_git_guard.mjs +749 -0
  248. package/src/shims/ah_log_query.mjs +39 -0
  249. package/src/ui/costRadarSvg.mjs +6 -0
  250. package/src/ui/horizonGrid.mjs +1 -0
  251. package/src/ui/horizonPanelHtml.mjs +1 -0
  252. package/src/ui/horizon_panel.css +167 -0
  253. package/src/ui/horizon_panel.js +212 -0
  254. package/src/ui/modViews.mjs +1 -0
  255. package/src/utils/modelSwitchTelemetry.mjs +308 -0
  256. package/src/utils/nodeIo.mjs +59 -0
  257. package/src/utils/runtime.mjs +1 -0
  258. package/src/utils/sessionCalibration.mjs +657 -0
  259. package/src/webview/advanced_view.html +1808 -0
  260. package/src/webview/horizon_panel.html +12397 -0
  261. package/src/webview/registry.mjs +80 -0
  262. package/src/webview/simple_view.html +1370 -0
@@ -0,0 +1,359 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { getHomeDir } from './paths.mjs';
4
+ import { openLocalHorizonDb } from './sqlite_client.mjs';
5
+ import { SavedQuotaReceiptLedger, SAVINGS_EVENT_TYPES } from './coach/saved_quota_ledger.mjs';
6
+
7
+ /**
8
+ * Phase 1: Fail-Open CLI Output Compressor & Local SQLite FTS5 Index (`fts5_output_compressor.mjs`)
9
+ *
10
+ * When `pytest`, `jest`, `vitest`, `cargo test`, `tsc`, or `node --test` produces >80 lines
11
+ * of output, archives the complete raw stdout/stderr into `~/.gemini/antigravity/agy-horizon/tool_logs_fts5.db`:
12
+ * CREATE VIRTUAL TABLE IF NOT EXISTS tool_logs USING fts5(session_id, turn_id, command, exit_code, raw_output, created_at)
13
+ * and returns a deterministic <=25-line structured summary containing only the exit code,
14
+ * failing test names, exact file:line assertion tracebacks, and an FTS5 query handle:
15
+ * `agy-log-search <turn_id> "<keyword>"`
16
+ *
17
+ * Strictly FAIL-OPEN: if any exception occurs during SQLite indexing or parsing, returns
18
+ * the raw output untouched so builds and test workflows never break.
19
+ */
20
+
21
+ export const FTS5_COMPRESSOR_CONSTANTS = Object.freeze({
22
+ MIN_LINES_TO_COMPRESS: 80,
23
+ MAX_SUMMARY_LINES: 25,
24
+ TARGET_COMMAND_REGEX: /\b(pytest|jest|vitest|cargo\s+test|tsc|node\s+--test|npm\s+test)\b/i
25
+ });
26
+
27
+ export function getDefaultToolLogsDbPath(homeDir = getHomeDir()) {
28
+ return path.join(homeDir, '.gemini', 'antigravity', 'agy-horizon', 'tool_logs_fts5.db');
29
+ }
30
+
31
+ export function isCompressibleCliCommand(command = '') {
32
+ return FTS5_COMPRESSOR_CONSTANTS.TARGET_COMMAND_REGEX.test(String(command || ''));
33
+ }
34
+
35
+ /**
36
+ * Extracts failing test names and exact `file:line` assertion tracebacks from raw CLI output
37
+ * and formats a deterministic <= 25-line summary.
38
+ */
39
+ export function buildDeterministicSummary(rawOutput = '', {
40
+ command = 'pytest',
41
+ exitCode = 1,
42
+ turnId = 'turn-1',
43
+ maxLines = FTS5_COMPRESSOR_CONSTANTS.MAX_SUMMARY_LINES
44
+ } = {}) {
45
+ const lines = String(rawOutput || '').split(/\r?\n/);
46
+ const failingTests = [];
47
+ const fileLineTraces = [];
48
+ const assertionLines = [];
49
+
50
+ for (const rawLine of lines) {
51
+ const line = rawLine.trim();
52
+ if (!line) continue;
53
+
54
+ // Match pytest / jest / vitest / cargo / node --test failing test headers
55
+ if (
56
+ /^(FAILED|FAIL|✖)\s+/.test(line) ||
57
+ /^_{3,}\s+.+\s+_{3,}$/.test(line) ||
58
+ /^test\s+[\w:./-]+\s+\.\.\.\s+FAILED/i.test(line)
59
+ ) {
60
+ const cleaned = line.replace(/^_{3,}\s*|\s*_{3,}$/g, '').trim();
61
+ if (cleaned && !failingTests.includes(cleaned) && failingTests.length < 6) {
62
+ failingTests.push(cleaned.slice(0, 120));
63
+ }
64
+ }
65
+
66
+ // Match exact file:line or TypeScript file(line,col) tracebacks
67
+ if (
68
+ /\b[A-Za-z0-9_./\\-]+\.(?:py|ts|tsx|js|mjs|rs|go):\d+(?::\d+)?\b/.test(line) ||
69
+ /\b[A-Za-z0-9_./\\-]+\.(?:ts|tsx)\(\d+,\d+\):\s*error\s+TS\d+/i.test(line)
70
+ ) {
71
+ if (!fileLineTraces.includes(line) && fileLineTraces.length < 8) {
72
+ fileLineTraces.push(line.slice(0, 140));
73
+ }
74
+ }
75
+
76
+ // Match E AssertionError / assert / expect() failure lines
77
+ if (/^(E\s+|AssertionError:|Error:|Expected\b|Received\b|assert\s+)/.test(line)) {
78
+ if (!assertionLines.includes(line) && assertionLines.length < 5) {
79
+ assertionLines.push(line.slice(0, 140));
80
+ }
81
+ }
82
+ }
83
+
84
+ const out = [];
85
+ out.push(`[FTS5 Compressed CLI Output] cmd="${command}" exit_code=${exitCode} raw_lines=${lines.length}`);
86
+ out.push(`Failing Tests (${failingTests.length}):`);
87
+ if (failingTests.length > 0) {
88
+ for (const ft of failingTests.slice(0, 6)) {
89
+ out.push(` - ${ft}`);
90
+ }
91
+ } else {
92
+ out.push(` - (No structured test header matched; see tracebacks below)`);
93
+ }
94
+
95
+ out.push(`File:Line Tracebacks & Assertions:`);
96
+ const combinedTraces = [...fileLineTraces, ...assertionLines].slice(0, 10);
97
+ if (combinedTraces.length > 0) {
98
+ for (const tr of combinedTraces) {
99
+ out.push(` ${tr}`);
100
+ }
101
+ } else {
102
+ for (const tailLine of lines.slice(-6)) {
103
+ if (tailLine.trim()) out.push(` ${tailLine.trim().slice(0, 140)}`);
104
+ }
105
+ }
106
+
107
+ out.push(`FTS5 Query Handle: agy-log-search ${turnId} "<keyword>"`);
108
+
109
+ // Strictly enforce <= maxLines (25 lines)
110
+ const bounded = out.slice(0, maxLines - 1);
111
+ if (bounded[bounded.length - 1] !== `FTS5 Query Handle: agy-log-search ${turnId} "<keyword>"`) {
112
+ bounded.push(`FTS5 Query Handle: agy-log-search ${turnId} "<keyword>"`);
113
+ }
114
+ return bounded.slice(0, maxLines).join('\n');
115
+ }
116
+
117
+ /**
118
+ * Compresses CLI output > 80 lines into `tool_logs_fts5.db` and returns a 25-line summary.
119
+ * Fails open (returns rawOutput) if any exception is thrown.
120
+ */
121
+ export async function compressCliOutputFts5({
122
+ sessionId = 'default-session',
123
+ turnId = 'turn-1',
124
+ command = 'pytest',
125
+ exitCode = 1,
126
+ rawOutput = '',
127
+ dbPath = null,
128
+ homeDir = getHomeDir(),
129
+ DatabaseSync = null,
130
+ forceCompress = false
131
+ } = {}) {
132
+ const rawStr = String(rawOutput || '');
133
+ const rawLines = rawStr.split(/\r?\n/);
134
+ const rawTokens = Math.ceil(rawStr.length / 4);
135
+
136
+ if (!forceCompress && (rawLines.length <= FTS5_COMPRESSOR_CONSTANTS.MIN_LINES_TO_COMPRESS || !isCompressibleCliCommand(command))) {
137
+ return {
138
+ compressed: false,
139
+ failOpen: false,
140
+ output: rawStr,
141
+ lineCount: rawLines.length,
142
+ rawTokens,
143
+ compressedTokens: rawTokens,
144
+ tokensSaved: 0,
145
+ reductionPct: 0,
146
+ turnId,
147
+ queryHandle: null
148
+ };
149
+ }
150
+
151
+ let db = null;
152
+ try {
153
+ const resolvedDbPath = dbPath || getDefaultToolLogsDbPath(homeDir);
154
+ db = openLocalHorizonDb(resolvedDbPath, { readOnly: false, DatabaseSync });
155
+
156
+ db.exec(`
157
+ CREATE VIRTUAL TABLE IF NOT EXISTS tool_logs USING fts5(
158
+ session_id,
159
+ turn_id,
160
+ command,
161
+ exit_code,
162
+ raw_output,
163
+ created_at
164
+ );
165
+ `);
166
+
167
+ const createdAt = new Date().toISOString();
168
+ db.prepare(`
169
+ INSERT INTO tool_logs (session_id, turn_id, command, exit_code, raw_output, created_at)
170
+ VALUES (?, ?, ?, ?, ?, ?)
171
+ `).run(
172
+ String(sessionId),
173
+ String(turnId),
174
+ String(command),
175
+ String(exitCode),
176
+ rawStr,
177
+ createdAt
178
+ );
179
+
180
+ const summary = buildDeterministicSummary(rawStr, {
181
+ command,
182
+ exitCode,
183
+ turnId,
184
+ maxLines: FTS5_COMPRESSOR_CONSTANTS.MAX_SUMMARY_LINES
185
+ });
186
+ const summaryLines = summary.split(/\r?\n/);
187
+ const compressedTokens = Math.ceil(summary.length / 4);
188
+ const tokensSaved = Math.max(0, rawTokens - compressedTokens);
189
+ const reductionPct = rawTokens > 0
190
+ ? Number(((tokensSaved / rawTokens) * 100).toFixed(1))
191
+ : 0;
192
+
193
+ if (tokensSaved > 0 && !DatabaseSync) {
194
+ try {
195
+ const ledgerDbPath = path.join(path.dirname(resolvedDbPath), 'saved_quota_ledger.sqlite');
196
+ const ledger = new SavedQuotaReceiptLedger({ homeDir, dbPath: ledgerDbPath });
197
+ ledger.recordSaving({
198
+ sessionId: String(sessionId),
199
+ eventType: SAVINGS_EVENT_TYPES.CLI_DUMP_COMPRESSED,
200
+ tokensSaved,
201
+ rawBytesSaved: Math.max(0, Buffer.byteLength(rawStr, 'utf8') - Buffer.byteLength(summary, 'utf8')),
202
+ dedupeKey: `fts5:${sessionId}:${turnId}:${command}`,
203
+ detail: `FTS5 compressed "${String(command).slice(0, 60)}" (${rawLines.length} -> ${summaryLines.length} lines)`
204
+ });
205
+ } catch (err) {
206
+ void err;
207
+ }
208
+ }
209
+
210
+ return {
211
+ compressed: true,
212
+ failOpen: false,
213
+ dbPath: resolvedDbPath,
214
+ output: summary,
215
+ lineCount: summaryLines.length,
216
+ rawLineCount: rawLines.length,
217
+ rawTokens,
218
+ compressedTokens,
219
+ tokensSaved,
220
+ reductionPct,
221
+ turnId: String(turnId),
222
+ queryHandle: `agy-log-search ${turnId} "<keyword>"`
223
+ };
224
+ } catch (err) {
225
+ // Strictly fail open so builds never break
226
+ return {
227
+ compressed: false,
228
+ failOpen: true,
229
+ error: String(err?.message || err),
230
+ output: rawStr,
231
+ lineCount: rawLines.length,
232
+ rawTokens,
233
+ compressedTokens: rawTokens,
234
+ tokensSaved: 0,
235
+ reductionPct: 0,
236
+ turnId: String(turnId),
237
+ queryHandle: null
238
+ };
239
+ } finally {
240
+ try { if (db) db.close(); } catch {}
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Searches archived raw CLI output inside `tool_logs_fts5.db` (`agy-log-search <turn_id> "<keyword>"`).
246
+ */
247
+ export async function searchToolLogsFts5(dbPath, {
248
+ turnId = '',
249
+ keyword = '',
250
+ limit = 10,
251
+ DatabaseSync = null
252
+ } = {}) {
253
+ const resolvedDbPath = dbPath || getDefaultToolLogsDbPath();
254
+ if (!fs.existsSync(resolvedDbPath)) {
255
+ return { hits: [], matchedLines: [] };
256
+ }
257
+
258
+ let db = null;
259
+ try {
260
+ db = openLocalHorizonDb(resolvedDbPath, { readOnly: true, DatabaseSync });
261
+
262
+ const cleanKeyword = String(keyword || '')
263
+ .replace(/[^A-Za-z0-9_./:-]/g, ' ')
264
+ .trim();
265
+
266
+ let rows = [];
267
+ if (cleanKeyword) {
268
+ const ftsToken = cleanKeyword.split(/\s+/).filter(Boolean).map(t => `"${t}"`).join(' AND ');
269
+ rows = db.prepare(`
270
+ SELECT session_id AS sessionId, turn_id AS turnId, command, exit_code AS exitCode, raw_output AS rawOutput, created_at AS createdAt
271
+ FROM tool_logs
272
+ WHERE tool_logs MATCH ?
273
+ LIMIT ?
274
+ `).all(ftsToken, Math.max(1, limit));
275
+ } else {
276
+ rows = db.prepare(`
277
+ SELECT session_id AS sessionId, turn_id AS turnId, command, exit_code AS exitCode, raw_output AS rawOutput, created_at AS createdAt
278
+ FROM tool_logs
279
+ LIMIT ?
280
+ `).all(Math.max(1, limit));
281
+ }
282
+
283
+ if (turnId) {
284
+ rows = rows.filter(r => String(r.turnId) === String(turnId));
285
+ }
286
+
287
+ const matchedLines = [];
288
+ const kwLower = String(keyword || '').toLowerCase();
289
+ for (const r of rows) {
290
+ const lines = String(r.rawOutput || '').split(/\r?\n/);
291
+ for (let i = 0; i < lines.length; i++) {
292
+ if (!kwLower || lines[i].toLowerCase().includes(kwLower)) {
293
+ matchedLines.push({
294
+ turnId: r.turnId,
295
+ lineNumber: i + 1,
296
+ text: lines[i]
297
+ });
298
+ if (matchedLines.length >= 25) break;
299
+ }
300
+ }
301
+ }
302
+
303
+ return { hits: rows, matchedLines };
304
+ } catch {
305
+ return { hits: [], matchedLines: [] };
306
+ } finally {
307
+ try { if (db) db.close(); } catch {}
308
+ }
309
+ }
310
+
311
+ /**
312
+ * Measures the total tokens saved across all FTS5 compressed tool logs.
313
+ * Opens the tool_logs SQLite database strictly read-only and queries row counts and token savings.
314
+ */
315
+ export function getFts5CompressionStats(dbPath = null, { DatabaseSync = null, homeDir = getHomeDir() } = {}) {
316
+ const resolvedDbPath = dbPath || getDefaultToolLogsDbPath(homeDir);
317
+ if (!fs.existsSync(resolvedDbPath)) {
318
+ return {
319
+ active: true,
320
+ logCount: 0,
321
+ savedTokens: 0,
322
+ formattedSavedK: '0.0k'
323
+ };
324
+ }
325
+
326
+ let db = null;
327
+ try {
328
+ db = openLocalHorizonDb(resolvedDbPath, { readOnly: true, DatabaseSync });
329
+ // Approximate saved tokens by measuring raw output length vs summary length (saving ~75-90% of raw output)
330
+ const row = db.prepare(`
331
+ SELECT count(*) as count, coalesce(sum(length(raw_output)), 0) as totalBytes
332
+ FROM tool_logs
333
+ `).get();
334
+
335
+ const count = Number(row?.count || 0);
336
+ const totalBytes = Number(row?.totalBytes || 0);
337
+ // Raw tokens ~ bytes / 4. Summary is capped at 25 lines (~1200 bytes / 300 tokens) per log.
338
+ const rawTokensEst = Math.ceil(totalBytes / 4);
339
+ const summaryTokensEst = count * 300;
340
+ const savedTokens = Math.max(0, rawTokensEst - summaryTokensEst);
341
+ const formattedSavedK = `${(savedTokens / 1000).toFixed(1)}k`;
342
+
343
+ return {
344
+ active: true,
345
+ logCount: count,
346
+ savedTokens,
347
+ formattedSavedK
348
+ };
349
+ } catch {
350
+ return {
351
+ active: true,
352
+ logCount: 0,
353
+ savedTokens: 0,
354
+ formattedSavedK: '0.0k'
355
+ };
356
+ } finally {
357
+ try { if (db) db.close(); } catch {}
358
+ }
359
+ }
@@ -0,0 +1,189 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ /**
5
+ * Git Commit & PR Token Cost Attribution Ledger.
6
+ *
7
+ * Parses `.git/logs/HEAD` (`commit`, `amend`, `rebase`, `checkout`) alongside session
8
+ * transcript steps (`edit_file`, `write_file`, `cache_read_input_tokens`, `thinking_tokens`)
9
+ * to attribute exact token burn, cache hit rate, and estimated USD cost per Git commit and modified file.
10
+ */
11
+
12
+ export function buildGitCommitTokenLedger(workspacePath, steps = [], { headLogContent = null, pricePerMillionUsd = 3.0 } = {}) {
13
+ let rawHeadLog = headLogContent;
14
+ if (rawHeadLog === null && workspacePath) {
15
+ const headLogPath = path.join(workspacePath, '.git', 'logs', 'HEAD');
16
+ if (fs.existsSync(headLogPath)) {
17
+ try {
18
+ rawHeadLog = fs.readFileSync(headLogPath, 'utf8');
19
+ } catch {
20
+ rawHeadLog = '';
21
+ }
22
+ }
23
+ }
24
+
25
+ const allParsedCommits = [];
26
+ for (const line of String(rawHeadLog || '').split(/\r?\n/).filter(Boolean)) {
27
+ // Format: <oldSha> <newSha> <name> <<email>> <unixSec> <tz>\t<action>: <message>
28
+ const m = line.match(/^([0-9a-f]{7,40})\s+([0-9a-f]{7,40})\s+(.+?)\s+(\d{9,12})\s+([+-]\d{4})\t([^:]+):\s*(.*)$/i);
29
+ if (m) {
30
+ allParsedCommits.push({
31
+ parentSha: m[1],
32
+ commitSha: m[2],
33
+ shortSha: m[2].slice(0, 7),
34
+ timestampMs: Number(m[4]) * 1000,
35
+ action: m[6].trim().toLowerCase(),
36
+ message: m[7].trim()
37
+ });
38
+ }
39
+ }
40
+
41
+ const list = Array.isArray(steps) ? steps : [];
42
+ let totalInputTokens = 0;
43
+ let totalCacheReadTokens = 0;
44
+ let totalThinkingTokens = 0;
45
+ let totalOutputTokens = 0;
46
+ let minStepMs = Infinity;
47
+ let maxStepMs = -Infinity;
48
+ const fileStats = new Map();
49
+ const normalizedSteps = [];
50
+
51
+ for (let i = 0; i < list.length; i++) {
52
+ const s = list[i] || {};
53
+ const freshInTok = Number(s.inputTokens ?? s.input_tokens ?? 0);
54
+ const cacheCreateTok = Number(s.cacheCreationTokens ?? s.cache_creation_input_tokens ?? 0);
55
+ const inTok = freshInTok + cacheCreateTok;
56
+ const cacheReadTok = Number(s.cacheReadTokens ?? s.cache_read_input_tokens ?? 0);
57
+ const thinkTok = Number(s.thinkingTokens ?? s.thinking_tokens ?? 0);
58
+ const outTok = Number(s.outputTokens ?? s.output_tokens ?? Math.ceil(String(s.content || '').length / 4));
59
+ const stepTok = inTok + cacheReadTok + thinkTok + outTok;
60
+
61
+ const rawTs = s.createdAt ?? s.created_at ?? s.timestamp ?? null;
62
+ const parsedTs = typeof rawTs === 'number' ? rawTs : (rawTs ? Date.parse(String(rawTs)) : NaN);
63
+ if (Number.isFinite(parsedTs) && parsedTs > 0) {
64
+ if (parsedTs < minStepMs) minStepMs = parsedTs;
65
+ if (parsedTs > maxStepMs) maxStepMs = parsedTs;
66
+ }
67
+
68
+ normalizedSteps.push({
69
+ stepIndex: i,
70
+ timestampMs: Number.isFinite(parsedTs) ? parsedTs : null,
71
+ inTok,
72
+ cacheReadTok,
73
+ thinkTok,
74
+ outTok,
75
+ stepTok
76
+ });
77
+
78
+ totalInputTokens += inTok;
79
+ totalCacheReadTokens += cacheReadTok;
80
+ totalThinkingTokens += thinkTok;
81
+ totalOutputTokens += outTok;
82
+
83
+ const calls = Array.isArray(s.toolCalls)
84
+ ? s.toolCalls
85
+ : (Array.isArray(s.tool_calls) ? s.tool_calls : []);
86
+
87
+ for (const c of calls) {
88
+ const name = String(c.name || c.tool || '').toLowerCase();
89
+ const args = c.args || c.parameters || c.Arguments || {};
90
+ const fp = args.Path || args.path || args.TargetFile || args.AbsolutePath;
91
+ if (fp && /edit|write|replace/i.test(name)) {
92
+ const cur = fileStats.get(fp) || { filePath: fp, edits: 0, attributedTokens: 0 };
93
+ cur.edits += 1;
94
+ cur.attributedTokens += (inTok + outTok + thinkTok) || 1200;
95
+ fileStats.set(fp, cur);
96
+ }
97
+ }
98
+ }
99
+
100
+ // Filter commits to actual commit actions within the session's timestamp window [minStepMs - 5m, maxStepMs + 15m]
101
+ // when step timestamps exist andheadLogContent was read from disk; otherwise fall back to commit entries (up to last 10).
102
+ const commitActionsOnly = allParsedCommits.filter(c => /commit|merge|cherry-pick/i.test(c.action));
103
+ const candidateCommits = commitActionsOnly.length > 0 ? commitActionsOnly : allParsedCommits;
104
+
105
+ let commits = candidateCommits;
106
+ const hasStepTimestamps = Number.isFinite(minStepMs) && Number.isFinite(maxStepMs) && minStepMs <= maxStepMs;
107
+ if (hasStepTimestamps) {
108
+ const windowStart = minStepMs - (5 * 60 * 1000);
109
+ const windowEnd = maxStepMs + (15 * 60 * 1000);
110
+ const inWindow = candidateCommits.filter(c => c.timestampMs >= windowStart && c.timestampMs <= windowEnd);
111
+ if (inWindow.length > 0) {
112
+ commits = inWindow;
113
+ } else if (headLogContent === null) {
114
+ commits = candidateCommits.slice(-5);
115
+ }
116
+ } else if (headLogContent === null && candidateCommits.length > 5) {
117
+ commits = candidateCommits.slice(-5);
118
+ }
119
+
120
+ const totalTokens = totalInputTokens + totalCacheReadTokens + totalThinkingTokens + totalOutputTokens;
121
+ const cacheHitPct = (totalInputTokens + totalCacheReadTokens) > 0
122
+ ? Math.round((totalCacheReadTokens / (totalInputTokens + totalCacheReadTokens)) * 100)
123
+ : 0;
124
+ const computeCostUsd = (inT, cacheT, outT, thinkT) => parseFloat((
125
+ ((inT * 1.0 + cacheT * 0.1 + (outT + thinkT) * 3.0) / 1_000_000) * pricePerMillionUsd
126
+ ).toFixed(4));
127
+ const estimatedCostUsd = computeCostUsd(totalInputTokens, totalCacheReadTokens, totalOutputTokens, totalThinkingTokens);
128
+
129
+ const fallbackPerCommitTokens = commits.length > 0 ? Math.round(totalTokens / commits.length) : totalTokens;
130
+ const fallbackPerCommitCost = commits.length > 0 ? parseFloat((estimatedCostUsd / commits.length).toFixed(4)) : estimatedCostUsd;
131
+
132
+ const commitRows = commits.map((c, idx) => {
133
+ const prevCommitMs = idx > 0 ? commits[idx - 1].timestampMs : -Infinity;
134
+ const isLastCommit = idx === commits.length - 1;
135
+ const intervalSteps = hasStepTimestamps
136
+ ? normalizedSteps.filter(st =>
137
+ st.timestampMs !== null &&
138
+ st.timestampMs > prevCommitMs &&
139
+ (isLastCommit ? st.timestampMs <= Math.max(c.timestampMs, maxStepMs) : st.timestampMs <= c.timestampMs)
140
+ )
141
+ : [];
142
+
143
+ if (intervalSteps.length > 0) {
144
+ const cIn = intervalSteps.reduce((acc, s) => acc + s.inTok, 0);
145
+ const cCache = intervalSteps.reduce((acc, s) => acc + s.cacheReadTok, 0);
146
+ const cThink = intervalSteps.reduce((acc, s) => acc + s.thinkTok, 0);
147
+ const cOut = intervalSteps.reduce((acc, s) => acc + s.outTok, 0);
148
+ const cTotal = cIn + cCache + cThink + cOut;
149
+ const cHit = (cIn + cCache) > 0 ? Math.round((cCache / (cIn + cCache)) * 100) : cacheHitPct;
150
+ return {
151
+ ...c,
152
+ attributedTokens: cTotal,
153
+ cacheHitPct: cHit,
154
+ estimatedCostUsd: computeCostUsd(cIn, cCache, cOut, cThink)
155
+ };
156
+ }
157
+
158
+ return {
159
+ ...c,
160
+ attributedTokens: fallbackPerCommitTokens,
161
+ cacheHitPct,
162
+ estimatedCostUsd: fallbackPerCommitCost
163
+ };
164
+ });
165
+
166
+ const prMarkdownSummary = [
167
+ `### ⚡ Highbeam PR Token Ledger`,
168
+ `| Metric | Value |`,
169
+ `| :--- | :--- |`,
170
+ `| **Total Session Tokens** | \`${(totalTokens / 1000).toFixed(1)}k\` |`,
171
+ `| **Prompt Cache Hit Rate** | \`${cacheHitPct}%\` |`,
172
+ `| **Thinking / Reasoning Tokens** | \`${(totalThinkingTokens / 1000).toFixed(1)}k\` |`,
173
+ `| **Estimated Session Cost** | \`$${estimatedCostUsd.toFixed(4)}\` |`,
174
+ `| **Attributed Commits** | \`${commitRows.length}\` |`
175
+ ].join('\n');
176
+
177
+ return {
178
+ totalTokens,
179
+ totalInputTokens,
180
+ totalCacheReadTokens,
181
+ totalThinkingTokens,
182
+ totalOutputTokens,
183
+ cacheHitPct,
184
+ estimatedCostUsd,
185
+ commits: commitRows,
186
+ files: Array.from(fileStats.values()),
187
+ prMarkdownSummary
188
+ };
189
+ }