ctxtop 0.0.0-stage → 4.0.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 +149 -0
- package/LICENSE +27 -0
- package/README.md +330 -3
- 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 +145 -0
- package/bin/cli.mjs +3 -0
- package/bin/ctxtop.mjs +1474 -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 +114 -0
- package/config/modelCatalog.json +620 -0
- package/config/platformQuirks.json +19 -0
- package/hooks/handlers/compact-lifecycle.mjs +262 -0
- package/hooks/handlers/hookIo.mjs +255 -0
- package/hooks/handlers/hookIpcClient.mjs +83 -0
- package/hooks/handlers/post-tool-use.mjs +265 -0
- package/hooks/handlers/pre-model-switch.mjs +249 -0
- package/hooks/handlers/pre-tool-use.mjs +365 -0
- package/hooks/handlers/stop-gate.mjs +240 -0
- package/hooks/handlers/user-prompt-submit.mjs +143 -0
- package/hooks/hooks.json +81 -0
- package/hooks/post_invocation.mjs +13 -0
- package/hooks/post_tool_use.mjs +17 -0
- package/hooks/pre_invocation.mjs +14 -0
- package/hooks/pre_tool_use.mjs +97 -0
- package/hooks/register.js +687 -0
- package/hooks/session_start.mjs +14 -0
- package/hooks/stop.mjs +13 -0
- package/hooks.json +56 -0
- package/package.json +103 -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 +2002 -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 +42 -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 +79 -0
- package/src/adapters/claude-code/index.mjs +155 -0
- package/src/cli/cost_radar.mjs +70 -0
- package/src/cli/doctor.mjs +351 -0
- package/src/cli/horizon_hud.mjs +686 -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 +620 -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/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 +1800 -0
- package/src/core/detectors_det01_to_16.mjs +1474 -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 +966 -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 +1996 -0
- package/src/core/path_canonicalizer.mjs +73 -0
- package/src/core/paths.mjs +310 -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/sanitizer.mjs +121 -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/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 +720 -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/savedQuotaLedger.mjs +527 -0
- package/src/core-isomorphic/coach/stateBatonHydrator.mjs +810 -0
- package/src/core-isomorphic/coach/ttlWatchdog.mjs +457 -0
- package/src/core-isomorphic/config/anthropicModelCatalog.json +203 -0
- package/src/core-isomorphic/config/catalogLoader.mjs +768 -0
- package/src/core-isomorphic/config/horizonConfig.mjs +1009 -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 +622 -0
- package/src/core-isomorphic/economics/billingBlocks.mjs +251 -0
- package/src/core-isomorphic/economics/cacheGravity.mjs +351 -0
- package/src/core-isomorphic/economics/cliffBreaker.mjs +170 -0
- package/src/core-isomorphic/economics/driftLatency.mjs +319 -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/circuitBreakers.mjs +1202 -0
- package/src/core-isomorphic/guards/det01_03_shell_secret.mjs +1035 -0
- package/src/core-isomorphic/guards/det04_12_thrash_rot.mjs +480 -0
- package/src/core-isomorphic/guards/det13_20_mcp_scope.mjs +603 -0
- package/src/core-isomorphic/guards/losslessPostSanitizer.mjs +301 -0
- package/src/core-isomorphic/index.js +26 -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 +1132 -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 +1181 -0
- package/src/core-isomorphic/ui/modViews.mjs +1478 -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/doctorEngine.mjs +203 -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 +110 -0
- package/src/engine-node/db/sqlite_wrapper.mjs +122 -0
- package/src/engine-node/index.mjs +9 -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 +369 -0
- package/src/engine-node/sensors/git_merge_tree.mjs +223 -0
- package/src/engine-node/sensors/index.mjs +7 -0
- package/src/hooks/portfolio_runtime.mjs +858 -0
- package/src/hooks/post_invocation.mjs +419 -0
- package/src/hooks/pre_invocation.mjs +1015 -0
- package/src/hooks/session_start.mjs +161 -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/nodeIo.mjs +59 -0
- package/src/utils/runtime.mjs +1 -0
- package/src/webview/advanced_view.html +1808 -0
- package/src/webview/horizon_panel.html +11389 -0
- package/src/webview/registry.mjs +80 -0
- package/src/webview/simple_view.html +1370 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { openLocalHorizonDb } from './sqlite_client.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* SQLite FTS5 Episodic Memory Bridge & `[▶ RESUME HERE]` Context Synthesizer.
|
|
6
|
+
*
|
|
7
|
+
* Maintains a local zero-network SQLite FTS5 index (`~/.gemini/flightcheck/episodic_memory.db`)
|
|
8
|
+
* storing compact session summaries, architectural decisions, modified files, and resolved errors,
|
|
9
|
+
* and synthesizes a `<1,000 token` (`<=4,000 chars`) `[▶ RESUME HERE]` context pack.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export async function upsertEpisodicMemory(dbPath, record = {}, { DatabaseSync } = {}) {
|
|
13
|
+
if (!dbPath) {
|
|
14
|
+
return { success: false, error: 'dbPath is required' };
|
|
15
|
+
}
|
|
16
|
+
const db = openLocalHorizonDb(dbPath, { readOnly: false, DatabaseSync });
|
|
17
|
+
try {
|
|
18
|
+
db.exec(`
|
|
19
|
+
CREATE TABLE IF NOT EXISTS episodic_sessions (
|
|
20
|
+
session_id TEXT PRIMARY KEY,
|
|
21
|
+
workspace_path TEXT,
|
|
22
|
+
title TEXT,
|
|
23
|
+
summary TEXT,
|
|
24
|
+
decisions TEXT,
|
|
25
|
+
modified_files TEXT,
|
|
26
|
+
updated_at TEXT
|
|
27
|
+
);
|
|
28
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS episodic_fts USING fts5(
|
|
29
|
+
session_id UNINDEXED,
|
|
30
|
+
workspace_path,
|
|
31
|
+
title,
|
|
32
|
+
summary,
|
|
33
|
+
decisions,
|
|
34
|
+
modified_files
|
|
35
|
+
);
|
|
36
|
+
`);
|
|
37
|
+
|
|
38
|
+
const sessionId = String(record.sessionId || `sess-${Date.now()}`);
|
|
39
|
+
const workspacePath = String(record.workspacePath || '');
|
|
40
|
+
const title = String(record.title || 'Session');
|
|
41
|
+
const summary = String(record.summary || '');
|
|
42
|
+
const decisions = Array.isArray(record.decisions) ? record.decisions.join('\n') : String(record.decisions || '');
|
|
43
|
+
const modifiedFiles = Array.isArray(record.modifiedFiles) ? record.modifiedFiles.join(', ') : String(record.modifiedFiles || '');
|
|
44
|
+
const updatedAt = record.updatedAt || new Date().toISOString();
|
|
45
|
+
|
|
46
|
+
db.prepare(`
|
|
47
|
+
INSERT OR REPLACE INTO episodic_sessions (session_id, workspace_path, title, summary, decisions, modified_files, updated_at)
|
|
48
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
49
|
+
`).run(sessionId, workspacePath, title, summary, decisions, modifiedFiles, updatedAt);
|
|
50
|
+
|
|
51
|
+
db.prepare('DELETE FROM episodic_fts WHERE session_id = ?').run(sessionId);
|
|
52
|
+
db.prepare(`
|
|
53
|
+
INSERT INTO episodic_fts (session_id, workspace_path, title, summary, decisions, modified_files)
|
|
54
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
55
|
+
`).run(sessionId, workspacePath, title, summary, decisions, modifiedFiles);
|
|
56
|
+
|
|
57
|
+
return { success: true, sessionId };
|
|
58
|
+
} finally {
|
|
59
|
+
try { db.close(); } catch {}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function queryEpisodicMemoryBm25(dbPath, { query = '', workspacePath = '', limit = 8, DatabaseSync } = {}) {
|
|
64
|
+
if (!dbPath || !fs.existsSync(dbPath)) {
|
|
65
|
+
return { hits: [] };
|
|
66
|
+
}
|
|
67
|
+
const db = openLocalHorizonDb(dbPath, { readOnly: true, DatabaseSync });
|
|
68
|
+
try {
|
|
69
|
+
const cleanQuery = String(query || '')
|
|
70
|
+
.replace(/[^A-Za-z0-9_\s]/g, ' ')
|
|
71
|
+
.trim()
|
|
72
|
+
.split(/\s+/)
|
|
73
|
+
.filter(Boolean)
|
|
74
|
+
.join(' OR ');
|
|
75
|
+
|
|
76
|
+
let rows = [];
|
|
77
|
+
if (cleanQuery) {
|
|
78
|
+
rows = db.prepare(`
|
|
79
|
+
SELECT session_id AS sessionId, workspace_path AS workspacePath, title, summary, decisions, modified_files AS modifiedFiles, bm25(episodic_fts) AS rank
|
|
80
|
+
FROM episodic_fts
|
|
81
|
+
WHERE episodic_fts MATCH ?
|
|
82
|
+
ORDER BY rank ASC
|
|
83
|
+
LIMIT ?
|
|
84
|
+
`).all(cleanQuery, Math.max(1, limit));
|
|
85
|
+
} else {
|
|
86
|
+
rows = db.prepare(`
|
|
87
|
+
SELECT session_id AS sessionId, workspace_path AS workspacePath, title, summary, decisions, modified_files AS modifiedFiles, 0.0 AS rank
|
|
88
|
+
FROM episodic_sessions
|
|
89
|
+
ORDER BY updated_at DESC
|
|
90
|
+
LIMIT ?
|
|
91
|
+
`).all(Math.max(1, limit));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (workspacePath) {
|
|
95
|
+
const normWs = workspacePath.toLowerCase().replace(/\\/g, '/');
|
|
96
|
+
const filtered = rows.filter(r => String(r.workspacePath || '').toLowerCase().replace(/\\/g, '/').includes(normWs));
|
|
97
|
+
if (filtered.length > 0) rows = filtered;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return { hits: rows };
|
|
101
|
+
} catch {
|
|
102
|
+
return { hits: [] };
|
|
103
|
+
} finally {
|
|
104
|
+
try { db.close(); } catch {}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export async function synthesizeResumeHerePack(dbPath, { workspacePath = '', query = '', maxChars = 4000, DatabaseSync } = {}) {
|
|
109
|
+
const { hits } = await queryEpisodicMemoryBm25(dbPath, { query, workspacePath, limit: 5, DatabaseSync });
|
|
110
|
+
const lines = [
|
|
111
|
+
`# [▶ RESUME HERE] Episodic Context Pack`,
|
|
112
|
+
`- **Workspace**: \`${workspacePath || 'active'}\``,
|
|
113
|
+
`- **Matched Prior Sessions**: ${hits.length}`,
|
|
114
|
+
``
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
for (let i = 0; i < hits.length; i++) {
|
|
118
|
+
const h = hits[i];
|
|
119
|
+
lines.push(`## ${i + 1}. ${h.title} (\`${h.sessionId}\`)`);
|
|
120
|
+
if (h.summary) lines.push(`- **Summary**: ${String(h.summary).slice(0, 300)}`);
|
|
121
|
+
if (h.decisions) lines.push(`- **Decisions**: ${String(h.decisions).slice(0, 260)}`);
|
|
122
|
+
if (h.modifiedFiles) lines.push(`- **Files**: \`${String(h.modifiedFiles).slice(0, 240)}\``);
|
|
123
|
+
lines.push('');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const rawMarkdown = lines.join('\n').slice(0, maxChars);
|
|
127
|
+
const estimatedTokens = Math.ceil(rawMarkdown.length / 4);
|
|
128
|
+
return {
|
|
129
|
+
header: '[▶ RESUME HERE]',
|
|
130
|
+
hitCount: hits.length,
|
|
131
|
+
estimatedTokens,
|
|
132
|
+
withinTokenBudget: estimatedTokens <= 1000,
|
|
133
|
+
markdown: rawMarkdown
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -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
|
+
}
|