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,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/core-isomorphic/economics/cliffBreaker.mjs
|
|
3
|
+
* Feature 20: Haiku 100k 5x Cliff Breaker (DET-12).
|
|
4
|
+
*
|
|
5
|
+
* Implements:
|
|
6
|
+
* - Total prompt token envelope monitoring for Haiku models
|
|
7
|
+
* - Exemption of all non-Haiku models (Sonnet, Opus, Gemini, etc.)
|
|
8
|
+
* - 4-Tier trigger matrix:
|
|
9
|
+
* < 90k: allow (nominal)
|
|
10
|
+
* 90k - 98k: advisory (advise_compact)
|
|
11
|
+
* 98k - 100k: warn / deny (hard circuit breaker trip)
|
|
12
|
+
* >= 100k: halt / deny (emergency halt for 5x cost cliff)
|
|
13
|
+
*
|
|
14
|
+
* Adheres strictly to Claude Mod Laws 1–5: pure ESM, zero `node:*` imports, zero Node globals.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { computeTotalPromptTokens } from '../config/catalogLoader.mjs';
|
|
18
|
+
|
|
19
|
+
export const HAIKU_CLIFF_DETECTOR_ID = 'DET-12';
|
|
20
|
+
|
|
21
|
+
export const HAIKU_CLIFF_THRESHOLDS = Object.freeze({
|
|
22
|
+
ADVISORY_TOKENS: 90000,
|
|
23
|
+
BREAKER_TOKENS: 98000,
|
|
24
|
+
CLIFF_TOKENS: 100000,
|
|
25
|
+
PRICE_MULTIPLIER: 5.0
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export const GEMINI_PRO_200K_CLIFF_THRESHOLDS = Object.freeze({
|
|
29
|
+
ADVISORY_TOKENS: 180000,
|
|
30
|
+
BREAKER_TOKENS: 196000,
|
|
31
|
+
CLIFF_TOKENS: 200000,
|
|
32
|
+
INPUT_MULTIPLIER: 2.0,
|
|
33
|
+
OUTPUT_MULTIPLIER: 1.5,
|
|
34
|
+
PRICE_MULTIPLIER: 2.0
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Checks whether a given model string corresponds to an Anthropic Claude Haiku model.
|
|
39
|
+
* @param {string} [modelId='']
|
|
40
|
+
* @returns {boolean}
|
|
41
|
+
*/
|
|
42
|
+
export function isHaikuModel(modelId = '') {
|
|
43
|
+
return String(modelId || '').toLowerCase().includes('haiku');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Checks whether a given model string corresponds to Gemini 3.1 Pro or Gemini 2.5 Pro
|
|
48
|
+
* (subject to the 200,000-token 2x input / 1.5x output pricing cliff).
|
|
49
|
+
* @param {string} [modelId='']
|
|
50
|
+
* @returns {boolean}
|
|
51
|
+
*/
|
|
52
|
+
export function isGeminiPro200kCliffModel(modelId = '') {
|
|
53
|
+
const lower = String(modelId || '').toLowerCase().trim();
|
|
54
|
+
return /gemini[-_.\s]*(?:3[._-]1|2[._-]5)[-_.\s]*pro\b/.test(lower);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Resolves parameterized pricing cliff metadata for models with stepped prompt pricing
|
|
59
|
+
* (Claude Haiku 5.5 at 100K 5x, Gemini 3.1/2.5 Pro at 200K 2x input / 1.5x output).
|
|
60
|
+
* Returns `null` for models without a prompt pricing cliff (Sonnet, Opus, Fable, Gemini 4, Flash).
|
|
61
|
+
*
|
|
62
|
+
* @param {string} [modelId='']
|
|
63
|
+
* @returns {{
|
|
64
|
+
* family: 'haiku' | 'gemini-pro',
|
|
65
|
+
* advisoryTokens: number,
|
|
66
|
+
* breakerTokens: number,
|
|
67
|
+
* cliffTokens: number,
|
|
68
|
+
* inputMultiplier: number,
|
|
69
|
+
* outputMultiplier: number,
|
|
70
|
+
* priceMultiplier: number,
|
|
71
|
+
* label: string
|
|
72
|
+
* } | null}
|
|
73
|
+
*/
|
|
74
|
+
export function resolveModelCliffSpec(modelId = '') {
|
|
75
|
+
if (isHaikuModel(modelId)) {
|
|
76
|
+
return {
|
|
77
|
+
family: 'haiku',
|
|
78
|
+
advisoryTokens: HAIKU_CLIFF_THRESHOLDS.ADVISORY_TOKENS,
|
|
79
|
+
breakerTokens: HAIKU_CLIFF_THRESHOLDS.BREAKER_TOKENS,
|
|
80
|
+
cliffTokens: HAIKU_CLIFF_THRESHOLDS.CLIFF_TOKENS,
|
|
81
|
+
inputMultiplier: HAIKU_CLIFF_THRESHOLDS.PRICE_MULTIPLIER,
|
|
82
|
+
outputMultiplier: HAIKU_CLIFF_THRESHOLDS.PRICE_MULTIPLIER,
|
|
83
|
+
priceMultiplier: HAIKU_CLIFF_THRESHOLDS.PRICE_MULTIPLIER,
|
|
84
|
+
label: 'Haiku 100k 5x cost cliff'
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (isGeminiPro200kCliffModel(modelId)) {
|
|
88
|
+
return {
|
|
89
|
+
family: 'gemini-pro',
|
|
90
|
+
advisoryTokens: GEMINI_PRO_200K_CLIFF_THRESHOLDS.ADVISORY_TOKENS,
|
|
91
|
+
breakerTokens: GEMINI_PRO_200K_CLIFF_THRESHOLDS.BREAKER_TOKENS,
|
|
92
|
+
cliffTokens: GEMINI_PRO_200K_CLIFF_THRESHOLDS.CLIFF_TOKENS,
|
|
93
|
+
inputMultiplier: GEMINI_PRO_200K_CLIFF_THRESHOLDS.INPUT_MULTIPLIER,
|
|
94
|
+
outputMultiplier: GEMINI_PRO_200K_CLIFF_THRESHOLDS.OUTPUT_MULTIPLIER,
|
|
95
|
+
priceMultiplier: GEMINI_PRO_200K_CLIFF_THRESHOLDS.PRICE_MULTIPLIER,
|
|
96
|
+
label: 'Gemini Pro 200k 2x input / 1.5x output cliff'
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Evaluates prompt token volume against the Haiku 100k 5x price cliff.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} [modelId='']
|
|
106
|
+
* @param {number} [promptTokens=0]
|
|
107
|
+
* @returns {{
|
|
108
|
+
* triggered: boolean,
|
|
109
|
+
* threshold: number,
|
|
110
|
+
* action: 'allow' | 'advisory' | 'warn' | 'halt',
|
|
111
|
+
* reason: string
|
|
112
|
+
* }}
|
|
113
|
+
*/
|
|
114
|
+
export function checkHaiku100kCliffBreaker(modelId = '', promptTokens = 0) {
|
|
115
|
+
const isHaiku = isHaikuModel(modelId);
|
|
116
|
+
const threshold = HAIKU_CLIFF_THRESHOLDS.BREAKER_TOKENS;
|
|
117
|
+
const tokens = Math.max(0, Number(promptTokens) || 0);
|
|
118
|
+
|
|
119
|
+
if (!isHaiku) {
|
|
120
|
+
return {
|
|
121
|
+
triggered: false,
|
|
122
|
+
threshold,
|
|
123
|
+
action: 'allow',
|
|
124
|
+
reason: 'Non-Haiku model; 100k cliff check exempt'
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (tokens >= HAIKU_CLIFF_THRESHOLDS.CLIFF_TOKENS) {
|
|
129
|
+
return {
|
|
130
|
+
triggered: true,
|
|
131
|
+
threshold,
|
|
132
|
+
action: 'halt',
|
|
133
|
+
reason: `Prompt tokens (${tokens.toLocaleString()}) exceeded 100k 5x cost cliff`
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (tokens >= threshold) {
|
|
138
|
+
return {
|
|
139
|
+
triggered: true,
|
|
140
|
+
threshold,
|
|
141
|
+
action: 'warn',
|
|
142
|
+
reason: `Prompt tokens (${tokens.toLocaleString()}) reached Haiku cliff breaker threshold (${threshold})`
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (tokens >= HAIKU_CLIFF_THRESHOLDS.ADVISORY_TOKENS) {
|
|
147
|
+
return {
|
|
148
|
+
triggered: false,
|
|
149
|
+
threshold,
|
|
150
|
+
action: 'advisory',
|
|
151
|
+
reason: `Prompt tokens (${tokens.toLocaleString()}) approaching 90k advisory boundary`
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
triggered: false,
|
|
157
|
+
threshold,
|
|
158
|
+
action: 'allow',
|
|
159
|
+
reason: `Prompt tokens (${tokens.toLocaleString()}) within safe boundary`
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Evaluates DET-12 Haiku Pre-Cliff Breaker conforming to canonical detector schema.
|
|
165
|
+
*
|
|
166
|
+
* @param {object} [params={}]
|
|
167
|
+
* @param {string} [params.modelId='']
|
|
168
|
+
* @param {number} [params.totalInputTokens]
|
|
169
|
+
* @param {number} [params.promptTokens]
|
|
170
|
+
* @returns {{
|
|
171
|
+
* detectorId: 'DET-12',
|
|
172
|
+
* triggered: boolean,
|
|
173
|
+
* decision: 'allow' | 'advise_compact' | 'deny',
|
|
174
|
+
* advisory: boolean,
|
|
175
|
+
* modelId: string,
|
|
176
|
+
* totalInputTokens: number,
|
|
177
|
+
* cliffThresholdTokens: 100000,
|
|
178
|
+
* reason: string
|
|
179
|
+
* }}
|
|
180
|
+
*/
|
|
181
|
+
export function evaluateDet12HaikuPreCliffBreaker(params = {}) {
|
|
182
|
+
const safeParams = (params && typeof params === 'object') ? params : {};
|
|
183
|
+
const modelId = String(safeParams.modelId || '');
|
|
184
|
+
|
|
185
|
+
// Extract candidate token volume, prioritizing totalInputTokens, then promptTokens, then computeTotalPromptTokens
|
|
186
|
+
let candidate = safeParams.totalInputTokens;
|
|
187
|
+
if (candidate === undefined || candidate === null || Number.isNaN(Number(candidate))) {
|
|
188
|
+
candidate = safeParams.promptTokens;
|
|
189
|
+
}
|
|
190
|
+
if (candidate === undefined || candidate === null || Number.isNaN(Number(candidate))) {
|
|
191
|
+
candidate = computeTotalPromptTokens(safeParams);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const totalTokens = Math.max(0, Number(candidate) || 0);
|
|
195
|
+
const projectedDelta = Math.max(0, Number(safeParams.projectedDeltaTokens) || 0);
|
|
196
|
+
const effectiveTokens = totalTokens + projectedDelta;
|
|
197
|
+
|
|
198
|
+
const cliffSpec = resolveModelCliffSpec(modelId);
|
|
199
|
+
if (!cliffSpec || effectiveTokens < cliffSpec.advisoryTokens) {
|
|
200
|
+
return {
|
|
201
|
+
detectorId: HAIKU_CLIFF_DETECTOR_ID,
|
|
202
|
+
triggered: false,
|
|
203
|
+
decision: 'allow',
|
|
204
|
+
advisory: false,
|
|
205
|
+
modelId,
|
|
206
|
+
totalInputTokens: effectiveTokens,
|
|
207
|
+
cliffThresholdTokens: cliffSpec ? cliffSpec.cliffTokens : HAIKU_CLIFF_THRESHOLDS.CLIFF_TOKENS,
|
|
208
|
+
reason: cliffSpec
|
|
209
|
+
? `${cliffSpec.family === 'haiku' ? 'Haiku' : 'Gemini Pro'} prompt size (${effectiveTokens.toLocaleString()}) is within safe volume (<${Math.round(cliffSpec.advisoryTokens / 1000)}k).`
|
|
210
|
+
: `Non-cliff model (${modelId || 'unspecified'}); exempt from DET-12 pricing cliff breaker.`
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const isHardDeny = effectiveTokens >= cliffSpec.breakerTokens;
|
|
215
|
+
const breakerLabelK = Math.round(cliffSpec.breakerTokens / 1000);
|
|
216
|
+
const advisoryLabelK = Math.round(cliffSpec.advisoryTokens / 1000);
|
|
217
|
+
const cliffLabelK = Math.round(cliffSpec.cliffTokens / 1000);
|
|
218
|
+
const multDesc =
|
|
219
|
+
cliffSpec.family === 'haiku'
|
|
220
|
+
? '5x price jump'
|
|
221
|
+
: '2x input / 1.5x output price jump';
|
|
222
|
+
|
|
223
|
+
const familyTag = cliffSpec.family === 'haiku' ? 'Haiku' : 'Gemini Pro';
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
detectorId: HAIKU_CLIFF_DETECTOR_ID,
|
|
227
|
+
triggered: true,
|
|
228
|
+
decision: isHardDeny ? 'deny' : 'advise_compact',
|
|
229
|
+
advisory: !isHardDeny,
|
|
230
|
+
modelId,
|
|
231
|
+
totalInputTokens: effectiveTokens,
|
|
232
|
+
cliffThresholdTokens: cliffSpec.cliffTokens,
|
|
233
|
+
cliffSpec,
|
|
234
|
+
reason: isHardDeny
|
|
235
|
+
? `[DET-12 ${familyTag} Cliff Breaker] Prompt volume (${effectiveTokens.toLocaleString()} tokens) reached or exceeded ${breakerLabelK}k buffer. Tripping circuit breaker before ${cliffLabelK}k ${multDesc}.`
|
|
236
|
+
: `[DET-12 ${familyTag} ${advisoryLabelK}k Pre-Cliff Breaker] Prompt volume (${effectiveTokens.toLocaleString()} tokens) approaching ${cliffLabelK}k ${multDesc}. Compact now to avoid stepped billing penalty.`
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Parameterized cross-provider pricing cliff breaker (`Haiku 5.5` 100K 5x + `Gemini 3.1/2.5 Pro` 200K 2x/1.5x).
|
|
242
|
+
* @param {object} [params={}]
|
|
243
|
+
*/
|
|
244
|
+
export function evaluateParameterizedCliffBreaker(params = {}) {
|
|
245
|
+
return evaluateDet12HaikuPreCliffBreaker(params);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Convenience wrapper returning unified breaker decision.
|
|
250
|
+
* @param {string} modelId
|
|
251
|
+
* @param {number} promptTokens
|
|
252
|
+
* @returns {object}
|
|
253
|
+
*/
|
|
254
|
+
export function evaluateHaikuCliffBreaker(modelId, promptTokens) {
|
|
255
|
+
return checkHaiku100kCliffBreaker(modelId, promptTokens);
|
|
256
|
+
}
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/core-isomorphic/economics/driftLatency.mjs
|
|
3
|
+
* Feature 17: Time-To-First-Token (TTFT) & Thinking vs. Generation Drift Detection.
|
|
4
|
+
*
|
|
5
|
+
* Implements:
|
|
6
|
+
* - TTFT latency tracking in milliseconds (null / "—" on <= 0 or clock skew)
|
|
7
|
+
* - Latency spike detection (> 5x baseline)
|
|
8
|
+
* - Thinking vs. generation token drift ratio (reported tokens only, non-estimated)
|
|
9
|
+
* - Scratchpad loop detection: canonical DET-30 (preserving DET-21 alias)
|
|
10
|
+
*
|
|
11
|
+
* Adheres strictly to Claude Mod Laws 1–5: pure ESM, zero `node:*` imports, zero Node globals.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export const THINKING_DRIFT_DETECTOR_ID = 'DET-30';
|
|
15
|
+
export const LEGACY_THINKING_DRIFT_ALIAS = 'DET-21';
|
|
16
|
+
|
|
17
|
+
export const DRIFT_THRESHOLDS = Object.freeze({
|
|
18
|
+
MIN_THINKING_TOKENS: 4000,
|
|
19
|
+
LOOP_RATIO: 12.0,
|
|
20
|
+
ELEVATED_RATIO: 4.0,
|
|
21
|
+
CUMULATIVE_THINKING_TOKENS: 10000,
|
|
22
|
+
CONSECUTIVE_TURNS_CRITICAL: 2,
|
|
23
|
+
ROLLING_WINDOW: 3,
|
|
24
|
+
SPIKE_MULTIPLIER: 5.0
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export const DRIFT_STATES = Object.freeze({
|
|
28
|
+
NOMINAL: 'nominal',
|
|
29
|
+
ELEVATED: 'elevated',
|
|
30
|
+
LOOP_SUSPECT: 'loop_suspect'
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Creates an empty initial thinking drift accumulator.
|
|
35
|
+
* @returns {object}
|
|
36
|
+
*/
|
|
37
|
+
export function createInitialThinkingDrift() {
|
|
38
|
+
return {
|
|
39
|
+
measured: false,
|
|
40
|
+
turns: [],
|
|
41
|
+
lastTtftMs: null,
|
|
42
|
+
avgTtftMs: null,
|
|
43
|
+
lastDriftRatio: null,
|
|
44
|
+
rollingDriftRatio: null,
|
|
45
|
+
cumulativeThinkingTokens: 0,
|
|
46
|
+
cumulativeOutputTokens: 0,
|
|
47
|
+
consecutiveLoopTurns: 0,
|
|
48
|
+
loop: {
|
|
49
|
+
triggered: false,
|
|
50
|
+
severity: 'nominal',
|
|
51
|
+
gaDetectorId: THINKING_DRIFT_DETECTOR_ID,
|
|
52
|
+
aliasDetectorId: LEGACY_THINKING_DRIFT_ALIAS,
|
|
53
|
+
consecutiveLoopTurns: 0,
|
|
54
|
+
cumulativeThinkingTokens: 0,
|
|
55
|
+
message: ''
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Formats millisecond TTFT into a human-readable high data-ink string.
|
|
62
|
+
* Clamps <= 0 or invalid numbers to '—' (never fabricated 0ms).
|
|
63
|
+
*
|
|
64
|
+
* @param {number|null|undefined} ms
|
|
65
|
+
* @returns {string}
|
|
66
|
+
*/
|
|
67
|
+
export function formatTtft(ms) {
|
|
68
|
+
if (ms == null || typeof ms !== 'number' || !Number.isFinite(ms) || ms <= 0) {
|
|
69
|
+
return '—';
|
|
70
|
+
}
|
|
71
|
+
const rounded = Math.round(ms);
|
|
72
|
+
if (rounded < 1000) {
|
|
73
|
+
return `${rounded}ms`;
|
|
74
|
+
}
|
|
75
|
+
if (rounded < 60000) {
|
|
76
|
+
return `${(ms / 1000).toFixed(1)}s`;
|
|
77
|
+
}
|
|
78
|
+
const mins = Math.floor(rounded / 60000);
|
|
79
|
+
const remSec = Math.round((rounded % 60000) / 1000);
|
|
80
|
+
return `${mins}m${remSec}s`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Classifies a turn's thinking drift into 'nominal' | 'elevated' | 'loop_suspect'.
|
|
85
|
+
*
|
|
86
|
+
* @param {object} turn
|
|
87
|
+
* @param {number} [turn.thinkingTokens=0]
|
|
88
|
+
* @param {number} [turn.visibleOutputTokens=0]
|
|
89
|
+
* @param {number} [turn.outputTokens=0]
|
|
90
|
+
* @param {number} [turn.mutatingCalls=0]
|
|
91
|
+
* @returns {'nominal' | 'elevated' | 'loop_suspect'}
|
|
92
|
+
*/
|
|
93
|
+
export function classifyTurnDrift(turn = {}) {
|
|
94
|
+
const thinking = Math.max(0, Number(turn.thinkingTokens ?? 0));
|
|
95
|
+
const output = Math.max(0, Number(turn.visibleOutputTokens ?? turn.outputTokens ?? 0));
|
|
96
|
+
const mutating = Math.max(0, Number(turn.mutatingCalls ?? 0));
|
|
97
|
+
|
|
98
|
+
const ratio = thinking / Math.max(1, output);
|
|
99
|
+
|
|
100
|
+
if (thinking >= DRIFT_THRESHOLDS.MIN_THINKING_TOKENS &&
|
|
101
|
+
ratio >= DRIFT_THRESHOLDS.LOOP_RATIO &&
|
|
102
|
+
mutating === 0) {
|
|
103
|
+
return DRIFT_STATES.LOOP_SUSPECT;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (thinking > 0 && ratio >= DRIFT_THRESHOLDS.ELEVATED_RATIO) {
|
|
107
|
+
return DRIFT_STATES.ELEVATED;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return DRIFT_STATES.NOMINAL;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Evaluates a sequence of turn records for DET-30 (alias DET-21) scratchpad loop conditions.
|
|
115
|
+
*
|
|
116
|
+
* @param {Array<object>} [turnRecords=[]]
|
|
117
|
+
* @returns {{
|
|
118
|
+
* triggered: boolean,
|
|
119
|
+
* severity: 'nominal' | 'warn' | 'critical',
|
|
120
|
+
* gaDetectorId: 'DET-30',
|
|
121
|
+
* aliasDetectorId: 'DET-21',
|
|
122
|
+
* consecutiveLoopTurns: number,
|
|
123
|
+
* cumulativeThinkingTokens: number,
|
|
124
|
+
* message: string
|
|
125
|
+
* }}
|
|
126
|
+
*/
|
|
127
|
+
export function evaluateThinkingDriftLoop(turnRecords = []) {
|
|
128
|
+
if (!Array.isArray(turnRecords) || turnRecords.length === 0) {
|
|
129
|
+
return {
|
|
130
|
+
triggered: false,
|
|
131
|
+
severity: 'nominal',
|
|
132
|
+
gaDetectorId: THINKING_DRIFT_DETECTOR_ID,
|
|
133
|
+
aliasDetectorId: LEGACY_THINKING_DRIFT_ALIAS,
|
|
134
|
+
consecutiveLoopTurns: 0,
|
|
135
|
+
cumulativeThinkingTokens: 0,
|
|
136
|
+
message: ''
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let consecutiveLoop = 0;
|
|
141
|
+
let nonMutatingThinking = 0;
|
|
142
|
+
let lastSuspectRatio = 0;
|
|
143
|
+
|
|
144
|
+
for (let i = 0; i < turnRecords.length; i++) {
|
|
145
|
+
const turn = turnRecords[i];
|
|
146
|
+
const mutating = Math.max(0, Number(turn.mutatingCalls ?? 0));
|
|
147
|
+
const thinking = Math.max(0, Number(turn.thinkingTokens ?? 0));
|
|
148
|
+
const output = Math.max(0, Number(turn.visibleOutputTokens ?? turn.outputTokens ?? 0));
|
|
149
|
+
const state = classifyTurnDrift(turn);
|
|
150
|
+
|
|
151
|
+
if (mutating > 0) {
|
|
152
|
+
consecutiveLoop = 0;
|
|
153
|
+
nonMutatingThinking = 0;
|
|
154
|
+
} else {
|
|
155
|
+
nonMutatingThinking += thinking;
|
|
156
|
+
if (state === DRIFT_STATES.LOOP_SUSPECT) {
|
|
157
|
+
consecutiveLoop += 1;
|
|
158
|
+
lastSuspectRatio = Math.round(thinking / Math.max(1, output));
|
|
159
|
+
} else {
|
|
160
|
+
consecutiveLoop = 0;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const isCritical =
|
|
166
|
+
consecutiveLoop >= DRIFT_THRESHOLDS.CONSECUTIVE_TURNS_CRITICAL ||
|
|
167
|
+
(consecutiveLoop >= 1 && nonMutatingThinking >= DRIFT_THRESHOLDS.CUMULATIVE_THINKING_TOKENS);
|
|
168
|
+
|
|
169
|
+
const isWarn = consecutiveLoop === 1 && !isCritical;
|
|
170
|
+
const triggered = isCritical || isWarn;
|
|
171
|
+
const severity = isCritical ? 'critical' : isWarn ? 'warn' : 'nominal';
|
|
172
|
+
|
|
173
|
+
const message = triggered
|
|
174
|
+
? `🚨 DET-30 (DET-21) Scratchpad Loop: ${lastSuspectRatio}x reasoning-to-output over ${consecutiveLoop} turn(s) with 0 edits. Inject a concrete constraint (target file + acceptance test) or step effort down.`
|
|
175
|
+
: '';
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
triggered,
|
|
179
|
+
severity,
|
|
180
|
+
gaDetectorId: THINKING_DRIFT_DETECTOR_ID,
|
|
181
|
+
aliasDetectorId: LEGACY_THINKING_DRIFT_ALIAS,
|
|
182
|
+
consecutiveLoopTurns: consecutiveLoop,
|
|
183
|
+
cumulativeThinkingTokens: nonMutatingThinking,
|
|
184
|
+
message
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Applies a turn event to the thinking drift accumulator.
|
|
190
|
+
*
|
|
191
|
+
* @param {object} drift Current drift state
|
|
192
|
+
* @param {object} ev Event data
|
|
193
|
+
* @param {string} [ev.eventType]
|
|
194
|
+
* @param {number} [ev.turnStartedAtMs]
|
|
195
|
+
* @param {number} [ev.nowMs]
|
|
196
|
+
* @param {number} [ev.ttftMs]
|
|
197
|
+
* @param {number} [ev.thinkingTokens=0]
|
|
198
|
+
* @param {number} [ev.outputTokens=0]
|
|
199
|
+
* @param {number} [ev.mutatingCalls=0]
|
|
200
|
+
* @param {boolean} [ev.thinkingEstimated=false]
|
|
201
|
+
* @returns {object} Updated drift state
|
|
202
|
+
*/
|
|
203
|
+
export function applyThinkingDriftEvent(drift, ev = {}) {
|
|
204
|
+
const current = drift ? { ...drift, turns: [...drift.turns] } : createInitialThinkingDrift();
|
|
205
|
+
|
|
206
|
+
// TTFT calculation: measured from turnStartedAtMs to nowMs
|
|
207
|
+
let ttft = null;
|
|
208
|
+
if (typeof ev.ttftMs === 'number' && ev.ttftMs > 0) {
|
|
209
|
+
ttft = Math.round(ev.ttftMs);
|
|
210
|
+
} else if (
|
|
211
|
+
typeof ev.turnStartedAtMs === 'number' &&
|
|
212
|
+
typeof ev.nowMs === 'number' &&
|
|
213
|
+
ev.nowMs > ev.turnStartedAtMs
|
|
214
|
+
) {
|
|
215
|
+
ttft = Math.round(ev.nowMs - ev.turnStartedAtMs);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const thinking = Math.max(0, Number(ev.thinkingTokens ?? 0));
|
|
219
|
+
const output = Math.max(0, Number(ev.outputTokens ?? ev.visibleOutputTokens ?? 0));
|
|
220
|
+
const mutating = Math.max(0, Number(ev.mutatingCalls ?? 0));
|
|
221
|
+
const isEstimated = Boolean(ev.thinkingEstimated);
|
|
222
|
+
|
|
223
|
+
// Compute drift ratio only if thinking tokens are reported and non-estimated
|
|
224
|
+
let ratio = null;
|
|
225
|
+
if (!isEstimated && (thinking > 0 || output > 0)) {
|
|
226
|
+
ratio = parseFloat((thinking / Math.max(1, output)).toFixed(1));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const turnRecord = {
|
|
230
|
+
turnIndex: current.turns.length,
|
|
231
|
+
ttftMs: ttft,
|
|
232
|
+
thinkingTokens: thinking,
|
|
233
|
+
outputTokens: output,
|
|
234
|
+
visibleOutputTokens: output,
|
|
235
|
+
mutatingCalls: mutating,
|
|
236
|
+
driftRatio: ratio,
|
|
237
|
+
driftState: classifyTurnDrift({ thinkingTokens: thinking, visibleOutputTokens: output, mutatingCalls: mutating }),
|
|
238
|
+
thinkingEstimated: isEstimated,
|
|
239
|
+
timestamp: typeof ev.nowMs === 'number' ? ev.nowMs : Date.now()
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
current.turns.push(turnRecord);
|
|
243
|
+
current.measured = current.turns.length > 0;
|
|
244
|
+
|
|
245
|
+
if (ttft !== null) {
|
|
246
|
+
current.lastTtftMs = ttft;
|
|
247
|
+
const measuredTtfts = current.turns
|
|
248
|
+
.map(t => t.ttftMs)
|
|
249
|
+
.filter(t => typeof t === 'number' && t > 0);
|
|
250
|
+
if (measuredTtfts.length > 0) {
|
|
251
|
+
const sum = measuredTtfts.reduce((acc, v) => acc + v, 0);
|
|
252
|
+
current.avgTtftMs = Math.round(sum / measuredTtfts.length);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (ratio !== null) {
|
|
257
|
+
current.lastDriftRatio = ratio;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (!isEstimated) {
|
|
261
|
+
current.cumulativeThinkingTokens += thinking;
|
|
262
|
+
current.cumulativeOutputTokens += output;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Rolling drift across trailing window
|
|
266
|
+
const trailing = current.turns.slice(-DRIFT_THRESHOLDS.ROLLING_WINDOW);
|
|
267
|
+
const trailingThinking = trailing.reduce((acc, t) => acc + (t.thinkingEstimated ? 0 : t.thinkingTokens), 0);
|
|
268
|
+
const trailingOutput = trailing.reduce((acc, t) => acc + (t.thinkingEstimated ? 0 : t.outputTokens), 0);
|
|
269
|
+
if (trailingThinking > 0 || trailingOutput > 0) {
|
|
270
|
+
current.rollingDriftRatio = parseFloat((trailingThinking / Math.max(1, trailingOutput)).toFixed(1));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Evaluate loop detector
|
|
274
|
+
current.loop = evaluateThinkingDriftLoop(current.turns);
|
|
275
|
+
current.consecutiveLoopTurns = current.loop.consecutiveLoopTurns;
|
|
276
|
+
|
|
277
|
+
return current;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Checks whether a TTFT observation constitutes a sudden spike against baseline.
|
|
282
|
+
*
|
|
283
|
+
* @param {number} ttftMs
|
|
284
|
+
* @param {number} baselineAvgMs
|
|
285
|
+
* @returns {boolean}
|
|
286
|
+
*/
|
|
287
|
+
export function isTtftSpike(ttftMs, baselineAvgMs) {
|
|
288
|
+
if (typeof ttftMs !== 'number' || typeof baselineAvgMs !== 'number' || baselineAvgMs <= 0) {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
return ttftMs > baselineAvgMs * DRIFT_THRESHOLDS.SPIKE_MULTIPLIER;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Returns a compact 1-line summary of thinking drift for CLI statuslines.
|
|
296
|
+
* @param {object} drift
|
|
297
|
+
* @returns {string}
|
|
298
|
+
*/
|
|
299
|
+
export function formatDriftCompact(drift) {
|
|
300
|
+
if (!drift?.measured) return 'TTFT: — | Drift: —';
|
|
301
|
+
const ttftStr = formatTtft(drift.lastTtftMs);
|
|
302
|
+
const ratioStr = drift.lastDriftRatio != null ? `${drift.lastDriftRatio}x` : '—';
|
|
303
|
+
if (drift.loop?.triggered) {
|
|
304
|
+
return `TTFT: ${ttftStr} | 🚨 DET-30 Loop (${ratioStr})`;
|
|
305
|
+
}
|
|
306
|
+
return `TTFT: ${ttftStr} | Drift: ${ratioStr}`;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Formats an informative drift label for reports.
|
|
311
|
+
* @param {object} drift
|
|
312
|
+
* @returns {string}
|
|
313
|
+
*/
|
|
314
|
+
export function formatDriftLabel(drift) {
|
|
315
|
+
if (!drift?.measured) return 'Thinking drift unmeasured';
|
|
316
|
+
const ttftStr = drift.avgTtftMs != null ? formatTtft(drift.avgTtftMs) : '—';
|
|
317
|
+
const rollingStr = drift.rollingDriftRatio != null ? `${drift.rollingDriftRatio}x` : '—';
|
|
318
|
+
return `Avg TTFT ${ttftStr} · Rolling Drift ${rollingStr}`;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Item A6 (#11): Audits Gemini thinking + output token ceiling against client timeout
|
|
323
|
+
* (`maxOutputTokens + thinkingBudget > 32,768` with `timeoutMs < 120,000` risks billed partial thinking on timeout).
|
|
324
|
+
*
|
|
325
|
+
* @param {object} [params={}]
|
|
326
|
+
* @param {string} [params.modelId='']
|
|
327
|
+
* @param {number} [params.maxOutputTokens=0]
|
|
328
|
+
* @param {number} [params.thinkingBudget=0]
|
|
329
|
+
* @param {number} [params.timeoutMs=0]
|
|
330
|
+
*/
|
|
331
|
+
export function evaluateGeminiThinkingTimeoutRisk(params = {}) {
|
|
332
|
+
const modelId = String(params.modelId || '').toLowerCase();
|
|
333
|
+
const isGeminiThinking =
|
|
334
|
+
modelId === '' ||
|
|
335
|
+
/gemini-(?:4|3\.1-pro|3-pro|2\.5-pro|2\.5-flash)/i.test(modelId);
|
|
336
|
+
const maxOutput = Math.max(0, Number(params.maxOutputTokens ?? 0));
|
|
337
|
+
const thinkingBudget = Math.max(0, Number(params.thinkingBudget ?? 0));
|
|
338
|
+
const combinedBudget = maxOutput + thinkingBudget;
|
|
339
|
+
const timeoutMs = Number(params.timeoutMs ?? 0);
|
|
340
|
+
|
|
341
|
+
if (
|
|
342
|
+
isGeminiThinking &&
|
|
343
|
+
combinedBudget > 32768 &&
|
|
344
|
+
timeoutMs > 0 &&
|
|
345
|
+
timeoutMs < 120000
|
|
346
|
+
) {
|
|
347
|
+
return {
|
|
348
|
+
triggered: true,
|
|
349
|
+
detectorId: 'DET-30',
|
|
350
|
+
combinedBudget,
|
|
351
|
+
timeoutMs,
|
|
352
|
+
recommendedTimeoutMs: 180000,
|
|
353
|
+
advisory:
|
|
354
|
+
`[Highbeam Quota Shield — Thinking Timeout Billing Guard (#11)] ` +
|
|
355
|
+
`Combined maxOutputTokens + thinkingBudget (${combinedBudget.toLocaleString()} tok > 32,768) paired with client timeout ${Math.round(timeoutMs / 1000)}s (< 120s). ` +
|
|
356
|
+
`Under Gemini timeout-billing rules, aborted long-thinking requests bill generated thinking tokens — raise timeout >= 180s or cap thinkingBudget <= 16,000.`
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return {
|
|
361
|
+
triggered: false,
|
|
362
|
+
detectorId: 'DET-30',
|
|
363
|
+
combinedBudget,
|
|
364
|
+
timeoutMs,
|
|
365
|
+
advisory: null
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Item A6 (#11): Active `DET-30` Pre-Turn Step-Down Gate.
|
|
371
|
+
* Triggers when `thinkingTokens > 4,000` AND `thinkingTokens / max(1, visibleOutputTokens) > 12`
|
|
372
|
+
* across `>= 2` consecutive turns (`block` in `AUTO_PILOT`, steering advisory in `COACH`).
|
|
373
|
+
*
|
|
374
|
+
* @param {object} [params={}]
|
|
375
|
+
* @param {Array<object>} [params.recentTurns=[]]
|
|
376
|
+
* @param {object} [params.thinkingDrift=null]
|
|
377
|
+
* @param {string} [params.mode='COACH']
|
|
378
|
+
*/
|
|
379
|
+
export function evaluateActiveDet30StepDown(params = {}) {
|
|
380
|
+
const turns = Array.isArray(params.recentTurns) && params.recentTurns.length > 0
|
|
381
|
+
? params.recentTurns
|
|
382
|
+
: Array.isArray(params.thinkingDrift?.turns)
|
|
383
|
+
? params.thinkingDrift.turns
|
|
384
|
+
: [];
|
|
385
|
+
|
|
386
|
+
let streak = 0;
|
|
387
|
+
let streakThinkingTokens = 0;
|
|
388
|
+
let latestRatio = 0;
|
|
389
|
+
|
|
390
|
+
for (let i = turns.length - 1; i >= 0; i--) {
|
|
391
|
+
const t = turns[i] || {};
|
|
392
|
+
const thinking = Number(t.thinkingTokens ?? t.thinking_tokens ?? 0);
|
|
393
|
+
const visible = Number(
|
|
394
|
+
t.visibleOutputTokens ?? t.outputTokens ?? t.output_tokens ?? 0
|
|
395
|
+
);
|
|
396
|
+
const ratio = thinking / Math.max(1, visible);
|
|
397
|
+
if (thinking > 4000 && ratio > 12) {
|
|
398
|
+
streak += 1;
|
|
399
|
+
streakThinkingTokens += thinking;
|
|
400
|
+
if (latestRatio === 0) latestRatio = Math.round(ratio * 10) / 10;
|
|
401
|
+
} else {
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (streak < 2) {
|
|
407
|
+
return {
|
|
408
|
+
triggered: false,
|
|
409
|
+
detectorId: 'DET-30',
|
|
410
|
+
subType: null,
|
|
411
|
+
decision: 'allow',
|
|
412
|
+
consecutiveTurns: streak,
|
|
413
|
+
reason: null
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const flightMode = String(params.mode || 'COACH').toUpperCase();
|
|
418
|
+
const decision = flightMode === 'AUTO_PILOT' ? 'block' : 'steer';
|
|
419
|
+
return {
|
|
420
|
+
triggered: true,
|
|
421
|
+
detectorId: 'DET-30',
|
|
422
|
+
subType: 'THINKING_RUNAWAY_STEPDOWN',
|
|
423
|
+
decision,
|
|
424
|
+
consecutiveTurns: streak,
|
|
425
|
+
streakThinkingTokens,
|
|
426
|
+
latestRatio,
|
|
427
|
+
recommendedEffort: 'medium',
|
|
428
|
+
recommendedMaxThinkingTokens: 16000,
|
|
429
|
+
reason:
|
|
430
|
+
`[DET-30 Thinking Runaway Governor (#11)] ${streak} consecutive turns burned ${streakThinkingTokens.toLocaleString()} thinking tokens ` +
|
|
431
|
+
`(${latestRatio}x thinking-to-output ratio > 12x). Step down via /effort medium or set MAX_THINKING_TOKENS=16000 before continuing.`
|
|
432
|
+
};
|
|
433
|
+
}
|