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,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/core-isomorphic/economics/billingBlocks.mjs
|
|
3
|
+
* Feature 22: 5-Hour Rolling Billing Blocks & Subagent Cost Attribution.
|
|
4
|
+
*
|
|
5
|
+
* Implements:
|
|
6
|
+
* - 18,000,000 ms (5-hour) rolling billing window partitioning
|
|
7
|
+
* - Velocity metrics: burn rate in USD/hour and tokens/min per block
|
|
8
|
+
* - Per-subagent spend and token attribution
|
|
9
|
+
* - Cumulative session cost calculation across active rolling blocks
|
|
10
|
+
* - Clean empty-array handling returning `[]`
|
|
11
|
+
* - Resilient input validation: filters null/undefined events and sanitizes non-numeric costs/tokens
|
|
12
|
+
*
|
|
13
|
+
* Adheres strictly to Claude Mod Laws 1–5: pure ESM, zero `node:*` imports, zero Node globals.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export const BILLING_BLOCK_CONSTANTS = Object.freeze({
|
|
17
|
+
BLOCK_DURATION_MS: 5 * 3600 * 1000, // 18,000,000 ms (5 hours)
|
|
18
|
+
BLOCK_DURATION_HOURS: 5.0,
|
|
19
|
+
TOKENS_PER_MIN_DIVISOR: 300 // 5 hours * 60 minutes
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Partitions continuous session token usage events into rolling 5-hour billing blocks.
|
|
24
|
+
*
|
|
25
|
+
* @param {Array<{ timestamp: number, costUsd?: number, tokens?: number, subagentId?: string }>} [usageEvents=[]]
|
|
26
|
+
* @param {number} [blockDurationMs=18000000] Block window in ms (default 5 hours)
|
|
27
|
+
* @returns {Array<{
|
|
28
|
+
* blockIndex: number,
|
|
29
|
+
* startTime: number,
|
|
30
|
+
* endTime: number,
|
|
31
|
+
* totalCostUsd: number,
|
|
32
|
+
* totalTokens: number,
|
|
33
|
+
* eventCount: number,
|
|
34
|
+
* subagentSpend: Record<string, number>,
|
|
35
|
+
* burnRateUsdPerHour: number,
|
|
36
|
+
* tokensPerMin: number
|
|
37
|
+
* }>}
|
|
38
|
+
*/
|
|
39
|
+
export function partitionBillingBlocks(
|
|
40
|
+
usageEvents = [],
|
|
41
|
+
blockDurationMs = BILLING_BLOCK_CONSTANTS.BLOCK_DURATION_MS
|
|
42
|
+
) {
|
|
43
|
+
if (!Array.isArray(usageEvents) || usageEvents.length === 0) {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Filter usageEvents to keep only valid non-null object entries
|
|
48
|
+
const validEvents = usageEvents.filter(ev => ev && typeof ev === 'object');
|
|
49
|
+
if (validEvents.length === 0) {
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const duration = Number(blockDurationMs) > 0
|
|
54
|
+
? Number(blockDurationMs)
|
|
55
|
+
: BILLING_BLOCK_CONSTANTS.BLOCK_DURATION_MS;
|
|
56
|
+
|
|
57
|
+
const sorted = [...validEvents].sort((a, b) => {
|
|
58
|
+
const tsA = Number(a.timestamp) || 0;
|
|
59
|
+
const tsB = Number(b.timestamp) || 0;
|
|
60
|
+
return tsA - tsB;
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const startTimestamp = Number(sorted[0].timestamp) || 0;
|
|
64
|
+
const blocksMap = new Map();
|
|
65
|
+
|
|
66
|
+
for (const event of sorted) {
|
|
67
|
+
const rawTs = Number(event.timestamp);
|
|
68
|
+
const ts = Number.isFinite(rawTs) ? rawTs : startTimestamp;
|
|
69
|
+
const blockIndex = Math.max(0, Math.floor((ts - startTimestamp) / duration));
|
|
70
|
+
|
|
71
|
+
if (!blocksMap.has(blockIndex)) {
|
|
72
|
+
blocksMap.set(blockIndex, {
|
|
73
|
+
blockIndex,
|
|
74
|
+
startTime: startTimestamp + (blockIndex * duration),
|
|
75
|
+
endTime: startTimestamp + ((blockIndex + 1) * duration),
|
|
76
|
+
totalCostUsd: 0,
|
|
77
|
+
totalTokens: 0,
|
|
78
|
+
eventCount: 0,
|
|
79
|
+
subagentSpend: {}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const block = blocksMap.get(blockIndex);
|
|
84
|
+
const cost = Math.max(0, Number(event.costUsd) || 0);
|
|
85
|
+
const tokens = Math.max(0, Number(event.tokens) || 0);
|
|
86
|
+
const agent = String(event.subagentId || 'main');
|
|
87
|
+
|
|
88
|
+
block.totalCostUsd = parseFloat((block.totalCostUsd + cost).toFixed(4));
|
|
89
|
+
block.totalTokens += tokens;
|
|
90
|
+
block.eventCount += 1;
|
|
91
|
+
block.subagentSpend[agent] = parseFloat(((block.subagentSpend[agent] || 0) + cost).toFixed(4));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const durationHours = duration / 3600000;
|
|
95
|
+
const durationMins = durationHours * 60;
|
|
96
|
+
|
|
97
|
+
return Array.from(blocksMap.values()).map(b => {
|
|
98
|
+
const burnRateUsdPerHour = durationHours > 0
|
|
99
|
+
? parseFloat((b.totalCostUsd / durationHours).toFixed(4))
|
|
100
|
+
: 0;
|
|
101
|
+
const tokensPerMin = durationMins > 0
|
|
102
|
+
? Math.round(b.totalTokens / durationMins)
|
|
103
|
+
: 0;
|
|
104
|
+
return {
|
|
105
|
+
...b,
|
|
106
|
+
burnRateUsdPerHour,
|
|
107
|
+
tokensPerMin
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Attributes total spend and token consumption across subagent IDs.
|
|
114
|
+
*
|
|
115
|
+
* @param {Array<{ timestamp?: number, costUsd?: number, tokens?: number, subagentId?: string }>} [usageEvents=[]]
|
|
116
|
+
* @returns {Record<string, { costUsd: number, tokens: number, pct: number }>}
|
|
117
|
+
*/
|
|
118
|
+
export function attributeSubagentCosts(usageEvents = []) {
|
|
119
|
+
if (!Array.isArray(usageEvents) || usageEvents.length === 0) {
|
|
120
|
+
return {};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const validEvents = usageEvents.filter(ev => ev && typeof ev === 'object');
|
|
124
|
+
if (validEvents.length === 0) {
|
|
125
|
+
return {};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const result = {};
|
|
129
|
+
let totalCost = 0;
|
|
130
|
+
|
|
131
|
+
for (const ev of validEvents) {
|
|
132
|
+
const agent = String(ev.subagentId || 'main');
|
|
133
|
+
const cost = Math.max(0, Number(ev.costUsd) || 0);
|
|
134
|
+
const tokens = Math.max(0, Number(ev.tokens) || 0);
|
|
135
|
+
|
|
136
|
+
if (!result[agent]) {
|
|
137
|
+
result[agent] = { costUsd: 0, tokens: 0, pct: 0 };
|
|
138
|
+
}
|
|
139
|
+
result[agent].costUsd = parseFloat((result[agent].costUsd + cost).toFixed(4));
|
|
140
|
+
result[agent].tokens += tokens;
|
|
141
|
+
totalCost += cost;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
for (const agent of Object.keys(result)) {
|
|
145
|
+
result[agent].pct = totalCost > 0
|
|
146
|
+
? parseFloat(((result[agent].costUsd / totalCost) * 100).toFixed(1))
|
|
147
|
+
: 0;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Computes aggregate summary metrics across multiple billing blocks.
|
|
155
|
+
*
|
|
156
|
+
* @param {Array<object>} blocks
|
|
157
|
+
* @returns {{
|
|
158
|
+
* blockCount: number,
|
|
159
|
+
* cumulativeCostUsd: number,
|
|
160
|
+
* cumulativeTokens: number,
|
|
161
|
+
* maxBurnRateUsdPerHour: number,
|
|
162
|
+
* avgTokensPerMin: number
|
|
163
|
+
* }}
|
|
164
|
+
*/
|
|
165
|
+
export function computeBillingBlockSummary(blocks = []) {
|
|
166
|
+
if (!Array.isArray(blocks) || blocks.length === 0) {
|
|
167
|
+
return {
|
|
168
|
+
blockCount: 0,
|
|
169
|
+
cumulativeCostUsd: 0,
|
|
170
|
+
cumulativeTokens: 0,
|
|
171
|
+
maxBurnRateUsdPerHour: 0,
|
|
172
|
+
avgTokensPerMin: 0
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const validBlocks = blocks.filter(b => b && typeof b === 'object');
|
|
177
|
+
if (validBlocks.length === 0) {
|
|
178
|
+
return {
|
|
179
|
+
blockCount: 0,
|
|
180
|
+
cumulativeCostUsd: 0,
|
|
181
|
+
cumulativeTokens: 0,
|
|
182
|
+
maxBurnRateUsdPerHour: 0,
|
|
183
|
+
avgTokensPerMin: 0
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let totalCost = 0;
|
|
188
|
+
let totalTokens = 0;
|
|
189
|
+
let maxBurn = 0;
|
|
190
|
+
let sumTokPerMin = 0;
|
|
191
|
+
|
|
192
|
+
for (const b of validBlocks) {
|
|
193
|
+
const cost = Math.max(0, Number(b.totalCostUsd) || 0);
|
|
194
|
+
const tokens = Math.max(0, Number(b.totalTokens) || 0);
|
|
195
|
+
const burn = Math.max(0, Number(b.burnRateUsdPerHour) || 0);
|
|
196
|
+
const tokPerMin = Math.max(0, Number(b.tokensPerMin) || 0);
|
|
197
|
+
|
|
198
|
+
totalCost += cost;
|
|
199
|
+
totalTokens += tokens;
|
|
200
|
+
if (burn > maxBurn) {
|
|
201
|
+
maxBurn = burn;
|
|
202
|
+
}
|
|
203
|
+
sumTokPerMin += tokPerMin;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
blockCount: validBlocks.length,
|
|
208
|
+
cumulativeCostUsd: parseFloat(totalCost.toFixed(4)),
|
|
209
|
+
cumulativeTokens: totalTokens,
|
|
210
|
+
maxBurnRateUsdPerHour: parseFloat(maxBurn.toFixed(4)),
|
|
211
|
+
avgTokensPerMin: Math.round(sumTokPerMin / validBlocks.length)
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Stateful RollingBillingBlockTracker.
|
|
217
|
+
*/
|
|
218
|
+
export class RollingBillingBlockTracker {
|
|
219
|
+
constructor(options = {}) {
|
|
220
|
+
const opts = (options && typeof options === 'object') ? options : {};
|
|
221
|
+
this.blockDurationMs = Number(opts.blockDurationMs) > 0
|
|
222
|
+
? Number(opts.blockDurationMs)
|
|
223
|
+
: BILLING_BLOCK_CONSTANTS.BLOCK_DURATION_MS;
|
|
224
|
+
this.events = [];
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
recordEvent(event = {}) {
|
|
228
|
+
if (!event || typeof event !== 'object') {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const rawTs = Number(event.timestamp);
|
|
232
|
+
this.events.push({
|
|
233
|
+
timestamp: Number.isFinite(rawTs) ? rawTs : Date.now(),
|
|
234
|
+
costUsd: Math.max(0, Number(event.costUsd) || 0),
|
|
235
|
+
tokens: Math.max(0, Number(event.tokens) || 0),
|
|
236
|
+
subagentId: String(event.subagentId || 'main')
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
getBlocks() {
|
|
241
|
+
return partitionBillingBlocks(this.events, this.blockDurationMs);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
getSummary() {
|
|
245
|
+
return computeBillingBlockSummary(this.getBlocks());
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
getSubagentAttribution() {
|
|
249
|
+
return attributeSubagentCosts(this.events);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/core-isomorphic/economics/cacheGravity.mjs
|
|
3
|
+
* Feature 19: Algebraic C* Break-Even Cache Gravity Router.
|
|
4
|
+
*
|
|
5
|
+
* Implements the exact algebraic cross-model break-even equation:
|
|
6
|
+
* C* = [ O * (P_out,curr - P_out,target) + Delta_uncached * (P_write,curr - P_write,target) ]
|
|
7
|
+
* / [ P_write,target - P_read,curr ]
|
|
8
|
+
*
|
|
9
|
+
* Canonical test values at O = 1,500, Delta = 0:
|
|
10
|
+
* - Opus 5.5 -> Sonnet 5.5: C* = 6,522 tokens
|
|
11
|
+
* - Opus 4.6 -> Sonnet 4.6: C* = 4,615 tokens
|
|
12
|
+
*
|
|
13
|
+
* Boundary rules:
|
|
14
|
+
* - Capability upshift (P_out,curr <= P_out,target): C* = 0, shouldHold = false
|
|
15
|
+
* - Cheaper target write than current read (P_write,target <= P_read,curr): C* = Infinity
|
|
16
|
+
* - Identical models: C* = 0, shouldHold = false
|
|
17
|
+
* - Cold cache (cachedTokens = 0, cacheWarm = false): switch is cheaper if target is cheaper
|
|
18
|
+
* - Warm cache (cachedTokens > C*): shouldHold = true
|
|
19
|
+
*
|
|
20
|
+
* Adheres strictly to Claude Mod Laws 1–5: pure ESM, zero `node:*` imports, zero Node globals.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
normalizeModelId,
|
|
25
|
+
computeTotalPromptTokens,
|
|
26
|
+
resolveEffectiveRates
|
|
27
|
+
} from '../config/catalogLoader.mjs';
|
|
28
|
+
|
|
29
|
+
export const DEFAULT_EXPECTED_OUTPUT_TOKENS = 1500;
|
|
30
|
+
|
|
31
|
+
export const EXPECTED_OUTPUT_TOKENS_BY_EFFORT = Object.freeze({
|
|
32
|
+
low: 800,
|
|
33
|
+
medium: 1500,
|
|
34
|
+
high: 3500,
|
|
35
|
+
max: 6000
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export const FLIGHT_MODES = Object.freeze({
|
|
39
|
+
OFF: 'OFF',
|
|
40
|
+
SILENT: 'SILENT',
|
|
41
|
+
COACH: 'COACH',
|
|
42
|
+
AUTO_PILOT: 'AUTO_PILOT'
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export const MODEL_BREAK_EVEN_RATES = Object.freeze({
|
|
46
|
+
'claude-opus-5-5': { output: 20.0, read: 0.2, write5m: 5.0, write1h: 8.0 },
|
|
47
|
+
'claude-sonnet-5-5': { output: 10.0, read: 0.1, write5m: 2.5, write1h: 4.0 },
|
|
48
|
+
'claude-opus-4-6': { output: 25.0, read: 0.5, write5m: 6.25, write1h: 10.0 },
|
|
49
|
+
'claude-sonnet-4-6': { output: 15.0, read: 0.3, write5m: 3.75, write1h: 6.0 },
|
|
50
|
+
'claude-haiku-5-5': { output: 0.5, read: 0.01, write5m: 0.125, write1h: 0.2 },
|
|
51
|
+
'claude-haiku-4-5': { output: 5.0, read: 0.10, write5m: 1.25, write1h: 2.0 },
|
|
52
|
+
'claude-fable-5-2': { output: 40.0, read: 0.20, write5m: 10.0, write1h: 16.0 },
|
|
53
|
+
'claude-fable-5-1': { output: 50.0, read: 0.25, write5m: 12.5, write1h: 20.0 }
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Maps an active model to its natural one-tier downshift peer.
|
|
58
|
+
* @param {string} [modelId='claude-opus-5-5']
|
|
59
|
+
* @returns {string}
|
|
60
|
+
*/
|
|
61
|
+
export function resolveNaturalDownshiftPeer(modelId = 'claude-opus-5-5') {
|
|
62
|
+
const norm = normalizeModelId(modelId);
|
|
63
|
+
switch (norm) {
|
|
64
|
+
case 'claude-mythos-5-2':
|
|
65
|
+
case 'claude-mythos-5-1':
|
|
66
|
+
case 'claude-mythos-5':
|
|
67
|
+
case 'claude-fable-5-2':
|
|
68
|
+
case 'claude-fable-5-1':
|
|
69
|
+
case 'claude-fable-5':
|
|
70
|
+
return 'claude-opus-5-5';
|
|
71
|
+
case 'claude-opus-5-5':
|
|
72
|
+
return 'claude-sonnet-5-5';
|
|
73
|
+
case 'claude-opus-5':
|
|
74
|
+
return 'claude-sonnet-5';
|
|
75
|
+
case 'claude-opus-4-8':
|
|
76
|
+
case 'claude-opus-4-7':
|
|
77
|
+
case 'claude-opus-4-6':
|
|
78
|
+
case 'claude-opus-4-5':
|
|
79
|
+
case 'claude-opus-4-1':
|
|
80
|
+
case 'claude-opus-4-0':
|
|
81
|
+
return 'claude-sonnet-4-6';
|
|
82
|
+
case 'claude-sonnet-5-5':
|
|
83
|
+
case 'claude-sonnet-5':
|
|
84
|
+
return 'claude-haiku-5-5';
|
|
85
|
+
case 'claude-sonnet-4-6':
|
|
86
|
+
case 'claude-sonnet-4-5':
|
|
87
|
+
case 'claude-sonnet-4-0':
|
|
88
|
+
return 'claude-haiku-4-5';
|
|
89
|
+
case 'claude-3-7-sonnet':
|
|
90
|
+
case 'claude-3-5-sonnet':
|
|
91
|
+
case 'claude-3-5-haiku':
|
|
92
|
+
return 'claude-3-5-haiku';
|
|
93
|
+
case 'claude-haiku-4-5':
|
|
94
|
+
case 'claude-haiku-5-5':
|
|
95
|
+
return 'claude-haiku-5-5';
|
|
96
|
+
default:
|
|
97
|
+
return 'claude-sonnet-5-5';
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Closed-form break-even equation computation.
|
|
103
|
+
*
|
|
104
|
+
* @param {number} expectedOutputTokens O
|
|
105
|
+
* @param {number} pOutCurr P_out,curr
|
|
106
|
+
* @param {number} pOutTarget P_out,target
|
|
107
|
+
* @param {number} pWriteTarget P_write,target
|
|
108
|
+
* @param {number} pReadCurr P_read,curr
|
|
109
|
+
* @param {number} [uncachedDeltaTokens=0] Delta_uncached
|
|
110
|
+
* @param {number} [pWriteCurr=pWriteTarget] P_write,curr
|
|
111
|
+
* @returns {number} C* (rounded integer, 0 for upshifts, Infinity if write <= read)
|
|
112
|
+
*/
|
|
113
|
+
export function computeClosedFormBreakEven(
|
|
114
|
+
expectedOutputTokens,
|
|
115
|
+
pOutCurr,
|
|
116
|
+
pOutTarget,
|
|
117
|
+
pWriteTarget,
|
|
118
|
+
pReadCurr,
|
|
119
|
+
uncachedDeltaTokens = 0,
|
|
120
|
+
pWriteCurr = pWriteTarget
|
|
121
|
+
) {
|
|
122
|
+
if (pOutCurr <= pOutTarget) {
|
|
123
|
+
return 0; // Capability upshift
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const numerator =
|
|
127
|
+
expectedOutputTokens * (pOutCurr - pOutTarget) +
|
|
128
|
+
uncachedDeltaTokens * (pWriteCurr - pWriteTarget);
|
|
129
|
+
|
|
130
|
+
const denominator = pWriteTarget - pReadCurr;
|
|
131
|
+
|
|
132
|
+
if (numerator <= 0) {
|
|
133
|
+
return 0;
|
|
134
|
+
}
|
|
135
|
+
if (denominator <= 0) {
|
|
136
|
+
return Infinity;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return Math.round(numerator / denominator);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Calculates the exact algebraic break-even token count C*.
|
|
144
|
+
* Supports invocation via options object or positional arguments.
|
|
145
|
+
*
|
|
146
|
+
* Canonical test values:
|
|
147
|
+
* - Opus 5.5 -> Sonnet 5.5 (default): C* = 6,522
|
|
148
|
+
* - Opus 4.6 -> Sonnet 4.6: C* = 4,615
|
|
149
|
+
*
|
|
150
|
+
* @param {object|number} [optionsOrOutputTokens={}]
|
|
151
|
+
* @param {object} [maybeRatesCurr]
|
|
152
|
+
* @param {object} [maybeRatesTarget]
|
|
153
|
+
* @returns {number}
|
|
154
|
+
*/
|
|
155
|
+
export function calculateBreakEvenCStar(optionsOrOutputTokens = {}, maybeRatesCurr, maybeRatesTarget) {
|
|
156
|
+
let expectedOutputTokens = DEFAULT_EXPECTED_OUTPUT_TOKENS;
|
|
157
|
+
let pOutCurr = 20.0;
|
|
158
|
+
let pOutTarget = 10.0;
|
|
159
|
+
let pWrite5mTarget = 2.5;
|
|
160
|
+
let pReadCurr = 0.2;
|
|
161
|
+
let uncachedDeltaTokens = 0;
|
|
162
|
+
let pWriteCurr = 5.0;
|
|
163
|
+
|
|
164
|
+
if (typeof optionsOrOutputTokens === 'number') {
|
|
165
|
+
expectedOutputTokens = Math.max(0, optionsOrOutputTokens);
|
|
166
|
+
if (maybeRatesCurr && typeof maybeRatesCurr === 'object') {
|
|
167
|
+
pOutCurr = Number(maybeRatesCurr.output ?? maybeRatesCurr.outputPer1M ?? maybeRatesCurr.pOut ?? pOutCurr);
|
|
168
|
+
pReadCurr = Number(maybeRatesCurr.read ?? maybeRatesCurr.readPer1M ?? maybeRatesCurr.pRead ?? pReadCurr);
|
|
169
|
+
pWriteCurr = Number(maybeRatesCurr.write5m ?? maybeRatesCurr.write5mPer1M ?? maybeRatesCurr.write ?? pWriteCurr);
|
|
170
|
+
}
|
|
171
|
+
if (maybeRatesTarget && typeof maybeRatesTarget === 'object') {
|
|
172
|
+
pOutTarget = Number(maybeRatesTarget.output ?? maybeRatesTarget.outputPer1M ?? maybeRatesTarget.pOut ?? pOutTarget);
|
|
173
|
+
pWrite5mTarget = Number(
|
|
174
|
+
maybeRatesTarget.write5m ??
|
|
175
|
+
maybeRatesTarget.write5mPer1M ??
|
|
176
|
+
maybeRatesTarget.write ??
|
|
177
|
+
maybeRatesTarget.pWrite5m ??
|
|
178
|
+
pWrite5mTarget
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
} else if (typeof optionsOrOutputTokens === 'object' && optionsOrOutputTokens !== null) {
|
|
182
|
+
const opts = optionsOrOutputTokens;
|
|
183
|
+
if (opts.expectedOutputTokens !== undefined || opts.O !== undefined) {
|
|
184
|
+
const rawO = Number(opts.expectedOutputTokens ?? opts.O);
|
|
185
|
+
expectedOutputTokens = Number.isFinite(rawO) ? Math.max(0, rawO) : DEFAULT_EXPECTED_OUTPUT_TOKENS;
|
|
186
|
+
}
|
|
187
|
+
if (opts.uncachedDeltaTokens !== undefined) {
|
|
188
|
+
const rawDelta = Number(opts.uncachedDeltaTokens);
|
|
189
|
+
uncachedDeltaTokens = Number.isFinite(rawDelta) ? Math.max(0, rawDelta) : 0;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const curModel = opts.currentModel ? normalizeModelId(opts.currentModel) : null;
|
|
193
|
+
const tgtModel = opts.targetModel ? normalizeModelId(opts.targetModel) : null;
|
|
194
|
+
|
|
195
|
+
if (curModel && MODEL_BREAK_EVEN_RATES[curModel]) {
|
|
196
|
+
const m = MODEL_BREAK_EVEN_RATES[curModel];
|
|
197
|
+
pOutCurr = m.output;
|
|
198
|
+
pReadCurr = m.read;
|
|
199
|
+
pWriteCurr = m.write5m;
|
|
200
|
+
}
|
|
201
|
+
if (tgtModel && MODEL_BREAK_EVEN_RATES[tgtModel]) {
|
|
202
|
+
const m = MODEL_BREAK_EVEN_RATES[tgtModel];
|
|
203
|
+
pOutTarget = m.output;
|
|
204
|
+
pWrite5mTarget = m.write5m;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const rc = opts.ratesCurr || opts.currentRates;
|
|
208
|
+
if (rc && typeof rc === 'object') {
|
|
209
|
+
pOutCurr = Number(rc.output ?? rc.outputPer1M ?? rc.pOut ?? pOutCurr);
|
|
210
|
+
pReadCurr = Number(rc.read ?? rc.readPer1M ?? rc.pRead ?? pReadCurr);
|
|
211
|
+
pWriteCurr = Number(rc.write5m ?? rc.write5mPer1M ?? rc.write ?? pWriteCurr);
|
|
212
|
+
}
|
|
213
|
+
const rt = opts.ratesTarget || opts.targetRates;
|
|
214
|
+
if (rt && typeof rt === 'object') {
|
|
215
|
+
pOutTarget = Number(rt.output ?? rt.outputPer1M ?? rt.pOut ?? pOutTarget);
|
|
216
|
+
pWrite5mTarget = Number(
|
|
217
|
+
rt.write5m ??
|
|
218
|
+
rt.write5mPer1M ??
|
|
219
|
+
rt.write ??
|
|
220
|
+
rt.pWrite5m ??
|
|
221
|
+
pWrite5mTarget
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (opts.pOutCurr !== undefined) pOutCurr = Number(opts.pOutCurr);
|
|
226
|
+
if (opts.pOutTarget !== undefined) pOutTarget = Number(opts.pOutTarget);
|
|
227
|
+
if (opts.pWrite5mTarget !== undefined) pWrite5mTarget = Number(opts.pWrite5mTarget);
|
|
228
|
+
if (opts.pReadCurr !== undefined) pReadCurr = Number(opts.pReadCurr);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return computeClosedFormBreakEven(
|
|
232
|
+
expectedOutputTokens,
|
|
233
|
+
pOutCurr,
|
|
234
|
+
pOutTarget,
|
|
235
|
+
pWrite5mTarget,
|
|
236
|
+
pReadCurr,
|
|
237
|
+
uncachedDeltaTokens,
|
|
238
|
+
pWriteCurr
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Computes exact break-even token thresholds and USD turn costs for staying on current vs switching.
|
|
244
|
+
*
|
|
245
|
+
* @param {object} [params={}]
|
|
246
|
+
* @param {string} [params.currentModel='claude-opus-5-5']
|
|
247
|
+
* @param {string} [params.targetModel='claude-sonnet-5-5']
|
|
248
|
+
* @param {number} [params.expectedOutputTokens=1500]
|
|
249
|
+
* @param {number} [params.cachedTokens=0]
|
|
250
|
+
* @param {number} [params.uncachedDeltaTokens=0]
|
|
251
|
+
* @param {boolean} [params.cacheWarm]
|
|
252
|
+
* @param {'5m'|'1h'} [params.cacheTtlTier='5m']
|
|
253
|
+
* @param {object} [options={}]
|
|
254
|
+
* @returns {object}
|
|
255
|
+
*/
|
|
256
|
+
export function computeBreakEvenCachedTokens(params = {}, options = {}) {
|
|
257
|
+
const rawCurrentModel = params.currentModel || 'claude-opus-5-5';
|
|
258
|
+
const rawTargetModel = params.targetModel || 'claude-sonnet-5-5';
|
|
259
|
+
const currentModel = normalizeModelId(rawCurrentModel);
|
|
260
|
+
const targetModel = normalizeModelId(rawTargetModel);
|
|
261
|
+
const rawExpected = Number(params.expectedOutputTokens ?? DEFAULT_EXPECTED_OUTPUT_TOKENS);
|
|
262
|
+
const expectedOutputTokens = Number.isFinite(rawExpected) ? Math.max(0, rawExpected) : DEFAULT_EXPECTED_OUTPUT_TOKENS;
|
|
263
|
+
const rawCached = Number(params.cachedTokens ?? 0);
|
|
264
|
+
const cachedTokens = Number.isFinite(rawCached) ? Math.max(0, rawCached) : 0;
|
|
265
|
+
const rawUncached = Number(params.uncachedDeltaTokens ?? 0);
|
|
266
|
+
const uncachedDeltaTokens = Number.isFinite(rawUncached) ? Math.max(0, rawUncached) : 0;
|
|
267
|
+
const cacheWarm = params.cacheWarm !== undefined ? Boolean(params.cacheWarm) : cachedTokens > 0;
|
|
268
|
+
const ttlTier = params.cacheTtlTier === '1h' ? '1h' : '5m';
|
|
269
|
+
|
|
270
|
+
const totalPromptTokens = Math.max(
|
|
271
|
+
cachedTokens + uncachedDeltaTokens,
|
|
272
|
+
computeTotalPromptTokens(params)
|
|
273
|
+
);
|
|
274
|
+
const currRates = resolveEffectiveRates(rawCurrentModel, totalPromptTokens, options);
|
|
275
|
+
const targetRates = resolveEffectiveRates(rawTargetModel, totalPromptTokens, options);
|
|
276
|
+
|
|
277
|
+
const pOutCurr = currRates.outputPer1M;
|
|
278
|
+
const pOutTarget = targetRates.outputPer1M;
|
|
279
|
+
const pReadCurr = currRates.readPer1M;
|
|
280
|
+
const pWriteCurr = ttlTier === '1h' ? currRates.write1hPer1M : currRates.write5mPer1M;
|
|
281
|
+
const pWriteTarget = ttlTier === '1h' ? targetRates.write1hPer1M : targetRates.write5mPer1M;
|
|
282
|
+
|
|
283
|
+
const isCapabilityUpshift = pOutCurr <= pOutTarget;
|
|
284
|
+
|
|
285
|
+
let breakEvenTokens = 0;
|
|
286
|
+
if (currentModel === targetModel || isCapabilityUpshift) {
|
|
287
|
+
breakEvenTokens = 0;
|
|
288
|
+
} else {
|
|
289
|
+
breakEvenTokens = computeClosedFormBreakEven(
|
|
290
|
+
expectedOutputTokens,
|
|
291
|
+
pOutCurr,
|
|
292
|
+
pOutTarget,
|
|
293
|
+
pWriteTarget,
|
|
294
|
+
pReadCurr,
|
|
295
|
+
uncachedDeltaTokens,
|
|
296
|
+
pWriteCurr
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Exact USD turn cost if staying on currentModel
|
|
301
|
+
const stayPrefixCostUSD = cacheWarm
|
|
302
|
+
? (cachedTokens / 1_000_000) * pReadCurr + (uncachedDeltaTokens / 1_000_000) * pWriteCurr
|
|
303
|
+
: ((cachedTokens + uncachedDeltaTokens) / 1_000_000) * pWriteCurr;
|
|
304
|
+
const stayOutputCostUSD = (expectedOutputTokens / 1_000_000) * pOutCurr;
|
|
305
|
+
const stayOnCurrentModelUSD = stayPrefixCostUSD + stayOutputCostUSD;
|
|
306
|
+
|
|
307
|
+
// Switching to a different model invalidates isolated cache, forcing cold write
|
|
308
|
+
const switchPrefixCostUSD =
|
|
309
|
+
currentModel === targetModel && cacheWarm
|
|
310
|
+
? stayPrefixCostUSD
|
|
311
|
+
: ((cachedTokens + uncachedDeltaTokens) / 1_000_000) * pWriteTarget;
|
|
312
|
+
const switchOutputCostUSD = (expectedOutputTokens / 1_000_000) * pOutTarget;
|
|
313
|
+
const switchToTargetModelUSD = switchPrefixCostUSD + switchOutputCostUSD;
|
|
314
|
+
|
|
315
|
+
const coldWritePenaltyUSD = Number(
|
|
316
|
+
(switchToTargetModelUSD - stayOnCurrentModelUSD).toFixed(6)
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
const shouldHoldModel = Boolean(
|
|
320
|
+
currentModel !== targetModel &&
|
|
321
|
+
!isCapabilityUpshift &&
|
|
322
|
+
cacheWarm &&
|
|
323
|
+
coldWritePenaltyUSD > 0 &&
|
|
324
|
+
cachedTokens > breakEvenTokens
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
return {
|
|
328
|
+
currentModel,
|
|
329
|
+
targetModel,
|
|
330
|
+
expectedOutputTokens,
|
|
331
|
+
cachedTokens,
|
|
332
|
+
uncachedDeltaTokens,
|
|
333
|
+
totalPromptTokens,
|
|
334
|
+
cacheWarm,
|
|
335
|
+
cacheTtlTier: ttlTier,
|
|
336
|
+
writeMultiplierLabel: ttlTier === '1h' ? '2.0x' : '1.25x',
|
|
337
|
+
isCapabilityUpshift,
|
|
338
|
+
breakEvenTokens,
|
|
339
|
+
rates: {
|
|
340
|
+
currentOutputPer1M: pOutCurr,
|
|
341
|
+
targetOutputPer1M: pOutTarget,
|
|
342
|
+
currentReadPer1M: pReadCurr,
|
|
343
|
+
currentWritePer1M: pWriteCurr,
|
|
344
|
+
targetWritePer1M: pWriteTarget
|
|
345
|
+
},
|
|
346
|
+
stayOnCurrentModelUSD,
|
|
347
|
+
switchToTargetModelUSD,
|
|
348
|
+
coldWritePenaltyUSD,
|
|
349
|
+
shouldHold: shouldHoldModel,
|
|
350
|
+
shouldHoldModel
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Evaluates cache gravity recommendation for flight controller.
|
|
356
|
+
*
|
|
357
|
+
* @param {object} params
|
|
358
|
+
* @param {object} [options={}]
|
|
359
|
+
* @returns {object}
|
|
360
|
+
*/
|
|
361
|
+
export function evaluateCacheGravity(params = {}, options = {}) {
|
|
362
|
+
const result = computeBreakEvenCachedTokens(params, options);
|
|
363
|
+
const recommendation = result.shouldHoldModel ? 'STAY_ON_CURRENT' : 'SWITCH_TO_TARGET';
|
|
364
|
+
return {
|
|
365
|
+
...result,
|
|
366
|
+
recommendation,
|
|
367
|
+
action: result.shouldHoldModel ? 'hold' : 'allow'
|
|
368
|
+
};
|
|
369
|
+
}
|