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,475 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/ui/horizonGrid.mjs
|
|
3
|
+
* 40-Cell Non-Linear Dynamic Horizon Bar for Claude Code Highbeam.
|
|
4
|
+
*
|
|
5
|
+
* Maps active context window tokens onto a 40-cell grid with 3 dynamic zoom tiers:
|
|
6
|
+
* 1. `100k` ceiling (`2,500` tokens/cell) when running `claude-haiku-5-5` at <= 100,000 tokens
|
|
7
|
+
* 2. `200k` ceiling (`5,000` tokens/cell) standard operational horizon (<= 180,000 tokens)
|
|
8
|
+
* 3. `1M` ceiling (`25,000` tokens/cell) auto-zoom when `total_input_tokens > 180,000`
|
|
9
|
+
*
|
|
10
|
+
* Token Categories & Canonical Palette:
|
|
11
|
+
* - `[S]` System & MCP Schema Overhead (`#1A73E8`)
|
|
12
|
+
* - `[C]` Warm Cached History (`#81C995`)
|
|
13
|
+
* - `[D]` Middle-Window Distractor / Stale Tool Debt (`#F538A0`)
|
|
14
|
+
* - `[W]` Fresh `1.25x` Cache Write Delta (`#FA903E`)
|
|
15
|
+
* - `[.]` Available Capacity (`#5F6368`)
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
normalizeModelId,
|
|
20
|
+
resolveCustomCeilingForModel
|
|
21
|
+
} from '../config/catalogLoader.mjs';
|
|
22
|
+
|
|
23
|
+
export const GRID_CELL_COUNT = 40;
|
|
24
|
+
|
|
25
|
+
export const CELL_CATEGORIES = Object.freeze({
|
|
26
|
+
S: {
|
|
27
|
+
code: 'S',
|
|
28
|
+
tag: '[S]',
|
|
29
|
+
label: 'System & MCP Schema Overhead',
|
|
30
|
+
colorHex: '#1A73E8',
|
|
31
|
+
ansiFg: '\x1b[38;2;26;115;232m',
|
|
32
|
+
blockChar: '█'
|
|
33
|
+
},
|
|
34
|
+
C: {
|
|
35
|
+
code: 'C',
|
|
36
|
+
tag: '[C]',
|
|
37
|
+
label: 'Warm Cached History',
|
|
38
|
+
colorHex: '#81C995',
|
|
39
|
+
ansiFg: '\x1b[38;2;129;201;149m',
|
|
40
|
+
blockChar: '▓'
|
|
41
|
+
},
|
|
42
|
+
D: {
|
|
43
|
+
code: 'D',
|
|
44
|
+
tag: '[D]',
|
|
45
|
+
label: 'Middle-Window Distractor / Stale Tool Debt',
|
|
46
|
+
colorHex: '#F538A0',
|
|
47
|
+
ansiFg: '\x1b[38;2;245;56;160m',
|
|
48
|
+
blockChar: '▒'
|
|
49
|
+
},
|
|
50
|
+
W: {
|
|
51
|
+
code: 'W',
|
|
52
|
+
tag: '[W]',
|
|
53
|
+
label: 'Fresh 1.25x Cache Write Delta',
|
|
54
|
+
colorHex: '#FA903E',
|
|
55
|
+
ansiFg: '\x1b[38;2;250;144;62m',
|
|
56
|
+
blockChar: '█'
|
|
57
|
+
},
|
|
58
|
+
EMPTY: {
|
|
59
|
+
code: '.',
|
|
60
|
+
tag: '[.]',
|
|
61
|
+
label: 'Available Horizon Capacity',
|
|
62
|
+
colorHex: '#5F6368',
|
|
63
|
+
ansiFg: '\x1b[90m',
|
|
64
|
+
blockChar: '░'
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
export const ZOOM_TIERS = Object.freeze({
|
|
69
|
+
TIER_100K: {
|
|
70
|
+
id: '100k',
|
|
71
|
+
ceilingTokens: 100000,
|
|
72
|
+
tokensPerCell: 2500,
|
|
73
|
+
label: '100k Haiku Cliff Horizon (2.5k/cell)'
|
|
74
|
+
},
|
|
75
|
+
TIER_200K: {
|
|
76
|
+
id: '200k',
|
|
77
|
+
ceilingTokens: 200000,
|
|
78
|
+
tokensPerCell: 5000,
|
|
79
|
+
label: '200k Standard Operational Horizon (5k/cell)'
|
|
80
|
+
},
|
|
81
|
+
TIER_1M: {
|
|
82
|
+
id: '1M',
|
|
83
|
+
ceilingTokens: 1000000,
|
|
84
|
+
tokensPerCell: 25000,
|
|
85
|
+
label: '1M Extended Auto-Zoom Horizon (25k/cell)'
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const ANSI_RESET = '\x1b[0m';
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Checks whether ANSI escape codes must be suppressed (`ISS-050`).
|
|
93
|
+
* Honors `options.noColor === true` or `'NO_COLOR' in (globalThis.process?.env || {})`
|
|
94
|
+
* unless `options.forceColor === true`.
|
|
95
|
+
*
|
|
96
|
+
* @param {object} [options={}]
|
|
97
|
+
* @returns {boolean}
|
|
98
|
+
*/
|
|
99
|
+
export function isNoColorEnabled(options = {}) {
|
|
100
|
+
if (options?.noColor === true) return true;
|
|
101
|
+
if (options?.forceColor === true) return false;
|
|
102
|
+
return 'NO_COLOR' in (globalThis.process?.env || {});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Formats a custom ceiling token count into a compact label ID (e.g. `400k`, `1.5M`, `128k`).
|
|
107
|
+
* @param {number} tokens
|
|
108
|
+
* @returns {string}
|
|
109
|
+
*/
|
|
110
|
+
function formatCompactCeilingId(tokens) {
|
|
111
|
+
const n = Math.max(1, Math.round(Number(tokens || 0)));
|
|
112
|
+
if (n >= 1_000_000) {
|
|
113
|
+
const m = n / 1_000_000;
|
|
114
|
+
return Number.isInteger(m) ? `${m}M` : `${Number(m.toFixed(2))}M`;
|
|
115
|
+
}
|
|
116
|
+
if (n >= 1_000) {
|
|
117
|
+
const k = n / 1_000;
|
|
118
|
+
return Number.isInteger(k) ? `${k}k` : `${Number(k.toFixed(1))}k`;
|
|
119
|
+
}
|
|
120
|
+
return String(n);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Builds a custom zoom tier scaled directly to an explicit user ceiling override (`ISS-005`).
|
|
125
|
+
* @param {number} ceilingTokens
|
|
126
|
+
*/
|
|
127
|
+
function buildCustomZoomTier(ceilingTokens) {
|
|
128
|
+
const ceiling = Math.max(1, Math.round(Number(ceilingTokens)));
|
|
129
|
+
const tokensPerCell = Math.max(1, Math.round(ceiling / GRID_CELL_COUNT));
|
|
130
|
+
const id = formatCompactCeilingId(ceiling);
|
|
131
|
+
const perCellLabel = formatCompactCeilingId(tokensPerCell);
|
|
132
|
+
return {
|
|
133
|
+
id,
|
|
134
|
+
ceilingTokens: ceiling,
|
|
135
|
+
tokensPerCell,
|
|
136
|
+
isCustom: true,
|
|
137
|
+
label: `${id} Custom Horizon (${perCellLabel}/cell)`
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Resolves which dynamic zoom tier (or explicit custom ceiling override) applies to a model and token count.
|
|
143
|
+
*
|
|
144
|
+
* @param {string} [model='claude-sonnet-5-5']
|
|
145
|
+
* @param {number} [totalInputTokens=0]
|
|
146
|
+
* @param {object} [options={}]
|
|
147
|
+
* @returns {{ id: string, ceilingTokens: number, tokensPerCell: number, label: string, isCustom?: boolean }}
|
|
148
|
+
*/
|
|
149
|
+
export function resolveHorizonZoomTier(
|
|
150
|
+
model = 'claude-sonnet-5-5',
|
|
151
|
+
totalInputTokens = 0,
|
|
152
|
+
options = {}
|
|
153
|
+
) {
|
|
154
|
+
const explicitOptCeiling = Number(options.customCeiling ?? options.contextCeiling ?? 0);
|
|
155
|
+
if (Number.isFinite(explicitOptCeiling) && explicitOptCeiling > 0) {
|
|
156
|
+
return buildCustomZoomTier(explicitOptCeiling);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const catalogOverride = resolveCustomCeilingForModel(model, options);
|
|
160
|
+
if (catalogOverride !== null && catalogOverride > 0) {
|
|
161
|
+
return buildCustomZoomTier(catalogOverride);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const normModel = normalizeModelId(model);
|
|
165
|
+
const tokens = Math.max(0, Number(totalInputTokens || 0));
|
|
166
|
+
|
|
167
|
+
if (tokens > 180000) {
|
|
168
|
+
return ZOOM_TIERS.TIER_1M;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (normModel === 'claude-haiku-5-5' && tokens <= 100000) {
|
|
172
|
+
return ZOOM_TIERS.TIER_100K;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return ZOOM_TIERS.TIER_200K;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Allocates integer cell counts for `[S]`, `[C]`, `[D]`, `[W]` summing to `occupiedCells`.
|
|
180
|
+
* Ensures any non-zero token category with enough room gets at least 1 cell for visual fidelity.
|
|
181
|
+
*
|
|
182
|
+
* @param {{ S: number, C: number, D: number, W: number }} tokenBreakdown
|
|
183
|
+
* @param {number} occupiedCells
|
|
184
|
+
* @returns {{ S: number, C: number, D: number, W: number }}
|
|
185
|
+
*/
|
|
186
|
+
function allocateCategoryCells(tokenBreakdown, occupiedCells) {
|
|
187
|
+
if (occupiedCells <= 0) {
|
|
188
|
+
return { S: 0, C: 0, D: 0, W: 0 };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const keys = ['S', 'C', 'D', 'W'];
|
|
192
|
+
const totalTokens = keys.reduce((acc, k) => acc + Math.max(0, tokenBreakdown[k] || 0), 0);
|
|
193
|
+
if (totalTokens <= 0) {
|
|
194
|
+
return { S: 0, C: occupiedCells, D: 0, W: 0 };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const rawCounts = {};
|
|
198
|
+
const counts = { S: 0, C: 0, D: 0, W: 0 };
|
|
199
|
+
const remainders = [];
|
|
200
|
+
|
|
201
|
+
for (const key of keys) {
|
|
202
|
+
const val = Math.max(0, tokenBreakdown[key] || 0);
|
|
203
|
+
const exact = (val / totalTokens) * occupiedCells;
|
|
204
|
+
rawCounts[key] = exact;
|
|
205
|
+
counts[key] = Math.floor(exact);
|
|
206
|
+
remainders.push({ key, rem: exact - counts[key], val });
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
let currentSum = keys.reduce((acc, k) => acc + counts[k], 0);
|
|
210
|
+
remainders.sort((a, b) => b.rem - a.rem || b.val - a.val);
|
|
211
|
+
|
|
212
|
+
for (const item of remainders) {
|
|
213
|
+
if (currentSum >= occupiedCells) break;
|
|
214
|
+
if (item.val > 0) {
|
|
215
|
+
counts[item.key] += 1;
|
|
216
|
+
currentSum += 1;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Guarantee at least 1 cell for non-trivial categories (>0 tokens) if occupiedCells >= number of active categories
|
|
221
|
+
const activeKeys = keys.filter((k) => (tokenBreakdown[k] || 0) > 0);
|
|
222
|
+
if (occupiedCells >= activeKeys.length) {
|
|
223
|
+
for (const key of activeKeys) {
|
|
224
|
+
if (counts[key] === 0) {
|
|
225
|
+
// Borrow 1 cell from the largest category with count > 1
|
|
226
|
+
let donor = null;
|
|
227
|
+
let maxCount = 1;
|
|
228
|
+
for (const candidate of activeKeys) {
|
|
229
|
+
if (counts[candidate] > maxCount) {
|
|
230
|
+
maxCount = counts[candidate];
|
|
231
|
+
donor = candidate;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (donor) {
|
|
235
|
+
counts[donor] -= 1;
|
|
236
|
+
counts[key] += 1;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return counts;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Extracts normalized token breakdown from either a telemetry state object or direct options.
|
|
247
|
+
*
|
|
248
|
+
* @param {object} [input={}]
|
|
249
|
+
* @returns {{
|
|
250
|
+
* modelId: string,
|
|
251
|
+
* totalInputTokens: number,
|
|
252
|
+
* systemTokens: number,
|
|
253
|
+
* cachedHistoryTokens: number,
|
|
254
|
+
* distractorTokens: number,
|
|
255
|
+
* writeDeltaTokens: number
|
|
256
|
+
* }}
|
|
257
|
+
*/
|
|
258
|
+
export function extractHorizonTokens(input = {}) {
|
|
259
|
+
const cw = input.contextWindow || input;
|
|
260
|
+
const modelId = normalizeModelId(input.modelId || input.model || 'claude-sonnet-5-5');
|
|
261
|
+
|
|
262
|
+
const rawSystem = Math.max(
|
|
263
|
+
0,
|
|
264
|
+
Number(cw.systemAndMcpTokens ?? input.systemAndMcpTokens ?? input.systemTokens ?? 0)
|
|
265
|
+
);
|
|
266
|
+
const rawDistractor = Math.max(
|
|
267
|
+
0,
|
|
268
|
+
Number(cw.distractorTokens ?? input.distractorTokens ?? 0)
|
|
269
|
+
);
|
|
270
|
+
const rawWrite = Math.max(
|
|
271
|
+
0,
|
|
272
|
+
Number(
|
|
273
|
+
cw.lastWriteTokens ??
|
|
274
|
+
input.writeDeltaTokens ??
|
|
275
|
+
input.cacheWriteTokens ??
|
|
276
|
+
input.cacheCreationTokens ??
|
|
277
|
+
cw.cacheCreationTokens ??
|
|
278
|
+
0
|
|
279
|
+
) + Number(cw.uncachedInputTokens ?? input.uncachedInputTokens ?? 0)
|
|
280
|
+
);
|
|
281
|
+
const rawCached = Math.max(
|
|
282
|
+
0,
|
|
283
|
+
Number(cw.cachedTokens ?? input.cachedTokens ?? input.cacheReadTokens ?? 0)
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
const explicitTotal = Number(
|
|
287
|
+
cw.totalInputTokens ?? input.totalInputTokens ?? input.totalTokens ?? 0
|
|
288
|
+
);
|
|
289
|
+
const sumParts = rawSystem + rawDistractor + rawWrite + rawCached;
|
|
290
|
+
const totalInputTokens = Math.max(0, explicitTotal > 0 ? explicitTotal : sumParts);
|
|
291
|
+
|
|
292
|
+
if (totalInputTokens === 0) {
|
|
293
|
+
return {
|
|
294
|
+
modelId,
|
|
295
|
+
totalInputTokens: 0,
|
|
296
|
+
systemTokens: 0,
|
|
297
|
+
cachedHistoryTokens: 0,
|
|
298
|
+
distractorTokens: 0,
|
|
299
|
+
writeDeltaTokens: 0
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Clamp S, D, W so they never exceed totalInputTokens, and assign the remainder to C (Warm Cached History)
|
|
304
|
+
const systemTokens = Math.min(totalInputTokens, rawSystem);
|
|
305
|
+
const remainingAfterS = Math.max(0, totalInputTokens - systemTokens);
|
|
306
|
+
const distractorTokens = Math.min(remainingAfterS, rawDistractor);
|
|
307
|
+
const remainingAfterD = Math.max(0, remainingAfterS - distractorTokens);
|
|
308
|
+
const writeDeltaTokens = Math.min(remainingAfterD, rawWrite);
|
|
309
|
+
const cachedHistoryTokens = Math.max(0, remainingAfterD - writeDeltaTokens);
|
|
310
|
+
|
|
311
|
+
return {
|
|
312
|
+
modelId,
|
|
313
|
+
totalInputTokens,
|
|
314
|
+
systemTokens,
|
|
315
|
+
cachedHistoryTokens,
|
|
316
|
+
distractorTokens,
|
|
317
|
+
writeDeltaTokens
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Computes the 40-Cell Dynamic Horizon Grid and its ASCII/Unicode/ANSI representations.
|
|
323
|
+
*
|
|
324
|
+
* @param {object} [input={}]
|
|
325
|
+
* @param {object} [options={}]
|
|
326
|
+
* @param {number} [options.cellCount=40]
|
|
327
|
+
* @returns {{
|
|
328
|
+
* cellCount: number,
|
|
329
|
+
* modelId: string,
|
|
330
|
+
* zoomTier: { id: '100k'|'200k'|'1M', ceilingTokens: number, tokensPerCell: number, label: string },
|
|
331
|
+
* totalInputTokens: number,
|
|
332
|
+
* utilizationPct: number,
|
|
333
|
+
* occupiedCells: number,
|
|
334
|
+
* emptyCells: number,
|
|
335
|
+
* tokens: { S: number, C: number, D: number, W: number },
|
|
336
|
+
* cellCounts: { S: number, C: number, D: number, W: number, empty: number },
|
|
337
|
+
* cells: Array<{ index: number, code: string, tag: string, colorHex: string, label: string }>,
|
|
338
|
+
* barChars: string,
|
|
339
|
+
* bracketedBar: string,
|
|
340
|
+
* unicodeBar: string,
|
|
341
|
+
* ansiBar: string,
|
|
342
|
+
* ansiCodeBar: string,
|
|
343
|
+
* legend: string
|
|
344
|
+
* }}
|
|
345
|
+
*/
|
|
346
|
+
export function computeHorizonGrid(input = {}, options = {}) {
|
|
347
|
+
const cellCount = Math.max(8, Number(options.cellCount || GRID_CELL_COUNT));
|
|
348
|
+
const extracted = extractHorizonTokens(input);
|
|
349
|
+
const rawModel = input.rawModelId || input.modelId || input.model || extracted.modelId;
|
|
350
|
+
|
|
351
|
+
const explicitCustomProp = Number(
|
|
352
|
+
options.customCeiling ??
|
|
353
|
+
options.contextCeiling ??
|
|
354
|
+
input.customCeiling ??
|
|
355
|
+
input.contextWindow?.customCeiling ??
|
|
356
|
+
0
|
|
357
|
+
);
|
|
358
|
+
const rawInputCeiling = Number(
|
|
359
|
+
input.contextCeiling ?? input.contextWindow?.contextCeiling ?? 0
|
|
360
|
+
);
|
|
361
|
+
const isNonStandardInputCeiling =
|
|
362
|
+
rawInputCeiling > 0 &&
|
|
363
|
+
rawInputCeiling !== 200000 &&
|
|
364
|
+
rawInputCeiling !== 1000000 &&
|
|
365
|
+
!(extracted.modelId === 'claude-haiku-5-5' && rawInputCeiling === 100000);
|
|
366
|
+
|
|
367
|
+
const zoomOptions =
|
|
368
|
+
explicitCustomProp > 0
|
|
369
|
+
? { ...options, customCeiling: explicitCustomProp }
|
|
370
|
+
: isNonStandardInputCeiling
|
|
371
|
+
? { ...options, customCeiling: rawInputCeiling }
|
|
372
|
+
: options;
|
|
373
|
+
|
|
374
|
+
const zoomTier = resolveHorizonZoomTier(rawModel, extracted.totalInputTokens, zoomOptions);
|
|
375
|
+
|
|
376
|
+
// Scale tokensPerCell if a custom cellCount (e.g. 16 or 20 for narrow terminal) is requested
|
|
377
|
+
const tokensPerCell =
|
|
378
|
+
cellCount === GRID_CELL_COUNT
|
|
379
|
+
? zoomTier.tokensPerCell
|
|
380
|
+
: Math.max(1, Math.round(zoomTier.ceilingTokens / cellCount));
|
|
381
|
+
|
|
382
|
+
const rawOccupied =
|
|
383
|
+
extracted.totalInputTokens > 0
|
|
384
|
+
? Math.ceil(extracted.totalInputTokens / tokensPerCell)
|
|
385
|
+
: 0;
|
|
386
|
+
const occupiedCells = Math.max(0, Math.min(cellCount, rawOccupied));
|
|
387
|
+
const emptyCells = Math.max(0, cellCount - occupiedCells);
|
|
388
|
+
|
|
389
|
+
const tokenBreakdown = {
|
|
390
|
+
S: extracted.systemTokens,
|
|
391
|
+
C: extracted.cachedHistoryTokens,
|
|
392
|
+
D: extracted.distractorTokens,
|
|
393
|
+
W: extracted.writeDeltaTokens
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
const allocated = allocateCategoryCells(tokenBreakdown, occupiedCells);
|
|
397
|
+
const cellCounts = {
|
|
398
|
+
S: allocated.S,
|
|
399
|
+
C: allocated.C,
|
|
400
|
+
D: allocated.D,
|
|
401
|
+
W: allocated.W,
|
|
402
|
+
empty: emptyCells
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
const cells = [];
|
|
406
|
+
const orderedCodes = [
|
|
407
|
+
...Array(cellCounts.S).fill('S'),
|
|
408
|
+
...Array(cellCounts.C).fill('C'),
|
|
409
|
+
...Array(cellCounts.D).fill('D'),
|
|
410
|
+
...Array(cellCounts.W).fill('W'),
|
|
411
|
+
...Array(cellCounts.empty).fill('.')
|
|
412
|
+
];
|
|
413
|
+
|
|
414
|
+
const noColor = isNoColorEnabled(options);
|
|
415
|
+
let barChars = '';
|
|
416
|
+
let unicodeBar = '';
|
|
417
|
+
let ansiBar = '';
|
|
418
|
+
let ansiCodeBar = '';
|
|
419
|
+
|
|
420
|
+
for (let i = 0; i < cellCount; i++) {
|
|
421
|
+
const code = orderedCodes[i] || '.';
|
|
422
|
+
const meta = code === '.' ? CELL_CATEGORIES.EMPTY : CELL_CATEGORIES[code];
|
|
423
|
+
cells.push({
|
|
424
|
+
index: i,
|
|
425
|
+
code: meta.code,
|
|
426
|
+
tag: meta.tag,
|
|
427
|
+
colorHex: meta.colorHex,
|
|
428
|
+
label: meta.label
|
|
429
|
+
});
|
|
430
|
+
barChars += meta.code;
|
|
431
|
+
unicodeBar += meta.blockChar;
|
|
432
|
+
if (noColor) {
|
|
433
|
+
ansiBar += meta.blockChar;
|
|
434
|
+
ansiCodeBar += meta.code;
|
|
435
|
+
} else {
|
|
436
|
+
ansiBar += `${meta.ansiFg}${meta.blockChar}${ANSI_RESET}`;
|
|
437
|
+
ansiCodeBar += `${meta.ansiFg}${meta.code}${ANSI_RESET}`;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const utilizationPct = Number(
|
|
442
|
+
Math.min(100, (extracted.totalInputTokens / zoomTier.ceilingTokens) * 100).toFixed(1)
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
const legend = '[S] Sys/MCP [C] Warm Cache [D] Distractor [W] 1.25x Write';
|
|
446
|
+
|
|
447
|
+
return {
|
|
448
|
+
cellCount,
|
|
449
|
+
modelId: extracted.modelId,
|
|
450
|
+
zoomTier,
|
|
451
|
+
totalInputTokens: extracted.totalInputTokens,
|
|
452
|
+
utilizationPct,
|
|
453
|
+
occupiedCells,
|
|
454
|
+
emptyCells,
|
|
455
|
+
tokens: tokenBreakdown,
|
|
456
|
+
cellCounts,
|
|
457
|
+
cells,
|
|
458
|
+
barChars,
|
|
459
|
+
bracketedBar: `[${barChars}]`,
|
|
460
|
+
unicodeBar,
|
|
461
|
+
ansiBar,
|
|
462
|
+
ansiCodeBar: `[${ansiCodeBar}]`,
|
|
463
|
+
legend
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export default {
|
|
468
|
+
GRID_CELL_COUNT,
|
|
469
|
+
CELL_CATEGORIES,
|
|
470
|
+
ZOOM_TIERS,
|
|
471
|
+
isNoColorEnabled,
|
|
472
|
+
resolveHorizonZoomTier,
|
|
473
|
+
extractHorizonTokens,
|
|
474
|
+
computeHorizonGrid
|
|
475
|
+
};
|